-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOllamaTools.py
60 lines (52 loc) · 1.43 KB
/
OllamaTools.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
48
49
50
51
52
53
54
55
56
57
58
59
60
from ollama import chat
import datetime
# Tool definition
tools = [
{
"type": "function",
"function": {
"name": "get_current_time",
"description": "Get the current time",
"parameters": {"type": "object", "properties": {}}
}
}
]
# Initial conversation
messages = [
{"role": "system", "content": "Use tools when asked for the time."},
{"role": "user", "content": "What time is it?"}
]
# Initial call
response = chat(
model="llama3.1",
messages=messages,
tools=tools
)
def get_current_time():
return datetime.datetime.now().strftime("%H:%M:%S")
# If a function call is detected
if hasattr(response.message, 'tool_calls') and response.message.tool_calls:
# Add the assistant's response to the context
messages.append({"role": "assistant", "content": "", "tool_calls": response.message.tool_calls})
# Fictional tool response
tool_response = {
"role": "tool",
"name": "get_current_time",
"content": f"{get_current_time()}"
}
messages.append(tool_response)
# Final response
response2 = chat(
model="llama3.1",
messages=messages,
stream=True
)
final_response = ""
print("Final response: ", end="")
for chunk in response2 :
a = chunk.message.content
print(a, end="")
final_response += a
print()
else:
print("No function call detected.")