-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy path01_simple_client.py
More file actions
46 lines (37 loc) · 1.16 KB
/
01_simple_client.py
File metadata and controls
46 lines (37 loc) · 1.16 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
40
41
42
43
44
45
46
import json
import os
import requests
OLLAMA_URL = os.environ.get("OLLAMA_URL", "http://localhost:11434")
def ask_ollama(prompt):
url = f"{OLLAMA_URL}/api/chat"
headers = {"Content-Type": "application/json", "accept": "application/json"}
model = "qwen3:0.6b"
system_prompt = "Answer in Portuguese"
payload = {
"model": model,
"messages": [
{"role": "system", "content": system_prompt},
{"role": "user", "content": prompt},
],
}
response = requests.post(url, headers=headers, json=payload, stream=True)
for line in response.iter_lines():
if line:
data = json.loads(line.decode("utf-8"))
message = data.get("message", {}).get("content", "")
if message:
print(message, end="")
print()
def main():
while True:
prompt = input("Enter your prompt: ")
if not prompt:
print("Prompt cannot be empty.")
continue
if prompt.strip() in ["exit", "quit", "q"]:
print("Exiting...")
break
ask_ollama(prompt)
print("-" * 50)
if __name__ == "__main__":
main()