Instituting LLM fallback to OpenAI if gaming PC is not on
This commit is contained in:
13
llm.py
13
llm.py
@@ -4,9 +4,14 @@ from ollama import Client
|
|||||||
from openai import OpenAI
|
from openai import OpenAI
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
from dotenv import load_dotenv
|
||||||
|
|
||||||
|
load_dotenv()
|
||||||
|
|
||||||
logging.basicConfig(level=logging.INFO)
|
logging.basicConfig(level=logging.INFO)
|
||||||
|
|
||||||
|
TRY_OLLAMA = os.getenv("TRY_OLLAMA", False)
|
||||||
|
|
||||||
|
|
||||||
class LLMClient:
|
class LLMClient:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
@@ -30,7 +35,9 @@ class LLMClient:
|
|||||||
prompt: str,
|
prompt: str,
|
||||||
system_prompt: str,
|
system_prompt: str,
|
||||||
):
|
):
|
||||||
|
# Instituting a fallback if my gaming PC is not on
|
||||||
if self.PROVIDER == "ollama":
|
if self.PROVIDER == "ollama":
|
||||||
|
try:
|
||||||
response = self.ollama_client.chat(
|
response = self.ollama_client.chat(
|
||||||
model="gemma3:4b",
|
model="gemma3:4b",
|
||||||
messages=[
|
messages=[
|
||||||
@@ -41,9 +48,11 @@ class LLMClient:
|
|||||||
{"role": "user", "content": prompt},
|
{"role": "user", "content": prompt},
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
print(response)
|
|
||||||
output = response.message.content
|
output = response.message.content
|
||||||
elif self.PROVIDER == "openai":
|
return output
|
||||||
|
except Exception as e:
|
||||||
|
logging.error(f"Could not connect to OLLAMA: {str(e)}")
|
||||||
|
|
||||||
response = self.openai_client.responses.create(
|
response = self.openai_client.responses.create(
|
||||||
model="gpt-4o-mini",
|
model="gpt-4o-mini",
|
||||||
input=[
|
input=[
|
||||||
|
|||||||
Reference in New Issue
Block a user