forked from nashsu/FreeAskInternet
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathipython.py
More file actions
39 lines (32 loc) · 1.03 KB
/
Copy pathipython.py
File metadata and controls
39 lines (32 loc) · 1.03 KB
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
import json
import requests
def main():
body = {
"model": "ollama",
"messages": [
{
"role": "user",
"content": "How would you build a log cabin if you were really cold?",
}
],
"ollama_model": "orca-mini",
}
response = requests.post("http://localhost:8888/v1/chat/completions", json=body)
response_text = response.text
data_chunks = response_text.split("\n")
total_content = ""
for chunk in data_chunks:
if chunk:
clean_json = chunk.replace("data: ", "")
try:
if clean_json:
dict_data = json.loads(clean_json)
print(f"dict_data: {dict_data}")
token = dict_data["choices"][0]["delta"].get("content", "")
if token:
total_content += token
except json.JSONDecodeError as e:
...
print(f"Total content: {total_content}")
if __name__ == "__main__":
main()