Spaces:
Runtime error
Runtime error
Add database.
Browse files- README.md +2 -2
- README_zh.md +1 -1
- analyze_conversation_history.py +68 -0
- app.py +135 -54
- app_mqa.py +52 -40
- app_mqa_database.py +214 -0
- app_qa.py +0 -106
- assets/banner.svg +2 -1
- documents/LightZero_README.md +23 -1
- documents/{LightZero_README.zh.md → LightZero_README_zh.md} +25 -4
- rag_demo.py +7 -6
README.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
emoji: 📖
|
| 4 |
colorFrom: yellow
|
| 5 |
colorTo: blue
|
|
@@ -58,7 +58,7 @@ QUESTION_LANG='cn' # The language of the question, currently available option is
|
|
| 58 |
|
| 59 |
if __name__ == "__main__":
|
| 60 |
# Assuming documents are already present locally
|
| 61 |
-
file_path = './documents/
|
| 62 |
# Load and split document
|
| 63 |
chunks = load_and_split_document(file_path)
|
| 64 |
# Create vector store
|
|
|
|
| 1 |
---
|
| 2 |
+
title: ZeroPal
|
| 3 |
emoji: 📖
|
| 4 |
colorFrom: yellow
|
| 5 |
colorTo: blue
|
|
|
|
| 58 |
|
| 59 |
if __name__ == "__main__":
|
| 60 |
# Assuming documents are already present locally
|
| 61 |
+
file_path = './documents/LightZero_README_zh.md'
|
| 62 |
# Load and split document
|
| 63 |
chunks = load_and_split_document(file_path)
|
| 64 |
# Create vector store
|
README_zh.md
CHANGED
|
@@ -45,7 +45,7 @@ QUESTION_LANG='cn' # 问题语言,目前可选值为 'cn'
|
|
| 45 |
|
| 46 |
if __name__ == "__main__":
|
| 47 |
# 假设文档已存在于本地
|
| 48 |
-
file_path = './documents/
|
| 49 |
# 加载和分割文档
|
| 50 |
chunks = load_and_split_document(file_path)
|
| 51 |
# 创建向量存储
|
|
|
|
| 45 |
|
| 46 |
if __name__ == "__main__":
|
| 47 |
# 假设文档已存在于本地
|
| 48 |
+
file_path = './documents/LightZero_README_zh.md'
|
| 49 |
# 加载和分割文档
|
| 50 |
chunks = load_and_split_document(file_path)
|
| 51 |
# 创建向量存储
|
analyze_conversation_history.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sqlite3
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
def analyze_conversation_history():
|
| 5 |
+
"""
|
| 6 |
+
分析对话历史数据库中的数据
|
| 7 |
+
"""
|
| 8 |
+
# 连接到SQLite数据库
|
| 9 |
+
conn = sqlite3.connect('database/conversation_history.db')
|
| 10 |
+
c = conn.cursor()
|
| 11 |
+
|
| 12 |
+
# 获取总的对话记录数
|
| 13 |
+
c.execute("SELECT COUNT(*) FROM history")
|
| 14 |
+
total_records = c.fetchone()[0]
|
| 15 |
+
print(f"总对话记录数: {total_records}")
|
| 16 |
+
|
| 17 |
+
# 获取不同用户的对话记录数
|
| 18 |
+
c.execute("SELECT user_id, COUNT(*) as count FROM history GROUP BY user_id")
|
| 19 |
+
user_records = c.fetchall()
|
| 20 |
+
print("每个用户的对话记录数:")
|
| 21 |
+
for user_id, count in user_records:
|
| 22 |
+
print(f"用户 {user_id}: {count} 条记录")
|
| 23 |
+
|
| 24 |
+
# 获取平均对话轮数
|
| 25 |
+
c.execute("SELECT AVG(cnt) FROM (SELECT user_id, COUNT(*) as cnt FROM history GROUP BY user_id)")
|
| 26 |
+
avg_turns = c.fetchone()[0]
|
| 27 |
+
print(f"平均对话轮数: {avg_turns}")
|
| 28 |
+
|
| 29 |
+
# 获取最长的用户输入和助手输出
|
| 30 |
+
c.execute("SELECT MAX(LENGTH(user_input)) FROM history")
|
| 31 |
+
max_user_input_length = c.fetchone()[0]
|
| 32 |
+
print(f"最长的用户输入: {max_user_input_length} 个字符")
|
| 33 |
+
|
| 34 |
+
c.execute("SELECT MAX(LENGTH(assistant_output)) FROM history")
|
| 35 |
+
max_assistant_output_length = c.fetchone()[0]
|
| 36 |
+
print(f"最长的助手输出: {max_assistant_output_length} 个字符")
|
| 37 |
+
|
| 38 |
+
# 关闭游标
|
| 39 |
+
c.close()
|
| 40 |
+
# 关闭数据库连接
|
| 41 |
+
conn.close()
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def clear_context():
|
| 45 |
+
"""
|
| 46 |
+
清除对话历史
|
| 47 |
+
"""
|
| 48 |
+
# 连接到SQLite数据库
|
| 49 |
+
conn = sqlite3.connect('conversation_history.db')
|
| 50 |
+
c = conn.cursor()
|
| 51 |
+
c.execute("DELETE FROM history")
|
| 52 |
+
conn.commit()
|
| 53 |
+
return "", "", ""
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
def get_history():
|
| 57 |
+
"""
|
| 58 |
+
获取对话历史记录
|
| 59 |
+
"""
|
| 60 |
+
# 连接到SQLite数据库
|
| 61 |
+
conn = sqlite3.connect('conversation_history.db')
|
| 62 |
+
c = conn.cursor()
|
| 63 |
+
c.execute("SELECT user_input, assistant_output FROM history")
|
| 64 |
+
rows = c.fetchall()
|
| 65 |
+
history = ""
|
| 66 |
+
for row in rows:
|
| 67 |
+
history += f"User: {row[0]}\nAssistant: {row[1]}\n\n"
|
| 68 |
+
return history
|
app.py
CHANGED
|
@@ -1,9 +1,12 @@
|
|
| 1 |
import os
|
|
|
|
|
|
|
| 2 |
|
| 3 |
import gradio as gr
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
from langchain.document_loaders import TextLoader
|
| 6 |
|
|
|
|
| 7 |
from rag_demo import load_and_split_document, create_vector_store, setup_rag_chain, execute_query
|
| 8 |
|
| 9 |
# 环境设置
|
|
@@ -12,122 +15,200 @@ QUESTION_LANG = os.getenv("QUESTION_LANG") # 从环境变量获取 QUESTION_LAN
|
|
| 12 |
assert QUESTION_LANG in ['cn', 'en'], QUESTION_LANG
|
| 13 |
|
| 14 |
if QUESTION_LANG == "cn":
|
| 15 |
-
title = "
|
| 16 |
title_markdown = """
|
| 17 |
<div align="center">
|
| 18 |
<img src="https://raw.githubusercontent.com/puyuan1996/RAG/main/assets/banner.svg" width="80%" height="20%" alt="Banner Image">
|
| 19 |
</div>
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
"""
|
| 25 |
tos_markdown = """
|
| 26 |
### 使用条款
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
"""
|
| 33 |
|
| 34 |
-
#
|
| 35 |
-
file_path = './documents/
|
| 36 |
|
| 37 |
# 加载原始Markdown文档
|
| 38 |
loader = TextLoader(file_path)
|
| 39 |
orig_documents = loader.load()
|
| 40 |
|
| 41 |
# 存储对话历史
|
| 42 |
-
conversation_history =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
|
| 45 |
-
def rag_answer(question,
|
| 46 |
"""
|
| 47 |
处理用户问题并返回答案和高亮显示的上下文
|
| 48 |
|
| 49 |
:param question: 用户输入的问题
|
| 50 |
-
:param model_name: 使用的语言模型名称
|
| 51 |
:param temperature: 生成答案时使用的温度参数
|
| 52 |
-
:param embedding_model: 使用的嵌入模型
|
| 53 |
:param k: 检索到的文档块数量
|
|
|
|
| 54 |
:return: 模型生成的答案和高亮显示上下文的Markdown文本
|
| 55 |
"""
|
| 56 |
try:
|
| 57 |
chunks = load_and_split_document(file_path, chunk_size=5000, chunk_overlap=500)
|
| 58 |
-
retriever = create_vector_store(chunks, model=
|
| 59 |
-
rag_chain = setup_rag_chain(model_name=
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
|
| 64 |
-
|
| 65 |
-
history_str = "\n".join([f"{role}: {text}" for role, text in conversation_history])
|
| 66 |
|
| 67 |
-
|
|
|
|
|
|
|
| 68 |
temperature=temperature)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
# 在文档中高亮显示上下文
|
| 70 |
context = [retrieved_documents[i].page_content for i in range(len(retrieved_documents))]
|
| 71 |
highlighted_document = orig_documents[0].page_content
|
| 72 |
for i in range(len(context)):
|
| 73 |
highlighted_document = highlighted_document.replace(context[i], f"<mark>{context[i]}</mark>")
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
|
|
|
| 77 |
except Exception as e:
|
| 78 |
print(f"An error occurred: {e}")
|
| 79 |
-
return "
|
| 80 |
-
|
|
|
|
|
|
|
| 81 |
|
|
|
|
| 82 |
|
| 83 |
-
|
|
|
|
| 84 |
"""
|
| 85 |
清除对话历史
|
| 86 |
"""
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
return "", ""
|
| 90 |
|
| 91 |
|
| 92 |
if __name__ == "__main__":
|
| 93 |
-
with gr.Blocks(title=title, theme='ParityError/Interstellar') as
|
| 94 |
gr.Markdown(title_markdown)
|
| 95 |
|
| 96 |
with gr.Row():
|
| 97 |
with gr.Column():
|
|
|
|
|
|
|
|
|
|
| 98 |
inputs = gr.Textbox(
|
| 99 |
-
placeholder="
|
| 100 |
-
label="问题
|
| 101 |
-
model_name = gr.Dropdown(
|
| 102 |
-
choices=['kimi', 'abab6-chat', 'glm-4', 'gpt-3.5-turbo', 'gpt-4', 'gpt-4-turbo', 'azure_gpt-4', 'azure_gpt-35-turbo-16k', 'azure_gpt-35-turbo'],
|
| 103 |
-
# value='azure_gpt-4',
|
| 104 |
-
value='kimi',
|
| 105 |
-
label="选择语言模型")
|
| 106 |
temperature = gr.Slider(minimum=0.0, maximum=1.0, value=0.01, step=0.01, label="温度参数")
|
| 107 |
-
embedding_model = gr.Dropdown(
|
| 108 |
-
choices=['HuggingFace', 'TensorflowHub', 'OpenAI'],
|
| 109 |
-
value='OpenAI',
|
| 110 |
-
label="选择嵌入模型")
|
| 111 |
k = gr.Slider(minimum=1, maximum=10, value=5, step=1, label="检索到的文档块数量")
|
| 112 |
with gr.Row():
|
| 113 |
gr_submit = gr.Button('提交')
|
| 114 |
gr_clear = gr.Button('清除上下文')
|
| 115 |
|
| 116 |
-
outputs_answer = gr.Textbox(placeholder="
|
| 117 |
-
label="回答
|
|
|
|
| 118 |
with gr.Row():
|
| 119 |
-
outputs_context = gr.Markdown(label="
|
| 120 |
-
|
| 121 |
-
gr.Markdown(tos_markdown)
|
| 122 |
-
|
| 123 |
gr_submit.click(
|
| 124 |
rag_answer,
|
| 125 |
-
inputs=[inputs,
|
| 126 |
-
outputs=[outputs_answer, outputs_context],
|
| 127 |
)
|
| 128 |
-
|
| 129 |
|
| 130 |
concurrency = int(os.environ.get('CONCURRENCY', os.cpu_count()))
|
| 131 |
favicon_path = os.path.join(os.path.dirname(__file__), 'assets', 'avatar.png')
|
| 132 |
-
|
| 133 |
-
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import sqlite3
|
| 3 |
+
import threading
|
| 4 |
|
| 5 |
import gradio as gr
|
| 6 |
from dotenv import load_dotenv
|
| 7 |
from langchain.document_loaders import TextLoader
|
| 8 |
|
| 9 |
+
from RAG.analyze_conversation_history import analyze_conversation_history
|
| 10 |
from rag_demo import load_and_split_document, create_vector_store, setup_rag_chain, execute_query
|
| 11 |
|
| 12 |
# 环境设置
|
|
|
|
| 15 |
assert QUESTION_LANG in ['cn', 'en'], QUESTION_LANG
|
| 16 |
|
| 17 |
if QUESTION_LANG == "cn":
|
| 18 |
+
title = "ZeroPal"
|
| 19 |
title_markdown = """
|
| 20 |
<div align="center">
|
| 21 |
<img src="https://raw.githubusercontent.com/puyuan1996/RAG/main/assets/banner.svg" width="80%" height="20%" alt="Banner Image">
|
| 22 |
</div>
|
| 23 |
+
|
| 24 |
+
📢 **操作说明**:请在下方的“问题”框中输入关于 LightZero 的问题,并点击“提交”按钮。右侧的“回答”框将展示 RAG 模型提供的答案。
|
| 25 |
+
您可以在问答框下方查看当前“对话历史”,点击“清除上下文”按钮可清空历史记录。在“对话历史”框下方,您将找到相关参考文档,其中相关文段将以黄色高亮显示。
|
| 26 |
+
如果您喜欢这个项目,请在 GitHub [LightZero RAG Demo](https://github.com/puyuan1996/RAG) 上给我们点赞!✨ 您的支持是我们持续更新的动力。
|
| 27 |
+
|
| 28 |
+
<div align="center">
|
| 29 |
+
<strong>注意:算法模型输出可能包含一定的随机性。结果不代表开发者和相关 AI 服务的态度和意见。本项目开发者不对结果作出任何保证,仅供参考之用。使用该服务即代表同意后文所述的使用条款。</strong>
|
| 30 |
+
</div>
|
| 31 |
"""
|
| 32 |
tos_markdown = """
|
| 33 |
### 使用条款
|
| 34 |
+
|
| 35 |
+
使用本服务的玩家需同意以下条款:
|
| 36 |
+
|
| 37 |
+
- 本服务为探索性研究的预览版,仅供非商业用途。
|
| 38 |
+
- 服务不得用于任何非法、有害、暴力、种族主义或其他令人反感的目的。
|
| 39 |
+
- 服务提供有限的安全措施,并可能生成令人反感的内容。
|
| 40 |
+
- 如果您对服务体验不满,请通过 opendilab@pjlab.org.cn 与我们联系!我们承诺修复问题并不断改进项目。
|
| 41 |
+
- 为了获得最佳体验,请使用台式电脑,因为移动设备可能会影响视觉效果。
|
| 42 |
+
|
| 43 |
+
**版权所有 © 2024 OpenDILab。保留所有权利。**
|
| 44 |
"""
|
| 45 |
|
| 46 |
+
# 路径变量,方便之后的文件使用
|
| 47 |
+
file_path = './documents/LightZero_README_zh.md'
|
| 48 |
|
| 49 |
# 加载原始Markdown文档
|
| 50 |
loader = TextLoader(file_path)
|
| 51 |
orig_documents = loader.load()
|
| 52 |
|
| 53 |
# 存储对话历史
|
| 54 |
+
conversation_history = {}
|
| 55 |
+
|
| 56 |
+
# 创建线程局部数据对象
|
| 57 |
+
threadLocal = threading.local()
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def get_db_connection():
|
| 61 |
+
"""
|
| 62 |
+
返回当前线程的数据库连接
|
| 63 |
+
"""
|
| 64 |
+
conn = getattr(threadLocal, 'conn', None)
|
| 65 |
+
if conn is None:
|
| 66 |
+
# 连接到SQLite数据库
|
| 67 |
+
conn = sqlite3.connect('database/conversation_history.db')
|
| 68 |
+
c = conn.cursor()
|
| 69 |
+
# Drop the existing 'history' table if it exists
|
| 70 |
+
# c.execute('DROP TABLE IF EXISTS history')
|
| 71 |
+
# 创建存储对话历史的表
|
| 72 |
+
c.execute('''CREATE TABLE IF NOT EXISTS history
|
| 73 |
+
(id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 74 |
+
user_id TEXT NOT NULL,
|
| 75 |
+
user_input TEXT NOT NULL,
|
| 76 |
+
assistant_output TEXT NOT NULL,
|
| 77 |
+
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP)''')
|
| 78 |
+
threadLocal.conn = conn
|
| 79 |
+
return conn
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
def get_db_cursor():
|
| 83 |
+
"""
|
| 84 |
+
返回当前线程的数据库游标
|
| 85 |
+
"""
|
| 86 |
+
conn = get_db_connection()
|
| 87 |
+
c = getattr(threadLocal, 'cursor', None)
|
| 88 |
+
if c is None:
|
| 89 |
+
c = conn.cursor()
|
| 90 |
+
threadLocal.cursor = c
|
| 91 |
+
return c
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
# 程序结束时清理数据库连接
|
| 95 |
+
def close_db_connection():
|
| 96 |
+
conn = getattr(threadLocal, 'conn', None)
|
| 97 |
+
if conn is not None:
|
| 98 |
+
conn.close()
|
| 99 |
+
setattr(threadLocal, 'conn', None)
|
| 100 |
+
|
| 101 |
+
c = getattr(threadLocal, 'cursor', None)
|
| 102 |
+
if c is not None:
|
| 103 |
+
c.close()
|
| 104 |
+
setattr(threadLocal, 'cursor', None)
|
| 105 |
|
| 106 |
|
| 107 |
+
def rag_answer(question, temperature, k, user_id):
|
| 108 |
"""
|
| 109 |
处理用户问题并返回答案和高亮显示的上下文
|
| 110 |
|
| 111 |
:param question: 用户输入的问题
|
|
|
|
| 112 |
:param temperature: 生成答案时使用的温度参数
|
|
|
|
| 113 |
:param k: 检索到的文档块数量
|
| 114 |
+
:param user_id: 用户ID
|
| 115 |
:return: 模型生成的答案和高亮显示上下文的Markdown文本
|
| 116 |
"""
|
| 117 |
try:
|
| 118 |
chunks = load_and_split_document(file_path, chunk_size=5000, chunk_overlap=500)
|
| 119 |
+
retriever = create_vector_store(chunks, model='OpenAI', k=k)
|
| 120 |
+
rag_chain = setup_rag_chain(model_name='kimi', temperature=temperature)
|
| 121 |
|
| 122 |
+
if user_id not in conversation_history:
|
| 123 |
+
conversation_history[user_id] = []
|
| 124 |
|
| 125 |
+
conversation_history[user_id].append((f"User[{user_id}]", question))
|
|
|
|
| 126 |
|
| 127 |
+
history_str = "\n".join([f"{role}: {text}" for role, text in conversation_history[user_id]])
|
| 128 |
+
|
| 129 |
+
retrieved_documents, answer = execute_query(retriever, rag_chain, history_str, model_name='kimi',
|
| 130 |
temperature=temperature)
|
| 131 |
+
|
| 132 |
+
############################
|
| 133 |
+
# 获取当前线程的数据库连接和游标
|
| 134 |
+
############################
|
| 135 |
+
conn = get_db_connection()
|
| 136 |
+
c = get_db_cursor()
|
| 137 |
+
|
| 138 |
+
# 分析对话历史
|
| 139 |
+
# analyze_conversation_history()
|
| 140 |
+
# 获取总的对话记录数
|
| 141 |
+
c.execute("SELECT COUNT(*) FROM history")
|
| 142 |
+
total_records = c.fetchone()[0]
|
| 143 |
+
print(f"总对话记录数: {total_records}")
|
| 144 |
+
|
| 145 |
+
# 将问题和回答存储到数据库
|
| 146 |
+
c.execute("INSERT INTO history (user_id, user_input, assistant_output) VALUES (?, ?, ?)",
|
| 147 |
+
(user_id, question, answer))
|
| 148 |
+
conn.commit()
|
| 149 |
+
|
| 150 |
# 在文档中高亮显示上下文
|
| 151 |
context = [retrieved_documents[i].page_content for i in range(len(retrieved_documents))]
|
| 152 |
highlighted_document = orig_documents[0].page_content
|
| 153 |
for i in range(len(context)):
|
| 154 |
highlighted_document = highlighted_document.replace(context[i], f"<mark>{context[i]}</mark>")
|
| 155 |
|
| 156 |
+
conversation_history[user_id].append(("Assistant", answer))
|
| 157 |
+
|
| 158 |
+
full_history = "\n".join([f"{role}: {text}" for role, text in conversation_history[user_id]])
|
| 159 |
except Exception as e:
|
| 160 |
print(f"An error occurred: {e}")
|
| 161 |
+
return "处理您的问题时出现错误,请稍后再试。", "", ""
|
| 162 |
+
finally:
|
| 163 |
+
# 不再在这里关闭游标和连接
|
| 164 |
+
pass
|
| 165 |
|
| 166 |
+
return answer, highlighted_document, full_history
|
| 167 |
|
| 168 |
+
|
| 169 |
+
def clear_context(user_id):
|
| 170 |
"""
|
| 171 |
清除对话历史
|
| 172 |
"""
|
| 173 |
+
if user_id in conversation_history:
|
| 174 |
+
conversation_history[user_id] = []
|
| 175 |
+
return "", "", ""
|
| 176 |
|
| 177 |
|
| 178 |
if __name__ == "__main__":
|
| 179 |
+
with gr.Blocks(title=title, theme='ParityError/Interstellar') as zero_pal:
|
| 180 |
gr.Markdown(title_markdown)
|
| 181 |
|
| 182 |
with gr.Row():
|
| 183 |
with gr.Column():
|
| 184 |
+
user_id = gr.Textbox(
|
| 185 |
+
placeholder="请输入您的真实姓名或昵称作为用户ID",
|
| 186 |
+
label="用户ID")
|
| 187 |
inputs = gr.Textbox(
|
| 188 |
+
placeholder="请您在这里输入任何关于 LightZero 的问题。",
|
| 189 |
+
label="问题")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
temperature = gr.Slider(minimum=0.0, maximum=1.0, value=0.01, step=0.01, label="温度参数")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
k = gr.Slider(minimum=1, maximum=10, value=5, step=1, label="检索到的文档块数量")
|
| 192 |
with gr.Row():
|
| 193 |
gr_submit = gr.Button('提交')
|
| 194 |
gr_clear = gr.Button('清除上下文')
|
| 195 |
|
| 196 |
+
outputs_answer = gr.Textbox(placeholder="当你点击提交按钮后,这里会显示 RAG 模型给出的回答。",
|
| 197 |
+
label="回答")
|
| 198 |
+
outputs_history = gr.Textbox(label="对话历史")
|
| 199 |
with gr.Row():
|
| 200 |
+
outputs_context = gr.Markdown(label="参考的文档(检索得到的相关文段用高亮显示)")
|
| 201 |
+
gr_clear.click(clear_context, inputs=user_id, outputs=[outputs_context, outputs_history])
|
|
|
|
|
|
|
| 202 |
gr_submit.click(
|
| 203 |
rag_answer,
|
| 204 |
+
inputs=[inputs, temperature, k, user_id],
|
| 205 |
+
outputs=[outputs_answer, outputs_context, outputs_history],
|
| 206 |
)
|
| 207 |
+
gr.Markdown(tos_markdown)
|
| 208 |
|
| 209 |
concurrency = int(os.environ.get('CONCURRENCY', os.cpu_count()))
|
| 210 |
favicon_path = os.path.join(os.path.dirname(__file__), 'assets', 'avatar.png')
|
| 211 |
+
zero_pal.queue().launch(max_threads=concurrency, favicon_path=favicon_path, share=True)
|
| 212 |
+
|
| 213 |
+
# 在合适的地方,例如程序退出时,调用close_db_connection函数
|
| 214 |
+
close_db_connection()
|
app_mqa.py
CHANGED
|
@@ -1,9 +1,7 @@
|
|
| 1 |
import os
|
| 2 |
-
|
| 3 |
import gradio as gr
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
from langchain.document_loaders import TextLoader
|
| 6 |
-
|
| 7 |
from rag_demo import load_and_split_document, create_vector_store, setup_rag_chain, execute_query
|
| 8 |
|
| 9 |
# 环境设置
|
|
@@ -12,27 +10,36 @@ QUESTION_LANG = os.getenv("QUESTION_LANG") # 从环境变量获取 QUESTION_LAN
|
|
| 12 |
assert QUESTION_LANG in ['cn', 'en'], QUESTION_LANG
|
| 13 |
|
| 14 |
if QUESTION_LANG == "cn":
|
| 15 |
-
title = "
|
| 16 |
title_markdown = """
|
| 17 |
<div align="center">
|
| 18 |
<img src="https://raw.githubusercontent.com/puyuan1996/RAG/main/assets/banner.svg" width="80%" height="20%" alt="Banner Image">
|
| 19 |
</div>
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
"""
|
| 25 |
tos_markdown = """
|
| 26 |
### 使用条款
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
"""
|
| 33 |
|
| 34 |
# 路径变量,方便之后的文件使用
|
| 35 |
-
file_path = './documents/
|
| 36 |
|
| 37 |
# 加载原始Markdown文档
|
| 38 |
loader = TextLoader(file_path)
|
|
@@ -42,21 +49,19 @@ orig_documents = loader.load()
|
|
| 42 |
conversation_history = []
|
| 43 |
|
| 44 |
|
| 45 |
-
def rag_answer(question,
|
| 46 |
"""
|
| 47 |
处理用户问题并返回答案和高亮显示的上下文
|
| 48 |
|
| 49 |
:param question: 用户输入的问题
|
| 50 |
-
:param model_name: 使用的语言模型名称
|
| 51 |
:param temperature: 生成答案时使用的温度参数
|
| 52 |
-
:param embedding_model: 使用的嵌入模型
|
| 53 |
:param k: 检索到的文档块数量
|
| 54 |
:return: 模型生成的答案和高亮显示上下文的Markdown文本
|
| 55 |
"""
|
| 56 |
try:
|
| 57 |
chunks = load_and_split_document(file_path, chunk_size=5000, chunk_overlap=500)
|
| 58 |
-
retriever = create_vector_store(chunks, model=
|
| 59 |
-
rag_chain = setup_rag_chain(model_name=
|
| 60 |
|
| 61 |
# 将问题添加到对话历史中
|
| 62 |
conversation_history.append(("User", question))
|
|
@@ -64,8 +69,9 @@ def rag_answer(question, model_name, temperature, embedding_model, k):
|
|
| 64 |
# 将对话历史转换为字符串
|
| 65 |
history_str = "\n".join([f"{role}: {text}" for role, text in conversation_history])
|
| 66 |
|
| 67 |
-
retrieved_documents, answer = execute_query(retriever, rag_chain, history_str, model_name=
|
| 68 |
temperature=temperature)
|
|
|
|
| 69 |
# 在文档中高亮显示上下文
|
| 70 |
context = [retrieved_documents[i].page_content for i in range(len(retrieved_documents))]
|
| 71 |
highlighted_document = orig_documents[0].page_content
|
|
@@ -74,10 +80,17 @@ def rag_answer(question, model_name, temperature, embedding_model, k):
|
|
| 74 |
|
| 75 |
# 将回答添加到对话历史中
|
| 76 |
conversation_history.append(("Assistant", answer))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
except Exception as e:
|
| 78 |
print(f"An error occurred: {e}")
|
| 79 |
-
return "处理您的问题时出现错误,请稍后再试。", ""
|
| 80 |
-
|
|
|
|
| 81 |
|
| 82 |
|
| 83 |
def clear_context():
|
|
@@ -86,28 +99,28 @@ def clear_context():
|
|
| 86 |
"""
|
| 87 |
global conversation_history
|
| 88 |
conversation_history = []
|
| 89 |
-
return "", ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
|
| 92 |
if __name__ == "__main__":
|
| 93 |
-
with gr.Blocks(title=title, theme='ParityError/Interstellar') as
|
| 94 |
gr.Markdown(title_markdown)
|
| 95 |
|
| 96 |
with gr.Row():
|
| 97 |
with gr.Column():
|
| 98 |
inputs = gr.Textbox(
|
| 99 |
-
placeholder="
|
| 100 |
label="问题 (Q)")
|
| 101 |
-
model_name = gr.Dropdown(
|
| 102 |
-
choices=['kimi', 'abab6-chat', 'glm-4', 'gpt-3.5-turbo', 'gpt-4', 'gpt-4-turbo', 'azure_gpt-4', 'azure_gpt-35-turbo-16k', 'azure_gpt-35-turbo'],
|
| 103 |
-
# value='azure_gpt-4',
|
| 104 |
-
value='kimi',
|
| 105 |
-
label="选择语言模型")
|
| 106 |
temperature = gr.Slider(minimum=0.0, maximum=1.0, value=0.01, step=0.01, label="温度参数")
|
| 107 |
-
embedding_model = gr.Dropdown(
|
| 108 |
-
choices=['HuggingFace', 'TensorflowHub', 'OpenAI'],
|
| 109 |
-
value='OpenAI',
|
| 110 |
-
label="选择嵌入模型")
|
| 111 |
k = gr.Slider(minimum=1, maximum=10, value=5, step=1, label="检索到的文档块数量")
|
| 112 |
with gr.Row():
|
| 113 |
gr_submit = gr.Button('提交')
|
|
@@ -115,18 +128,17 @@ if __name__ == "__main__":
|
|
| 115 |
|
| 116 |
outputs_answer = gr.Textbox(placeholder="当你点击提交按钮后,这里会显示 RAG 模型给出的回答。",
|
| 117 |
label="回答 (A)")
|
|
|
|
| 118 |
with gr.Row():
|
| 119 |
outputs_context = gr.Markdown(label="参考的文档,检索得到的 context 用高亮显示 (C)")
|
| 120 |
-
|
| 121 |
-
gr.Markdown(tos_markdown)
|
| 122 |
-
|
| 123 |
gr_submit.click(
|
| 124 |
rag_answer,
|
| 125 |
-
inputs=[inputs,
|
| 126 |
-
outputs=[outputs_answer, outputs_context],
|
| 127 |
)
|
| 128 |
-
|
| 129 |
|
| 130 |
concurrency = int(os.environ.get('CONCURRENCY', os.cpu_count()))
|
| 131 |
favicon_path = os.path.join(os.path.dirname(__file__), 'assets', 'avatar.png')
|
| 132 |
-
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from dotenv import load_dotenv
|
| 4 |
from langchain.document_loaders import TextLoader
|
|
|
|
| 5 |
from rag_demo import load_and_split_document, create_vector_store, setup_rag_chain, execute_query
|
| 6 |
|
| 7 |
# 环境设置
|
|
|
|
| 10 |
assert QUESTION_LANG in ['cn', 'en'], QUESTION_LANG
|
| 11 |
|
| 12 |
if QUESTION_LANG == "cn":
|
| 13 |
+
title = "ZeroPal"
|
| 14 |
title_markdown = """
|
| 15 |
<div align="center">
|
| 16 |
<img src="https://raw.githubusercontent.com/puyuan1996/RAG/main/assets/banner.svg" width="80%" height="20%" alt="Banner Image">
|
| 17 |
</div>
|
| 18 |
+
|
| 19 |
+
📢 **操作说明**:请在下方的“问题”框中输入关于 LightZero 的问题,并点击“提交”按钮。右侧的“回答”框将展示 RAG 模型提供的答案。
|
| 20 |
+
您可以在问答框下方查看当前“对话历史”,点击“清除上下文”按钮可清空历史记录。在“对话历史”框下方,您将找到相关参考文档,其中相关文段将以黄色高亮显示。
|
| 21 |
+
如果您喜欢这个项目,请在 GitHub [LightZero RAG Demo](https://github.com/puyuan1996/RAG) 上给我们点赞!✨ 您的支持是我们持续更新的动力。
|
| 22 |
+
|
| 23 |
+
<div align="center">
|
| 24 |
+
<strong>注意:算法模型输出可能包含一定的随机性。结果不代表开发者和相关 AI 服务的态度和意见。本项目开发者不对结果作出任何保证,仅供参考之用。使用该服务即代表同意后文所述的使用条款。</strong>
|
| 25 |
+
</div>
|
| 26 |
"""
|
| 27 |
tos_markdown = """
|
| 28 |
### 使用条款
|
| 29 |
+
|
| 30 |
+
使用本服务的玩家需同意以下条款:
|
| 31 |
+
|
| 32 |
+
- 本服务为探索性研究的预览版,仅供非商业用途。
|
| 33 |
+
- 服务不得用于任何非法、有害、暴力、种族主义或其他令人反感的目的。
|
| 34 |
+
- 服务提供有限的安全措施,并可能生成令人反感的内容。
|
| 35 |
+
- 如果您对服务体验不满,请通过 opendilab@pjlab.org.cn 与我们联系!我们承诺修复问题并不断改进项目。
|
| 36 |
+
- 为了获得最佳体验,请使用台式电脑,因为移动设备可能会影响视觉效果。
|
| 37 |
+
|
| 38 |
+
**版权所有 © 2024 OpenDILab。保留所有权利。**
|
| 39 |
"""
|
| 40 |
|
| 41 |
# 路径变量,方便之后的文件使用
|
| 42 |
+
file_path = './documents/LightZero_README_zh.md'
|
| 43 |
|
| 44 |
# 加载原始Markdown文档
|
| 45 |
loader = TextLoader(file_path)
|
|
|
|
| 49 |
conversation_history = []
|
| 50 |
|
| 51 |
|
| 52 |
+
def rag_answer(question, temperature, k):
|
| 53 |
"""
|
| 54 |
处理用户问题并返回答案和高亮显示的上下文
|
| 55 |
|
| 56 |
:param question: 用户输入的问题
|
|
|
|
| 57 |
:param temperature: 生成答案时使用的温度参数
|
|
|
|
| 58 |
:param k: 检索到的文档块数量
|
| 59 |
:return: 模型生成的答案和高亮显示上下文的Markdown文本
|
| 60 |
"""
|
| 61 |
try:
|
| 62 |
chunks = load_and_split_document(file_path, chunk_size=5000, chunk_overlap=500)
|
| 63 |
+
retriever = create_vector_store(chunks, model='OpenAI', k=k)
|
| 64 |
+
rag_chain = setup_rag_chain(model_name='kimi', temperature=temperature)
|
| 65 |
|
| 66 |
# 将问题添加到对话历史中
|
| 67 |
conversation_history.append(("User", question))
|
|
|
|
| 69 |
# 将对话历史转换为字符串
|
| 70 |
history_str = "\n".join([f"{role}: {text}" for role, text in conversation_history])
|
| 71 |
|
| 72 |
+
retrieved_documents, answer = execute_query(retriever, rag_chain, history_str, model_name='kimi',
|
| 73 |
temperature=temperature)
|
| 74 |
+
|
| 75 |
# 在文档中高亮显示上下文
|
| 76 |
context = [retrieved_documents[i].page_content for i in range(len(retrieved_documents))]
|
| 77 |
highlighted_document = orig_documents[0].page_content
|
|
|
|
| 80 |
|
| 81 |
# 将回答添加到对话历史中
|
| 82 |
conversation_history.append(("Assistant", answer))
|
| 83 |
+
|
| 84 |
+
# 将对话历史存储到数据库中(此处省略数据库操作代码)
|
| 85 |
+
|
| 86 |
+
# 返回完整的对话历史
|
| 87 |
+
full_history = "\n".join([f"{role}: {text}" for role, text in conversation_history])
|
| 88 |
+
|
| 89 |
except Exception as e:
|
| 90 |
print(f"An error occurred: {e}")
|
| 91 |
+
return "处理您的问题时出现错误,请稍后再试。", "", ""
|
| 92 |
+
|
| 93 |
+
return answer, highlighted_document, full_history
|
| 94 |
|
| 95 |
|
| 96 |
def clear_context():
|
|
|
|
| 99 |
"""
|
| 100 |
global conversation_history
|
| 101 |
conversation_history = []
|
| 102 |
+
return "", "", ""
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
def export_history():
|
| 106 |
+
"""
|
| 107 |
+
导出对话历史记录
|
| 108 |
+
"""
|
| 109 |
+
# 从数据库中获取完整的对话历史记录(此处省略数据库操作代码)
|
| 110 |
+
exported_history = "对话历史记录:\n" + "\n".join([f"{role}: {text}" for role, text in conversation_history])
|
| 111 |
+
return exported_history
|
| 112 |
|
| 113 |
|
| 114 |
if __name__ == "__main__":
|
| 115 |
+
with gr.Blocks(title=title, theme='ParityError/Interstellar') as zero_pal:
|
| 116 |
gr.Markdown(title_markdown)
|
| 117 |
|
| 118 |
with gr.Row():
|
| 119 |
with gr.Column():
|
| 120 |
inputs = gr.Textbox(
|
| 121 |
+
placeholder="请您在这里输入任何关于 LightZero 的问题。",
|
| 122 |
label="问题 (Q)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
temperature = gr.Slider(minimum=0.0, maximum=1.0, value=0.01, step=0.01, label="温度参数")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
k = gr.Slider(minimum=1, maximum=10, value=5, step=1, label="检索到的文档块数量")
|
| 125 |
with gr.Row():
|
| 126 |
gr_submit = gr.Button('提交')
|
|
|
|
| 128 |
|
| 129 |
outputs_answer = gr.Textbox(placeholder="当你点击提交按钮后,这里会显示 RAG 模型给出的回答。",
|
| 130 |
label="回答 (A)")
|
| 131 |
+
outputs_history = gr.Textbox(label="对话历史")
|
| 132 |
with gr.Row():
|
| 133 |
outputs_context = gr.Markdown(label="参考的文档,检索得到的 context 用高亮显示 (C)")
|
| 134 |
+
gr_clear.click(clear_context, outputs=[outputs_context, outputs_history])
|
|
|
|
|
|
|
| 135 |
gr_submit.click(
|
| 136 |
rag_answer,
|
| 137 |
+
inputs=[inputs, temperature, k],
|
| 138 |
+
outputs=[outputs_answer, outputs_context, outputs_history],
|
| 139 |
)
|
| 140 |
+
gr.Markdown(tos_markdown)
|
| 141 |
|
| 142 |
concurrency = int(os.environ.get('CONCURRENCY', os.cpu_count()))
|
| 143 |
favicon_path = os.path.join(os.path.dirname(__file__), 'assets', 'avatar.png')
|
| 144 |
+
zero_pal.queue().launch(max_threads=concurrency, favicon_path=favicon_path, share=True)
|
app_mqa_database.py
ADDED
|
@@ -0,0 +1,214 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import sqlite3
|
| 3 |
+
import threading
|
| 4 |
+
|
| 5 |
+
import gradio as gr
|
| 6 |
+
from dotenv import load_dotenv
|
| 7 |
+
from langchain.document_loaders import TextLoader
|
| 8 |
+
|
| 9 |
+
from RAG.analyze_conversation_history import analyze_conversation_history
|
| 10 |
+
from rag_demo import load_and_split_document, create_vector_store, setup_rag_chain, execute_query
|
| 11 |
+
|
| 12 |
+
# 环境设置
|
| 13 |
+
load_dotenv() # 加载环境变量
|
| 14 |
+
QUESTION_LANG = os.getenv("QUESTION_LANG") # 从环境变量获取 QUESTION_LANG
|
| 15 |
+
assert QUESTION_LANG in ['cn', 'en'], QUESTION_LANG
|
| 16 |
+
|
| 17 |
+
if QUESTION_LANG == "cn":
|
| 18 |
+
title = "ZeroPal"
|
| 19 |
+
title_markdown = """
|
| 20 |
+
<div align="center">
|
| 21 |
+
<img src="https://raw.githubusercontent.com/puyuan1996/RAG/main/assets/banner.svg" width="80%" height="20%" alt="Banner Image">
|
| 22 |
+
</div>
|
| 23 |
+
|
| 24 |
+
📢 **操作说明**:请在下方的“问题”框中输入关于 LightZero 的问题,并点击“提交”按钮。右侧的“回答”框将展示 RAG 模型提供的答案。
|
| 25 |
+
您可以在问答框下方查看当前“对话历史”,点击“清除上下文”按钮可清空历史记录。在“对话历史”框下方,您将找到相关参考文档,其中相关文段将以黄色高亮显示。
|
| 26 |
+
如果您喜欢这个项目,请在 GitHub [LightZero RAG Demo](https://github.com/puyuan1996/RAG) 上给我们点赞!✨ 您的支持是我们持续更新的动力。
|
| 27 |
+
|
| 28 |
+
<div align="center">
|
| 29 |
+
<strong>注意:算法模型输出可能包含一定的随机性。结果不代表开发者和相关 AI 服务的态度和意见。本项目开发者不对结果作出任何保证,仅供参考之用。使用该服务即代表同意后文所述的使用条款。</strong>
|
| 30 |
+
</div>
|
| 31 |
+
"""
|
| 32 |
+
tos_markdown = """
|
| 33 |
+
### 使用条款
|
| 34 |
+
|
| 35 |
+
使用本服务的玩家需同意以下条款:
|
| 36 |
+
|
| 37 |
+
- 本服务为探索性研究的预览版,仅供非商业用途。
|
| 38 |
+
- 服务不得用于任何非法、有害、暴力、种族主义或其他令人反感的目的。
|
| 39 |
+
- 服务提供有限的安全措施,并可能生成令人反感的内容。
|
| 40 |
+
- 如果您对服务体验不满,请通过 opendilab@pjlab.org.cn 与我们联系!我们承诺修复问题并不断改进项目。
|
| 41 |
+
- 为了获得最佳体验,请使用台式电脑,因为移动设备可能会影响视觉效果。
|
| 42 |
+
|
| 43 |
+
**版权所有 © 2024 OpenDILab。保留所有权利。**
|
| 44 |
+
"""
|
| 45 |
+
|
| 46 |
+
# 路径变量,方便之后的文件使用
|
| 47 |
+
file_path = './documents/LightZero_README_zh.md'
|
| 48 |
+
|
| 49 |
+
# 加载原始Markdown文档
|
| 50 |
+
loader = TextLoader(file_path)
|
| 51 |
+
orig_documents = loader.load()
|
| 52 |
+
|
| 53 |
+
# 存储对话历史
|
| 54 |
+
conversation_history = {}
|
| 55 |
+
|
| 56 |
+
# 创建线程局部数据对象
|
| 57 |
+
threadLocal = threading.local()
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def get_db_connection():
|
| 61 |
+
"""
|
| 62 |
+
返回当前线程的数据库连接
|
| 63 |
+
"""
|
| 64 |
+
conn = getattr(threadLocal, 'conn', None)
|
| 65 |
+
if conn is None:
|
| 66 |
+
# 连接到SQLite数据库
|
| 67 |
+
conn = sqlite3.connect('database/conversation_history.db')
|
| 68 |
+
c = conn.cursor()
|
| 69 |
+
# Drop the existing 'history' table if it exists
|
| 70 |
+
# c.execute('DROP TABLE IF EXISTS history')
|
| 71 |
+
# 创建存储对话历史的表
|
| 72 |
+
c.execute('''CREATE TABLE IF NOT EXISTS history
|
| 73 |
+
(id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 74 |
+
user_id TEXT NOT NULL,
|
| 75 |
+
user_input TEXT NOT NULL,
|
| 76 |
+
assistant_output TEXT NOT NULL,
|
| 77 |
+
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP)''')
|
| 78 |
+
threadLocal.conn = conn
|
| 79 |
+
return conn
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
def get_db_cursor():
|
| 83 |
+
"""
|
| 84 |
+
返回当前线程的数据库游标
|
| 85 |
+
"""
|
| 86 |
+
conn = get_db_connection()
|
| 87 |
+
c = getattr(threadLocal, 'cursor', None)
|
| 88 |
+
if c is None:
|
| 89 |
+
c = conn.cursor()
|
| 90 |
+
threadLocal.cursor = c
|
| 91 |
+
return c
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
# 程序结束时清理数据库连接
|
| 95 |
+
def close_db_connection():
|
| 96 |
+
conn = getattr(threadLocal, 'conn', None)
|
| 97 |
+
if conn is not None:
|
| 98 |
+
conn.close()
|
| 99 |
+
setattr(threadLocal, 'conn', None)
|
| 100 |
+
|
| 101 |
+
c = getattr(threadLocal, 'cursor', None)
|
| 102 |
+
if c is not None:
|
| 103 |
+
c.close()
|
| 104 |
+
setattr(threadLocal, 'cursor', None)
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
def rag_answer(question, temperature, k, user_id):
|
| 108 |
+
"""
|
| 109 |
+
处理用户问题并返回答案和高亮显示的上下文
|
| 110 |
+
|
| 111 |
+
:param question: 用户输入的问题
|
| 112 |
+
:param temperature: 生成答案时使用的温度参数
|
| 113 |
+
:param k: 检索到的文档块数量
|
| 114 |
+
:param user_id: 用户ID
|
| 115 |
+
:return: 模型生成的答案和高亮显示上下文的Markdown文本
|
| 116 |
+
"""
|
| 117 |
+
try:
|
| 118 |
+
chunks = load_and_split_document(file_path, chunk_size=5000, chunk_overlap=500)
|
| 119 |
+
retriever = create_vector_store(chunks, model='OpenAI', k=k)
|
| 120 |
+
rag_chain = setup_rag_chain(model_name='kimi', temperature=temperature)
|
| 121 |
+
|
| 122 |
+
if user_id not in conversation_history:
|
| 123 |
+
conversation_history[user_id] = []
|
| 124 |
+
|
| 125 |
+
conversation_history[user_id].append((f"User[{user_id}]", question))
|
| 126 |
+
|
| 127 |
+
history_str = "\n".join([f"{role}: {text}" for role, text in conversation_history[user_id]])
|
| 128 |
+
|
| 129 |
+
retrieved_documents, answer = execute_query(retriever, rag_chain, history_str, model_name='kimi',
|
| 130 |
+
temperature=temperature)
|
| 131 |
+
|
| 132 |
+
############################
|
| 133 |
+
# 获取当前线程的数据库连接和游标
|
| 134 |
+
############################
|
| 135 |
+
conn = get_db_connection()
|
| 136 |
+
c = get_db_cursor()
|
| 137 |
+
|
| 138 |
+
# 分析对话历史
|
| 139 |
+
# analyze_conversation_history()
|
| 140 |
+
# 获取总的对话记录数
|
| 141 |
+
c.execute("SELECT COUNT(*) FROM history")
|
| 142 |
+
total_records = c.fetchone()[0]
|
| 143 |
+
print(f"总对话记录数: {total_records}")
|
| 144 |
+
|
| 145 |
+
# 将问题和回答存储到数据库
|
| 146 |
+
c.execute("INSERT INTO history (user_id, user_input, assistant_output) VALUES (?, ?, ?)",
|
| 147 |
+
(user_id, question, answer))
|
| 148 |
+
conn.commit()
|
| 149 |
+
|
| 150 |
+
# 在文档中高亮显示上下文
|
| 151 |
+
context = [retrieved_documents[i].page_content for i in range(len(retrieved_documents))]
|
| 152 |
+
highlighted_document = orig_documents[0].page_content
|
| 153 |
+
for i in range(len(context)):
|
| 154 |
+
highlighted_document = highlighted_document.replace(context[i], f"<mark>{context[i]}</mark>")
|
| 155 |
+
|
| 156 |
+
conversation_history[user_id].append(("Assistant", answer))
|
| 157 |
+
|
| 158 |
+
full_history = "\n".join([f"{role}: {text}" for role, text in conversation_history[user_id]])
|
| 159 |
+
except Exception as e:
|
| 160 |
+
print(f"An error occurred: {e}")
|
| 161 |
+
return "处理您的问题时出现错误,请稍后再试。", "", ""
|
| 162 |
+
finally:
|
| 163 |
+
# 不再在这里关闭游标和连接
|
| 164 |
+
pass
|
| 165 |
+
|
| 166 |
+
return answer, highlighted_document, full_history
|
| 167 |
+
|
| 168 |
+
|
| 169 |
+
def clear_context(user_id):
|
| 170 |
+
"""
|
| 171 |
+
清除对话历史
|
| 172 |
+
"""
|
| 173 |
+
if user_id in conversation_history:
|
| 174 |
+
conversation_history[user_id] = []
|
| 175 |
+
return "", "", ""
|
| 176 |
+
|
| 177 |
+
|
| 178 |
+
if __name__ == "__main__":
|
| 179 |
+
with gr.Blocks(title=title, theme='ParityError/Interstellar') as zero_pal:
|
| 180 |
+
gr.Markdown(title_markdown)
|
| 181 |
+
|
| 182 |
+
with gr.Row():
|
| 183 |
+
with gr.Column():
|
| 184 |
+
user_id = gr.Textbox(
|
| 185 |
+
placeholder="请输入您的真实姓名或昵称作为用户ID",
|
| 186 |
+
label="用户ID")
|
| 187 |
+
inputs = gr.Textbox(
|
| 188 |
+
placeholder="请您在这里输入任何关于 LightZero 的问题。",
|
| 189 |
+
label="问题")
|
| 190 |
+
temperature = gr.Slider(minimum=0.0, maximum=1.0, value=0.01, step=0.01, label="温度参数")
|
| 191 |
+
k = gr.Slider(minimum=1, maximum=10, value=5, step=1, label="检索到的文档块数量")
|
| 192 |
+
with gr.Row():
|
| 193 |
+
gr_submit = gr.Button('提交')
|
| 194 |
+
gr_clear = gr.Button('清除上下文')
|
| 195 |
+
|
| 196 |
+
outputs_answer = gr.Textbox(placeholder="当你点击提交按钮后,这里会显示 RAG 模型给出的回答。",
|
| 197 |
+
label="回答")
|
| 198 |
+
outputs_history = gr.Textbox(label="对话历史")
|
| 199 |
+
with gr.Row():
|
| 200 |
+
outputs_context = gr.Markdown(label="参考的文档(检索得到的相关文段用高亮显示)")
|
| 201 |
+
gr_clear.click(clear_context, inputs=user_id, outputs=[outputs_context, outputs_history])
|
| 202 |
+
gr_submit.click(
|
| 203 |
+
rag_answer,
|
| 204 |
+
inputs=[inputs, temperature, k, user_id],
|
| 205 |
+
outputs=[outputs_answer, outputs_context, outputs_history],
|
| 206 |
+
)
|
| 207 |
+
gr.Markdown(tos_markdown)
|
| 208 |
+
|
| 209 |
+
concurrency = int(os.environ.get('CONCURRENCY', os.cpu_count()))
|
| 210 |
+
favicon_path = os.path.join(os.path.dirname(__file__), 'assets', 'avatar.png')
|
| 211 |
+
zero_pal.queue().launch(max_threads=concurrency, favicon_path=favicon_path, share=True)
|
| 212 |
+
|
| 213 |
+
# 在合适的地方,例如程序退出时,调用close_db_connection函数
|
| 214 |
+
close_db_connection()
|
app_qa.py
DELETED
|
@@ -1,106 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
|
| 3 |
-
import gradio as gr
|
| 4 |
-
from dotenv import load_dotenv
|
| 5 |
-
from langchain.document_loaders import TextLoader
|
| 6 |
-
|
| 7 |
-
from rag_demo import load_and_split_document, create_vector_store, setup_rag_chain, execute_query
|
| 8 |
-
|
| 9 |
-
# 环境设置
|
| 10 |
-
load_dotenv() # 加载环境变量
|
| 11 |
-
QUESTION_LANG = os.getenv("QUESTION_LANG") # 从环境变量获取 QUESTION_LANG
|
| 12 |
-
assert QUESTION_LANG in ['cn', 'en'], QUESTION_LANG
|
| 13 |
-
|
| 14 |
-
if QUESTION_LANG == "cn":
|
| 15 |
-
title = "LightZero RAG Demo"
|
| 16 |
-
title_markdown = """
|
| 17 |
-
<div align="center">
|
| 18 |
-
<img src="https://raw.githubusercontent.com/puyuan1996/RAG/main/assets/banner.svg" width="80%" height="20%" alt="Banner Image">
|
| 19 |
-
</div>
|
| 20 |
-
<h2 style="text-align: center; color: black;"><a href="https://github.com/puyuan1996/RAG"> LightZero RAG Demo</a></h2>
|
| 21 |
-
<h4 align="center"> 📢说明:请您在下面的"问题(Q)"框中输入任何关于 LightZero 的问题,然后点击"提交"按钮。右侧"回答(A)"框中会显示 RAG 模型给出的回答。在 QA 栏的下方会给出参考文档(其中检索得到的相关文段会用黄色高亮显示)。</h4>
|
| 22 |
-
<h4 align="center"> 如果你喜欢这个项目,请给我们在 GitHub 点个 star ✨ 。我们将会持续保持更新。 </h4>
|
| 23 |
-
<strong><h5 align="center">注意:算法模型的输出可能包含一定的随机性。相关结果不代表任何开发者和相关 AI 服务的态度和意见。本项目开发者不对生成结果作任何保证,仅供参考。<h5></strong>
|
| 24 |
-
"""
|
| 25 |
-
tos_markdown = """
|
| 26 |
-
### 使用条款
|
| 27 |
-
玩家使用本服务须同意以下条款:
|
| 28 |
-
该服务是一项探索性研究预览版,仅供非商业用途。它仅提供有限的安全措施,并可能生成令人反感的内容。不得将其用于任何非法、有害、暴力、种族主义等目的。
|
| 29 |
-
如果您的游玩体验有不佳之处,请发送邮件至 opendilab@pjlab.org.cn ! 我们将删除相关信息,并不断改进这个项目。
|
| 30 |
-
为了获得最佳体验,请使用台式电脑,因为移动设备可能会影响可视化效果。
|
| 31 |
-
**版权所有 2024 OpenDILab。**
|
| 32 |
-
"""
|
| 33 |
-
|
| 34 |
-
# 路径变量,方便之后的文件使用
|
| 35 |
-
file_path = './documents/LightZero_README.zh.md'
|
| 36 |
-
|
| 37 |
-
# 加载原始Markdown文档
|
| 38 |
-
loader = TextLoader(file_path)
|
| 39 |
-
orig_documents = loader.load()
|
| 40 |
-
|
| 41 |
-
def rag_answer(question, model_name, temperature, embedding_model, k):
|
| 42 |
-
"""
|
| 43 |
-
处理用户问题并返回答案和高亮显示的上下文
|
| 44 |
-
|
| 45 |
-
:param question: 用户输入的问题
|
| 46 |
-
:param model_name: 使用的语言模型名称
|
| 47 |
-
:param temperature: 生成答案时使用的温度参数
|
| 48 |
-
:param embedding_model: 使用的嵌入模型
|
| 49 |
-
:param k: 检索到的文档块数量
|
| 50 |
-
:return: 模型生成的答案和高亮显示上下文的Markdown文本
|
| 51 |
-
"""
|
| 52 |
-
try:
|
| 53 |
-
chunks = load_and_split_document(file_path, chunk_size=5000, chunk_overlap=500)
|
| 54 |
-
retriever = create_vector_store(chunks, model=embedding_model, k=k)
|
| 55 |
-
rag_chain = setup_rag_chain(model_name=model_name, temperature=temperature)
|
| 56 |
-
|
| 57 |
-
retrieved_documents, answer = execute_query(retriever, rag_chain, question, model_name=model_name, temperature=temperature)
|
| 58 |
-
# 在文档中高亮显示上下文
|
| 59 |
-
context = [retrieved_documents[i].page_content for i in range(len(retrieved_documents))]
|
| 60 |
-
highlighted_document = orig_documents[0].page_content
|
| 61 |
-
for i in range(len(context)):
|
| 62 |
-
highlighted_document = highlighted_document.replace(context[i], f"<mark>{context[i]}</mark>")
|
| 63 |
-
except Exception as e:
|
| 64 |
-
print(f"An error occurred: {e}")
|
| 65 |
-
return "处理您的问题时出现错误,请稍后再试。", ""
|
| 66 |
-
return answer, highlighted_document
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
if __name__ == "__main__":
|
| 70 |
-
with gr.Blocks(title=title, theme='ParityError/Interstellar') as rag_demo:
|
| 71 |
-
gr.Markdown(title_markdown)
|
| 72 |
-
|
| 73 |
-
with gr.Row():
|
| 74 |
-
with gr.Column():
|
| 75 |
-
inputs = gr.Textbox(
|
| 76 |
-
placeholder="请您输入任何关于 LightZero 的问题。",
|
| 77 |
-
label="问题 (Q)")
|
| 78 |
-
model_name = gr.Dropdown(
|
| 79 |
-
choices=['kimi', 'abab6-chat', 'glm-4', 'gpt-3.5-turbo', 'gpt-4', 'gpt-4-turbo', 'azure_gpt-4', 'azure_gpt-35-turbo-16k', 'azure_gpt-35-turbo'],
|
| 80 |
-
# value='azure_gpt-4',
|
| 81 |
-
value='kimi',
|
| 82 |
-
label="选择语言模型")
|
| 83 |
-
temperature = gr.Slider(minimum=0.0, maximum=1.0, value=0.01, step=0.01, label="温度参数")
|
| 84 |
-
embedding_model = gr.Dropdown(
|
| 85 |
-
choices=['HuggingFace', 'TensorflowHub', 'OpenAI'],
|
| 86 |
-
value='OpenAI',
|
| 87 |
-
label="选择嵌入模型")
|
| 88 |
-
k = gr.Slider(minimum=1, maximum=10, value=5, step=1, label="检索到的文档块数量")
|
| 89 |
-
gr_submit = gr.Button('提交')
|
| 90 |
-
|
| 91 |
-
outputs_answer = gr.Textbox(placeholder="当你点击提交按钮后,这里会显示 RAG 模型给出的回答。",
|
| 92 |
-
label="回答 (A)")
|
| 93 |
-
with gr.Row():
|
| 94 |
-
outputs_context = gr.Markdown(label="参考的文档,检索得到的 context 用高亮显示 (C)")
|
| 95 |
-
|
| 96 |
-
gr.Markdown(tos_markdown)
|
| 97 |
-
|
| 98 |
-
gr_submit.click(
|
| 99 |
-
rag_answer,
|
| 100 |
-
inputs=[inputs, model_name, temperature, embedding_model, k],
|
| 101 |
-
outputs=[outputs_answer, outputs_context],
|
| 102 |
-
)
|
| 103 |
-
|
| 104 |
-
concurrency = int(os.environ.get('CONCURRENCY', os.cpu_count()))
|
| 105 |
-
favicon_path = os.path.join(os.path.dirname(__file__), 'assets', 'avatar.png')
|
| 106 |
-
rag_demo.queue().launch(max_threads=concurrency, favicon_path=favicon_path, share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assets/banner.svg
CHANGED
|
|
|
|
documents/LightZero_README.md
CHANGED
|
@@ -27,7 +27,7 @@
|
|
| 27 |
[](https://github.com/opendilab/LightZero/graphs/contributors)
|
| 28 |
[](https://github.com/opendilab/LightZero/blob/master/LICENSE)
|
| 29 |
|
| 30 |
-
Updated on
|
| 31 |
|
| 32 |
> LightZero is a lightweight, efficient, and easy-to-understand open-source algorithm toolkit that combines Monte Carlo Tree Search (MCTS) and Deep Reinforcement Learning (RL).
|
| 33 |
|
|
@@ -207,6 +207,15 @@ cd LightZero
|
|
| 207 |
python3 -u zoo/board_games/tictactoe/config/tictactoe_muzero_bot_mode_config.py
|
| 208 |
```
|
| 209 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
## Benchmark
|
| 211 |
|
| 212 |
<details open><summary>Click to collapse</summary>
|
|
@@ -374,6 +383,14 @@ Here is a collection of research papers about **Monte Carlo Tree Search**.
|
|
| 374 |
- ExpEnv: USPTO datasets
|
| 375 |
- [Code](https://github.com/binghong-ml/retro_star)
|
| 376 |
#### ICLR
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 377 |
- [Become a Proficient Player with Limited Data through Watching Pure Videos](https://openreview.net/pdf?id=Sy-o2N0hF4f) 2023
|
| 378 |
- Weirui Ye, Yunsheng Zhang, Pieter Abbeel, Yang Gao
|
| 379 |
- Key: pre-training from action-free videos, forward-inverse cycle consistency (FICC) objective based on vector quantization, pre-training phase, fine-tuning phase.
|
|
@@ -442,6 +459,10 @@ Here is a collection of research papers about **Monte Carlo Tree Search**.
|
|
| 442 |
- Yangqing Fu, Ming Sun, Buqing Nie, Yue Gao
|
| 443 |
- Key: probability tree state abstraction, transitivity and aggregation error bound
|
| 444 |
- ExpEnv: Atari, CartPole, LunarLander, Gomoku
|
|
|
|
|
|
|
|
|
|
|
|
|
| 445 |
- [Planning for Sample Efficient Imitation Learning](https://openreview.net/forum?id=BkN5UoAqF7) 2022
|
| 446 |
- Zhao-Heng Yin, Weirui Ye, Qifeng Chen, Yang Gao
|
| 447 |
- Key: Behavioral Cloning,Adversarial Imitation Learning (AIL),MCTS-based RL.
|
|
@@ -485,6 +506,7 @@ Here is a collection of research papers about **Monte Carlo Tree Search**.
|
|
| 485 |
- [Code](https://github.com/matthewfaw/mixnmatch)
|
| 486 |
|
| 487 |
#### Other Conference or Journal
|
|
|
|
| 488 |
- [On Monte Carlo Tree Search and Reinforcement Learning](https://www.jair.org/index.php/jair/article/download/11099/26289/20632) Journal of Artificial Intelligence Research 2017.
|
| 489 |
- [Sample-Efficient Neural Architecture Search by Learning Actions for Monte Carlo Tree Search](https://arxiv.org/pdf/1906.06832) IEEE Transactions on Pattern Analysis and Machine Intelligence 2022.
|
| 490 |
</details>
|
|
|
|
| 27 |
[](https://github.com/opendilab/LightZero/graphs/contributors)
|
| 28 |
[](https://github.com/opendilab/LightZero/blob/master/LICENSE)
|
| 29 |
|
| 30 |
+
Updated on 2024.03.15 LightZero-v0.0.4
|
| 31 |
|
| 32 |
> LightZero is a lightweight, efficient, and easy-to-understand open-source algorithm toolkit that combines Monte Carlo Tree Search (MCTS) and Deep Reinforcement Learning (RL).
|
| 33 |
|
|
|
|
| 207 |
python3 -u zoo/board_games/tictactoe/config/tictactoe_muzero_bot_mode_config.py
|
| 208 |
```
|
| 209 |
|
| 210 |
+
## Customization Documentation
|
| 211 |
+
|
| 212 |
+
For those looking to tailor environments and algorithms, we offer comprehensive guides:
|
| 213 |
+
|
| 214 |
+
- **Environments:** [Customize Environments](https://github.com/opendilab/LightZero/blob/main/docs/source/tutorials/envs/customize_envs.md)
|
| 215 |
+
- **Algorithms:** [Customize Algorithms](https://github.com/opendilab/LightZero/blob/main/docs/source/tutorials/algos/customize_algos.md)
|
| 216 |
+
|
| 217 |
+
Should you have any questions, feel free to contact us for support.
|
| 218 |
+
|
| 219 |
## Benchmark
|
| 220 |
|
| 221 |
<details open><summary>Click to collapse</summary>
|
|
|
|
| 383 |
- ExpEnv: USPTO datasets
|
| 384 |
- [Code](https://github.com/binghong-ml/retro_star)
|
| 385 |
#### ICLR
|
| 386 |
+
- [The Update Equivalence Framework for Decision-Time Planning](https://openreview.net/forum?id=JXGph215fL) 2024
|
| 387 |
+
- Samuel Sokota, Gabriele Farina, David J Wu, Hengyuan Hu, Kevin A. Wang, J Zico Kolter, Noam Brown
|
| 388 |
+
- Key: imperfect-information games, search, decision-time planning, update equivalence
|
| 389 |
+
- ExpEnv: Hanabi, 3x3 Abrupt Dark Hex and Phantom Tic-Tac-Toe
|
| 390 |
+
- [Efficient Multi-agent Reinforcement Learning by Planning](https://openreview.net/forum?id=CpnKq3UJwp) 2024
|
| 391 |
+
- Qihan Liu, Jianing Ye, Xiaoteng Ma, Jun Yang, Bin Liang, Chongjie Zhang
|
| 392 |
+
- Key: multi-agent reinforcement learning, planning, multi-agent MCTS
|
| 393 |
+
- ExpEnv: SMAC, LunarLander, MuJoCo, and Google Research Football
|
| 394 |
- [Become a Proficient Player with Limited Data through Watching Pure Videos](https://openreview.net/pdf?id=Sy-o2N0hF4f) 2023
|
| 395 |
- Weirui Ye, Yunsheng Zhang, Pieter Abbeel, Yang Gao
|
| 396 |
- Key: pre-training from action-free videos, forward-inverse cycle consistency (FICC) objective based on vector quantization, pre-training phase, fine-tuning phase.
|
|
|
|
| 459 |
- Yangqing Fu, Ming Sun, Buqing Nie, Yue Gao
|
| 460 |
- Key: probability tree state abstraction, transitivity and aggregation error bound
|
| 461 |
- ExpEnv: Atari, CartPole, LunarLander, Gomoku
|
| 462 |
+
- [Spending Thinking Time Wisely: Accelerating MCTS with Virtual Expansions](https://openreview.net/pdf?id=B_LdLljS842) 2022
|
| 463 |
+
- Weirui Ye, Pieter Abbeel, Yang Gao
|
| 464 |
+
- Key: trade off computation versus performancem, virtual expansions, spend thinking time adaptively.
|
| 465 |
+
- ExpEnv: Atari, 9x9 Go
|
| 466 |
- [Planning for Sample Efficient Imitation Learning](https://openreview.net/forum?id=BkN5UoAqF7) 2022
|
| 467 |
- Zhao-Heng Yin, Weirui Ye, Qifeng Chen, Yang Gao
|
| 468 |
- Key: Behavioral Cloning,Adversarial Imitation Learning (AIL),MCTS-based RL.
|
|
|
|
| 506 |
- [Code](https://github.com/matthewfaw/mixnmatch)
|
| 507 |
|
| 508 |
#### Other Conference or Journal
|
| 509 |
+
- [Learning to Stop: Dynamic Simulation Monte-Carlo Tree Search](https://arxiv.org/pdf/2012.07910.pdf) AAAI 2021.
|
| 510 |
- [On Monte Carlo Tree Search and Reinforcement Learning](https://www.jair.org/index.php/jair/article/download/11099/26289/20632) Journal of Artificial Intelligence Research 2017.
|
| 511 |
- [Sample-Efficient Neural Architecture Search by Learning Actions for Monte Carlo Tree Search](https://arxiv.org/pdf/1906.06832) IEEE Transactions on Pattern Analysis and Machine Intelligence 2022.
|
| 512 |
</details>
|
documents/{LightZero_README.zh.md → LightZero_README_zh.md}
RENAMED
|
@@ -27,7 +27,7 @@
|
|
| 27 |
[](https://github.com/opendilab/LightZero/graphs/contributors)
|
| 28 |
[](https://github.com/opendilab/LightZero/blob/master/LICENSE)
|
| 29 |
|
| 30 |
-
最近更新于
|
| 31 |
|
| 32 |
> LightZero 是一个轻量、高效、易懂的 MCTS+RL 开源算法库。
|
| 33 |
|
|
@@ -191,6 +191,14 @@ python3 -u zoo/atari/config/atari_muzero_config.py
|
|
| 191 |
cd LightZero
|
| 192 |
python3 -u zoo/board_games/tictactoe/config/tictactoe_muzero_bot_mode_config.py
|
| 193 |
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
|
| 195 |
## 基线算法比较
|
| 196 |
|
|
@@ -352,7 +360,7 @@ python3 -u zoo/board_games/tictactoe/config/tictactoe_muzero_bot_mode_config.py
|
|
| 352 |
- ExpEnv: Gridworld and SysAdmin
|
| 353 |
- [Efficient Learning for AlphaZero via Path Consistency](https://proceedings.mlr.press/v162/zhao22h/zhao22h.pdf) 2022
|
| 354 |
- Dengwei Zhao, Shikui Tu, Lei Xu
|
| 355 |
-
- Key: limited amount of self-plays,
|
| 356 |
- ExpEnv: Go, Othello, Gomoku
|
| 357 |
- [Visualizing MuZero Models](https://arxiv.org/abs/2102.12924) 2021
|
| 358 |
- Joery A. de Vries, Ken S. Voskuil, Thomas M. Moerland, Aske Plaat
|
|
@@ -361,7 +369,7 @@ python3 -u zoo/board_games/tictactoe/config/tictactoe_muzero_bot_mode_config.py
|
|
| 361 |
and internal state transition dynamics,
|
| 362 |
- [Convex Regularization in Monte-Carlo Tree Search](https://arxiv.org/pdf/2007.00391.pdf) 2021
|
| 363 |
- Tuan Dam, Carlo D'Eramo, Jan Peters, Joni Pajarinen
|
| 364 |
-
- Key: entropy-regularization backup operators, regret analysis, Tsallis etropy
|
| 365 |
- ExpEnv: synthetic tree, Atari
|
| 366 |
- [Information Particle Filter Tree: An Online Algorithm for POMDPs with Belief-Based Rewards on Continuous Domains](http://proceedings.mlr.press/v119/fischer20a/fischer20a.pdf) 2020
|
| 367 |
- Johannes Fischer, Ömer Sahin Tas
|
|
@@ -374,6 +382,14 @@ and internal state transition dynamics,
|
|
| 374 |
- ExpEnv: USPTO datasets
|
| 375 |
- [Code](https://github.com/binghong-ml/retro_star)
|
| 376 |
#### ICLR
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 377 |
- [Become a Proficient Player with Limited Data through Watching Pure Videos](https://openreview.net/pdf?id=Sy-o2N0hF4f) 2023
|
| 378 |
- Weirui Ye, Yunsheng Zhang, Pieter Abbeel, Yang Gao
|
| 379 |
- Key: pre-training from action-free videos, forward-inverse cycle consistency (FICC) objective based on vector quantization, pre-training phase, fine-tuning phase.
|
|
@@ -421,8 +437,8 @@ and internal state transition dynamics,
|
|
| 421 |
- Binghong Chen, Bo Dai, Qinjie Lin, Guo Ye, Han Liu, Le Song
|
| 422 |
- Key: meta path planning algorithm, exploits a novel neural architecture which can learn promising search directions from problem structures.
|
| 423 |
- ExpEnv: a 2d workspace with a 2 DoF (degrees of freedom) point robot, a 3 DoF stick robot and a 5 DoF snake robot
|
| 424 |
-
#### NeurIPS
|
| 425 |
|
|
|
|
| 426 |
- [LightZero: A Unified Benchmark for Monte Carlo Tree Search in General Sequential Decision Scenarios](https://openreview.net/pdf?id=oIUXpBnyjv) 2023
|
| 427 |
- Yazhe Niu, Yuan Pu, Zhenjie Yang, Xueyan Li, Tong Zhou, Jiyuan Ren, Shuai Hu, Hongsheng Li, Yu Liu
|
| 428 |
- Key: the first unified benchmark for deploying MCTS/MuZero in general sequential decision scenarios.
|
|
@@ -443,6 +459,10 @@ and internal state transition dynamics,
|
|
| 443 |
- Yangqing Fu, Ming Sun, Buqing Nie, Yue Gao
|
| 444 |
- Key: probability tree state abstraction, transitivity and aggregation error bound
|
| 445 |
- ExpEnv: Atari, CartPole, LunarLander, Gomoku
|
|
|
|
|
|
|
|
|
|
|
|
|
| 446 |
- [Planning for Sample Efficient Imitation Learning](https://openreview.net/forum?id=BkN5UoAqF7) 2022
|
| 447 |
- Zhao-Heng Yin, Weirui Ye, Qifeng Chen, Yang Gao
|
| 448 |
- Key: Behavioral Cloning,Adversarial Imitation Learning (AIL),MCTS-based RL,
|
|
@@ -486,6 +506,7 @@ and internal state transition dynamics,
|
|
| 486 |
- [Code](https://github.com/matthewfaw/mixnmatch)
|
| 487 |
|
| 488 |
#### Other Conference or Journal
|
|
|
|
| 489 |
- [On Monte Carlo Tree Search and Reinforcement Learning](https://www.jair.org/index.php/jair/article/download/11099/26289/20632) Journal of Artificial Intelligence Research 2017.
|
| 490 |
- [Sample-Efficient Neural Architecture Search by Learning Actions for Monte Carlo Tree Search](https://arxiv.org/pdf/1906.06832) IEEE Transactions on Pattern Analysis and Machine Intelligence 2022.
|
| 491 |
</details>
|
|
|
|
| 27 |
[](https://github.com/opendilab/LightZero/graphs/contributors)
|
| 28 |
[](https://github.com/opendilab/LightZero/blob/master/LICENSE)
|
| 29 |
|
| 30 |
+
最近更新于 2024.03.15 LightZero-v0.0.4
|
| 31 |
|
| 32 |
> LightZero 是一个轻量、高效、易懂的 MCTS+RL 开源算法库。
|
| 33 |
|
|
|
|
| 191 |
cd LightZero
|
| 192 |
python3 -u zoo/board_games/tictactoe/config/tictactoe_muzero_bot_mode_config.py
|
| 193 |
```
|
| 194 |
+
## 定制化文档
|
| 195 |
+
|
| 196 |
+
为希望定制环境和算法的用户,我们提供了全面的指南:
|
| 197 |
+
|
| 198 |
+
- **环境定制:** [定制环境](https://github.com/opendilab/LightZero/blob/main/docs/source/tutorials/envs/customize_envs_zh.md)
|
| 199 |
+
- **算法定制:** [定制算法](https://github.com/opendilab/LightZero/blob/main/docs/source/tutorials/algos/customize_algos_zh.md)
|
| 200 |
+
|
| 201 |
+
如有任何疑问,欢迎随时联系我们寻求帮助。
|
| 202 |
|
| 203 |
## 基线算法比较
|
| 204 |
|
|
|
|
| 360 |
- ExpEnv: Gridworld and SysAdmin
|
| 361 |
- [Efficient Learning for AlphaZero via Path Consistency](https://proceedings.mlr.press/v162/zhao22h/zhao22h.pdf) 2022
|
| 362 |
- Dengwei Zhao, Shikui Tu, Lei Xu
|
| 363 |
+
- Key: limited amount of self-plays, path consistency (PC) optimality
|
| 364 |
- ExpEnv: Go, Othello, Gomoku
|
| 365 |
- [Visualizing MuZero Models](https://arxiv.org/abs/2102.12924) 2021
|
| 366 |
- Joery A. de Vries, Ken S. Voskuil, Thomas M. Moerland, Aske Plaat
|
|
|
|
| 369 |
and internal state transition dynamics,
|
| 370 |
- [Convex Regularization in Monte-Carlo Tree Search](https://arxiv.org/pdf/2007.00391.pdf) 2021
|
| 371 |
- Tuan Dam, Carlo D'Eramo, Jan Peters, Joni Pajarinen
|
| 372 |
+
- Key: entropy-regularization backup operators, regret analysis, Tsallis etropy
|
| 373 |
- ExpEnv: synthetic tree, Atari
|
| 374 |
- [Information Particle Filter Tree: An Online Algorithm for POMDPs with Belief-Based Rewards on Continuous Domains](http://proceedings.mlr.press/v119/fischer20a/fischer20a.pdf) 2020
|
| 375 |
- Johannes Fischer, Ömer Sahin Tas
|
|
|
|
| 382 |
- ExpEnv: USPTO datasets
|
| 383 |
- [Code](https://github.com/binghong-ml/retro_star)
|
| 384 |
#### ICLR
|
| 385 |
+
- [The Update Equivalence Framework for Decision-Time Planning](https://openreview.net/forum?id=JXGph215fL) 2024
|
| 386 |
+
- Samuel Sokota, Gabriele Farina, David J Wu, Hengyuan Hu, Kevin A. Wang, J Zico Kolter, Noam Brown
|
| 387 |
+
- Key: imperfect-information games, search, decision-time planning, update equivalence
|
| 388 |
+
- ExpEnv: Hanabi, 3x3 Abrupt Dark Hex and Phantom Tic-Tac-Toe
|
| 389 |
+
- [Efficient Multi-agent Reinforcement Learning by Planning](https://openreview.net/forum?id=CpnKq3UJwp) 2024
|
| 390 |
+
- Qihan Liu, Jianing Ye, Xiaoteng Ma, Jun Yang, Bin Liang, Chongjie Zhang
|
| 391 |
+
- Key: multi-agent reinforcement learning, planning, multi-agent MCTS
|
| 392 |
+
- ExpEnv: SMAC, LunarLander, MuJoCo, and Google Research Football
|
| 393 |
- [Become a Proficient Player with Limited Data through Watching Pure Videos](https://openreview.net/pdf?id=Sy-o2N0hF4f) 2023
|
| 394 |
- Weirui Ye, Yunsheng Zhang, Pieter Abbeel, Yang Gao
|
| 395 |
- Key: pre-training from action-free videos, forward-inverse cycle consistency (FICC) objective based on vector quantization, pre-training phase, fine-tuning phase.
|
|
|
|
| 437 |
- Binghong Chen, Bo Dai, Qinjie Lin, Guo Ye, Han Liu, Le Song
|
| 438 |
- Key: meta path planning algorithm, exploits a novel neural architecture which can learn promising search directions from problem structures.
|
| 439 |
- ExpEnv: a 2d workspace with a 2 DoF (degrees of freedom) point robot, a 3 DoF stick robot and a 5 DoF snake robot
|
|
|
|
| 440 |
|
| 441 |
+
#### NeurIPS
|
| 442 |
- [LightZero: A Unified Benchmark for Monte Carlo Tree Search in General Sequential Decision Scenarios](https://openreview.net/pdf?id=oIUXpBnyjv) 2023
|
| 443 |
- Yazhe Niu, Yuan Pu, Zhenjie Yang, Xueyan Li, Tong Zhou, Jiyuan Ren, Shuai Hu, Hongsheng Li, Yu Liu
|
| 444 |
- Key: the first unified benchmark for deploying MCTS/MuZero in general sequential decision scenarios.
|
|
|
|
| 459 |
- Yangqing Fu, Ming Sun, Buqing Nie, Yue Gao
|
| 460 |
- Key: probability tree state abstraction, transitivity and aggregation error bound
|
| 461 |
- ExpEnv: Atari, CartPole, LunarLander, Gomoku
|
| 462 |
+
- [Spending Thinking Time Wisely: Accelerating MCTS with Virtual Expansions](https://openreview.net/pdf?id=B_LdLljS842) 2022
|
| 463 |
+
- Weirui Ye, Pieter Abbeel, Yang Gao
|
| 464 |
+
- Key: trade off computation versus performancem, virtual expansions, spend thinking time adaptively.
|
| 465 |
+
- ExpEnv: Atari, 9x9 Go
|
| 466 |
- [Planning for Sample Efficient Imitation Learning](https://openreview.net/forum?id=BkN5UoAqF7) 2022
|
| 467 |
- Zhao-Heng Yin, Weirui Ye, Qifeng Chen, Yang Gao
|
| 468 |
- Key: Behavioral Cloning,Adversarial Imitation Learning (AIL),MCTS-based RL,
|
|
|
|
| 506 |
- [Code](https://github.com/matthewfaw/mixnmatch)
|
| 507 |
|
| 508 |
#### Other Conference or Journal
|
| 509 |
+
- [Learning to Stop: Dynamic Simulation Monte-Carlo Tree Search](https://arxiv.org/pdf/2012.07910.pdf) AAAI 2021.
|
| 510 |
- [On Monte Carlo Tree Search and Reinforcement Learning](https://www.jair.org/index.php/jair/article/download/11099/26289/20632) Journal of Artificial Intelligence Research 2017.
|
| 511 |
- [Sample-Efficient Neural Architecture Search by Learning Actions for Monte Carlo Tree Search](https://arxiv.org/pdf/1906.06832) IEEE Transactions on Pattern Analysis and Machine Intelligence 2022.
|
| 512 |
</details>
|
rag_demo.py
CHANGED
|
@@ -234,11 +234,11 @@ def execute_query_no_rag(model_name="gpt-4", temperature=0, query=""):
|
|
| 234 |
|
| 235 |
if __name__ == "__main__":
|
| 236 |
# 假设文档已存在于本地
|
| 237 |
-
file_path = './documents/
|
| 238 |
# model_name = "glm-4" # model_name=['abab6-chat', 'glm-4', 'gpt-3.5-turbo', 'gpt-4', 'gpt-4-turbo', 'azure_gpt-4', 'azure_gpt-35-turbo-16k', 'azure_gpt-35-turbo']
|
| 239 |
-
model_name = 'azure_gpt-4'
|
|
|
|
| 240 |
temperature = 0.01
|
| 241 |
-
# embedding_model = 'HuggingFace' # embedding_model=['HuggingFace', 'TensorflowHub', 'OpenAI']
|
| 242 |
embedding_model = 'OpenAI' # embedding_model=['HuggingFace', 'TensorflowHub', 'OpenAI']
|
| 243 |
|
| 244 |
# 加载和分割文档
|
|
@@ -251,11 +251,11 @@ if __name__ == "__main__":
|
|
| 251 |
rag_chain = setup_rag_chain(model_name=model_name, temperature=temperature)
|
| 252 |
|
| 253 |
# 提出问题并获取答案
|
| 254 |
-
query = ("
|
| 255 |
"""
|
| 256 |
-
(1)请简要介绍一下 LightZero
|
| 257 |
(2)请详细介绍 LightZero 的框架结构。
|
| 258 |
-
(3)请给出安装 LightZero,运行他们的示例代码的详细步骤
|
| 259 |
(4)请问 LightZero 具体支持什么任务(tasks/environments)?
|
| 260 |
(5)请问 LightZero 具体支持什么算法?
|
| 261 |
(6)请问 LightZero 具体支持什么算法,各自支持在哪些任务上运行?
|
|
@@ -266,6 +266,7 @@ if __name__ == "__main__":
|
|
| 266 |
(11)请问对这个仓库提出详细的改进建议。
|
| 267 |
"""
|
| 268 |
|
|
|
|
| 269 |
# 使用 RAG 链获取参考的文档与答案
|
| 270 |
retrieved_documents, result_with_rag = execute_query(retriever, rag_chain, query, model_name=model_name,
|
| 271 |
temperature=temperature)
|
|
|
|
| 234 |
|
| 235 |
if __name__ == "__main__":
|
| 236 |
# 假设文档已存在于本地
|
| 237 |
+
file_path = './documents/LightZero_README_zh.md'
|
| 238 |
# model_name = "glm-4" # model_name=['abab6-chat', 'glm-4', 'gpt-3.5-turbo', 'gpt-4', 'gpt-4-turbo', 'azure_gpt-4', 'azure_gpt-35-turbo-16k', 'azure_gpt-35-turbo']
|
| 239 |
+
# model_name = 'azure_gpt-4'
|
| 240 |
+
model_name = 'kimi'
|
| 241 |
temperature = 0.01
|
|
|
|
| 242 |
embedding_model = 'OpenAI' # embedding_model=['HuggingFace', 'TensorflowHub', 'OpenAI']
|
| 243 |
|
| 244 |
# 加载和分割文档
|
|
|
|
| 251 |
rag_chain = setup_rag_chain(model_name=model_name, temperature=temperature)
|
| 252 |
|
| 253 |
# 提出问题并获取答案
|
| 254 |
+
query = ("请回答下面的问题:(1)请简要介绍一下 LightZero。(2)请详细介绍 LightZero 的框架结构。 (3)请给出安装 LightZero,运行他们的示例代码的详细步骤。(4)- 请问 LightZero 具体支持什么任务(tasks/environments)? (5)请问 LightZero 具体支持什么算法?(6)请问 LightZero 具体支持什么算法,各自支持在哪些任务上运行? (7)请问 LightZero 里面实现的 MuZero 算法支持在 Atari 任务上运行吗?(8)请问 LightZero 里面实现的 AlphaZero 算法支持在 Atari 任务上运行吗?(9)LightZero 支持哪些算法? 各自的优缺点是什么? 我应该如何根据任务特点进行选择呢?(10)请结合 LightZero 中的代码介绍他们是如何实现 MCTS 的。(11)请问对这个仓库提出详细的改进建议")
|
| 255 |
"""
|
| 256 |
+
(1)请简要介绍一下 LightZero。
|
| 257 |
(2)请详细介绍 LightZero 的框架结构。
|
| 258 |
+
(3)请给出安装 LightZero,运行他们的示例代码的详细步骤 。
|
| 259 |
(4)请问 LightZero 具体支持什么任务(tasks/environments)?
|
| 260 |
(5)请问 LightZero 具体支持什么算法?
|
| 261 |
(6)请问 LightZero 具体支持什么算法,各自支持在哪些任务上运行?
|
|
|
|
| 266 |
(11)请问对这个仓库提出详细的改进建议。
|
| 267 |
"""
|
| 268 |
|
| 269 |
+
# query = ("请检索最近关于Transformer+RL的最新论文,并给出详细介绍")
|
| 270 |
# 使用 RAG 链获取参考的文档与答案
|
| 271 |
retrieved_documents, result_with_rag = execute_query(retriever, rag_chain, query, model_name=model_name,
|
| 272 |
temperature=temperature)
|