41 lines
1.2 KiB
Python
41 lines
1.2 KiB
Python
from langchain_community.chat_models import ChatOllama
|
|
|
|
class OllamaChatBot:
|
|
def __init__(self, base_url, model, headers):
|
|
self.base_url = base_url
|
|
self.model = model
|
|
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
|
|
)
|
|
|
|
def is_empty(self, dictionary):
|
|
return len(dictionary) == 1 and list(dictionary.keys())[0] == '' and list(dictionary.values())[0] == ''
|
|
|
|
|
|
|
|
def get_request(self, prompt):
|
|
messanges = []
|
|
self.messanges.append(prompt)
|
|
if len(self.messanges) > 5:
|
|
messanges = messanges[:5]
|
|
else:
|
|
messanges = self.messanges
|
|
try:
|
|
return self.ollama.invoke(messanges).content
|
|
except ValueError as e:
|
|
return f"An unexpected Error occuried: {e}"
|