-
Notifications
You must be signed in to change notification settings - Fork 0
/
provider.py
324 lines (271 loc) · 10.5 KB
/
provider.py
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
import abc
import os
import logging
from notionai import NotionAI
from typing import List, Optional, Dict
from notionai.enums import ToneEnum, TranslateLanguageEnum, PromptTypeEnum
import openai
LOGGER = logging.getLogger("ask_ai")
# PYLLM_PROVODERS = [
# "openai",
# "anthropic",
# "ai21",
# "cohere",
# "alephalpha",
# "huggingface_hub",
# "google",
# ]
# OTHER_PROVIDERS = ["notionai", "bingchat", "openai"]
# PROVODERS = PYLLM_PROVODERS + OTHER_PROVIDERS
HUGCHAT_LLMS = ['OpenAssistant/oasst-sft-6-llama-30b-xor', 'meta-llama/Llama-2-70b-chat-hf']
class AIProvider(abc.ABC):
@abc.abstractmethod
def complete(self, prompt: str, **kwargs) -> str:
pass
def complete_and_remove_prompt(self, prompt: str, **kwargs) -> str:
result = self.complete(prompt, **kwargs)
if result.startswith(prompt):
result = result[len(prompt):]
return result
def change_tone(self, tone: str, context: str):
promt = f"Change the tone to {tone}:\n{context}"
return self.complete_and_remove_prompt(promt)
def improve_writing(self, context):
promt = f"Improve the writing:\n{context}"
return self.complete_and_remove_prompt(promt)
def continue_writing(self, context, page_title=""):
promt = f"Continue writing:\n{context}"
return self.complete_and_remove_prompt(promt)
def translate(self, language, context):
promt = f"Translate to {language}:\n{context}"
return self.complete_and_remove_prompt(promt)
def summarize(self, context):
promp = f"Summarize:\n{context}"
return self.complete_and_remove_prompt(promp)
@classmethod
def _build_one(cls, model_provoder: str):
parts = model_provoder.split("_", 1)
provider = parts[0]
if provider == "openai":
return OpenAIProvider(
parts[1]) if len(parts) == 2 else OpenAIProvider()
# elif provider in PYLLM_PROVODERS:
# model = parts[1]
# return PyLLMProvider(provider, model)
elif provider == "hugchat":
model = model_provoder.split("_", 1)[1]
return HugChatProvider(model)
elif provider == "notionai":
return NotionAIProvider()
elif provider == "bingchat":
style = model_provoder.split("_", 1)[1]
return BingChatProvider(style)
else:
raise Exception(f"not support provider {provider}")
@classmethod
def build(cls, provoders: List[str]):
LOGGER.debug(f"Providers: {provoders}")
if len(provoders) == 1:
return AIProvider._build_one(provoders[0])
else:
return MultiProvider(provoders)
class HugChatProvider(AIProvider):
def __init__(self, model):
self.name = f"hugchat_{model}"
self.model = model
HUGCHAT_EMAIL = os.getenv("HUGCHAT_EMAIL")
HUGCHAT_PASSWORD = os.getenv("HUGCHAT_PASSWORD")
HUGCHAT_COOKIE_DIR = os.getenv("HUGCHAT_COOKIE_DIR", ".hugchat_cookies")
if not HUGCHAT_EMAIL:
LOGGER.error("HUGCHAT_EMAIL is not set")
if not HUGCHAT_PASSWORD:
LOGGER.error("HUGCHAT_PASSWORD is not set")
logging.debug(
f"Create HugChatProvider with email {HUGCHAT_EMAIL}"
)
from hugchat import hugchat
from hugchat.login import Login
# login
sign = Login(HUGCHAT_EMAIL, HUGCHAT_PASSWORD)
cookie_file_path = os.path.join(HUGCHAT_COOKIE_DIR, f"{HUGCHAT_EMAIL}.json")
if not os.path.exists(cookie_file_path):
cookies = sign.login()
sign.saveCookiesToDir(HUGCHAT_COOKIE_DIR)
else:
# load cookies from usercookies/<email>.json
sign = Login(HUGCHAT_EMAIL, None)
cookies = sign.loadCookiesFromDir(HUGCHAT_COOKIE_DIR)
# Create a ChatBot
self.chatbot = hugchat.ChatBot(cookies=cookies.get_dict())
self.chatbot.active_model = model
def complete(self,
prompt: str,
# web_search: bool=False,
temperature: float=0.9,
top_p: float=0.95,
repetition_penalty: float=1.2,
top_k: int=50,
truncate: int=1024,
watermark: bool=False,
max_new_tokens: int=1024,
stop: list=["</s>"],
return_full_text: bool=False,
stream: bool=True,
use_cache: bool=False,
is_retry: bool=False,
retry_count: int=5,
**kwargs) -> str:
return self.chatbot.chat(
prompt,
temperature=temperature,
top_p=top_p,
repetition_penalty=repetition_penalty,
top_k=top_k,
truncate=truncate,
watermark=watermark,
max_new_tokens=max_new_tokens,
stop=stop,
return_full_text=return_full_text,
stream=stream,
use_cache=use_cache,
is_retry=is_retry,
retry_count=retry_count,
)
class OpenAIProvider(AIProvider):
def __init__(self, model='gpt-3.5-turbo', api_key=None):
self.model = model
self.name = f"openai_{model}"
self.api_key = api_key if api_key is not None else os.getenv(
'OPENAI_API_KEY')
# Set the OpenAI API key
openai.api_key = self.api_key
self.client = openai.ChatCompletion if self.is_chat_model else openai.Completion
@property
def is_chat_model(self) -> bool:
return self.model.startswith("gpt")
def _prepapre_model_inputs(
self,
prompt: str,
history: Optional[List[dict]] = None,
system_message: str = None,
temperature: float = 0,
max_tokens: int = 30000,
stream: bool = False,
**kwargs,
) -> Dict:
if self.is_chat_model:
messages = [{"role": "user", "content": prompt}]
if history:
messages = [*history, *messages]
if system_message:
messages = [{
"role": "system",
"content": system_message
}, *messages]
model_inputs = {
"messages": messages,
"temperature": temperature,
"max_tokens": max_tokens,
"stream": stream,
**kwargs,
}
else:
if history:
raise ValueError(
f"history argument is not supported for {self.model} model"
)
if system_message:
raise ValueError(
f"system_message argument is not supported for {self.model} model"
)
model_inputs = {
"prompt": prompt,
"temperature": temperature,
"max_tokens": max_tokens,
"stream": stream,
**kwargs,
}
return model_inputs
def complete(self,
prompt: str,
history: Optional[List[dict]] = None,
system_message: Optional[List[dict]] = None,
temperature: float = 0,
max_tokens: int = 300,
**kwargs) -> str:
model_inputs = self._prepapre_model_inputs(
prompt=prompt,
history=None,
system_message=None,
temperature=temperature,
max_tokens=max_tokens,
**kwargs,
)
response = self.client.create(model=self.model,
**model_inputs,
**kwargs)
if self.is_chat_model:
return response.choices[0].message.content.strip()
return response.choices[0].text.strip()
class NotionAIProvider(AIProvider):
def __init__(self):
self.name = "notionai"
TOKEN = os.getenv("NOTION_TOKEN")
SPACE_ID = os.getenv("NOTION_SPACE_ID")
if not TOKEN:
LOGGER.error("NOTION_TOKEN is not set")
if not SPACE_ID:
LOGGER.error("NOTION_SPACE_ID is not set")
logging.debug(
f"Create NotionAIProvider with token {TOKEN} and space id {SPACE_ID}"
)
self.ai = NotionAI(TOKEN, SPACE_ID)
def complete(self, prompt: str, **kwargs) -> str:
return self.ai.writing_with_prompt(PromptTypeEnum.continue_writing,
context=prompt,
**kwargs)
def change_tone(self, tone: str, context: str):
tone_enum = ToneEnum(tone)
return self.ai.translate(tone_enum, context)
def improve_writing(self, context):
return self.ai.improve_writing(context)
def continue_writing(self, context, page_title=""):
return self.ai.continue_write(context)
def translate(self, language, context):
language_enum = TranslateLanguageEnum(language)
return self.ai.translate(language_enum, context)
def summarize(self, context):
return self.ai.summarize(context)
class BingChatProvider(AIProvider):
def __init__(self, style):
from sydney import SydneyClient
self.name = "bingchat"
LOGGER.debug(f"Create BingChatProvider with style {style}")
self.sydney = SydneyClient(style=style)
async def _async_complete(self, prompt: str, **kwargs) -> str:
await self.sydney.start_conversation()
result = await self.sydney.ask(prompt, citations=False)
# self.sydney.reset_conversation()
return result
def complete(self, prompt: str, **kwargs) -> str:
import asyncio
answer = asyncio.run(self._async_complete(prompt, **kwargs))
return answer
# class PyLLMProvider(AIProvider):
# def __init__(self, provider: str, model: str):
# import llms
# self.name = f"{provider}_{model}"
# LOGGER.debug(
# f"Create PyLLMProvider with provider {provider} and model {model}")
# self.ai = llms.init(model)
# def complete(self, prompt: str, **kwargs) -> str:
# return self.ai.complete(prompt, **kwargs).text
class MultiProvider(AIProvider):
def __init__(self, providers: List[str]):
self.providers = [AIProvider._build_one(p) for p in providers]
def complete(self, prompt: str, **kwargs) -> str:
results = []
for provider in self.providers:
results.append(f"{provider.name}:")
results.append(provider.complete(prompt, **kwargs))
return "\n".join(results)