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

160 lines
4.4 KiB
TypeScript
Raw Normal View History

/**
* Mistral text completion service.
*
* Env:
* MISTRAL_TOKEN (required Mistral API key)
* MISTRAL_MODEL (default: ministral-8b-latest)
*/
import { Mistral } from "@mistralai/mistralai";
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,
2026-05-12 08:06:58 -05:00
tooManyRequestsError,
} from "@trustgraph/base";
2026-06-01 16:22:25 -05:00
import { Effect, Layer } from "effect";
2026-06-01 20:26:47 -05:00
export type MistralProcessorConfig = ProcessorConfig & {
model?: string;
apiKey?: string;
temperature?: number;
maxOutput?: number;
};
export function makeMistralProvider(config: MistralProcessorConfig): LlmProvider {
const defaultModel =
config.model ?? process.env.MISTRAL_MODEL ?? "ministral-8b-latest";
const defaultTemperature = config.temperature ?? 0.0;
const maxOutput = config.maxOutput ?? 4096;
const apiKey = config.apiKey ?? process.env.MISTRAL_TOKEN;
2026-05-12 08:06:58 -05:00
if (apiKey === undefined || apiKey.length === 0) {
throw new Error("Mistral API key not specified");
}
2026-06-01 20:26:47 -05:00
const client = new Mistral({ apiKey });
console.log("[Mistral] 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.complete({
model: modelName,
messages: [
{ role: "system", content: system },
{ role: "user", content: prompt },
],
temperature: temp,
maxTokens: maxOutput,
});
return {
text: (resp.choices?.[0]?.message?.content as string) ?? "",
inToken: resp.usage?.promptTokens ?? 0,
outToken: resp.usage?.completionTokens ?? 0,
model: modelName,
};
} catch (err) {
if ((err as any)?.statusCode === 429 || (err as any)?.status === 429) {
throw tooManyRequestsError();
}
throw err;
}
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.stream({
model: modelName,
messages: [
{ role: "system", content: system },
{ role: "user", content: prompt },
],
temperature: temp,
maxTokens: maxOutput,
});
let totalInputTokens = 0;
let totalOutputTokens = 0;
for await (const chunk of stream) {
const delta = chunk.data?.choices?.[0]?.delta;
const content = delta?.content;
if (typeof content === "string" && content.length > 0) {
yield {
text: content,
inToken: null,
outToken: null,
model: modelName,
isFinal: false,
};
}
if (chunk.data?.usage !== undefined) {
totalInputTokens = chunk.data.usage.promptTokens ?? 0;
totalOutputTokens = chunk.data.usage.completionTokens ?? 0;
}
}
2026-06-01 20:26:47 -05:00
yield {
text: "",
inToken: totalInputTokens,
outToken: totalOutputTokens,
model: modelName,
isFinal: true,
};
} catch (err) {
if ((err as any)?.statusCode === 429 || (err as any)?.status === 429) {
throw tooManyRequestsError();
}
2026-06-01 20:26:47 -05:00
throw err;
}
2026-06-01 20:26:47 -05:00
},
};
}
2026-06-01 20:26:47 -05:00
export type MistralProcessor = ReturnType<typeof makeMistralProcessor>;
export function makeMistralProcessor(config: MistralProcessorConfig): ReturnType<typeof makeLlmService> {
return makeLlmService(config, makeMistralProvider(config));
}
2026-06-01 20:26:47 -05:00
export const MistralProcessor = makeMistralProcessor;
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(makeMistralProvider(config))),
2026-06-01 16:22:25 -05:00
),
2026-05-12 08:06:58 -05:00
});
export async function run(): Promise<void> {
2026-06-01 16:22:25 -05:00
await Effect.runPromise(program);
}