Skip to content

Commit

Permalink
Make chatnoir-chat more object oriented and suitable to run in TIRA.
Browse files Browse the repository at this point in the history
  • Loading branch information
mam10eks committed Aug 21, 2023
1 parent d247fb1 commit 906e560
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions chatnoir_api/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@ def default_from_tira_environment(key):
continue

for file_name in ['metadata.json', '.chatnoir-settings.json']:
try:
ret = load(open(Path(os.environ.get(potential_key)) / file_name))
return ret[key]
except:
pass
ret = load(open(Path(os.environ.get(potential_key)) / file_name))
return ret[key]


def default_from_environment(key, default=None):
Expand All @@ -35,15 +32,18 @@ def __init__(self, api_key=default_from_environment('chatnoir_chat_api_key'), mo
self.api_key = api_key
self.model = model
self.endpoint = endpoint

if not self.api_key:
raise ValueError('Please provide a proper api_key, got: ' + str(self.api_key))
raise ValueError('Please provide an api_key, got: '
+ str(self.api_key))

if not self.model:
raise ValueError('Please provide a proper model, got: ' + str(self.api_key))
raise ValueError('Please provide an model, got: '
+ str(self.model))

if not self.endpoint:
raise ValueError('Please provide a proper model, got: ' + str(self.api_key))
raise ValueError('Please provide an endpoint, got: '
+ str(self.endpoint))

def chat(self, input_sentence: str) -> str:
url = urljoin(BASE_URL_CHAT, f"generate/{self.model}")
Expand All @@ -59,6 +59,6 @@ def chat(self, input_sentence: str) -> str:
response_json = response.json()

if "response" not in response_json:
raise ValueError(f"Invalid ChatNoir Chat response: {response_json}")
raise ValueError(f"Invalid ChatNoir response: {response_json}")
return response_json["response"]

0 comments on commit 906e560

Please sign in to comment.