First AI API.

This commit is contained in:
Patrick_Pluto 2024-09-16 10:29:26 +02:00
parent 3d63c5a37c
commit 7cdcc35a2a

View file

@ -1,7 +1,29 @@
import requests import requests
import json
class api: class api:
def initialize_ollama(self, prompt):
ollama_url = "http://localhost:11434"
model = "phi3.5"
def send_message(message): response = requests.post(
answer = "Test" 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 return answer