-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathfunction_calling.py
47 lines (40 loc) · 1.52 KB
/
function_calling.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from openai import OpenAI
# Configure client to use local server
client = OpenAI(
# base_url="http://localhost:11434/v1", # Point to ollama server
base_url="http://localhost:10240/v1", # Point to mlx omni server
api_key="not-needed", # API key is not required for local server
)
def chat(model: str = "mlx-community/Llama-3.2-3B-Instruct-4bit"):
tools = [
{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA",
},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
},
"required": ["location"],
},
},
}
]
messages = [{"role": "user", "content": "What's the weather like in Boston today?"}]
return client.chat.completions.create(
model=model, messages=messages, tools=tools, tool_choice="auto"
)
if __name__ == "__main__":
completion = chat(model="mlx-community/Llama-3.2-3B-Instruct-4bit")
message = completion.choices[0].message
print(f"message: {message}")
if message.tool_calls:
print("success")
else:
print("failed")