You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Response: Message(role='assistant', content="Hello! I can help you with a wide range of tasks and questions. Whether you need assistance with information, problem-solving, learning something new, or just want to have a conversation, I'm here to help. What specifically would you like assistance with today?", tool_calls=None, tool_results=None, refusal=None)
49
51
```
50
52
51
53
## Parameters
@@ -109,27 +111,33 @@ tools = [
109
111
}
110
112
]
111
113
112
-
# Implement the function
114
+
# Implement the tool function
113
115
defget_weather(location: str) -> str:
114
116
returnf"The weather in {location} is sunny."
115
117
116
118
# Initialize the client with tools and function implementations
117
-
xai= XAI(
118
-
api_key="your_api_key",
119
+
llm= XAI(
120
+
api_key=api_key,
119
121
model="grok-2-1212",
120
122
tools=tools,
121
-
function_map={"get_weather": get_weather}# Map function names to implementations
123
+
function_map={"get_weather": get_weather}
122
124
)
123
125
124
-
# Make a request that might trigger function calling
125
-
response =xai.invoke(
126
+
# Make a request that will use function calling
127
+
response =llm.invoke(
126
128
messages=[
127
-
{"role": "user", "content": "What's the weather like in San Francisco?"}
129
+
{"role": "user", "content": "What is the weather in San Francisco?"},
128
130
],
129
-
tool_choice="auto"# Can be 'auto', 'required', 'none', or a specific function
131
+
tool_choice="auto"# Can be "auto", "required", or "none"
130
132
)
131
133
132
-
print(response["message"])
134
+
response_message = response.choices[0].message
135
+
print(response_message)
136
+
# Response: Message(role='assistant', content='I am retrieving the weather for San Francisco.', tool_calls=[{'id': '0', 'function': {'name': 'get_weather', 'arguments': '{"location":"San Francisco, CA"}'}, 'type': 'function'}], tool_results=[{'tool_call_id': '0', 'role': 'tool', 'name': 'get_weather', 'content': 'The weather in San Francisco, CA is sunny.'}], refusal=None)
# Response: The weather in San Francisco, CA is sunny.
133
141
```
134
142
135
143
The SDK supports various function calling modes through the `tool_choice` parameter:
@@ -144,37 +152,242 @@ The SDK supports various function calling modes through the `tool_choice` parame
144
152
145
153
For more details, see the [xAI Function Calling Guide](https://docs.x.ai/docs/guides/function-calling) and the [API Reference](https://docs.x.ai/api/endpoints#chat-completions).
146
154
147
-
> **Note**: Currently, using `"required"` or specific function calls may produce unexpected outputs. It is recommended to use either `"auto"` or `"none"` for more reliable results.
148
-
149
155
### Required Parameters for Function Calling
150
156
151
157
When using function calling, you need to provide:
152
158
153
159
-`tools`: List of tool definitions with their schemas
154
-
-`function_map`: Dictionary mapping function names to their actual implementations
155
160
156
-
> **Note**: The `function_map` parameter is required when tools are provided. Each tool name must have a corresponding implementation in the function map.
161
+
### Function Map
162
+
163
+
The `function_map` optional parameter maps tool names to their Python implementations. This allows you to actually invoke the function and append its result to the response. Each function in the map must:
164
+
165
+
- Have a name matching a tool definition
166
+
- Accept the parameters specified in the tool's JSON Schema
167
+
- Return a value that can be converted to a string
168
+
169
+
> **Note**: The `function_map` parameter is not required when tools are provided. However, when omitted, only the tool call with the parameters used by the model will be included in the response.
0 commit comments