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-06-01 16:22:25 -05:00
|
|
|
import {
|
|
|
|
|
Llm,
|
2026-06-01 20:26:47 -05:00
|
|
|
makeLlmService,
|
2026-06-01 16:22:25 -05:00
|
|
|
makeFlowProcessorProgram,
|
|
|
|
|
makeLlmServiceShape,
|
|
|
|
|
makeLlmSpecs,
|
2026-06-01 20:26:47 -05:00
|
|
|
type LlmProvider,
|
2026-06-01 16:22:25 -05:00
|
|
|
type ProcessorConfig,
|
|
|
|
|
type LlmResult,
|
|
|
|
|
type LlmChunk,
|
|
|
|
|
tooManyRequestsError,
|
|
|
|
|
} from "@trustgraph/base";
|
|
|
|
|
import { Effect, Layer } from "effect";
|
2026-04-05 21:09:33 -05:00
|
|
|
|
2026-06-01 20:26:47 -05:00
|
|
|
export type OpenAIProcessorConfig = ProcessorConfig & {
|
|
|
|
|
model?: string;
|
|
|
|
|
apiKey?: string;
|
|
|
|
|
baseUrl?: string;
|
|
|
|
|
temperature?: number;
|
|
|
|
|
maxOutput?: number;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export function makeOpenAIProvider(config: OpenAIProcessorConfig): LlmProvider {
|
|
|
|
|
const defaultModel = config.model ?? "gpt-4o";
|
|
|
|
|
const defaultTemperature = config.temperature ?? 0.0;
|
|
|
|
|
const maxOutput = config.maxOutput ?? 4096;
|
2026-04-05 21:09:33 -05:00
|
|
|
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
|
|
|
|
2026-06-01 20:26:47 -05:00
|
|
|
const client = new OpenAI({
|
2026-04-05 21:09:33 -05:00
|
|
|
apiKey,
|
|
|
|
|
baseURL: config.baseUrl ?? process.env.OPENAI_BASE_URL,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
console.log("[OpenAI] LLM service initialized");
|
2026-06-01 20:26:47 -05:00
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
generateContent: async (
|
|
|
|
|
system: string,
|
|
|
|
|
prompt: string,
|
|
|
|
|
model?: string,
|
|
|
|
|
temperature?: number,
|
|
|
|
|
): Promise<LlmResult> => {
|
|
|
|
|
const modelName = model ?? defaultModel;
|
|
|
|
|
const temp = temperature ?? defaultTemperature;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const resp = await client.chat.completions.create({
|
|
|
|
|
model: modelName,
|
|
|
|
|
messages: [
|
|
|
|
|
{ role: "system", content: system },
|
|
|
|
|
{ role: "user", content: prompt },
|
|
|
|
|
],
|
|
|
|
|
temperature: temp,
|
|
|
|
|
max_completion_tokens: 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) {
|
|
|
|
|
throw tooManyRequestsError();
|
|
|
|
|
}
|
|
|
|
|
throw err;
|
2026-04-05 21:09:33 -05:00
|
|
|
}
|
2026-06-01 20:26:47 -05:00
|
|
|
},
|
|
|
|
|
supportsStreaming: () => true,
|
|
|
|
|
generateContentStream: async function* (
|
|
|
|
|
system: string,
|
|
|
|
|
prompt: string,
|
|
|
|
|
model?: string,
|
|
|
|
|
temperature?: number,
|
|
|
|
|
): AsyncGenerator<LlmChunk> {
|
|
|
|
|
const modelName = model ?? defaultModel;
|
|
|
|
|
const temp = temperature ?? defaultTemperature;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const stream = await client.chat.completions.create({
|
|
|
|
|
model: modelName,
|
|
|
|
|
messages: [
|
|
|
|
|
{ role: "system", content: system },
|
|
|
|
|
{ role: "user", content: prompt },
|
|
|
|
|
],
|
|
|
|
|
temperature: temp,
|
|
|
|
|
max_completion_tokens: maxOutput,
|
|
|
|
|
stream: true,
|
|
|
|
|
stream_options: { include_usage: true },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let totalInputTokens = 0;
|
|
|
|
|
let totalOutputTokens = 0;
|
|
|
|
|
|
|
|
|
|
for await (const chunk of stream) {
|
|
|
|
|
const content = chunk.choices[0]?.delta?.content;
|
|
|
|
|
if (content !== null && content !== undefined && content.length > 0) {
|
|
|
|
|
yield {
|
|
|
|
|
text: content,
|
|
|
|
|
inToken: null,
|
|
|
|
|
outToken: null,
|
|
|
|
|
model: modelName,
|
|
|
|
|
isFinal: false,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (chunk.usage !== null && chunk.usage !== undefined) {
|
|
|
|
|
totalInputTokens = chunk.usage.prompt_tokens;
|
|
|
|
|
totalOutputTokens = chunk.usage.completion_tokens;
|
|
|
|
|
}
|
2026-04-05 21:09:33 -05:00
|
|
|
}
|
|
|
|
|
|
2026-06-01 20:26:47 -05:00
|
|
|
yield {
|
|
|
|
|
text: "",
|
|
|
|
|
inToken: totalInputTokens,
|
|
|
|
|
outToken: totalOutputTokens,
|
|
|
|
|
model: modelName,
|
|
|
|
|
isFinal: true,
|
|
|
|
|
};
|
|
|
|
|
} catch (err) {
|
|
|
|
|
if (err instanceof OpenAI.RateLimitError) {
|
|
|
|
|
throw tooManyRequestsError();
|
2026-04-05 21:09:33 -05:00
|
|
|
}
|
2026-06-01 20:26:47 -05:00
|
|
|
throw err;
|
2026-04-05 21:09:33 -05:00
|
|
|
}
|
2026-06-01 20:26:47 -05:00
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
}
|
2026-04-05 21:09:33 -05:00
|
|
|
|
2026-06-01 20:26:47 -05:00
|
|
|
export type OpenAIProcessor = ReturnType<typeof makeOpenAIProcessor>;
|
|
|
|
|
|
|
|
|
|
export function makeOpenAIProcessor(config: OpenAIProcessorConfig): ReturnType<typeof makeLlmService> {
|
|
|
|
|
return makeLlmService(config, makeOpenAIProvider(config));
|
2026-04-05 21:09:33 -05:00
|
|
|
}
|
|
|
|
|
|
2026-06-01 20:26:47 -05:00
|
|
|
export const OpenAIProcessor = makeOpenAIProcessor;
|
|
|
|
|
|
2026-06-01 16:22:25 -05:00
|
|
|
export const program = makeFlowProcessorProgram<ProcessorConfig, never, Llm>({
|
2026-05-12 08:06:58 -05:00
|
|
|
id: "text-completion",
|
2026-06-01 16:22:25 -05:00
|
|
|
specs: () => makeLlmSpecs(),
|
|
|
|
|
layer: (config) =>
|
|
|
|
|
Layer.succeed(
|
|
|
|
|
Llm,
|
2026-06-01 20:26:47 -05:00
|
|
|
Llm.of(makeLlmServiceShape(makeOpenAIProvider(config))),
|
2026-06-01 16:22:25 -05:00
|
|
|
),
|
2026-05-12 08:06:58 -05:00
|
|
|
});
|
|
|
|
|
|
2026-04-05 21:09:33 -05:00
|
|
|
export async function run(): Promise<void> {
|
2026-06-01 16:22:25 -05:00
|
|
|
await Effect.runPromise(program);
|
2026-04-05 21:09:33 -05:00
|
|
|
}
|