Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -42,7 +42,16 @@ def doc_index_llama(open_ai_key, txt): # 建立索引
|
|
42 |
openai.api_key = open_ai_key # 设置OpenAI API Key
|
43 |
documents = SimpleDirectoryReader(path).load_data() # 读取文档
|
44 |
index = GPTVectorStoreIndex.from_documents(documents) # 建立索引
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
|
@@ -112,20 +121,12 @@ def get_response_by_self(open_ai_key, msg, bot, doc_text_list, doc_embeddings):
|
|
112 |
|
113 |
def get_response_by_llama_index(open_ai_key, msg, bot, query_engine): # 获取机器人回复
|
114 |
openai.api_key = open_ai_key
|
115 |
-
|
116 |
-
"你是一个有用的助手,可以使用文章内容准确地回答问题。使用提供的文章来生成你的答案,但避免逐字复制文章。尽可能使用自己的话。准确、有用、简洁、清晰。文章内容如下: \n"
|
117 |
-
"---------------------\n"
|
118 |
-
"{context_str}"
|
119 |
-
"\n---------------------\n"
|
120 |
-
"{query_str}\n"
|
121 |
-
"请基于文章内容回答用户的问题。\n"
|
122 |
-
) # 定义模板
|
123 |
query_str = "历史对话如下:\n"
|
124 |
for his in bot: # 遍历历史记录
|
125 |
query_str += "用户:" + his[0] + "\n" # 加入用户的历史记录
|
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]) # 加入历史记录
|
|
|
42 |
openai.api_key = open_ai_key # 设置OpenAI API Key
|
43 |
documents = SimpleDirectoryReader(path).load_data() # 读取文档
|
44 |
index = GPTVectorStoreIndex.from_documents(documents) # 建立索引
|
45 |
+
template = (
|
46 |
+
"你是一个有用的助手,可以使用文章内容准确地回答问题。使用提供的文章来生成你的答案,但避免逐字复制文章。尽可能使用自己的话。准确、有用、简洁、清晰。文章内容如下: \n"
|
47 |
+
"---------------------\n"
|
48 |
+
"{context_str}"
|
49 |
+
"\n---------------------\n"
|
50 |
+
"{query_str}\n"
|
51 |
+
"请基于文章内容回答用户的问题。\n"
|
52 |
+
) # 定义模板
|
53 |
+
qa_template = Prompt(template) # 将模板转换成Prompt对象
|
54 |
+
query_engine = index.as_query_engine(text_qa_template=qa_template) # 建立查询引擎
|
55 |
return query_engine, gr.Textbox.update(visible=True), gr.Button.update(visible=True), gr.Markdown.update(
|
56 |
value="""操作说明 step 3:建立索引(by llama_index)成功! 🙋 可以开始对话啦~"""), gr.Chatbot.update(visible=True), 0
|
57 |
|
|
|
121 |
|
122 |
def get_response_by_llama_index(open_ai_key, msg, bot, query_engine): # 获取机器人回复
|
123 |
openai.api_key = open_ai_key
|
124 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
125 |
query_str = "历史对话如下:\n"
|
126 |
for his in bot: # 遍历历史记录
|
127 |
query_str += "用户:" + his[0] + "\n" # 加入用户的历史记录
|
128 |
query_str += "机器人:" + his[1] + "\n" # 加入机器人的历史记录
|
129 |
query_str += "用户:" + msg + "\n" # 加入用户的当前输入
|
|
|
130 |
res = query_engine.query(msg) # 获取回答
|
131 |
print(res) # 显示回答
|
132 |
bot.append([msg, res]) # 加入历史记录
|