ai-virtual-assistant/py/api.py
2024-09-16 10:29:26 +02:00

29 lines
878 B
Python

import requests
import json
class api:
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
def send_message(self, message):
answer = self.initialize_ollama(message)
return answer