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

226 lines
6.9 KiB
TypeScript
Raw Normal View History

/**
* OpenAI-compatible text completion service (generic local server).
*
* Works with LM Studio, llama.cpp, vLLM, Ollama OpenAI-compat endpoint, etc.
*
* Env:
* OPENAI_COMPAT_URL (required e.g. http://localhost:1234/v1)
* OPENAI_COMPAT_KEY (default: sk-no-key-required)
* OPENAI_COMPAT_MODEL (default: default)
*/
import OpenAI from "openai";
import {
2026-06-01 16:22:25 -05:00
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,
type ProcessorConfig,
type LlmResult,
type LlmChunk,
} from "@trustgraph/base";
2026-06-01 23:19:54 -05:00
import { Effect, Layer, Stream } from "effect";
import {
optionalStringConfig,
providerStatusError,
requiredString,
toAsyncGenerator,
type TextCompletionRuntimeError,
} from "./common.ts";
2026-06-01 20:26:47 -05:00
export type OpenAICompatibleProcessorConfig = ProcessorConfig & {
model?: string;
apiKey?: string;
baseUrl?: string;
temperature?: number;
maxOutput?: number;
};
2026-06-01 23:19:54 -05:00
type ResolvedOpenAICompatibleConfig = {
readonly defaultModel: string;
readonly defaultTemperature: number;
readonly maxOutput: number;
readonly apiKey: string;
readonly baseURL: string;
};
const loadOpenAICompatibleConfig = Effect.fn("loadOpenAICompatibleConfig")(function*(
config: OpenAICompatibleProcessorConfig,
) {
const defaultModel =
config.model ?? (yield* optionalStringConfig("OpenAI-Compatible", "OPENAI_COMPAT_MODEL")) ?? "default";
const baseURL = yield* requiredString(
config.baseUrl ?? (yield* optionalStringConfig("OpenAI-Compatible", "OPENAI_COMPAT_URL")),
"OpenAI-Compatible",
"OPENAI_COMPAT_URL",
"OpenAI-compatible server URL not specified (set OPENAI_COMPAT_URL)",
);
const apiKey =
config.apiKey ?? (yield* optionalStringConfig("OpenAI-Compatible", "OPENAI_COMPAT_KEY")) ?? "sk-no-key-required";
return {
defaultModel,
defaultTemperature: config.temperature ?? 0.0,
maxOutput: config.maxOutput ?? 4096,
apiKey,
baseURL,
} satisfies ResolvedOpenAICompatibleConfig;
});
const mapOpenAICompatibleError = (error: unknown): TextCompletionRuntimeError =>
providerStatusError("OpenAI-Compatible", error);
2026-06-01 20:26:47 -05:00
export function makeOpenAICompatibleProvider(
config: OpenAICompatibleProcessorConfig,
): LlmProvider {
2026-06-01 23:19:54 -05:00
const {
defaultModel,
defaultTemperature,
maxOutput,
apiKey,
baseURL,
} = Effect.runSync(loadOpenAICompatibleConfig(config)) satisfies ResolvedOpenAICompatibleConfig;
2026-06-01 20:26:47 -05:00
const client = new OpenAI({ baseURL, apiKey });
2026-06-01 23:19:54 -05:00
Effect.runSync(Effect.log("[OpenAI-Compatible] LLM service initialized"));
2026-06-01 20:26:47 -05:00
return {
2026-06-01 23:19:54 -05:00
generateContent: (
2026-06-01 20:26:47 -05:00
system: string,
prompt: string,
model?: string,
temperature?: number,
): Promise<LlmResult> => {
const modelName = model ?? defaultModel;
const temp = temperature ?? defaultTemperature;
2026-06-01 23:19:54 -05:00
return Effect.runPromise(
Effect.tryPromise({
try: () =>
client.chat.completions.create({
model: modelName,
messages: [
{ role: "system", content: system },
{ role: "user", content: prompt },
],
temperature: temp,
max_tokens: maxOutput,
}),
catch: mapOpenAICompatibleError,
}).pipe(
Effect.map((resp): LlmResult => ({
text: resp.choices[0].message.content ?? "",
inToken: resp.usage?.prompt_tokens ?? 0,
outToken: resp.usage?.completion_tokens ?? 0,
model: modelName,
})),
),
);
2026-06-01 20:26:47 -05:00
},
supportsStreaming: () => true,
2026-06-01 23:19:54 -05:00
generateContentStream: (
2026-06-01 20:26:47 -05:00
system: string,
prompt: string,
model?: string,
temperature?: number,
2026-06-01 23:19:54 -05:00
): AsyncGenerator<LlmChunk> => {
2026-06-01 20:26:47 -05:00
const modelName = model ?? defaultModel;
const temp = temperature ?? defaultTemperature;
2026-06-01 23:19:54 -05:00
const stream = Stream.fromEffect(
Effect.tryPromise({
try: () =>
client.chat.completions.create({
model: modelName,
messages: [
{ role: "system", content: system },
{ role: "user", content: prompt },
],
temperature: temp,
max_tokens: maxOutput,
stream: true,
}),
catch: mapOpenAICompatibleError,
}),
).pipe(
Stream.flatMap((openAIStream) => {
const iterator = openAIStream[Symbol.asyncIterator]();
2026-06-01 20:26:47 -05:00
let totalInputTokens = 0;
let totalOutputTokens = 0;
2026-06-01 23:19:54 -05:00
return Stream.unfold<"pulling" | "done", LlmChunk, TextCompletionRuntimeError, never>(
"pulling",
(state) => {
if (state === "done") return Effect.void as Effect.Effect<undefined>;
return Effect.gen(function* () {
while (true) {
const next = yield* Effect.tryPromise({
try: () => iterator.next(),
catch: mapOpenAICompatibleError,
});
if (next.done === true) {
return [{
text: "",
inToken: totalInputTokens,
outToken: totalOutputTokens,
model: modelName,
isFinal: true,
}, "done"] as const;
}
const chunk = next.value;
const content = chunk.choices[0]?.delta?.content;
if (chunk.usage !== null && chunk.usage !== undefined) {
totalInputTokens = chunk.usage.prompt_tokens;
totalOutputTokens = chunk.usage.completion_tokens;
}
if (content !== null && content !== undefined && content.length > 0) {
return [{
text: content,
inToken: null,
outToken: null,
model: modelName,
isFinal: false,
}, "pulling"] as const;
}
}
});
},
);
}),
);
return toAsyncGenerator(Stream.toAsyncIterable(stream), mapOpenAICompatibleError);
2026-06-01 20:26:47 -05:00
},
};
}
2026-06-01 20:26:47 -05:00
export type OpenAICompatibleProcessor = ReturnType<typeof makeOpenAICompatibleProcessor>;
export function makeOpenAICompatibleProcessor(
config: OpenAICompatibleProcessorConfig,
): ReturnType<typeof makeLlmService> {
return makeLlmService(config, makeOpenAICompatibleProvider(config));
}
export const OpenAICompatibleProcessor = makeOpenAICompatibleProcessor;
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(makeOpenAICompatibleProvider(config))),
2026-06-01 16:22:25 -05:00
),
2026-05-12 08:06:58 -05:00
});
2026-06-01 23:19:54 -05:00
export function run(): Promise<void> {
return Effect.runPromise(program);
}