-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathai.py
More file actions
47 lines (36 loc) · 1.62 KB
/
Copy pathai.py
File metadata and controls
47 lines (36 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from typing import Optional
from hugchat import hugchat
from hugchat.hugchat import ChatBot
from hugchat.login import Login
from openai import OpenAI
import logging as log
class AiClient:
def generate_response(self, prompt: str) -> Optional[str]:
pass
class OpenAiClient(AiClient):
def __init__(self, api_key: str):
self.__openai_client: Optional[OpenAI] = OpenAI(api_key=api_key)
def generate_response(self, prompt: str) -> Optional[str]:
log.info(f'OpenAiClient generate_response >> requesting: {prompt}')
try:
response = self.__openai_client.completions.create(
model='gpt-3.5-turbo-instruct',
prompt=prompt,
max_tokens=2048)
log.info(f"OpenAiClient generate_response >> response: {response}")
return response.choices[0].text
except Exception as e:
log.error(f"OpenAiClient generate_response >> {e}")
class HuggingChatClient(AiClient):
def __init__(self, email: str, password: str):
sign = Login(email, password)
cookies = sign.login()
self.__hugging_chat_client: Optional[ChatBot] = hugchat.ChatBot(cookies=cookies.get_dict())
def generate_response(self, prompt: str) -> Optional[str]:
log.info(f'HuggingChatClient generate_response >> requesting: {prompt}')
try:
response = self.__hugging_chat_client.chat(prompt)
log.info(f"HuggingChatClient generate_response >> response: {response}")
return response.text
except Exception as e:
log.error(f"HuggingChatClient generate_response >> {e}")