forked from newfyu/Braindoor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mygpt.py
253 lines (224 loc) · 9.3 KB
/
mygpt.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
from pathlib import Path
import openai
import backoff
import yaml
from yaml.loader import SafeLoader
from update_base import load_base
from langchain.embeddings import OpenAIEmbeddings
from utils import logger, tiktoken_encoder, cutoff_localtext
from langchain.text_splitter import RecursiveCharacterTextSplitter
from functools import partial
from create_base import token_len
import importlib
class Result:
def __init__(self, page_content, metadata):
self.page_content = page_content
self.metadata = metadata
class MyGPT:
def __init__(self, config_path="config.yaml"):
self.temp_result = ""
self.load_config(config_path)
self.bases_root = self.opt["bases_root"]
self.bases = dict()
base_paths = list(Path(self.bases_root).glob("*.base"))
self.load_base(base_paths)
self.magictags = self.load_magictags()
openai.api_key = self.opt["key"]
if self.opt["key"]:
self.base_embedding = OpenAIEmbeddings(openai_api_key=self.opt["key"])
self.fulltext_splitter = RecursiveCharacterTextSplitter(
chunk_size=self.opt["review_chunk_size"],
chunk_overlap=self.opt["review_chunk_overlap"],
length_function=partial(token_len, encoder=tiktoken_encoder),
)
# load magic tag from tags/
def load_magictags(self):
tag_file = list(Path('magictags').glob("*.py"))
magictags = dict()
for tag_file in tag_file:
tag_name = tag_file.stem
magictag = importlib.import_module(f"magictags.{tag_name}")
magictag = magictag.MagicTag()
magictags[magictag.tag] = magictag
return magictags
def load_base(self, base_paths):
if len(base_paths) > 0:
base_paths = list(Path(self.bases_root).glob("*.base"))
for base_path in base_paths:
vstore, df_file_md5, df_docs, metadata = load_base(base_path)
base_name = metadata["name"]
self.bases[base_name] = {
"df_docs": df_docs,
"df_file_md5": df_file_md5,
"metadata": metadata,
"vstore": vstore,
}
else:
logger.info("no base exists")
def load_config(self, config_path="config.yaml"):
with open(config_path) as f:
self.opt = yaml.load(f, Loader=SafeLoader)
return self.opt
def search(self, query, base_name, mode="similarity"):
base = self.bases[base_name]
if mode == "keyword":
results = []
df = base["df_docs"]
df_results = df[df["doc"].str.contains(query, case=False)]
for i, row in df_results.iterrows():
page_content = row["doc"]
metadata = {"file_path": row["file_path"]}
result = Result(page_content, metadata)
results.append(result)
else:
results = base["vstore"].similarity_search_with_score(
query, k=self.opt["search_topk"]
)
return results
@backoff.on_exception(
backoff.expo,
(
openai.error.RateLimitError,
openai.error.ServiceUnavailableError,
openai.error.APIConnectionError,
),
)
def chatgpt(
self,
input,
context=[],
sys_msg="",
temperature=1.0,
max_tokens=1500,
stream=False,
):
if sys_msg == "":
messages = [{"role": "system", "content": "You are a helpful assistant."}]
else:
messages = [{"role": "system", "content": f"{sys_msg}"}]
if len(context) > 0:
for q, a in context:
messages.append({"role": "user", "content": f"{q}"})
messages.append({"role": "assistant", "content": f"{a}"})
messages.append({"role": "user", "content": f"{input}"})
if not stream:
completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
api_key=self.opt["key"],
max_tokens=max_tokens,
messages=messages,
temperature=temperature,
)
logger.info("[message]: " + str(messages) + "\n" + "-" * 60)
return completion.choices[0].message.content
else:
completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
api_key=self.opt["key"],
max_tokens=max_tokens,
messages=messages,
temperature=temperature,
stream=True,
)
report = []
for resp in completion:
if hasattr(resp['choices'][0].delta, 'content'):
report.append(resp['choices'][0].delta.content)
mygpt.temp_result = "".join(report).strip()
logger.info("[message]: " + str(messages) + "\n" + "-" * 60)
return mygpt.temp_result
def ask(self, question, context, base_name):
# 解析question中的magic tag,加入use_magictag列表
use_magictags = []
for key in self.magictags.keys():
tag = f" #{key} "
if tag in question:
question = question.replace(tag, "")
use_magictags.append(self.magictags[key])
# qustion前处理
for magictag in use_magictags:
try:
question = magictag.before_llm(question)
except Exception as e:
logger.error(e)
if base_name != "default":
base = self.bases[base_name]
if self.opt["HyDE"]:
draft = self.chatgpt(question, context, stream=True)
query = question + "\n" + draft
logger.info("[draft]: " + draft + "\n" + "-" * 60)
else:
draft = ""
context_str = "\n".join(["\n".join(t) for t in context])
query = context_str + "\n" + question
mydocs = base["vstore"].similarity_search_with_score(
query, k=self.opt["ask_topk"]
)
local_text = mydocs[0][0].page_content
if self.opt["answer_depth"] < 2: # simple answer
ask_prompt = f"""You can refer to given local text and your own knowledge to answer users' questions. If local text does not provide relevant information, feel free to generate a answer for question based on general knowledge and context:
local text:{local_text}
user question:{question}"""
answer = self.chatgpt(ask_prompt, context, stream=True)
mygpt.temp_result = ''
else: # deep answer
answer_depth = min(self.opt["answer_depth"], self.opt["ask_topk"])
chunks = [i[0].page_content for i in mydocs[0: int(answer_depth)][::-1]]
answer = self.review(question, chunks)
mygpt.temp_result = ''
else: # default answer
draft = self.chatgpt(question, context, stream=True)
answer = draft
mydocs = []
mygpt.temp_result = ''
# question后处理
for magictag in use_magictags:
try:
answer = magictag.after_llm(answer)
except Exception as e:
logger.error(e)
return answer, mydocs, draft
# prompt 1
def review(self, question, chunks):
prev_answer = ""
logger.info(f"Start long text reading, estimated to take {len(chunks)*15} seconds")
chunk_num = len(chunks)
for i,chunk in enumerate(chunks):
if i != chunk_num - 1:
ask_prompt = f"""known:{prev_answer}
Extra text:{chunk}
quesion:{question}
The text is incomplete. Don't answer the questions immediately. First record the related text about the question
"""
else:
ask_prompt = f"""known:{prev_answer}
Extra text:{chunk}
Please answer the following question only according to the text provided above:
{question}"""
answer = mygpt.chatgpt(ask_prompt, temperature=1, stream=True)
prev_answer = answer
logger.info(f"answer {i}: {answer} \n Reading progress {i+1}/{len(chunks)}")
mygpt.temp_result = ''
return prev_answer
# def review(self, question, chunks):
# prev_answer = ""
# logger.info(
# f"Start long text reading, estimated to take {len(chunks)*15} seconds"
# )
# chunk_num = len(chunks)
# for i, chunk in enumerate(chunks):
# if i != chunk_num - 1:
# ask_prompt = f"""known:{prev_answer}
# Extra text:{chunk}
# quesiton:{question}。完全根据前面提供的内容回答,不要自由回答。"""
# else:
# ask_prompt = f"""known:{prev_answer}
# Extra text:{chunk}
# Please answer the following question only according to the text provided above:
# {question}"""
# answer = mygpt.chatgpt(ask_prompt, temperature=1, stream=True)
# prev_answer = answer
# logger.info(f"answer {i}: {answer} \n Reading progress {i+1}/{len(chunks)}")
# mygpt.temp_result = ''
# return prev_answer
mygpt = MyGPT()