-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathai.py
47 lines (36 loc) · 1.34 KB
/
ai.py
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import requests
import json
import logging
import os.path as path
from env import Env
def load_prompt(prompt_name, words):
prompt = open(path.join("prompts", prompt_name)).read()
words_as_str = ", ".join(words)
prompt += f"\nHere are the words: {words_as_str}"
return prompt
class AiModel:
def __init__(self):
pass
def name(self) -> str:
pass
def generate_response(self):
pass
class Gpt4All(AiModel):
def __init__(self, prompt_name, words):
self.url = "http://localhost:4891/v1/chat/completions"
prompt = load_prompt(prompt_name, words)
self.data = {
"model": "Llama 3.2 3B Instruct",
"messages": [{"role":"user", "content":f"{prompt}"}],
"max_tokens": 1024,
}
def generate_response(self):
try:
logging.info(f"Sending a request to Gpt4All, API server - {self.url}")
response = requests.post(self.url, data=json.dumps(self.data))
logging.info("Waiting for a response...")
return response.json()["choices"][0]["message"]["content"]
except Exception as e:
logging.error(f"Error ocurred while generating response {e}, response status code: {response.status_code}, response description = {response.text}")
def name(self) -> str:
return "GPT"