🎨 use common streamResponse method for LLMs
This commit is contained in:
parent
d1bd115a82
commit
4de45f7956
9 changed files with 55 additions and 105 deletions
|
|
@ -1,7 +1,6 @@
|
|||
// import AnthropicClient from "@anthropic-ai/sdk";
|
||||
// import { CompletionCreateParamsBase } from "@anthropic-ai/sdk/resources/completions";
|
||||
import { BaseLLM } from "..";
|
||||
import { CompletionOptions, LLMOptions, ModelProvider } from "../..";
|
||||
import { streamResponse } from "../stream";
|
||||
import { anthropicTemplateMessages } from "../templates/chat";
|
||||
|
||||
class Anthropic extends BaseLLM {
|
||||
|
|
@ -17,14 +16,8 @@ class Anthropic extends BaseLLM {
|
|||
apiBase: "https://api.anthropic.com/v1",
|
||||
};
|
||||
|
||||
// private _anthropic: AnthropicClient;
|
||||
|
||||
constructor(options: LLMOptions) {
|
||||
super(options);
|
||||
|
||||
// this._anthropic = new AnthropicClient({
|
||||
// apiKey: options.apiKey,
|
||||
// });
|
||||
}
|
||||
|
||||
private _convertArgs(options: CompletionOptions, prompt: string) {
|
||||
|
|
@ -79,18 +72,10 @@ class Anthropic extends BaseLLM {
|
|||
stream: true,
|
||||
}),
|
||||
});
|
||||
// Receive as SSE
|
||||
if (response.body === null) {
|
||||
return;
|
||||
}
|
||||
const reader = response.body.getReader();
|
||||
|
||||
let chunk = "";
|
||||
while (true) {
|
||||
const { done, value } = await reader.read();
|
||||
if (done) {
|
||||
break;
|
||||
}
|
||||
chunk += new TextDecoder("utf-8").decode(value);
|
||||
for await (const value of streamResponse(response)) {
|
||||
chunk += value;
|
||||
const lines = chunk.split("\n");
|
||||
chunk = lines.pop() as string;
|
||||
for (const line of lines) {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
import { BaseLLM } from "..";
|
||||
import { ChatMessage, CompletionOptions, ModelProvider } from "../..";
|
||||
import { streamResponse } from "../stream";
|
||||
|
||||
// const SERVER_URL = "http://localhost:8080";
|
||||
const SERVER_URL = "https://proxy-server-l6vsfbzhba-uw.a.run.app";
|
||||
|
|
@ -47,18 +48,8 @@ class FreeTrial extends BaseLLM {
|
|||
}),
|
||||
});
|
||||
|
||||
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;
|
||||
}
|
||||
for await (const value of streamResponse(response)) {
|
||||
yield value;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -77,36 +68,22 @@ class FreeTrial extends BaseLLM {
|
|||
}),
|
||||
});
|
||||
|
||||
if (response.status !== 200) {
|
||||
throw new Error(await response.text());
|
||||
}
|
||||
for await (const value of streamResponse(response)) {
|
||||
const chunks = value.split("\n");
|
||||
|
||||
const reader = response.body?.getReader();
|
||||
let decoder = new TextDecoder("utf-8");
|
||||
for (const chunk of chunks) {
|
||||
if (chunk.trim() !== "") {
|
||||
const loadedChunk = JSON.parse(chunk);
|
||||
|
||||
while (true && reader) {
|
||||
const { done, value } = await reader.read();
|
||||
if (done) {
|
||||
break;
|
||||
}
|
||||
if (value) {
|
||||
let decodedChunk = decoder.decode(value);
|
||||
const chunks = decodedChunk.split("\n");
|
||||
yield {
|
||||
role: "assistant",
|
||||
content: loadedChunk.content || "",
|
||||
};
|
||||
|
||||
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);
|
||||
}
|
||||
if (this.model === "gpt-4") {
|
||||
await delay(0.03);
|
||||
} else {
|
||||
await delay(0.01);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -55,7 +55,6 @@ class Gemini extends BaseLLM {
|
|||
"Content-Type": "application/json; charset=utf-8",
|
||||
},
|
||||
});
|
||||
console.log(await response.text(), "GEMINIIIIIIIIIII...");
|
||||
const data = await response.json();
|
||||
if (data[0]?.error) {
|
||||
throw new Error(data[0].error.message);
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import {
|
|||
LLMOptions,
|
||||
ModelProvider,
|
||||
} from "../..";
|
||||
import { streamResponse } from "../stream";
|
||||
|
||||
class GooglePalm extends BaseLLM {
|
||||
static providerName: ModelProvider = "google-palm-real";
|
||||
|
|
@ -58,17 +59,8 @@ class GooglePalm extends BaseLLM {
|
|||
body: JSON.stringify(body),
|
||||
});
|
||||
|
||||
if (!response.body) {
|
||||
throw new Error("No response body");
|
||||
}
|
||||
const reader = response.body.getReader();
|
||||
let buffer = "";
|
||||
while (true) {
|
||||
const { done, value } = await reader.read();
|
||||
if (done) {
|
||||
break;
|
||||
}
|
||||
let chunk = new TextDecoder().decode(value);
|
||||
for await (const chunk of streamResponse(response)) {
|
||||
buffer += chunk;
|
||||
if (buffer.startsWith("[")) {
|
||||
buffer = buffer.slice(1);
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
import { BaseLLM } from "..";
|
||||
import { CompletionOptions, LLMOptions, ModelProvider } from "../..";
|
||||
import { streamResponse } from "../stream";
|
||||
|
||||
class HuggingFaceTGI extends BaseLLM {
|
||||
static providerName: ModelProvider = "huggingface-tgi";
|
||||
|
|
@ -36,17 +37,10 @@ class HuggingFaceTGI extends BaseLLM {
|
|||
body: JSON.stringify({ inputs: prompt, parameters: args }),
|
||||
});
|
||||
|
||||
const reader = response.body?.getReader();
|
||||
let chunk = "";
|
||||
|
||||
while (true && reader) {
|
||||
const { done, value } = await reader.read();
|
||||
|
||||
if (done) {
|
||||
break;
|
||||
}
|
||||
|
||||
chunk += new TextDecoder().decode(value);
|
||||
for await (const value of streamResponse(response)) {
|
||||
chunk += value;
|
||||
|
||||
const lines = chunk.split("\n");
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
import { BaseLLM } from "..";
|
||||
import { CompletionOptions, LLMOptions, ModelProvider } from "../..";
|
||||
import { streamResponse } from "../stream";
|
||||
|
||||
class LlamaCpp extends BaseLLM {
|
||||
static providerName: ModelProvider = "llama.cpp";
|
||||
|
|
@ -36,12 +37,8 @@ class LlamaCpp extends BaseLLM {
|
|||
}),
|
||||
});
|
||||
|
||||
const reader = resp.body?.getReader();
|
||||
let buffer = "";
|
||||
while (true && reader) {
|
||||
const { done, value } = await reader.read();
|
||||
if (done) break;
|
||||
const chunk = new TextDecoder().decode(value);
|
||||
for await (const chunk of streamResponse(resp)) {
|
||||
buffer += chunk;
|
||||
|
||||
const lines = buffer.split("\n");
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
import { BaseLLM } from "..";
|
||||
import { CompletionOptions, LLMOptions, ModelProvider } from "../..";
|
||||
import { streamResponse } from "../stream";
|
||||
|
||||
class Ollama extends BaseLLM {
|
||||
static providerName: ModelProvider = "ollama";
|
||||
|
|
@ -60,15 +61,10 @@ class Ollama extends BaseLLM {
|
|||
body: JSON.stringify(this._convertArgs(options, prompt)),
|
||||
});
|
||||
|
||||
const reader = response.body?.getReader();
|
||||
|
||||
let buffer = "";
|
||||
while (reader) {
|
||||
const { done, value } = await reader.read();
|
||||
|
||||
if (done) break;
|
||||
for await (const value of streamResponse(response)) {
|
||||
// Append the received chunk to the buffer
|
||||
buffer += new TextDecoder().decode(value);
|
||||
buffer += value;
|
||||
// Split the buffer into individual JSON chunks
|
||||
const chunks = buffer.split("\n");
|
||||
for (let i = 0; i < chunks.length; i++) {
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import {
|
|||
LLMOptions,
|
||||
ModelProvider,
|
||||
} from "../..";
|
||||
import { streamResponse } from "../stream";
|
||||
|
||||
class OpenAI extends BaseLLM {
|
||||
static providerName: ModelProvider = "openai";
|
||||
|
|
@ -83,22 +84,9 @@ class OpenAI extends BaseLLM {
|
|||
}),
|
||||
});
|
||||
|
||||
if (response.status !== 200) {
|
||||
throw new Error(await response.text());
|
||||
}
|
||||
|
||||
// Receive as SSE
|
||||
if (response.body === null) {
|
||||
return;
|
||||
}
|
||||
let buffer = "";
|
||||
const reader = response.body.getReader();
|
||||
while (true) {
|
||||
const { done, value } = await reader.read();
|
||||
if (done) {
|
||||
break;
|
||||
}
|
||||
buffer += new TextDecoder("utf-8").decode(value);
|
||||
for await (const value of streamResponse(response)) {
|
||||
buffer += value;
|
||||
let position;
|
||||
while ((position = buffer.indexOf("\n")) >= 0) {
|
||||
const line = buffer.slice(0, position);
|
||||
|
|
|
|||
22
core/llm/stream.ts
Normal file
22
core/llm/stream.ts
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
export async function* streamResponse(response: Response): AsyncGenerator<any> {
|
||||
if (response.status !== 200) {
|
||||
throw new Error(await response.text());
|
||||
}
|
||||
|
||||
if (!response.body) {
|
||||
throw new Error(`No response body returned.`);
|
||||
}
|
||||
|
||||
const reader = response.body.getReader();
|
||||
const decoder = new TextDecoder("utf-8");
|
||||
while (true) {
|
||||
const { done, value } = await reader.read();
|
||||
if (done) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (value) {
|
||||
yield decoder.decode(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue