2024-09-16 10:03:26 +02:00
|
|
|
import requests
|
2024-09-16 10:29:26 +02:00
|
|
|
import json
|
2024-09-16 11:44:35 +02:00
|
|
|
from transformers import AutoTokenizer, LlamaForCausalLM
|
2024-09-16 10:03:26 +02:00
|
|
|
|
2024-09-16 13:38:30 +02:00
|
|
|
|
2024-09-16 11:27:00 +02:00
|
|
|
class API:
|
2024-09-16 15:14:24 +02:00
|
|
|
# This method processes a message via transformers. (NOT FINISHED!)
|
2024-09-16 11:27:00 +02:00
|
|
|
@staticmethod
|
2024-09-16 11:44:35 +02:00
|
|
|
def process_text_transformers(prompt, model):
|
|
|
|
model = LlamaForCausalLM.from_pretrained(model)
|
|
|
|
tokenizer = AutoTokenizer.from_pretrained(model)
|
|
|
|
|
|
|
|
inputs = tokenizer(prompt, return_tensors="pt")
|
|
|
|
|
|
|
|
generate_ids = model.generate(inputs.input_ids, max_length=30)
|
|
|
|
return tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
|
|
|
|
|
2024-09-16 15:14:24 +02:00
|
|
|
# This method processes a message via ollama
|
2024-09-16 11:44:35 +02:00
|
|
|
@staticmethod
|
2024-09-17 08:18:19 +02:00
|
|
|
def process_text_local(prompt, model, system):
|
2024-09-16 10:29:26 +02:00
|
|
|
ollama_url = "http://localhost:11434"
|
|
|
|
|
|
|
|
response = requests.post(
|
2024-09-17 08:18:19 +02:00
|
|
|
f"{ollama_url}/api/generate", json={"model": model, "prompt": prompt, "system": system}
|
2024-09-16 10:29:26 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
2024-09-16 12:17:06 +02:00
|
|
|
return final_response
|
2024-09-16 10:29:26 +02:00
|
|
|
else:
|
|
|
|
return "Error: " + response.text
|
2024-09-16 10:03:26 +02:00
|
|
|
|
2024-09-16 15:14:24 +02:00
|
|
|
# This method sends a message to a certain AI.
|
2024-09-17 08:18:19 +02:00
|
|
|
|
|
|
|
|
|
|
|
def send_message(self, message, model, system):
|
2024-09-16 11:27:00 +02:00
|
|
|
if model == 1:
|
2024-09-17 08:18:19 +02:00
|
|
|
answer = self.process_text_local(message, "phi3.5", system)
|
2024-09-16 11:27:00 +02:00
|
|
|
elif model == 2:
|
2024-09-17 08:18:19 +02:00
|
|
|
answer = self.process_text_local(message, "gemma2:2b", system)
|
2024-09-16 11:27:00 +02:00
|
|
|
elif model == 3:
|
2024-09-17 08:18:19 +02:00
|
|
|
answer = self.process_text_local(message, "qwen2:0.5b", system)
|
2024-09-16 11:27:00 +02:00
|
|
|
elif model == 4:
|
2024-09-17 08:18:19 +02:00
|
|
|
answer = self.process_text_local(message, "codegemma:2b", system)
|
2024-09-16 11:44:35 +02:00
|
|
|
elif model == 5:
|
|
|
|
answer = self.process_text_transformers(message, "meta-llama/Meta-Llama-3.1-8B")
|
2024-09-16 11:27:00 +02:00
|
|
|
else:
|
|
|
|
return "Invalid choice"
|
2024-09-16 13:38:30 +02:00
|
|
|
return answer
|