Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,22 +14,37 @@ history_max_len = 500 # 机器人记忆的最大长度
|
|
| 14 |
all_max_len = 2000 # 输入的最大长度
|
| 15 |
|
| 16 |
|
| 17 |
-
def get_text_emb(open_ai_key, text):
|
| 18 |
-
openai.api_key = open_ai_key
|
| 19 |
response = openai.Embedding.create(
|
| 20 |
input=text,
|
| 21 |
model="text-embedding-ada-002"
|
| 22 |
-
)
|
| 23 |
-
return response['data'][0]['embedding']
|
| 24 |
|
| 25 |
|
| 26 |
def doc_index_self(open_ai_key, doc): # 文档向量化
|
| 27 |
texts = doc.split('\n') # 按行切分
|
| 28 |
-
emb_list = []
|
| 29 |
-
for text in texts:
|
| 30 |
-
emb_list.append(get_text_emb(open_ai_key, text))
|
| 31 |
return texts, emb_list, gr.Textbox.update(visible=True), gr.Button.update(visible=True), gr.Markdown.update(
|
| 32 |
-
value="""操作说明 step 3
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
|
| 35 |
def get_response_by_self(open_ai_key, msg, bot, doc_text_list, doc_embeddings): # 获取机器人回复
|
|
@@ -44,8 +59,8 @@ def get_response_by_self(open_ai_key, msg, bot, doc_text_list, doc_embeddings):
|
|
| 44 |
query_embedding = get_text_emb(open_ai_key, msg) # 获取输入的向量
|
| 45 |
cos_scores = [] # 用于存储相似度
|
| 46 |
|
| 47 |
-
def cos_sim(a, b):
|
| 48 |
-
return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))
|
| 49 |
|
| 50 |
for doc_embedding in doc_embeddings: # 遍历文档向量
|
| 51 |
cos_scores.append(cos_sim(query_embedding, doc_embedding)) # 计算相似度
|
|
@@ -111,7 +126,6 @@ def get_response_by_llama_index(open_ai_key, msg, bot, query_engine): # 获取
|
|
| 111 |
query_str += "机器人:" + his[1] + "\n" # 加入机器人的历史记录
|
| 112 |
query_str += "用户:" + msg + "\n" # 加入用户的当前输入
|
| 113 |
qa_template = Prompt(template) # 将模板转换成Prompt对象
|
| 114 |
-
query_engine = query_engine.as_query_engine(text_qa_template=qa_template) # 建立查询引擎
|
| 115 |
res = query_engine.query(msg) # 获取回答
|
| 116 |
print(res) # 显示回答
|
| 117 |
bot.append([msg, res]) # 加入历史记录
|
|
@@ -119,9 +133,9 @@ def get_response_by_llama_index(open_ai_key, msg, bot, query_engine): # 获取
|
|
| 119 |
|
| 120 |
|
| 121 |
def get_response(open_ai_key, msg, bot, doc_text_list, doc_embeddings, query_engine, index_type): # 获取机器人回复
|
| 122 |
-
if index_type == 1:
|
| 123 |
return get_response_by_self(open_ai_key, msg, bot, doc_text_list, doc_embeddings)
|
| 124 |
-
else:
|
| 125 |
return get_response_by_llama_index(open_ai_key, msg, bot, query_engine)
|
| 126 |
|
| 127 |
|
|
@@ -166,21 +180,6 @@ def up_file(files): # 上传文件
|
|
| 166 |
value="操作说明 step 2:确认PDF解析结果(可修正),点击“建立索引”,随后进行对话")
|
| 167 |
|
| 168 |
|
| 169 |
-
def doc_index_llama(open_ai_key, txt): # 建立索引
|
| 170 |
-
# 根据时间戳新建目录,保存txt文件
|
| 171 |
-
path = str(time.time())
|
| 172 |
-
import os
|
| 173 |
-
os.mkdir(path)
|
| 174 |
-
with open(path + '/doc.txt', mode='w', encoding='utf-8') as f:
|
| 175 |
-
f.write(txt)
|
| 176 |
-
openai.api_key = open_ai_key # 设置OpenAI API Key
|
| 177 |
-
documents = SimpleDirectoryReader(path).load_data() # 读取文档
|
| 178 |
-
index = GPTVectorStoreIndex.from_documents(documents) # 建立索引
|
| 179 |
-
query_engine = index.as_query_engine() # 建立查询引擎
|
| 180 |
-
return query_engine, gr.Textbox.update(visible=True), gr.Button.update(visible=True), gr.Markdown.update(
|
| 181 |
-
value="""操作说明 step 3:PDF解析提交成功! 🙋 可以开始对话啦~"""), gr.Chatbot.update(visible=True), 0
|
| 182 |
-
|
| 183 |
-
|
| 184 |
with gr.Blocks() as demo:
|
| 185 |
with gr.Row():
|
| 186 |
with gr.Column():
|
|
|
|
| 14 |
all_max_len = 2000 # 输入的最大长度
|
| 15 |
|
| 16 |
|
| 17 |
+
def get_text_emb(open_ai_key, text): # 文本向量化
|
| 18 |
+
openai.api_key = open_ai_key # 设置openai的key
|
| 19 |
response = openai.Embedding.create(
|
| 20 |
input=text,
|
| 21 |
model="text-embedding-ada-002"
|
| 22 |
+
) # 调用openai的api
|
| 23 |
+
return response['data'][0]['embedding'] # 返回向量
|
| 24 |
|
| 25 |
|
| 26 |
def doc_index_self(open_ai_key, doc): # 文档向量化
|
| 27 |
texts = doc.split('\n') # 按行切分
|
| 28 |
+
emb_list = [] # 用于存储向量
|
| 29 |
+
for text in texts: # 遍历每一行
|
| 30 |
+
emb_list.append(get_text_emb(open_ai_key, text)) # 获取向量
|
| 31 |
return texts, emb_list, gr.Textbox.update(visible=True), gr.Button.update(visible=True), gr.Markdown.update(
|
| 32 |
+
value="""操作说明 step 3:建立索引(by self)成功! 🙋 可以开始对话啦~"""), gr.Chatbot.update(visible=True), 1
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def doc_index_llama(open_ai_key, txt): # 建立索引
|
| 36 |
+
# 根据时间戳新建目录,保存txt文件
|
| 37 |
+
path = str(time.time())
|
| 38 |
+
import os
|
| 39 |
+
os.mkdir(path)
|
| 40 |
+
with open(path + '/doc.txt', mode='w', encoding='utf-8') as f:
|
| 41 |
+
f.write(txt)
|
| 42 |
+
openai.api_key = open_ai_key # 设置OpenAI API Key
|
| 43 |
+
documents = SimpleDirectoryReader(path).load_data() # 读取文档
|
| 44 |
+
index = GPTVectorStoreIndex.from_documents(documents) # 建立索引
|
| 45 |
+
query_engine = index.as_query_engine() # 建立查询引擎
|
| 46 |
+
return query_engine, gr.Textbox.update(visible=True), gr.Button.update(visible=True), gr.Markdown.update(
|
| 47 |
+
value="""操作说明 step 3:建立索引(by llama_index)成功! 🙋 可以开始对话啦~"""), gr.Chatbot.update(visible=True), 0
|
| 48 |
|
| 49 |
|
| 50 |
def get_response_by_self(open_ai_key, msg, bot, doc_text_list, doc_embeddings): # 获取机器人回复
|
|
|
|
| 59 |
query_embedding = get_text_emb(open_ai_key, msg) # 获取输入的向量
|
| 60 |
cos_scores = [] # 用于存储相似度
|
| 61 |
|
| 62 |
+
def cos_sim(a, b): # 计算余弦相似度
|
| 63 |
+
return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b)) # 返回相似度
|
| 64 |
|
| 65 |
for doc_embedding in doc_embeddings: # 遍历文档向量
|
| 66 |
cos_scores.append(cos_sim(query_embedding, doc_embedding)) # 计算相似度
|
|
|
|
| 126 |
query_str += "机器人:" + his[1] + "\n" # 加入机器人的历史记录
|
| 127 |
query_str += "用户:" + msg + "\n" # 加入用户的当前输入
|
| 128 |
qa_template = Prompt(template) # 将模板转换成Prompt对象
|
|
|
|
| 129 |
res = query_engine.query(msg) # 获取回答
|
| 130 |
print(res) # 显示回答
|
| 131 |
bot.append([msg, res]) # 加入历史记录
|
|
|
|
| 133 |
|
| 134 |
|
| 135 |
def get_response(open_ai_key, msg, bot, doc_text_list, doc_embeddings, query_engine, index_type): # 获取机器人回复
|
| 136 |
+
if index_type == 1: # 如果是使用自己的索引
|
| 137 |
return get_response_by_self(open_ai_key, msg, bot, doc_text_list, doc_embeddings)
|
| 138 |
+
else: # 如果是使用llama_index索引
|
| 139 |
return get_response_by_llama_index(open_ai_key, msg, bot, query_engine)
|
| 140 |
|
| 141 |
|
|
|
|
| 180 |
value="操作说明 step 2:确认PDF解析结果(可修正),点击“建立索引”,随后进行对话")
|
| 181 |
|
| 182 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
with gr.Blocks() as demo:
|
| 184 |
with gr.Row():
|
| 185 |
with gr.Column():
|