First AI API.
This commit is contained in:
parent
3d63c5a37c
commit
7cdcc35a2a
1 changed files with 25 additions and 3 deletions
26
py/api.py
26
py/api.py
|
@ -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
|
Reference in a new issue