QVAC Logo

completion( )

Generates completion from a language model based on conversation history.

function completion(params): object;

Parameters

NameTypeRequired?Description
paramsobjectThe completion parameters

params

FieldTypeRequired?Description
modelIdstringModel identifier
historyobject[]Conversation history (array of messages with role and content)
streambooleanWhether to stream the response
toolsTool[]Optional array of tools for function calling
kvCacheKeystringOptional KV cache key for persistent context

Returns

object — Object with tokenStream generator, toolCallStream generator, text promise, toolCalls promise, and stats promise

FieldTypeDescription
textPromise<string>Complete generated text
tokenStreamAsyncGenerator<string>Stream of tokens
toolCallsPromise<object[]>Tool calls made during completion
toolCallStreamAsyncGenerator<object>Stream of tool call events
statsPromise<object | undefined>Performance statistics

stats object

FieldTypeDescription
cacheTokensnumberNumber of cached tokens
timeToFirstTokennumberTime to first token in ms
tokensPerSecondnumberTokens per second

toolCallStream events

Tool call event:

FieldTypeDescription
type"toolCall"Event type
call.idstringCall identifier
call.namestringTool name
call.argumentsRecord<string, unknown>Tool arguments
call.rawstringRaw call text (optional)

Tool call error event:

FieldTypeDescription
type"toolCallError"Event type
error.code"PARSE_ERROR" | "VALIDATION_ERROR" | "UNKNOWN_TOOL"Error code
error.messagestringError message
error.rawstringRaw text (optional)

Example

import { z } from "zod";

// Simple tool definition with 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"),
      country: z.string().describe("Country code").optional(),
    }),
  }]
});

for await (const token of result.tokenStream) {
  process.stdout.write(token);
}

On this page