trustgraph/ts/packages/flow/src/model/text-completion/openai.ts

148 lines
3.9 KiB
TypeScript
Raw Normal View History

2026-04-05 21:09:33 -05:00
/**
* OpenAI text completion service.
*
* Python reference: trustgraph-flow/trustgraph/model/text_completion/openai/llm.py
*/
import OpenAI from "openai";
2026-05-12 08:06:58 -05:00
import { LlmService, type ProcessorConfig, type LlmResult, type LlmChunk, tooManyRequestsError } from "@trustgraph/base";
import { makeProcessorProgram } from "@trustgraph/base";
2026-04-05 21:09:33 -05:00
export class OpenAIProcessor extends LlmService {
private client: OpenAI;
private readonly defaultModel: string;
private readonly defaultTemperature: number;
private readonly maxOutput: number;
2026-04-05 21:09:33 -05:00
constructor(config: ProcessorConfig & {
model?: string;
apiKey?: string;
baseUrl?: string;
temperature?: number;
maxOutput?: number;
}) {
super(config);
this.defaultModel = config.model ?? "gpt-4o";
this.defaultTemperature = config.temperature ?? 0.0;
this.maxOutput = config.maxOutput ?? 4096;
const apiKey = config.apiKey ?? process.env.OPENAI_TOKEN;
2026-05-12 08:06:58 -05:00
if (apiKey === undefined || apiKey.length === 0) {
throw new Error("OpenAI API key not specified");
}
2026-04-05 21:09:33 -05:00
this.client = new OpenAI({
apiKey,
baseURL: config.baseUrl ?? process.env.OPENAI_BASE_URL,
});
console.log("[OpenAI] LLM service initialized");
}
async generateContent(
system: string,
prompt: string,
model?: string,
temperature?: number,
): Promise<LlmResult> {
const modelName = model ?? this.defaultModel;
const temp = temperature ?? this.defaultTemperature;
try {
const resp = await this.client.chat.completions.create({
model: modelName,
messages: [
{ role: "system", content: system },
{ role: "user", content: prompt },
],
temperature: temp,
max_completion_tokens: this.maxOutput,
});
return {
text: resp.choices[0].message.content ?? "",
inToken: resp.usage?.prompt_tokens ?? 0,
outToken: resp.usage?.completion_tokens ?? 0,
model: modelName,
};
} catch (err) {
if (err instanceof OpenAI.RateLimitError) {
2026-05-12 08:06:58 -05:00
throw tooManyRequestsError();
2026-04-05 21:09:33 -05:00
}
throw err;
}
}
override supportsStreaming(): boolean {
return true;
}
async *generateContentStream(
system: string,
prompt: string,
model?: string,
temperature?: number,
): AsyncGenerator<LlmChunk> {
const modelName = model ?? this.defaultModel;
const temp = temperature ?? this.defaultTemperature;
try {
const stream = await this.client.chat.completions.create({
model: modelName,
messages: [
{ role: "system", content: system },
{ role: "user", content: prompt },
],
temperature: temp,
max_completion_tokens: this.maxOutput,
stream: true,
stream_options: { include_usage: true },
});
let totalInputTokens = 0;
let totalOutputTokens = 0;
for await (const chunk of stream) {
2026-05-12 08:06:58 -05:00
const content = chunk.choices[0]?.delta?.content;
if (content !== null && content !== undefined && content.length > 0) {
2026-04-05 21:09:33 -05:00
yield {
2026-05-12 08:06:58 -05:00
text: content,
2026-04-05 21:09:33 -05:00
inToken: null,
outToken: null,
model: modelName,
isFinal: false,
};
}
2026-05-12 08:06:58 -05:00
if (chunk.usage !== null && chunk.usage !== undefined) {
2026-04-05 21:09:33 -05:00
totalInputTokens = chunk.usage.prompt_tokens;
totalOutputTokens = chunk.usage.completion_tokens;
}
}
yield {
text: "",
inToken: totalInputTokens,
outToken: totalOutputTokens,
model: modelName,
isFinal: true,
};
} catch (err) {
if (err instanceof OpenAI.RateLimitError) {
2026-05-12 08:06:58 -05:00
throw tooManyRequestsError();
2026-04-05 21:09:33 -05:00
}
throw err;
}
}
}
2026-05-12 08:06:58 -05:00
export const program = makeProcessorProgram({
id: "text-completion",
make: (config) => new OpenAIProcessor(config),
});
2026-04-05 21:09:33 -05:00
export async function run(): Promise<void> {
await OpenAIProcessor.launch("text-completion");
}