-
Notifications
You must be signed in to change notification settings - Fork 0
/
interactive.py
77 lines (60 loc) · 1.58 KB
/
interactive.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"""
This example shows how to create interactive tools.
Install `ollama` here: https://ollama.com
Download `uv` to run this example: https://github.com/astral-sh/uv
```
ollama pull llama3.2
uv run examples/interactive.py
```
"""
# /// script
# dependencies = [
# "ollama",
# "hype @ git+https://github.com/mattt/hype.git",
# ]
# ///
import ollama
import hype
@hype.up
def get_user_name() -> str:
"""
Get the user's name.
This method takes no arguments and returns the user's name.
:return: The user's name.
"""
return input("What is your name? ")
if __name__ == "__main__":
tools = hype.create_ollama_tools(
[
get_user_name,
],
)
response = ollama.chat(
model="llama3.2",
messages=[
{
"role": "user",
"content": "Ask the user their name and then greet them by name.",
},
],
tools=tools,
)
name, *_ = tools(response["message"]["tool_calls"]) # pylint: disable=unsubscriptable-object
print()
print(f"Hello, {name}!")
print("Please wait while I generate a poem for you...")
print()
response = ollama.chat(
model="llama3.2",
messages=[
{
"role": "user",
"content": f"""
Write an acrostic poem about a person named "{name}".
Return the poem as a string. Do not include any other text.
DO NOT MISSPELL THE PERSON'S NAME.
""",
},
],
)
print(response["message"]["content"])