add a mock llm to openai-adapters
This commit is contained in:
parent
62e2e96869
commit
c2c4a5f940
4 changed files with 167 additions and 1 deletions
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "@continuedev/openai-adapters",
|
||||
"version": "1.0.14",
|
||||
"version": "1.0.15",
|
||||
"description": "",
|
||||
"main": "dist/index.js",
|
||||
"types": "dist/index.d.ts",
|
||||
|
|
|
|||
157
packages/openai-adapters/src/apis/Mock.ts
Normal file
157
packages/openai-adapters/src/apis/Mock.ts
Normal file
|
|
@ -0,0 +1,157 @@
|
|||
import { OpenAI } from "openai/index";
|
||||
import {
|
||||
ChatCompletion,
|
||||
ChatCompletionChunk,
|
||||
ChatCompletionCreateParamsNonStreaming,
|
||||
ChatCompletionCreateParamsStreaming,
|
||||
Completion,
|
||||
CompletionCreateParamsNonStreaming,
|
||||
CompletionCreateParamsStreaming,
|
||||
Model,
|
||||
} from "openai/resources/index";
|
||||
import { chatChunk, chatCompletion } from "../util.js";
|
||||
import {
|
||||
BaseLlmApi,
|
||||
CreateRerankResponse,
|
||||
FimCreateParamsStreaming,
|
||||
RerankCreateParams,
|
||||
} from "./base.js";
|
||||
|
||||
const MOCK_RESPONSE =
|
||||
"This is a mock response from the OpenAI API. It can be returned all at once or streamed chunk by chunk.";
|
||||
|
||||
export class MockApi implements BaseLlmApi {
|
||||
async chatCompletionNonStream(
|
||||
body: ChatCompletionCreateParamsNonStreaming,
|
||||
signal: AbortSignal,
|
||||
): Promise<ChatCompletion> {
|
||||
await new Promise((resolve) => setTimeout(resolve, 800));
|
||||
const lastMessage = body.messages[body.messages.length - 1].content;
|
||||
const content = !lastMessage
|
||||
? MOCK_RESPONSE
|
||||
: typeof lastMessage === "string"
|
||||
? lastMessage
|
||||
: lastMessage[0].type === "text"
|
||||
? lastMessage[0].text
|
||||
: MOCK_RESPONSE;
|
||||
return chatCompletion({
|
||||
content,
|
||||
model: body.model,
|
||||
});
|
||||
}
|
||||
|
||||
async *chatCompletionStream(
|
||||
body: ChatCompletionCreateParamsStreaming,
|
||||
signal: AbortSignal,
|
||||
): AsyncGenerator<ChatCompletionChunk, any, unknown> {
|
||||
const lastMessage = body.messages[body.messages.length - 1].content;
|
||||
const content = !lastMessage
|
||||
? MOCK_RESPONSE
|
||||
: typeof lastMessage === "string"
|
||||
? lastMessage
|
||||
: lastMessage[0].type === "text"
|
||||
? lastMessage[0].text
|
||||
: MOCK_RESPONSE;
|
||||
const chunks = content.split(" ");
|
||||
for (const chunk of chunks) {
|
||||
await new Promise((resolve) => setTimeout(resolve, 100));
|
||||
yield chatChunk({
|
||||
content: chunk + " ",
|
||||
model: body.model,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async completionNonStream(
|
||||
body: CompletionCreateParamsNonStreaming,
|
||||
signal: AbortSignal,
|
||||
): Promise<Completion> {
|
||||
await new Promise((resolve) => setTimeout(resolve, 800));
|
||||
return {
|
||||
id: "mock-completion",
|
||||
object: "text_completion",
|
||||
created: Date.now(),
|
||||
model: body.model,
|
||||
choices: [
|
||||
{
|
||||
text: body.prompt as string,
|
||||
index: 0,
|
||||
finish_reason: "stop",
|
||||
},
|
||||
],
|
||||
} as Completion;
|
||||
}
|
||||
|
||||
async *completionStream(
|
||||
body: CompletionCreateParamsStreaming,
|
||||
signal: AbortSignal,
|
||||
): AsyncGenerator<Completion, any, unknown> {
|
||||
const chunks = (body.prompt as string).split(" ");
|
||||
for (const chunk of chunks) {
|
||||
await new Promise((resolve) => setTimeout(resolve, 100));
|
||||
yield {
|
||||
id: "mock-chunk",
|
||||
object: "text_completion",
|
||||
created: Date.now(),
|
||||
model: body.model,
|
||||
choices: [
|
||||
{
|
||||
text: chunk + " ",
|
||||
index: 0,
|
||||
finish_reason: "stop",
|
||||
},
|
||||
],
|
||||
} as Completion;
|
||||
}
|
||||
}
|
||||
|
||||
async *fimStream(
|
||||
body: FimCreateParamsStreaming,
|
||||
signal: AbortSignal,
|
||||
): AsyncGenerator<ChatCompletionChunk, any, unknown> {
|
||||
const chunks = (body.prompt as string).split(" ");
|
||||
for (const chunk of chunks) {
|
||||
await new Promise((resolve) => setTimeout(resolve, 100));
|
||||
yield chatChunk({
|
||||
content: chunk + " ",
|
||||
model: body.model,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async embed(
|
||||
body: OpenAI.Embeddings.EmbeddingCreateParams,
|
||||
): Promise<OpenAI.Embeddings.CreateEmbeddingResponse> {
|
||||
await new Promise((resolve) => setTimeout(resolve, 400));
|
||||
return {
|
||||
data: [
|
||||
{
|
||||
embedding: new Array(1536).fill(0),
|
||||
index: 0,
|
||||
object: "embedding",
|
||||
},
|
||||
],
|
||||
model: body.model,
|
||||
object: "list",
|
||||
usage: {
|
||||
prompt_tokens: 0,
|
||||
total_tokens: 0,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
async rerank(body: RerankCreateParams): Promise<CreateRerankResponse> {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
|
||||
async list(): Promise<Model[]> {
|
||||
return [
|
||||
{
|
||||
id: "mock-model",
|
||||
created: Date.now(),
|
||||
object: "model",
|
||||
owned_by: "mock",
|
||||
},
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
@ -7,6 +7,7 @@ import { CohereApi } from "./apis/Cohere.js";
|
|||
import { DeepSeekApi } from "./apis/DeepSeek.js";
|
||||
import { GeminiApi } from "./apis/Gemini.js";
|
||||
import { JinaApi } from "./apis/Jina.js";
|
||||
import { MockApi } from "./apis/Mock.js";
|
||||
import { MoonshotApi } from "./apis/Moonshot.js";
|
||||
import { OpenAIApi } from "./apis/OpenAI.js";
|
||||
import { LLMConfig, OpenAIConfigSchema } from "./types.js";
|
||||
|
|
@ -84,6 +85,8 @@ export function constructLlmApi(config: LLMConfig): BaseLlmApi | undefined {
|
|||
return openAICompatible("http://localhost:8000/", config);
|
||||
case "lmstudio":
|
||||
return openAICompatible("http://localhost:1234/", config);
|
||||
case "mock":
|
||||
return new MockApi();
|
||||
default:
|
||||
return undefined;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -69,6 +69,11 @@ export const DeepseekConfigSchema = OpenAIConfigSchema.extend({
|
|||
});
|
||||
export type DeepseekConfig = z.infer<typeof DeepseekConfigSchema>;
|
||||
|
||||
export const MockConfigSchema = BasePlusConfig.extend({
|
||||
provider: z.literal("mock"),
|
||||
});
|
||||
export type MockConfig = z.infer<typeof MockConfigSchema>;
|
||||
|
||||
// Other APIs
|
||||
export const CohereConfigSchema = OpenAIConfigSchema.extend({
|
||||
provider: z.literal("cohere"),
|
||||
|
|
@ -107,5 +112,6 @@ export const LLMConfigSchema = z.discriminatedUnion("provider", [
|
|||
GeminiConfigSchema,
|
||||
AnthropicConfigSchema,
|
||||
JinaConfigSchema,
|
||||
MockConfigSchema,
|
||||
]);
|
||||
export type LLMConfig = z.infer<typeof LLMConfigSchema>;
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue