continue-custom/core/llm/llms/CustomLLM.ts
Fred Bricon 57b18d73d6 Cancel stale completion requests as you type
Signed-off-by: Fred Bricon <fbricon@gmail.com>
2024-11-20 17:25:03 +01:00

86 lines
2.2 KiB
TypeScript

import {
ChatMessage,
CompletionOptions,
CustomLLM,
ModelProvider,
} from "../../index.js";
import { BaseLLM } from "../index.js";
class CustomLLMClass extends BaseLLM {
get providerName(): ModelProvider {
return "custom";
}
private customStreamCompletion?: (
prompt: string,
signal: AbortSignal,
options: CompletionOptions,
fetch: (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>,
) => AsyncGenerator<string>;
private customStreamChat?: (
messages: ChatMessage[],
signal: AbortSignal,
options: CompletionOptions,
fetch: (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>,
) => AsyncGenerator<string>;
constructor(custom: CustomLLM) {
super(custom.options || { model: "custom" });
this.customStreamCompletion = custom.streamCompletion;
this.customStreamChat = custom.streamChat;
}
protected async *_streamChat(
messages: ChatMessage[],
signal: AbortSignal,
options: CompletionOptions,
): AsyncGenerator<ChatMessage> {
if (this.customStreamChat) {
for await (const content of this.customStreamChat(
messages,
signal,
options,
(...args) => this.fetch(...args),
)) {
yield { role: "assistant", content };
}
} else {
for await (const update of super._streamChat(messages, signal, options)) {
yield update;
}
}
}
protected async *_streamComplete(
prompt: string,
signal: AbortSignal,
options: CompletionOptions,
): AsyncGenerator<string> {
if (this.customStreamCompletion) {
for await (const content of this.customStreamCompletion(
prompt,
signal,
options,
(...args) => this.fetch(...args),
)) {
yield content;
}
} else if (this.customStreamChat) {
for await (const content of this.customStreamChat(
[{ role: "user", content: prompt }],
signal,
options,
(...args) => this.fetch(...args),
)) {
yield content;
}
} else {
throw new Error(
"Either streamCompletion or streamChat must be defined in a custom LLM in config.ts",
);
}
}
}
export default CustomLLMClass;