pass abort signal to openai-adapters
This commit is contained in:
parent
ed6ef635cd
commit
608abeaa13
11 changed files with 89 additions and 89 deletions
|
|
@ -489,6 +489,7 @@ export abstract class BaseLLM implements ILLM {
|
|||
if (this.shouldUseOpenAIAdapter("streamFim") && this.openaiAdapter) {
|
||||
const stream = this.openaiAdapter.fimStream(
|
||||
toFimBody(prefix, suffix, completionOptions),
|
||||
signal,
|
||||
);
|
||||
for await (const chunk of stream) {
|
||||
const result = fromChatCompletionChunk(chunk);
|
||||
|
|
@ -558,10 +559,13 @@ export abstract class BaseLLM implements ILLM {
|
|||
let completion = "";
|
||||
try {
|
||||
if (this.shouldUseOpenAIAdapter("streamComplete") && this.openaiAdapter) {
|
||||
for await (const chunk of this.openaiAdapter.completionStream({
|
||||
...toCompleteBody(prompt, completionOptions),
|
||||
stream: true,
|
||||
})) {
|
||||
for await (const chunk of this.openaiAdapter.completionStream(
|
||||
{
|
||||
...toCompleteBody(prompt, completionOptions),
|
||||
stream: true,
|
||||
},
|
||||
signal,
|
||||
)) {
|
||||
const content = chunk.choices[0]?.text ?? "";
|
||||
completion += content;
|
||||
yield content;
|
||||
|
|
@ -622,10 +626,13 @@ export abstract class BaseLLM implements ILLM {
|
|||
|
||||
let completion: string;
|
||||
if (this.shouldUseOpenAIAdapter("complete") && this.openaiAdapter) {
|
||||
const result = await this.openaiAdapter.completionNonStream({
|
||||
...toCompleteBody(prompt, completionOptions),
|
||||
stream: false,
|
||||
});
|
||||
const result = await this.openaiAdapter.completionNonStream(
|
||||
{
|
||||
...toCompleteBody(prompt, completionOptions),
|
||||
stream: false,
|
||||
},
|
||||
signal,
|
||||
);
|
||||
completion = result.choices[0].text;
|
||||
} else {
|
||||
completion = await this._complete(prompt, signal, completionOptions);
|
||||
|
|
@ -688,10 +695,13 @@ export abstract class BaseLLM implements ILLM {
|
|||
}
|
||||
} else {
|
||||
if (this.shouldUseOpenAIAdapter("streamChat") && this.openaiAdapter) {
|
||||
const stream = this.openaiAdapter.chatCompletionStream({
|
||||
...toChatBody(messages, completionOptions),
|
||||
stream: true,
|
||||
});
|
||||
const stream = this.openaiAdapter.chatCompletionStream(
|
||||
{
|
||||
...toChatBody(messages, completionOptions),
|
||||
stream: true,
|
||||
},
|
||||
signal,
|
||||
);
|
||||
for await (const chunk of stream) {
|
||||
const result = fromChatCompletionChunk(chunk);
|
||||
if (result) {
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ They are purely a translation layer, and are not concerned with:
|
|||
- Keeping a private variable with anything that is already passed through the OpenAI request body
|
||||
- Appending "/" to the apiBase (but this is TODO)
|
||||
- Batching embeddings (yes, it requires some knowledge of max batch size, but it's more important to maintain 1 req = 1 req)
|
||||
- Using streamChat for streamComplete and vice-versa if one isn't defined
|
||||
|
||||
They are concerned with:
|
||||
|
||||
|
|
|
|||
|
|
@ -87,6 +87,7 @@ export class AnthropicApi implements BaseLlmApi {
|
|||
|
||||
async chatCompletionNonStream(
|
||||
body: ChatCompletionCreateParamsNonStreaming,
|
||||
signal: AbortSignal,
|
||||
): Promise<ChatCompletion> {
|
||||
const response = await fetch(new URL("messages", this.apiBase), {
|
||||
method: "POST",
|
||||
|
|
@ -97,6 +98,7 @@ export class AnthropicApi implements BaseLlmApi {
|
|||
"x-api-key": this.config.apiKey,
|
||||
},
|
||||
body: JSON.stringify(this._convertBody(body)),
|
||||
signal,
|
||||
});
|
||||
|
||||
const completion = (await response.json()) as any;
|
||||
|
|
@ -127,6 +129,7 @@ export class AnthropicApi implements BaseLlmApi {
|
|||
}
|
||||
async *chatCompletionStream(
|
||||
body: ChatCompletionCreateParamsStreaming,
|
||||
signal: AbortSignal,
|
||||
): AsyncGenerator<ChatCompletionChunk, any, unknown> {
|
||||
body.messages;
|
||||
const response = await fetch(new URL("messages", this.apiBase), {
|
||||
|
|
@ -138,6 +141,7 @@ export class AnthropicApi implements BaseLlmApi {
|
|||
"x-api-key": this.config.apiKey,
|
||||
},
|
||||
body: JSON.stringify(this._convertBody(body)),
|
||||
signal,
|
||||
});
|
||||
|
||||
for await (const value of streamSse(response as any)) {
|
||||
|
|
@ -165,16 +169,19 @@ export class AnthropicApi implements BaseLlmApi {
|
|||
}
|
||||
async completionNonStream(
|
||||
body: CompletionCreateParamsNonStreaming,
|
||||
signal: AbortSignal,
|
||||
): Promise<Completion> {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
async *completionStream(
|
||||
body: CompletionCreateParamsStreaming,
|
||||
signal: AbortSignal,
|
||||
): AsyncGenerator<Completion, any, unknown> {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
async *fimStream(
|
||||
body: FimCreateParamsStreaming,
|
||||
signal: AbortSignal,
|
||||
): AsyncGenerator<ChatCompletionChunk, any, unknown> {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -62,6 +62,7 @@ export class AzureOpenAIApi implements BaseLlmApi {
|
|||
body:
|
||||
| ChatCompletionCreateParamsStreaming
|
||||
| ChatCompletionCreateParamsNonStreaming,
|
||||
signal: AbortSignal,
|
||||
) {
|
||||
return {
|
||||
maxTokens: body.max_tokens ?? undefined,
|
||||
|
|
@ -71,16 +72,18 @@ export class AzureOpenAIApi implements BaseLlmApi {
|
|||
presencePenalty: body.presence_penalty ?? undefined,
|
||||
stop:
|
||||
typeof body.stop === "string" ? [body.stop] : (body.stop ?? undefined),
|
||||
abortSignal: signal,
|
||||
};
|
||||
}
|
||||
|
||||
async chatCompletionNonStream(
|
||||
body: ChatCompletionCreateParamsNonStreaming,
|
||||
signal: AbortSignal,
|
||||
): Promise<ChatCompletion> {
|
||||
const completion = await this.client.getChatCompletions(
|
||||
body.model,
|
||||
body.messages,
|
||||
this._bodyToOptions(body),
|
||||
this._bodyToOptions(body, signal),
|
||||
);
|
||||
return {
|
||||
...completion,
|
||||
|
|
@ -108,11 +111,12 @@ export class AzureOpenAIApi implements BaseLlmApi {
|
|||
}
|
||||
async *chatCompletionStream(
|
||||
body: ChatCompletionCreateParamsStreaming,
|
||||
signal: AbortSignal,
|
||||
): AsyncGenerator<ChatCompletionChunk> {
|
||||
const events = await this.client.streamChatCompletions(
|
||||
body.model,
|
||||
body.messages,
|
||||
this._bodyToOptions(body),
|
||||
this._bodyToOptions(body, signal),
|
||||
);
|
||||
|
||||
const eventBuffer: ChatCompletionChunk[] = [];
|
||||
|
|
@ -169,6 +173,7 @@ export class AzureOpenAIApi implements BaseLlmApi {
|
|||
}
|
||||
async completionNonStream(
|
||||
body: CompletionCreateParamsNonStreaming,
|
||||
signal: AbortSignal,
|
||||
): Promise<Completion> {
|
||||
const { prompt, logprobs, ...restOfBody } = body;
|
||||
const messages = [
|
||||
|
|
@ -177,10 +182,13 @@ export class AzureOpenAIApi implements BaseLlmApi {
|
|||
content: prompt as any,
|
||||
},
|
||||
];
|
||||
const resp = await this.chatCompletionNonStream({
|
||||
messages,
|
||||
...restOfBody,
|
||||
});
|
||||
const resp = await this.chatCompletionNonStream(
|
||||
{
|
||||
messages,
|
||||
...restOfBody,
|
||||
},
|
||||
signal,
|
||||
);
|
||||
return {
|
||||
...resp,
|
||||
object: "text_completion",
|
||||
|
|
@ -194,6 +202,7 @@ export class AzureOpenAIApi implements BaseLlmApi {
|
|||
}
|
||||
async *completionStream(
|
||||
body: CompletionCreateParamsStreaming,
|
||||
signal: AbortSignal,
|
||||
): AsyncGenerator<Completion> {
|
||||
const { prompt, logprobs, ...restOfBody } = body;
|
||||
const messages = [
|
||||
|
|
@ -202,10 +211,13 @@ export class AzureOpenAIApi implements BaseLlmApi {
|
|||
content: prompt as any,
|
||||
},
|
||||
];
|
||||
for await (const event of this.chatCompletionStream({
|
||||
messages,
|
||||
...restOfBody,
|
||||
})) {
|
||||
for await (const event of this.chatCompletionStream(
|
||||
{
|
||||
messages,
|
||||
...restOfBody,
|
||||
},
|
||||
signal,
|
||||
)) {
|
||||
yield {
|
||||
...event,
|
||||
object: "text_completion",
|
||||
|
|
|
|||
|
|
@ -1,59 +0,0 @@
|
|||
import {
|
||||
Completion,
|
||||
CompletionCreateParamsNonStreaming,
|
||||
CompletionCreateParamsStreaming,
|
||||
} from "openai/resources/completions";
|
||||
import {
|
||||
CreateEmbeddingResponse,
|
||||
EmbeddingCreateParams,
|
||||
} from "openai/resources/embeddings";
|
||||
import {
|
||||
ChatCompletion,
|
||||
ChatCompletionChunk,
|
||||
ChatCompletionCreateParamsNonStreaming,
|
||||
ChatCompletionCreateParamsStreaming,
|
||||
} from "openai/resources/index";
|
||||
import { Model } from "openai/resources/models";
|
||||
import {
|
||||
BaseLlmApi,
|
||||
CreateRerankResponse,
|
||||
FimCreateParamsStreaming,
|
||||
RerankCreateParams,
|
||||
} from "./base.js";
|
||||
|
||||
export class AskSage implements BaseLlmApi {
|
||||
chatCompletionNonStream(
|
||||
body: ChatCompletionCreateParamsNonStreaming,
|
||||
): Promise<ChatCompletion> {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
chatCompletionStream(
|
||||
body: ChatCompletionCreateParamsStreaming,
|
||||
): AsyncGenerator<ChatCompletionChunk> {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
completionNonStream(
|
||||
body: CompletionCreateParamsNonStreaming,
|
||||
): Promise<Completion> {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
completionStream(
|
||||
body: CompletionCreateParamsStreaming,
|
||||
): AsyncGenerator<Completion> {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
fimStream(
|
||||
body: FimCreateParamsStreaming,
|
||||
): AsyncGenerator<ChatCompletionChunk> {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
embed(body: EmbeddingCreateParams): Promise<CreateEmbeddingResponse> {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
rerank(body: RerankCreateParams): Promise<CreateRerankResponse> {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
list(): Promise<Model[]> {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
}
|
||||
|
|
@ -61,6 +61,7 @@ export class CohereApi implements BaseLlmApi {
|
|||
|
||||
async chatCompletionNonStream(
|
||||
body: ChatCompletionCreateParamsNonStreaming,
|
||||
signal: AbortSignal,
|
||||
): Promise<ChatCompletion> {
|
||||
const headers = {
|
||||
"Content-Type": "application/json",
|
||||
|
|
@ -71,6 +72,7 @@ export class CohereApi implements BaseLlmApi {
|
|||
method: "POST",
|
||||
headers,
|
||||
body: JSON.stringify(this._convertBody(body)),
|
||||
signal,
|
||||
});
|
||||
|
||||
const data = (await resp.json()) as any;
|
||||
|
|
@ -89,6 +91,7 @@ export class CohereApi implements BaseLlmApi {
|
|||
|
||||
async *chatCompletionStream(
|
||||
body: ChatCompletionCreateParamsStreaming,
|
||||
signal: AbortSignal,
|
||||
): AsyncGenerator<ChatCompletionChunk> {
|
||||
const headers = {
|
||||
"Content-Type": "application/json",
|
||||
|
|
@ -99,6 +102,7 @@ export class CohereApi implements BaseLlmApi {
|
|||
method: "POST",
|
||||
headers,
|
||||
body: JSON.stringify(this._convertBody(body)),
|
||||
signal,
|
||||
});
|
||||
|
||||
for await (const value of streamJSON(resp as any)) {
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@ export class DeepSeekApi extends OpenAIApi {
|
|||
|
||||
async *fimStream(
|
||||
body: FimCreateParamsStreaming,
|
||||
signal: AbortSignal,
|
||||
): AsyncGenerator<ChatCompletionChunk, any, unknown> {
|
||||
const endpoint = new URL("beta/completions", this.apiBase);
|
||||
const resp = await fetch(endpoint, {
|
||||
|
|
@ -38,6 +39,7 @@ export class DeepSeekApi extends OpenAIApi {
|
|||
Accept: "application/json",
|
||||
Authorization: `Bearer ${this.config.apiKey}`,
|
||||
},
|
||||
signal,
|
||||
});
|
||||
for await (const chunk of streamSse(resp as any)) {
|
||||
yield chatChunk({
|
||||
|
|
|
|||
|
|
@ -116,12 +116,16 @@ export class GeminiApi implements BaseLlmApi {
|
|||
|
||||
async chatCompletionNonStream(
|
||||
body: ChatCompletionCreateParamsNonStreaming,
|
||||
signal: AbortSignal,
|
||||
): Promise<ChatCompletion> {
|
||||
let completion = "";
|
||||
for await (const chunk of this.chatCompletionStream({
|
||||
...body,
|
||||
stream: true,
|
||||
})) {
|
||||
for await (const chunk of this.chatCompletionStream(
|
||||
{
|
||||
...body,
|
||||
stream: true,
|
||||
},
|
||||
signal,
|
||||
)) {
|
||||
completion += chunk.choices[0].delta.content;
|
||||
}
|
||||
return {
|
||||
|
|
@ -147,6 +151,7 @@ export class GeminiApi implements BaseLlmApi {
|
|||
|
||||
async *chatCompletionStream(
|
||||
body: ChatCompletionCreateParamsStreaming,
|
||||
signal: AbortSignal,
|
||||
): AsyncGenerator<ChatCompletionChunk> {
|
||||
const apiURL = new URL(
|
||||
`models/${body.model}:streamGenerateContent?key=${this.config.apiKey}`,
|
||||
|
|
@ -155,6 +160,7 @@ export class GeminiApi implements BaseLlmApi {
|
|||
const resp = await fetch(apiURL, {
|
||||
method: "POST",
|
||||
body: JSON.stringify(this._convertBody(body, apiURL)),
|
||||
signal,
|
||||
});
|
||||
|
||||
let buffer = "";
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@ export class MoonshotApi extends OpenAIApi {
|
|||
|
||||
async *fimStream(
|
||||
body: FimCreateParamsStreaming,
|
||||
signal: AbortSignal,
|
||||
): AsyncGenerator<ChatCompletionChunk, any, unknown> {
|
||||
const endpoint = new URL("v1/chat/completions", this.apiBase);
|
||||
const resp = await fetch(endpoint, {
|
||||
|
|
@ -42,6 +43,7 @@ export class MoonshotApi extends OpenAIApi {
|
|||
Accept: "application/json",
|
||||
Authorization: `Bearer ${this.config.apiKey}`,
|
||||
},
|
||||
signal,
|
||||
});
|
||||
|
||||
for await (const chunk of streamSse(resp as any)) {
|
||||
|
|
|
|||
|
|
@ -34,34 +34,43 @@ export class OpenAIApi implements BaseLlmApi {
|
|||
|
||||
async chatCompletionNonStream(
|
||||
body: ChatCompletionCreateParamsNonStreaming,
|
||||
signal: AbortSignal,
|
||||
): Promise<ChatCompletion> {
|
||||
const response = await this.openai.chat.completions.create(body);
|
||||
const response = await this.openai.chat.completions.create(body, {
|
||||
signal,
|
||||
});
|
||||
return response;
|
||||
}
|
||||
async *chatCompletionStream(
|
||||
body: ChatCompletionCreateParamsStreaming,
|
||||
signal: AbortSignal,
|
||||
): AsyncGenerator<ChatCompletionChunk, any, unknown> {
|
||||
const response = await this.openai.chat.completions.create(body);
|
||||
const response = await this.openai.chat.completions.create(body, {
|
||||
signal,
|
||||
});
|
||||
for await (const result of response) {
|
||||
yield result;
|
||||
}
|
||||
}
|
||||
async completionNonStream(
|
||||
body: CompletionCreateParamsNonStreaming,
|
||||
signal: AbortSignal,
|
||||
): Promise<Completion> {
|
||||
const response = await this.openai.completions.create(body);
|
||||
const response = await this.openai.completions.create(body, { signal });
|
||||
return response;
|
||||
}
|
||||
async *completionStream(
|
||||
body: CompletionCreateParamsStreaming,
|
||||
signal: AbortSignal,
|
||||
): AsyncGenerator<Completion, any, unknown> {
|
||||
const response = await this.openai.completions.create(body);
|
||||
const response = await this.openai.completions.create(body, { signal });
|
||||
for await (const result of response) {
|
||||
yield result;
|
||||
}
|
||||
}
|
||||
async *fimStream(
|
||||
body: FimCreateParamsStreaming,
|
||||
signal: AbortSignal,
|
||||
): AsyncGenerator<ChatCompletionChunk, any, unknown> {
|
||||
const endpoint = new URL("fim/completions", this.apiBase);
|
||||
const resp = await fetch(endpoint, {
|
||||
|
|
@ -85,6 +94,7 @@ export class OpenAIApi implements BaseLlmApi {
|
|||
"x-api-key": this.config.apiKey ?? "",
|
||||
Authorization: `Bearer ${this.config.apiKey}`,
|
||||
},
|
||||
signal,
|
||||
});
|
||||
for await (const chunk of streamSse(resp as any)) {
|
||||
if (chunk.choices && chunk.choices.length > 0) {
|
||||
|
|
|
|||
|
|
@ -41,26 +41,31 @@ export interface BaseLlmApi {
|
|||
// Chat, no stream
|
||||
chatCompletionNonStream(
|
||||
body: ChatCompletionCreateParamsNonStreaming,
|
||||
signal: AbortSignal,
|
||||
): Promise<ChatCompletion>;
|
||||
|
||||
// Chat, stream
|
||||
chatCompletionStream(
|
||||
body: ChatCompletionCreateParamsStreaming,
|
||||
signal: AbortSignal,
|
||||
): AsyncGenerator<ChatCompletionChunk>;
|
||||
|
||||
// Completion, no stream
|
||||
completionNonStream(
|
||||
body: CompletionCreateParamsNonStreaming,
|
||||
signal: AbortSignal,
|
||||
): Promise<Completion>;
|
||||
|
||||
// Completion, stream
|
||||
completionStream(
|
||||
body: CompletionCreateParamsStreaming,
|
||||
signal: AbortSignal,
|
||||
): AsyncGenerator<Completion>;
|
||||
|
||||
// FIM, stream
|
||||
fimStream(
|
||||
body: FimCreateParamsStreaming,
|
||||
signal: AbortSignal,
|
||||
): AsyncGenerator<ChatCompletionChunk>;
|
||||
|
||||
// Embeddings
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue