Fabelous-Ai-Chat/scripts/BaseOllama.py

41 lines
1.3 KiB
Python
Raw Normal View History

from langchain_community.chat_models import ChatOllama
class OllamaChatBot:
def __init__(self, base_url, model, headers):
self.base_url = base_url
self.model = model
2024-05-20 11:24:41 +00:00
# check if header exists
if self.is_empty(headers):
self.headers = ""
else:
self.headers = headers
self.messanges = []
if headers is None:
self.ollama = ChatOllama(
base_url=self.base_url,
model=self.model,
)
else:
self.ollama = ChatOllama(
base_url=self.base_url,
model=self.model,
headers = self.headers
)
2024-05-20 11:24:41 +00:00
def is_empty(self, dictionary: dict) -> bool:
return len(dictionary) == 1 and list(dictionary.keys())[0] == '' and list(dictionary.values())[0] == ''
2024-05-20 11:24:41 +00:00
def get_request(self, prompt: str) -> str:
messanges = []
self.messanges.append(prompt)
if len(self.messanges) > 5:
messanges = messanges[:5]
else:
messanges = self.messanges
2024-05-19 12:03:20 +00:00
try:
return self.ollama.invoke(messanges).content
2024-05-19 13:33:03 +00:00
except ValueError as e:
return f"An unexpected Error occuried: {e}"