-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackend.py
More file actions
30 lines (24 loc) · 839 Bytes
/
Copy pathbackend.py
File metadata and controls
30 lines (24 loc) · 839 Bytes
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
import subprocess
def chat_with_ollama(conversation):
"""Send conversation history to Ollama and return bot reply."""
full_prompt = "\n".join(conversation) + "\nBot:"
try:
process = subprocess.Popen(
["ollama", "run", "gemma3:1b"],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
encoding="utf-8", # 🔹 Add this
errors="ignore" # 🔹 Avoid crash on weird chars
)
# Send prompt to Ollama
process.stdin.write(full_prompt)
process.stdin.close()
bot_reply = ""
for line in process.stdout:
bot_reply += line
process.wait()
return bot_reply.strip()
except Exception as e:
return f"⚠️ Error: {str(e)}"