ai-virtual-assistant/py/api.py

30 lines
878 B
Python
Raw Normal View History

2024-09-16 10:03:26 +02:00
import requests
2024-09-16 10:29:26 +02:00
import json
2024-09-16 10:03:26 +02:00
2024-09-16 09:58:54 +02:00
class api:
2024-09-16 10:29:26 +02:00
def initialize_ollama(self, prompt):
ollama_url = "http://localhost:11434"
model = "phi3.5"
response = requests.post(
f"{ollama_url}/api/generate", json={"model": model, "prompt": prompt}
)
if response.status_code == 200:
response_data = []
for line in response.iter_lines():
line_decoded = line.decode("utf-8")
line_raw = json.loads(line_decoded)
response_data.append(line_raw["response"])
final_response = "".join(response_data)
generated_text = final_response.splitlines()[-1]
return generated_text
else:
return "Error: " + response.text
2024-09-16 10:03:26 +02:00
2024-09-16 10:29:26 +02:00
def send_message(self, message):
answer = self.initialize_ollama(message)
return answer