Tool calls, orDocumentation Index
Fetch the complete documentation index at: https://platform.kimi.ai/docs/llms.txt
Use this file to discover all available pages before exploring further.
tool_calls, evolved from function calls (function_call). In certain contexts, or when reading compatibility code, you can consider tool_calls and function_call to be the same. function_call is a subset of tool_calls.
What are Tool Calls?
Tool calls give the Kimi large language model the ability to perform specific actions. The Kimi large language model can engage in conversations and answer questions, which is its “talking” ability. Through tool calls, it also gains the ability to “do” things. Withtool_calls, the Kimi large language model can help you search the internet, query databases, and even control smart home devices.
A tool call involves several steps:
- Define the tool using JSON Schema format;
- Submit the defined tool to the Kimi large language model via the
toolsparameter. You can submit multiple tools at once; - The Kimi large language model will decide which tool(s) to use based on the context of the current conversation. It can also choose not to use any tools;
- The Kimi large language model will output the parameters and information needed to call the tool in JSON format;
- Use the parameters output by the Kimi large language model to execute the corresponding tool and submit the results back to the Kimi large language model;
- The Kimi large language model will respond to the user based on the results of the tool execution;
Why can’t the Kimi large language model execute the tools itself? Why do we need to “help” the Kimi large language model execute the tools based on the parameters it generates? If we are the ones executing the tool calls, what is the role of the Kimi large language model?We will use a practical example of a tool call to explain these questions to the reader.
Enable the Kimi Large Language Model to Access the Internet via tool_calls
The knowledge of the Kimi large language model comes from its training data. For questions that are time-sensitive, the Kimi large language model cannot find answers from its existing knowledge. In such cases, we want the Kimi large language model to search the internet for the latest information and answer our questions based on that information.
Define the Tools
Imagine how we find the information we want on the internet:- We open a search engine, such as Baidu or Bing, and search for the content we want. We then browse the search results and decide which one to click based on the website title and description;
- We might open one or more web pages from the search results and browse them to obtain the knowledge we need;
JSON Schema is a vocabulary that you can use to annotate and validate JSON documents. JSON Schema is a JSON document used to describe the format of JSON data.We define the following JSON Schema:
name, and the type of this field is string, for example:
- python
- node.js
name, description, and parameters.properties are defined by the tool provider. The description explains the specific function and when to use the tool, while parameters outlines the specific parameters needed to successfully call the tool, including parameter types and descriptions. Ultimately, the Kimi large language model will generate a JSON Object that meets the defined requirements as the parameters (arguments) for the tool call based on the JSON Schema.
Register Tools
Let’s try submitting thesearch tool to the Kimi large language model to see if it can correctly call the tool:
- python
- node.js
finish_reason is tool_calls, which means that the response is not the answer from Kimi large language model, but rather the tool that Kimi large language model has chosen to execute. You can determine whether the current response from Kimi large language model is a tool call tool_calls by checking the value of finish_reason.
In the message section, the content field is empty because the model is currently executing tool_calls and has not yet generated a response for the user. Meanwhile, a new field tool_calls has been added. The tool_calls field is a list that contains all the tool call information for this execution. This also indicates another characteristic of tool_calls: the model can choose to call multiple tools at once, which can be different tools or the same tool with different parameters. Each element in tool_calls represents a tool call. Kimi large language model generates a unique id for each tool call. The function.name field indicates the name of the function being executed, and the parameters are placed in function.arguments. The arguments parameter is a valid serialized JSON Object (additionally, the type parameter is currently a fixed value function).
Next, we should use the tool call parameters generated by Kimi large language model to execute the specific tools.
Execute the Tools
Kimi large language model does not execute the tools for us. We need to execute the parameters generated by Kimi large language model after receiving them. Before explaining how to execute the tools, let’s first address the question we raised earlier:Why can’t Kimi large language model execute the tools itself, but instead requires us to “help” it execute the tools based on the parameters generated by Kimi large language model? If we are the ones executing the tool calls, what is the purpose of Kimi large language model?Let’s imagine a scenario where we use Kimi large language model: we provide users with a smart robot based on Kimi large language model. In this scenario, there are three roles: the user, the robot, and Kimi large language model. The user asks the robot a question, the robot calls the Kimi large language model API, and returns the API result to the user. When using
tool_calls, the user asks the robot a question, the robot calls the Kimi API with tools, Kimi large language model returns the tool_calls parameters, the robot executes the tool_calls, submits the results back to the Kimi API, Kimi large language model generates the message to be returned to the user (finish_reason=stop), and only then does the robot return the message to the user. Throughout this process, the entire tool_calls process is transparent and implicit to the user.
Returning to the question above, as users, we are not actually executing the tool calls, nor do we directly “see” the tool calls. Instead, the robot that provides us with the service is completing the tool calls and presenting us with the final response generated by Kimi large language model.
Let’s explain how to execute the tool_calls returned by Kimi large language model from the perspective of the “robot”:
- python
- node.js
while loop to execute the code logic that includes tool calls because the Kimi large language model typically doesn’t make just one tool call, especially in the context of online searching. Usually, Kimi will first call the search tool to get search results, and then call the crawl tool to convert the URLs in the search results into actual web page content. The overall structure of the messages is as follows:
search and crawl methods, when you ask Kimi to search online, it will call the search and crawl tools and give you the correct response based on the tool call results.
Common Questions and Notes
About Streaming Output
In streaming output mode (stream), tool_calls are still applicable, but there are some additional things to note, as follows:
- During streaming output, since
finish_reasonwill appear in the last data chunk, it is recommended to check if thedelta.tool_callsfield exists to determine if the current response includes a tool call; - During streaming output,
delta.contentwill be output first, followed bydelta.tool_calls, so you must wait untildelta.contenthas finished outputting before you can determine and identifytool_calls; - During streaming output, we will specify the
tool_call.idandtool_call.function.namein the initial data chunk, and onlytool_call.function.argumentswill be output in subsequent chunks; - During streaming output, if Kimi returns multiple
tool_callsat once, we will use an additional field calledindexto indicate the index of the currenttool_call, so that you can correctly concatenate thetool_call.function.argumentsparameters. We use a code example from the streaming output section (without using the SDK) to illustrate how to do this:
- python
- node.js
tool_calls in streaming output using the openai SDK:
- python
- node.js
About tool_calls and function_call
tool_calls is an advanced version of function_call. Since OpenAI has marked parameters such as function_call (for example, functions) as “deprecated,” our API will no longer support function_call. You can consider using tool_calls instead of function_call. Compared to function_call, tool_calls has the following advantages:
- It supports parallel calls. The Kimi large language model can return multiple
tool_callsat once. You can use concurrency in your code to call thesetool_callsimultaneously, reducing time consumption; - For
tool_callsthat have no dependencies, the Kimi large language model will also tend to call them in parallel. Compared to the original sequential calls offunction_call, this reduces token consumption to some extent;
About content
When using the tool_calls tool, you may notice that under the condition of finish_reason=tool_calls, the message.content field is occasionally not empty. Typically, the content here is the Kimi large language model explaining which tools need to be called and why these tools need to be called. Its significance lies in the fact that if your tool call process takes a long time, or if completing a round of chat requires multiple sequential tool calls, providing a descriptive sentence to the user before calling the tool can reduce the anxiety or dissatisfaction that users may feel due to waiting. Additionally, explaining to the user which tools are being called and why helps them understand the entire tool call process and allows them to intervene and correct in a timely manner (for example, if the user thinks the current tool selection is incorrect, they can terminate the tool call in time, or correct the model’s tool selection in the next round of chat through a prompt).
About Tokens
The content in thetools parameter is also counted in the total Tokens. Please ensure that the total number of Tokens in tools and messages does not exceed the model’s context window size.
About Message Layout
In scenarios where tools are called, our messages are no longer laid out like this:tool_calls, ensure that each tool_call has a corresponding message with role=tool, and that this message has the correct tool_call_id. If the number of role=tool messages does not match the number of tool_calls, or if the tool_call_id in the role=tool messages cannot be matched with the tool_call.id in tool_calls, an error will occur.
If You Encounter the tool_call_id not found Error
If you encounter the tool_call_id not found error, it may be because you did not add the role=assistant message returned by the Kimi API to the messages list. The correct message sequence should look like this:
tool_call_id not found error by executing messages.append(message) each time you receive a return value from the Kimi API, to add the message returned by the Kimi API to the messages list.
Note: Assistant messages added to the messages list before the role=tool message must fully include the tool_calls field and its values returned by the Kimi API. We recommend directly adding the choice.message returned by the Kimi API to the messages list “as is” to avoid potential errors.