forked from sidbhat/gen-ai-sid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
open_ai_service.py
195 lines (171 loc) · 6.77 KB
/
open_ai_service.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
import logging
from typing import Optional
import pinecone
import requests
from http import HTTPStatus
from langchain.chains.question_answering import load_qa_chain
from langchain import OpenAI
from langchain.vectorstores import Pinecone
from langchain.embeddings.openai import OpenAIEmbeddings
from config import Config
class OpenAIService:
@staticmethod
def get_token() -> Optional[str]:
try:
resp = requests.post(url=Config.OPEN_AI_TOKEN_URL,
auth=(Config.OPEN_AI_CLIENT_ID, Config.OPEN_AI_CLIENT_SECRET),
params={"grant_type": "client_credentials"})
token = resp.json()["access_token"]
return token
except Exception as e:
logging.exception("Exception obtaining Open AI token: %s", e)
return None
@staticmethod
def open_ai_query(query: str, model: str, gpt_conversation_history: list ) -> str:
try:
url = f"{Config.OPEN_AI_SVC_URL}/api/v1/completions"
token = OpenAIService.get_token()
headers = {"Content-Type": "application/json", "Authorization": f"Bearer {token}"}
print(gpt_conversation_history)
payload = OpenAIService._generate_payload_for_model(query, model, gpt_conversation_history)
response = requests.request("POST", url, headers=headers, json=payload, timeout=Config.OPEN_AI_TIMEOUT)
if response.status_code != HTTPStatus.OK:
logging.info("Status code = %s: %s", response.status_code, response.text)
return "Error retrieving results"
if model == "gpt-35-turbo" or model == "gpt-4" or model == "gpt-4-32k":
result = response.json()["choices"]
return result[0]["message"]["content"]
elif model == "bloom-7b1" or model == "gptj-full":
result = response.json()
return result["text"][0]
else:
result = response.json()["choices"]
for choice in result:
return choice["text"]
except Exception as e:
logging.exception(e)
return "Error retrieving results"
@staticmethod
def _generate_payload_for_model(query: str, model: str, gpt_conversation_history) -> dict:
if model == "text-davinci-003":
return {
"deployment_id": model,
"prompt": query,
"max_tokens": 1000,
"temperature": 1.0,
"n": 1
}
elif model == "code-davinci-002":
return {
"deployment_id": model,
"prompt": query,
"max_tokens": 1000,
"temperature": 0.0,
"n": 1
}
elif model == "alephalpha":
return {
"deployment_id": model,
"prompt": query,
"maximum_tokens": 1000
}
# elif model == "gpt-35-turbo":
# return {
# "deployment_id": model,
# "prompt": f"{query}",
# "messages": [query],
# "max_tokens": 4000,
# "temperature": 1.0,
# "n": 1,
# "stop": ["<|im_end|>"]
# }
elif model == "gpt-35-turbo" or model == "gpt-4" or model == "gpt-4-32k":
return {
"deployment_id": model,
"messages": gpt_conversation_history,
"max_tokens": 5000,
"temperature": 0.7,
"frequency_penalty": 0,
"presence_penalty": 0,
"top_p": 0.95,
"stop": "null"
}
elif model == "bloom":
return {
"deployment_id": model,
"prompt": query,
"result_length": 100
}
elif model == "gptj":
return {
"deployment_id": model,
"prompt": query,
"result_length": 100
}
elif model == "bloom-7b1":
return {
"deployment_id": model,
"text": [
query
],
"temperature": 0.7,
"top_k": 10,
"top_p": 0.2,
"max_new_tokens": 50,
"repetition_penalty": 1.7,
"do_sample": True,
"remove_input_from_output": True
}
elif model == "gptj-full":
return {
"deployment_id": model,
"text": [
query
],
"temperature": 0.2,
"top_k": 10,
"top_p": 0.2,
"max_new_tokens": 50,
"repetition_penalty": 1.5,
"do_sample": True,
"remove_input_from_output": True
}
raise Exception(f"Invalid model {model}")
@staticmethod
def open_ai_enterprise_query(query: str) -> str:
try:
index_name = 'demo-index'
# initialize connection (get API key at app.pinecone.io)
pinecone.init(
api_key="5e6a8cb6-f036-4a23-9b34-c95aec8e317f",
environment="us-west1-gcp-free" # find next to API key
)
index = pinecone.Index(index_name)
embeddings = OpenAIEmbeddings(openai_api_key=Config.OPENAI_API_KEY)
docsearch = Pinecone.from_existing_index(index_name, embeddings)
llm = OpenAI(temperature=0, openai_api_key=Config.OPENAI_API_KEY)
chain = load_qa_chain(llm, chain_type="stuff")
docs = docsearch.similarity_search(query, include_metadata=True)
return chain.run(input_documents=docs, question=query)
except Exception as e:
logging.exception(e)
return "Error retrieving results"
@staticmethod
def open_ai_get_embeddings(query: str) -> list:
try:
url = f"{Config.OPEN_AI_SVC_URL}/api/v1/embeddings"
token = OpenAIService.get_token()
headers = {"Content-Type": "application/json", "Authorization": f"Bearer {token}"}
payload = {
"deployment_id": "text-embedding-ada-002-v2",
"input": query,
}
response = requests.request("POST", url, headers=headers, json=payload, timeout=Config.OPEN_AI_TIMEOUT)
if response.status_code != HTTPStatus.OK:
logging.info("Status code = %s: %s", response.status_code, response.text)
return []
print(response.json())
return response.json()["data"][0]["embedding"]
except Exception as e:
logging.exception(e)
return []