36 lines
1.1 KiB
TypeScript
36 lines
1.1 KiB
TypeScript
import { streamText } from "ai";
|
|
import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
|
|
|
|
const provider = createOpenAICompatible({
|
|
name: "llama.cpp",
|
|
apiKey: "local",
|
|
baseURL: "http://127.0.0.1:8080/v1",
|
|
includeUsage: true, // Include usage information in streaming responses
|
|
});
|
|
|
|
const { fullStream, reasoningText, text } = streamText({
|
|
model: provider("qwen3.5-35b-a3b"),
|
|
prompt: Bun.argv[2]!,
|
|
});
|
|
|
|
for await (const part of fullStream) {
|
|
if (part.type === "reasoning-start") {
|
|
process.stdout.write("\u001b[2m\n ");
|
|
} else if (part.type === "reasoning-delta") {
|
|
process.stdout.write(part.text.replaceAll('\n', '\n '));
|
|
} else if (part.type === "reasoning-end") {
|
|
process.stdout.write("\n\u001b[22m");
|
|
} else if (part.type === "text-start") {
|
|
process.stdout.write("\n");
|
|
} else if (part.type === "text-delta") {
|
|
process.stdout.write(part.text);
|
|
} else if (part.type === "text-end") {
|
|
process.stdout.write("\n");
|
|
}
|
|
}
|
|
|
|
const reasoning = await reasoningText;
|
|
const response = await text;
|
|
|
|
Bun.file("reasoning.md").write(reasoning ?? "");
|
|
Bun.file("response.md").write(response);
|