Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Mlc ai Web llm Tool Choice Request

From Leeroopedia

Template:Knowledge

Overview

Tool_Choice_Request implements the Mlc_ai_Web_llm_Tool_Choice_Configuration principle by providing the TypeScript type definitions that control whether and which tools the model should invoke. These types are used in the tool_choice field of ChatCompletionRequest.

Source Reference

  • Tool choice types: src/openai_api_protocols/chat_completion.ts, Lines 841-873
  • Request field: src/openai_api_protocols/chat_completion.ts, Lines 221-235
  • Repository: mlc-ai/web-llm

Code Reference

Named Tool Choice Interface

// src/openai_api_protocols/chat_completion.ts L841-857

/**
 * Specifies a tool the model should use. Use to force the model to call a specific
 * function.
 */
export interface ChatCompletionNamedToolChoice {
  function: ChatCompletionNamedToolChoice.Function;

  /**
   * The type of the tool. Currently, only `function` is supported.
   */
  type: "function";
}

export namespace ChatCompletionNamedToolChoice {
  export interface Function {
    /**
     * The name of the function to call.
     */
    name: string;
  }
}

Tool Choice Option Union Type

// src/openai_api_protocols/chat_completion.ts L859-873

/**
 * Controls which (if any) function is called by the model. `none` means the model
 * will not call a function and instead generates a message. `auto` means the model
 * can pick between generating a message or calling a function. Specifying a
 * particular function via
 * `{"type": "function", "function": {"name": "my_function"}}` forces the model to
 * call that function.
 *
 * `none` is the default when no functions are present. `auto` is the default if
 * functions are present.
 */
export type ChatCompletionToolChoiceOption =
  | "none"
  | "auto"
  | ChatCompletionNamedToolChoice;

Request Integration

// src/openai_api_protocols/chat_completion.ts L221-235

/**
 * Controls which (if any) function is called by the model.
 * `none` is the default when no functions are present. `auto` is the default if
 * functions are present.
 */
tool_choice?: ChatCompletionToolChoiceOption;

/**
 * A list of tools the model may call. Currently, only functions are supported as a
 * tool. Use this to provide a list of functions the model may generate JSON inputs for.
 */
tools?: Array<ChatCompletionTool>;

I/O Contract

Type breakdown:

Type Form Purpose
"none" String literal Disables all tool calling
"auto" String literal Model decides whether to call a tool
ChatCompletionNamedToolChoice Object Forces a specific function call

ChatCompletionNamedToolChoice structure:

{
  "type": "function",
  "function": {
    "name": "function_name_here"
  }
}

Import:

import type {
  ChatCompletionToolChoiceOption,
  ChatCompletionNamedToolChoice,
} from "@mlc-ai/web-llm";

Constraints:

  • ChatCompletionNamedToolChoice.type must always be the literal "function".
  • The function.name must match one of the tool names provided in the tools array.
  • tool_choice is optional on the request; it defaults to "none" when no tools are present and "auto" when tools are present.

Usage Examples

Using the "auto" string literal:

const request: webllm.ChatCompletionRequest = {
  messages: [{ role: "user", content: "What is 2 + 2?" }],
  tools: tools,
  tool_choice: "auto",
};

Using the "none" string literal:

const request: webllm.ChatCompletionRequest = {
  messages: [{ role: "user", content: "Summarize the results" }],
  tools: tools,
  tool_choice: "none",
};

Using a named tool choice object:

const namedChoice: webllm.ChatCompletionNamedToolChoice = {
  type: "function",
  function: { name: "get_current_weather" },
};

const request: webllm.ChatCompletionRequest = {
  messages: [{ role: "user", content: "Check the weather" }],
  tools: tools,
  tool_choice: namedChoice,
};

Omitting tool_choice (relying on defaults):

// When tools are provided and tool_choice is omitted, defaults to "auto"
const requestWithTools: webllm.ChatCompletionRequest = {
  messages: [{ role: "user", content: "Get weather for NYC" }],
  tools: tools,
  // tool_choice defaults to "auto"
};

// When tools are not provided, tool_choice defaults to "none"
const requestWithoutTools: webllm.ChatCompletionRequest = {
  messages: [{ role: "user", content: "Hello!" }],
  // tool_choice defaults to "none"
};

Type-safe tool choice construction:

function forceToolCall(
  functionName: string,
): webllm.ChatCompletionToolChoiceOption {
  return {
    type: "function",
    function: { name: functionName },
  };
}

const request: webllm.ChatCompletionRequest = {
  messages: [{ role: "user", content: "Look up the temperature" }],
  tools: tools,
  tool_choice: forceToolCall("get_current_weather"),
};

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment