Elffuss BraidHub

kikocis/continue public

main · 4.1 KiBRawPermalink
1import { OpenAI } from "openai/index";
2import {
3 ChatCompletion,
4 ChatCompletionChunk,
5 ChatCompletionCreateParamsNonStreaming,
6 ChatCompletionCreateParamsStreaming,
7 Completion,
8 CompletionCreateParamsNonStreaming,
9 CompletionCreateParamsStreaming,
10 Model,
11} from "openai/resources/index";
12import { chatChunk, chatCompletion } from "../util.js";
13import {
14 BaseLlmApi,
15 CreateRerankResponse,
16 FimCreateParamsStreaming,
17 RerankCreateParams,
18} from "./base.js";
19
20const MOCK_RESPONSE =
21 "This is a mock response from the OpenAI API. It can be returned all at once or streamed chunk by chunk.";
22
23export class MockApi implements BaseLlmApi {
24 async chatCompletionNonStream(
25 body: ChatCompletionCreateParamsNonStreaming,
26 signal: AbortSignal,
27 ): Promise<ChatCompletion> {
28 await new Promise((resolve) => setTimeout(resolve, 800));
29 const lastMessage = body.messages[body.messages.length - 1].content;
30 const content = !lastMessage
31 ? MOCK_RESPONSE
32 : typeof lastMessage === "string"
33 ? lastMessage
34 : lastMessage[0].type === "text"
35 ? lastMessage[0].text
36 : MOCK_RESPONSE;
37 return chatCompletion({
38 content,
39 model: body.model,
40 });
41 }
42
43 async *chatCompletionStream(
44 body: ChatCompletionCreateParamsStreaming,
45 signal: AbortSignal,
46 ): AsyncGenerator<ChatCompletionChunk, any, unknown> {
47 const lastMessage = body.messages[body.messages.length - 1].content;
48 const content = !lastMessage
49 ? MOCK_RESPONSE
50 : typeof lastMessage === "string"
51 ? lastMessage
52 : lastMessage[0].type === "text"
53 ? lastMessage[0].text
54 : MOCK_RESPONSE;
55 const chunks = content.split(" ");
56 for (const chunk of chunks) {
57 await new Promise((resolve) => setTimeout(resolve, 100));
58 yield chatChunk({
59 content: chunk + " ",
60 model: body.model,
61 });
62 }
63 }
64
65 async completionNonStream(
66 body: CompletionCreateParamsNonStreaming,
67 signal: AbortSignal,
68 ): Promise<Completion> {
69 await new Promise((resolve) => setTimeout(resolve, 800));
70 return {
71 id: "mock-completion",
72 object: "text_completion",
73 created: Date.now(),
74 model: body.model,
75 choices: [
76 {
77 text: body.prompt as string,
78 index: 0,
79 finish_reason: "stop",
80 },
81 ],
82 } as Completion;
83 }
84
85 async *completionStream(
86 body: CompletionCreateParamsStreaming,
87 signal: AbortSignal,
88 ): AsyncGenerator<Completion, any, unknown> {
89 const chunks = (body.prompt as string).split(" ");
90 for (const chunk of chunks) {
91 await new Promise((resolve) => setTimeout(resolve, 100));
92 yield {
93 id: "mock-chunk",
94 object: "text_completion",
95 created: Date.now(),
96 model: body.model,
97 choices: [
98 {
99 text: chunk + " ",
100 index: 0,
101 finish_reason: "stop",
102 },
103 ],
104 } as Completion;
105 }
106 }
107
108 async *fimStream(
109 body: FimCreateParamsStreaming,
110 signal: AbortSignal,
111 ): AsyncGenerator<ChatCompletionChunk, any, unknown> {
112 const chunks = (body.prompt as string).split(" ");
113 for (const chunk of chunks) {
114 await new Promise((resolve) => setTimeout(resolve, 100));
115 yield chatChunk({
116 content: chunk + " ",
117 model: body.model,
118 });
119 }
120 }
121
122 async embed(
123 body: OpenAI.Embeddings.EmbeddingCreateParams,
124 ): Promise<OpenAI.Embeddings.CreateEmbeddingResponse> {
125 await new Promise((resolve) => setTimeout(resolve, 400));
126 return {
127 data: [
128 {
129 embedding: new Array(1536).fill(0),
130 index: 0,
131 object: "embedding",
132 },
133 ],
134 model: body.model,
135 object: "list",
136 usage: {
137 prompt_tokens: 0,
138 total_tokens: 0,
139 },
140 };
141 }
142
143 async rerank(body: RerankCreateParams): Promise<CreateRerankResponse> {
144 throw new Error("Method not implemented.");
145 }
146
147 async list(): Promise<Model[]> {
148 return [
149 {
150 id: "mock-model",
151 created: Date.now(),
152 object: "model",
153 owned_by: "mock",
154 },
155 ];
156 }
157}