continue-custom/core/llm/llms/FreeTrial.ts
2023-12-15 20:22:13 -08:00

121 lines
2.9 KiB
TypeScript

import { BaseLLM } from "..";
import { ChatMessage, CompletionOptions, ModelProvider } from "../..";
// const SERVER_URL = "http://localhost:8080";
const SERVER_URL = "https://proxy-server-l6vsfbzhba-uw.a.run.app";
class FreeTrial extends BaseLLM {
static providerName: ModelProvider = "openai-free-trial";
private _getHeaders() {
return {
uniqueId: this.uniqueId || "None",
"Content-Type": "application/json",
};
}
protected async _complete(
prompt: string,
options: CompletionOptions
): Promise<string> {
const args = this.collectArgs(options);
const response = await this.fetch(`${SERVER_URL}/complete`, {
method: "POST",
headers: this._getHeaders(),
body: JSON.stringify({
messages: [{ role: "user", content: prompt }],
...args,
}),
});
return await response.json();
}
protected async *_streamComplete(
prompt: string,
options: CompletionOptions
): AsyncGenerator<string> {
const args = this.collectArgs(options);
const response = await this.fetch(`${SERVER_URL}/stream_complete`, {
method: "POST",
headers: this._getHeaders(),
body: JSON.stringify({
messages: [{ role: "user", content: prompt }],
...args,
}),
});
const reader = response.body?.getReader();
let decoder = new TextDecoder("utf-8");
while (true && reader) {
const { done, value } = await reader.read();
if (done) {
break;
}
if (value) {
let decodedChunk = decoder.decode(value);
yield decodedChunk;
}
}
}
protected async *_streamChat(
messages: ChatMessage[],
options: CompletionOptions
): AsyncGenerator<ChatMessage> {
const args = this.collectArgs(options);
const response = await this.fetch(`${SERVER_URL}/stream_chat`, {
method: "POST",
headers: this._getHeaders(),
body: JSON.stringify({
messages,
...args,
}),
});
if (response.status !== 200) {
throw new Error(await response.text());
}
const reader = response.body?.getReader();
let decoder = new TextDecoder("utf-8");
while (true && reader) {
const { done, value } = await reader.read();
if (done) {
break;
}
if (value) {
let decodedChunk = decoder.decode(value);
const chunks = decodedChunk.split("\n");
for (const chunk of chunks) {
if (chunk.trim() !== "") {
const loadedChunk = JSON.parse(chunk);
yield {
role: "assistant",
content: loadedChunk.content || "",
};
if (this.model === "gpt-4") {
await delay(0.03);
} else {
await delay(0.01);
}
}
}
}
}
}
}
async function delay(seconds: number) {
return new Promise((resolve) => setTimeout(resolve, seconds * 1000));
}
export default FreeTrial;