-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple_chat.py
52 lines (44 loc) · 1.22 KB
/
simple_chat.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
import ollama
from typing import List, Dict
import json
from datetime import datetime
def chat_with_model(
model: str = 'gemma2:2b',
messages: List[Dict[str, str]] = None,
print_full_response: bool = False
) -> Dict:
if messages is None:
messages = [{
'role': 'user',
'content': 'Hi'
}]
try:
# Make the API call
response = ollama.chat(
model=model,
messages=messages
)
# Print just the message content
print("\nModel's response:")
print(response['message']['content'])
# Optionally print the full response details
if print_full_response:
print("\nFull response details:")
print(json.dumps(response, indent=2))
return response
except Exception as e:
print(f"An error occurred: {str(e)}")
return None
if __name__ == "__main__":
# Example usage
messages = [
{
'role': 'user',
'content': 'What is the fastest animal on the planet?'
},
]
chat_with_model(
model='gemma2:2b',
messages=messages,
print_full_response=True
)