Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
from openai import OpenAI
|
| 2 |
from docx import Document
|
| 3 |
import numpy as np
|
|
@@ -20,7 +21,7 @@ client = OpenAI(
|
|
| 20 |
embedder = SentenceTransformer(EMBEDDING_MODEL)
|
| 21 |
|
| 22 |
def process_word_document():
|
| 23 |
-
"""处理Word文档并分块
|
| 24 |
doc = Document(WORD_DOC_PATH)
|
| 25 |
chunks = []
|
| 26 |
current_chunk = []
|
|
@@ -42,7 +43,7 @@ def process_word_document():
|
|
| 42 |
return chunks
|
| 43 |
|
| 44 |
def create_vector_store():
|
| 45 |
-
"""创建并保存向量存储
|
| 46 |
if os.path.exists(VECTOR_INDEX_PATH):
|
| 47 |
return
|
| 48 |
|
|
@@ -58,7 +59,7 @@ def create_vector_store():
|
|
| 58 |
np.save(TEXT_DATA_PATH, np.array(chunks))
|
| 59 |
|
| 60 |
def search_knowledge(query, top_k=3):
|
| 61 |
-
"""知识检索
|
| 62 |
index = faiss.read_index(VECTOR_INDEX_PATH)
|
| 63 |
text_data = np.load(TEXT_DATA_PATH, allow_pickle=True)
|
| 64 |
|
|
@@ -68,15 +69,15 @@ def search_knowledge(query, top_k=3):
|
|
| 68 |
distances, indices = index.search(query_embedding, top_k)
|
| 69 |
return "\n".join([text_data[i] for i in indices[0]])
|
| 70 |
|
| 71 |
-
def respond(message, history, max_tokens, temperature, top_p):
|
| 72 |
"""Gradio响应函数"""
|
| 73 |
# 检索相关知识
|
| 74 |
-
context = search_knowledge(
|
| 75 |
|
| 76 |
# 构建对话消息
|
| 77 |
messages = [
|
| 78 |
{"role": "system", "content": f"基于以下知识回答问题,如果不知道就说不知道:\n{context}"},
|
| 79 |
-
{"role": "user", "content":
|
| 80 |
]
|
| 81 |
|
| 82 |
# 流式生成响应
|
|
@@ -90,34 +91,23 @@ def respond(message, history, max_tokens, temperature, top_p):
|
|
| 90 |
top_p=top_p
|
| 91 |
)
|
| 92 |
|
|
|
|
| 93 |
for chunk in response:
|
| 94 |
-
# 从当前分块中获取推理内容和回答内容
|
| 95 |
reasoning_chunk = chunk.choices[0].delta.reasoning_content or ""
|
| 96 |
answer_chunk = chunk.choices[0].delta.content or ""
|
| 97 |
|
| 98 |
-
# 累加推理内容到全局变量
|
| 99 |
if reasoning_chunk:
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
yield full_reasoning + "\n\n=== 最终答案 ===\n" + full_response # 提前构建完整输出
|
| 103 |
-
|
| 104 |
-
# 累加回答内容到全局变量
|
| 105 |
elif answer_chunk:
|
| 106 |
-
full_response += answer_chunk
|
| 107 |
-
|
| 108 |
-
# 如果还没有进入答案输出阶段
|
| 109 |
if not done_reasoning:
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
yield full_reasoning + "\n\n=== 最终答案 ===\n" + full_response
|
| 117 |
-
yield full_response, full_reasoning
|
| 118 |
|
| 119 |
-
# Finally, yield the complete response and reasoning after all chunks are processed
|
| 120 |
-
yield full_response, full_reasoning
|
| 121 |
# 初始化向量存储
|
| 122 |
create_vector_store()
|
| 123 |
|
|
@@ -125,10 +115,12 @@ create_vector_store()
|
|
| 125 |
demo = gr.ChatInterface(
|
| 126 |
fn=respond,
|
| 127 |
additional_inputs=[
|
|
|
|
| 128 |
gr.Slider(512, 2048, value=512, step=1, label="最大Token数"),
|
| 129 |
gr.Slider(0.1, 2.0, value=0.7, step=0.1, label="温度参数"),
|
| 130 |
gr.Slider(0.1, 1.0, value=0.95, step=0.05, label="Top-p采样"),
|
| 131 |
],
|
|
|
|
| 132 |
title="制度文档问答系统",
|
| 133 |
description="输入关于广西警察学院制度的问题进行问答"
|
| 134 |
)
|
|
|
|
| 1 |
+
# coding: utf-8
|
| 2 |
from openai import OpenAI
|
| 3 |
from docx import Document
|
| 4 |
import numpy as np
|
|
|
|
| 21 |
embedder = SentenceTransformer(EMBEDDING_MODEL)
|
| 22 |
|
| 23 |
def process_word_document():
|
| 24 |
+
"""处理Word文档并分块"""
|
| 25 |
doc = Document(WORD_DOC_PATH)
|
| 26 |
chunks = []
|
| 27 |
current_chunk = []
|
|
|
|
| 43 |
return chunks
|
| 44 |
|
| 45 |
def create_vector_store():
|
| 46 |
+
"""创建并保存向量存储"""
|
| 47 |
if os.path.exists(VECTOR_INDEX_PATH):
|
| 48 |
return
|
| 49 |
|
|
|
|
| 59 |
np.save(TEXT_DATA_PATH, np.array(chunks))
|
| 60 |
|
| 61 |
def search_knowledge(query, top_k=3):
|
| 62 |
+
"""知识检索"""
|
| 63 |
index = faiss.read_index(VECTOR_INDEX_PATH)
|
| 64 |
text_data = np.load(TEXT_DATA_PATH, allow_pickle=True)
|
| 65 |
|
|
|
|
| 69 |
distances, indices = index.search(query_embedding, top_k)
|
| 70 |
return "\n".join([text_data[i] for i in indices[0]])
|
| 71 |
|
| 72 |
+
def respond(message, history, max_tokens, temperature, top_p, user_input):
|
| 73 |
"""Gradio响应函数"""
|
| 74 |
# 检索相关知识
|
| 75 |
+
context = search_knowledge(user_input)
|
| 76 |
|
| 77 |
# 构建对话消息
|
| 78 |
messages = [
|
| 79 |
{"role": "system", "content": f"基于以下知识回答问题,如果不知道就说不知道:\n{context}"},
|
| 80 |
+
{"role": "user", "content": user_input}
|
| 81 |
]
|
| 82 |
|
| 83 |
# 流式生成响应
|
|
|
|
| 91 |
top_p=top_p
|
| 92 |
)
|
| 93 |
|
| 94 |
+
done_reasoning = False
|
| 95 |
for chunk in response:
|
|
|
|
| 96 |
reasoning_chunk = chunk.choices[0].delta.reasoning_content or ""
|
| 97 |
answer_chunk = chunk.choices[0].delta.content or ""
|
| 98 |
|
|
|
|
| 99 |
if reasoning_chunk:
|
| 100 |
+
full_response += reasoning_chunk
|
| 101 |
+
print(reasoning_chunk, end='', flush=True)
|
|
|
|
|
|
|
|
|
|
| 102 |
elif answer_chunk:
|
|
|
|
|
|
|
|
|
|
| 103 |
if not done_reasoning:
|
| 104 |
+
print('\n\n=== 最终答案 ===\n')
|
| 105 |
+
done_reasoning = True
|
| 106 |
+
print(answer_chunk, end='', flush=True)
|
| 107 |
|
| 108 |
+
print("\n" + "="*50)
|
| 109 |
+
return full_response
|
|
|
|
|
|
|
| 110 |
|
|
|
|
|
|
|
| 111 |
# 初始化向量存储
|
| 112 |
create_vector_store()
|
| 113 |
|
|
|
|
| 115 |
demo = gr.ChatInterface(
|
| 116 |
fn=respond,
|
| 117 |
additional_inputs=[
|
| 118 |
+
gr.Textbox(label="用户提问"),
|
| 119 |
gr.Slider(512, 2048, value=512, step=1, label="最大Token数"),
|
| 120 |
gr.Slider(0.1, 2.0, value=0.7, step=0.1, label="温度参数"),
|
| 121 |
gr.Slider(0.1, 1.0, value=0.95, step=0.05, label="Top-p采样"),
|
| 122 |
],
|
| 123 |
+
theme=gr.themes.Soft(),
|
| 124 |
title="制度文档问答系统",
|
| 125 |
description="输入关于广西警察学院制度的问题进行问答"
|
| 126 |
)
|