completion( )
Generates completion from a language model based on conversation history.
function completion(params: CompletionParams): { stats: Promise<{ cacheTokens: number; timeToFirstToken: number; tokensPerSecond: number } | —>; text: Promise<string>; tokenStream: AsyncGenerator<string>; toolCalls: Promise<ToolCallWithCall[]>; toolCallStream: AsyncGenerator<{ call: { arguments: Record<string, unknown>; id: string; name: string; raw?: string }; type: "toolCall" } | { error: { code: "PARSE_ERROR" | "VALIDATION_ERROR" | "UNKNOWN_TOOL"; message: string; raw?: string }; type: "toolCallError" }> }
| Name | Type | Required? | Description |
|---|
params | CompletionParams | ✓ | The completion parameters |
{ stats: Promise<{ cacheTokens: number; timeToFirstToken: number; tokensPerSecond: number } | —>; text: Promise<string>; tokenStream: AsyncGenerator<string>; toolCalls: Promise<ToolCallWithCall[]>; toolCallStream: AsyncGenerator<{ call: { arguments: Record<string, unknown>; id: string; name: string; raw?: string }; type: "toolCall" } | { error: { code: "PARSE_ERROR" | "VALIDATION_ERROR" | "UNKNOWN_TOOL"; message: string; raw?: string }; type: "toolCallError" }> }
| Field | Type | Description |
|---|
stats | Promise | — |
text | Promise | — |
tokenStream | AsyncGenerator | — |
toolCalls | Promise | — |
toolCallStream | AsyncGenerator | — |
import { z } from "zod";
const result = completion({
modelId: "llama-2",
history: [
{ role: "user", content: "What's the weather in Tokyo?" }
],
stream: true,
tools: [{
name: "get_weather",
description: "Get current weather",
parameters: z.object({
city: z.string().describe("City name"),
}),
handler: async (args) => {
return { temperature: 22, condition: "sunny" };
}
}]
});
for await (const token of result.tokenStream) {
process.stdout.write(token);
}
for (const toolCall of await result.toolCalls) {
if (toolCall.invoke) {
const toolResult = await toolCall.invoke();
console.log(toolResult);
}
}