mirror of
https://github.com/FJurmanovic/sugarr.git
synced 2026-11-11 07:41:08 +00:00
51 lines
1.3 KiB
TypeScript
51 lines
1.3 KiB
TypeScript
import OpenAI from 'openai';
|
|
|
|
export interface ChatMessage {
|
|
role: 'system' | 'user' | 'assistant';
|
|
content: string;
|
|
}
|
|
|
|
export interface ChatOptions {
|
|
model: string;
|
|
temperature?: number;
|
|
maxTokens?: number;
|
|
}
|
|
|
|
export interface ChatResult {
|
|
content: string;
|
|
usage?: { promptTokens?: number; completionTokens?: number; totalTokens?: number };
|
|
}
|
|
|
|
export class LlmClient {
|
|
private readonly client: OpenAI;
|
|
|
|
constructor(opts: { baseUrl: string; apiKey?: string }) {
|
|
this.client = new OpenAI({ baseURL: opts.baseUrl, apiKey: opts.apiKey || 'not-needed' });
|
|
}
|
|
|
|
async chat(messages: ChatMessage[], options: ChatOptions): Promise<ChatResult> {
|
|
const response = await this.client.chat.completions.create({
|
|
model: options.model,
|
|
temperature: options.temperature,
|
|
max_tokens: options.maxTokens,
|
|
messages,
|
|
});
|
|
|
|
const choice = response.choices[0];
|
|
if (!choice) {
|
|
throw new Error('LLM returned no choices');
|
|
}
|
|
|
|
return {
|
|
content: choice.message?.content ?? '',
|
|
usage: response.usage
|
|
? {
|
|
promptTokens: response.usage.prompt_tokens,
|
|
completionTokens: response.usage.completion_tokens,
|
|
totalTokens: response.usage.total_tokens,
|
|
}
|
|
: undefined,
|
|
};
|
|
}
|
|
}
|