The Assistant API supports function calling. This feature allows an agent to automatically call external functions to perform tasks, such as translating text. This topic uses a simple "Translation Agent" example to help you quickly understand the basics of function calling.
Quick start
In this example, you will create a translation agent and a function named translate_text that the agent can call. You will then ask the agent to translate "Hello world" into Chinese.
Before you begin
You can install the required dependency libraries, such as requests and dashscope, by running the following command:
Step 1: Create the "translate_text" function
First, you can create a simple translation function. This function uses a predefined translation table for demonstration purposes.
- Translation feature: Simulates a translation feature using a predefined translation table that supports conversion between multiple languages.
- Error handling: The function includes a basic error handling mechanism to ensure that it returns an appropriate response in all situations.
translate_text function to provide translation services.
Step 2: Describe the "translate_text" function
You must describe the translate_text function to the agent. The agent uses this description to call the function correctly.
- name: The name of the function is
translate_text. The agent uses this name to call the function. - description: A description of the tool that helps the agent understand its purpose.
- parameters: Defines the function's parameters, which include the text to translate and the target language.
Step 3: Create an agent
Now, you can create an Assistant instance. This instance is an agent that will use the translation tool that you defined.
- model: Specifies the model to use. This example uses
qwen-plus, which supports language understanding and task processing. - name: The name of the agent. Set the value to "Translation Assistant".
- description: A description of the agent's purpose, which is to help users translate text.
- tools: Registers the
translation_toolthat you defined earlier. This allows the agent to call the tool.
Step 4: Create a conversation thread and interact with the agent
Create a new conversation thread, add a user message to it, and then run the agent to process the user's query.
- Threads.create(): Creates a new conversation thread for subsequent messages.
- Messages.create(): Adds a user message to the thread. In this case, the user asks to translate "Hello world" into Chinese.
- Runs.create(): Triggers the agent to start processing the user message.
- Runs.wait(): Waits for the agent to finish processing.
Step 5: Handle the function call and return the result
If the agent needs to call a tool during processing, the translate_text function is called and the result is returned.
- Check for a function call: If the agent needs to call a function, your code checks whether the requested function is
translate_textand then performs the translation using the previously defined function. - Submit the result: Submit the translation result to the agent using
Runs.submit_tool_outputs, and then wait for the agent's next response.
Step 6:Get the agent's response
After the agent finishes processing, you can retrieve the agent's response from the conversation thread and display it to the user.
Summary
By following these steps, you have successfully created an agent that can handle user translation requests and use a translation function to perform text conversion. The Assistant API makes it simple and efficient to build complex, task-driven agents.
You can extend the agent's features as needed, such as by adding more tools or modifying the agent's behavior instructions.
Quickly generate descriptions for business functions
In the Quick Start example, you need to describe the "translate_text" function to the agent. This process can be tedious. Therefore, we provide a simple conversion function to help you quickly describe your business functions.
translate_text function from the Quick Start:
translate_text function is automatically converted to:
Use streaming output
When you use streaming output, you must modify the code logic in Step 5: Handle the function call and return the result. This is because the Runs object now returns an Assistant event stream.
When the Assistant decides to call a function, the Runs object returns the thread.run.requires_action event and the input parameters data.required_action.submit_tool_outputs.tool_calls that are provided by the Large Language Model (LLM). You must submit the function output at this point.
Note that you must also enable streaming output when you submit the function output with run = Runs.submit_tool_outputs.
while loop outside the for loop that processes the event stream. This is because the system generates a new Runs object when you submit the function output. The while loop helps you automatically track the latest event stream. This allows the Assistant to continue generating a response after it receives the result of the function call.