tool_calls feature of the Kimi API to enable the Kimi large language model to perform internet searches. Let’s review the process we implemented:
- We defined tools using the JSON Schema format. For internet searches, we defined two tools:
searchandcrawl. - We submitted the defined
searchandcrawltools to the Kimi large language model via thetoolsparameter. - The Kimi large language model would select to call
searchandcrawlbased on the context of the current conversation, generate the relevant parameters, and output them in JSON format. - We used the parameters output by the Kimi large language model to execute the
searchandcrawlfunctions and submitted the results of these functions back to the Kimi large language model. - The Kimi large language model would then provide a response to the user based on the results of the tool executions.
search and crawl functions ourselves, which might include:
- Calling search engine APIs or implementing our own content search.
- Retrieving search results, including URLs and summaries.
- Fetching web page content based on URLs, which might require different reading rules for different websites.
- Cleaning and organizing the fetched web page content into a format that the model can easily recognize, such as Markdown.
- Handling various errors and exceptions, such as no search results or failure to fetch web page content.
tool_calls usage of the Kimi large language model, we have provided a built-in tool function builtin_function.$web_search to enable internet search functionality.
The basic usage and process of the $web_search function are the same as the usual tool_calls, but there are still some minor differences. We will explain in detail through examples how to call the built-in $web_search function of Kimi to enable internet search functionality and mark the items that need extra attention in the code and explanations.
$web_search Declaration
Unlike ordinary tool, the $web_search function does not require specific parameter descriptions. It only needs the type and function.name in the tools declaration to successfully register the $web_search function:
$web_search function is prefixed with a dollar sign $, which is our agreed way to indicate Kimi built-in functions (in ordinary function definitions, the dollar sign $ is not allowed), and if there are other Kimi built-in functions in the future, they will also be prefixed with the dollar sign $.
When using $web_search function, you must disable the thinking ability of the model.
When declaring tools, $web_search can coexist with other ordinary function. Furthermore, builtin_function can coexist with ordinary function. You can add both builtin_function and ordinary function to tools, or add both builtin_function and ordinary function at the same time.
Next, let’s modify the original tool_calls code to explain how to execute tool_calls.
$web_search Execution
Here is the modified tool_calls code:
- python
- node.js
$web_search function, its basic process is no different from that of a regular function. Developers don’t even need to modify the original code for executing tool calls. The part that is different and particularly noteworthy is that when we implement the search_impl function, we don’t include much logic for searching, parsing, or obtaining web content. We simply return the parameters generated by Kimi large language model, tool_call.function.arguments, as they are to complete the tool call. Why is that?
In fact, as the name builtin_function suggests, $web_search is a built-in function of Kimi large language model. It is defined and executed by Kimi large language model. The process is as follows:
- When Kimi large language model generates a response with
finish_reason=tool_calls, it means that Kimi large language model has realized that it needs to execute the$web_searchfunction and has already prepared everything for it; - Kimi large language model will return the necessary parameters for executing the function in the form of
tool_call.function.arguments. However, these parameters are not executed by the caller. The caller just needs to submittool_call.function.argumentsto Kimi large language model as they are, and Kimi large language model will execute the corresponding online search process; - When the user submits
tool_call.function.argumentsusing amessagewithrole=tool, Kimi large language model will immediately start the online search process and generate a readable message for the user based on the search and reading results, which is amessagewithfinish_reason=stop;
Compatibility Note
The online search function provided by the Kimi API aims to offer a reliable large language model online search solution without breaking the compatibility of the original API and SDK. It is fully compatible with the original tool call feature of Kimi large language model. This means that: if you want to switch from Kimi’s online search function to your own implementation, you can do so in just two simple steps without disrupting the overall structure of your code:- Modify the
tooldefinition of$web_searchto your own implementation (includingname,description, etc.). You may need to add additional information intool.functionto inform the model of the specific parameters it needs to generate. You can add any parameters you need in theparametersfield; - Change the implementation of the
search_implfunction. When using Kimi’s$web_search, you just need to return the input parametersargumentsas they are. However, if you use your own online search service, you may need to fully implement thesearchandcrawlfunctions mentioned at the beginning of the article;
About Token Consumption
When using the$web_search function provided by Kimi, the search results are also counted towards the tokens occupied by the prompt (i.e., prompt_tokens). Typically, since the results of web searches contain a lot of content, the token consumption can be quite high. To avoid unknowingly using up a large number of tokens, we add an extra field called total_tokens when generating the arguments for the $web_search function. This field informs the caller of the total number of tokens occupied by the search content, which will be included in the prompt_tokens once the entire web search process is completed. We will use specific code to demonstrate how to obtain these token consumptions:
About Model Size Selection
Another issue that arises is that when the web search function is enabled, the number of tokens can change significantly, exceeding the context window of the originally used model. This may trigger anInput token length too long error message. Therefore, when using the web search function, we recommend using the dynamic model kimi-k2.5 to adapt to changes in token counts. We slightly modify the chat function to use the kimi-k2.5 model:
About Other Tools
The$web_search tool can be used in combination with other regular tools. You can freely mix tools with type=builtin_function and type=function.