Implementation:Mlc ai Web llm Chat Completion Tool
Overview
Chat_Completion_Tool implements the Mlc_ai_Web_llm_Tool_Definition principle by providing the TypeScript type definitions for declaring callable functions as tools in the web-llm chat completion API. These types directly mirror the OpenAI function calling specification.
Source Reference
- File:
src/openai_api_protocols/chat_completion.ts, Lines 801-835 - Repository: mlc-ai/web-llm
Code Reference
The implementation consists of three type definitions that form a hierarchy:
/**
* The parameters the functions accepts, described as a JSON Schema object.
* Omitting `parameters` defines a function with an empty parameter list.
*/
export type FunctionParameters = Record<string, unknown>;
export interface FunctionDefinition {
/**
* The name of the function to be called. Must be a-z, A-Z, 0-9, or contain
* underscores and dashes, with a maximum length of 64.
*/
name: string;
/**
* A description of what the function does, used by the model to choose when and
* how to call the function.
*/
description?: string;
/**
* The parameters the functions accepts, described as a JSON Schema object.
* Omitting `parameters` defines a function with an empty parameter list.
*/
parameters?: FunctionParameters;
}
export interface ChatCompletionTool {
function: FunctionDefinition;
/**
* The type of the tool. Currently, only `function` is supported.
*/
type: "function";
}
I/O Contract
Type hierarchy:
ChatCompletionTool wraps a FunctionDefinition and tags it with a type discriminator.
| Type | Purpose | Fields |
|---|---|---|
FunctionParameters |
JSON Schema for function parameters | Record<string, unknown> (any valid JSON Schema object)
|
FunctionDefinition |
Describes a single callable function | name (required), description (optional), parameters (optional)
|
ChatCompletionTool |
Wraps a function definition as a tool | function (required), type: "function" (required, literal)
|
Integration point:
ChatCompletionTool objects are passed in the tools field of ChatCompletionRequest (line 235):
// From ChatCompletionRequest (line 235)
tools?: Array<ChatCompletionTool>;
Import:
import { ChatCompletionTool } from "@mlc-ai/web-llm";
Usage Examples
Basic tool definition:
import * as webllm from "@mlc-ai/web-llm";
const weatherTool: webllm.ChatCompletionTool = {
type: "function",
function: {
name: "get_current_weather",
description: "Get the current weather in a given location",
parameters: {
type: "object",
properties: {
location: {
type: "string",
description: "The city and state, e.g. San Francisco, CA",
},
unit: {
type: "string",
enum: ["celsius", "fahrenheit"],
},
},
required: ["location"],
},
},
};
Multiple tools in a request:
const tools: Array<webllm.ChatCompletionTool> = [
{
type: "function",
function: {
name: "get_stock_fundamentals",
description: "Get fundamental data for a given stock symbol",
parameters: {
type: "object",
properties: {
symbol: {
type: "string",
description: "The stock ticker symbol, e.g. TSLA",
},
},
required: ["symbol"],
},
},
},
{
type: "function",
function: {
name: "get_stock_price",
description: "Get the current price for a stock symbol",
parameters: {
type: "object",
properties: {
symbol: { type: "string" },
},
required: ["symbol"],
},
},
},
];
Parameterless tool:
const tool: webllm.ChatCompletionTool = {
type: "function",
function: {
name: "get_server_time",
description: "Returns the current server time in UTC",
// No parameters field -- empty parameter list
},
};
Related Pages
- Principle:Mlc_ai_Web_llm_Tool_Definition
- Mlc_ai_Web_llm_Function_Calling_Model_Ids -- Model identifiers that support these tool definitions
- Mlc_ai_Web_llm_Tool_Choice_Request -- Controlling which tool is called
- Mlc_ai_Web_llm_Get_Tool_Call_From_Output -- Parsing model output into tool call objects