-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat.py
More file actions
55 lines (45 loc) · 1.55 KB
/
Copy pathchat.py
File metadata and controls
55 lines (45 loc) · 1.55 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
47
48
49
50
51
52
53
54
55
import requests
import time
# Model name to use with Ollama
MODEL = "llama3"
# URL for the Ollama chat API (Docker container mapped to localhost)
OLLAMA_URL = "http://localhost:11434/api/chat"
def ask_llama3(prompt):
"""
Send a prompt to the Ollama API using the llama3 model.
Returns the model response and elapsed time.
"""
payload = {
"model": MODEL,
"messages": [{"role": "user", "content": prompt}],
"stream": False
}
start_time = time.time()
response = requests.post(OLLAMA_URL, json=payload)
elapsed_time = time.time() - start_time
if response.status_code == 200:
content = response.json()["message"]["content"]
return content.strip(), elapsed_time
else:
return f"❌ Error {response.status_code}: {response.text}", elapsed_time
def chat():
"""
Terminal chat loop for interacting with the llama3 model via Ollama.
"""
print("🤠 Welcome to Charro Bot (powered by llama3 via Ollama)")
print("Type 'exit' to quit.\n")
while True:
try:
user_input = input("🧑 You: ").strip()
if user_input.lower() in ["exit", "quit"]:
print("👋 Goodbye!")
break
print("✅ Thinking...")
reply, duration = ask_llama3(user_input)
print(f"🤖 Charro Bot: {reply}")
print(f"⏱ Response time: {duration:.2f} seconds\n")
except KeyboardInterrupt:
print("\n👋 Interrupted. Exiting...")
break
if __name__ == "__main__":
chat()