-
Notifications
You must be signed in to change notification settings - Fork 0
/
ollama.py
executable file
·62 lines (50 loc) · 1.78 KB
/
ollama.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
#!/usr/bin/env python3
import json
import requests
model = "hyppchoipus"
server = "http://0.0.0.0:11434/api/chat"
def chat(messages, model=model, server=server):
"""
Chat with the model using the given messages.
Parameters:
- messages (list): The list of messages to send to the model.
- model (str): The model to chat with. Default is "hyppchoipus".
- server (str): The URL of the server. Default is
"http://0.0.0.0:11434/api/chat".
Returns:
dict: The response from the model.
"""
if not isinstance(messages, list) or not messages:
raise ValueError("chat: messages must be a list")
if not isinstance(model, str) or model == "":
raise ValueError("chat: model must be a non-empty string")
if not isinstance(server, str) or server == "":
raise ValueError("chat: server must be a non-empty string")
try:
r = requests.post(
server,
json={
"model": model,
"messages": messages,
"stream": True},
stream=True
)
r.raise_for_status()
except Exception as e:
print(e)
return
output = ""
for line in r.iter_lines():
body = json.loads(line)
if "error" in body:
raise Exception(body["error"])
if body.get("done") is False:
message = body.get("message", "")
content = message.get("content", "")
output += content
print(content, end="", flush=True)
if body.get("done", False):
message["content"] = output
return message
if __name__ == "__main__":
print(chat([], model, server))