-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenai_ai_provider.py
50 lines (42 loc) · 1.83 KB
/
openai_ai_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
import time
from openai import OpenAI
from ai_provider import AIProvider
class OpenAIProvider(AIProvider):
def __init__(self):
self.client = OpenAI()
self.model_names = []
def name(self):
return 'openai'
def supports_sessions(self):
return True
def _list_models(self):
if not self.model_names:
models = self.client.models.list()
models.data = [m for m in models.data if 'gpt' in m.id]
models.data.sort(key=lambda x: x.created, reverse=True)
self.model_names = [m.id for m in models.data]
return self.model_names
def chat_completion(self, messages, model, stream=False):
return self.client.chat.completions.create(
model=model,
messages=messages,
stream=stream,
)
def convert_result_to_text(self, result, sources, handle_metadata_func):
text = result.choices[0].message.content
if handle_metadata_func:
handle_metadata_func("ID", str(result.id))
handle_metadata_func("Creation", time.strftime('%Y-%m-%dT%H:%M:%S%z', time.gmtime(result.created)))
handle_metadata_func("Choices", str(len(result.choices)))
handle_metadata_func("Model", result.model)
handle_metadata_func("Tokens", str(result.usage.total_tokens))
return text
def convert_chunk_to_text(self, chunk, text_chunks, sources, handle_metadata_func):
if handle_metadata_func:
handle_metadata_func("ID", str(chunk.id))
handle_metadata_func("Creation", time.strftime('%Y-%m-%dT%H:%M:%S%z', time.gmtime(chunk.created)))
handle_metadata_func("Choices", str(len(chunk.choices)))
handle_metadata_func("Model", chunk.model)
return chunk.choices[0].delta.content
def close(self):
pass