FlashCode-Lab commited on
Commit
4fb210a
·
verified ·
1 Parent(s): d7872e6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -34
app.py CHANGED
@@ -4,53 +4,34 @@ from langchain_huggingface import HuggingFaceEndpoint, HuggingFaceEmbeddings
4
  from langchain_community.vectorstores import FAISS
5
  from langchain_community.document_loaders import TextLoader
6
  from langchain_text_splitters import CharacterTextSplitter
7
- from langchain.chains import RetrievalQA
 
8
 
9
- # 1. 配置大模型动力源 (使用 Qwen 2.5)
10
- # 确保你在 Settings -> Secrets 中设置了 HF_TOKEN
11
  llm = HuggingFaceEndpoint(
12
  repo_id="Qwen/Qwen2.5-7B-Instruct",
13
- huggingfacehub_api_token=os.getenv("HF_TOKEN"),
14
- timeout=300
15
  )
16
 
17
- # 2. 初始化私有知识库
18
- # 如果 knowledge.txt 不存在,先创建一个简单的,防止报错
19
  if not os.path.exists("knowledge.txt"):
20
  with open("knowledge.txt", "w", encoding="utf-8") as f:
21
- f.write("欢迎使用全能 AI 大脑!请在 knowledge.txt 中输入你的私有知识。")
22
 
23
- # 加载并切分文档
24
  loader = TextLoader("knowledge.txt", encoding="utf-8")
25
- documents = loader.load()
26
- text_splitter = CharacterTextSplitter(chunk_size=500, chunk_overlap=50)
27
- docs = text_splitter.split_documents(documents)
28
-
29
- # 3. 创建向量检索系统 (使用中文优化的 Embedding 模型)
30
  embeddings = HuggingFaceEmbeddings(model_name="BAAI/bge-small-zh-v1.5")
31
  vectorstore = FAISS.from_documents(docs, embeddings)
32
 
33
- # 4. 构建问答链 (RAG 核心)
34
- qa_chain = RetrievalQA.from_chain_type(
35
- llm=llm,
36
- retriever=vectorstore.as_retriever(search_kwargs={"k": 3})
37
- )
38
 
39
- # 5. 定义界面交互
40
- def chat_fn(message, history):
41
  try:
42
- # 执行检索并生成回答
43
- response = qa_chain.invoke({"query": message})
44
- return response["result"]
45
  except Exception as e:
46
- return f"大脑连接超时或出错,请检查 Token 设置。错误详情: {str(e)}"
47
-
48
- # 启动 Gradio 界面
49
- demo = gr.ChatInterface(
50
- chat_fn,
51
- title="我的全能私有大脑",
52
- description="基于 Qwen 2.5 + RAG 技术。它会先查阅你的 knowledge.txt 再回答。"
53
- )
54
 
55
- if __name__ == "__main__":
56
- demo.launch()
 
4
  from langchain_community.vectorstores import FAISS
5
  from langchain_community.document_loaders import TextLoader
6
  from langchain_text_splitters import CharacterTextSplitter
7
+ # 修改这里:使用更稳定的导入路径
8
+ from langchain.chains.retrieval_qa.base import RetrievalQA
9
 
10
+ # 1. 引擎初始化
 
11
  llm = HuggingFaceEndpoint(
12
  repo_id="Qwen/Qwen2.5-7B-Instruct",
13
+ huggingfacehub_api_token=os.getenv("HF_TOKEN")
 
14
  )
15
 
16
+ # 2. 知识库加载
 
17
  if not os.path.exists("knowledge.txt"):
18
  with open("knowledge.txt", "w", encoding="utf-8") as f:
19
+ f.write("这里是你的私有大脑知识。")
20
 
 
21
  loader = TextLoader("knowledge.txt", encoding="utf-8")
22
+ docs = CharacterTextSplitter(chunk_size=500, chunk_overlap=50).split_documents(loader.load())
 
 
 
 
23
  embeddings = HuggingFaceEmbeddings(model_name="BAAI/bge-small-zh-v1.5")
24
  vectorstore = FAISS.from_documents(docs, embeddings)
25
 
26
+ # 3. 构建问答链
27
+ qa_chain = RetrievalQA.from_chain_type(llm=llm, retriever=vectorstore.as_retriever())
 
 
 
28
 
29
+ # 4. 界面逻辑
30
+ def chat(msg, history):
31
  try:
32
+ res = qa_chain.invoke({"query": msg})
33
+ return res["result"]
 
34
  except Exception as e:
35
+ return f"运行出错{str(e)}"
 
 
 
 
 
 
 
36
 
37
+ gr.ChatInterface(chat).launch()