Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,59 +1,64 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
-
from
|
|
|
|
| 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.
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
huggingfacehub_api_token=os.getenv("HF_TOKEN"),
|
| 14 |
-
timeout=300,
|
| 15 |
-
task="text-generation" # 明确任务类型
|
| 16 |
)
|
| 17 |
|
| 18 |
-
# 2. 知识库加载
|
| 19 |
-
def
|
| 20 |
if not os.path.exists("knowledge.txt"):
|
| 21 |
with open("knowledge.txt", "w", encoding="utf-8") as f:
|
| 22 |
-
f.write("私有大脑
|
| 23 |
|
| 24 |
loader = TextLoader("knowledge.txt", encoding="utf-8")
|
| 25 |
-
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
# 使用中文
|
| 28 |
embeddings = HuggingFaceEmbeddings(model_name="BAAI/bge-small-zh-v1.5")
|
| 29 |
-
|
| 30 |
-
return vectorstore
|
| 31 |
|
| 32 |
-
|
| 33 |
-
vs = load_kb()
|
| 34 |
-
# 注意:这里我们使用最新的 invoke 接口
|
| 35 |
-
qa_chain = RetrievalQA.from_chain_type(
|
| 36 |
-
llm=llm,
|
| 37 |
-
retriever=vs.as_retriever(search_kwargs={"k": 3})
|
| 38 |
-
)
|
| 39 |
|
| 40 |
-
# 3. 聊天
|
| 41 |
def chat_fn(message, history):
|
| 42 |
try:
|
| 43 |
-
#
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
except Exception as e:
|
| 47 |
-
|
| 48 |
-
if "401" in str(e):
|
| 49 |
-
return "错误:Token 无效或权限不足,请检查 Settings 里的 HF_TOKEN。"
|
| 50 |
-
return f"大脑响应异常:{str(e)}"
|
| 51 |
|
| 52 |
-
# 4.
|
| 53 |
demo = gr.ChatInterface(
|
| 54 |
chat_fn,
|
| 55 |
-
title="全能私有大脑
|
| 56 |
-
description="
|
| 57 |
)
|
| 58 |
|
| 59 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
+
from huggingface_hub import InferenceClient
|
| 4 |
+
from langchain_huggingface import HuggingFaceEmbeddings
|
| 5 |
from langchain_community.vectorstores import FAISS
|
| 6 |
from langchain_community.document_loaders import TextLoader
|
| 7 |
from langchain_text_splitters import CharacterTextSplitter
|
|
|
|
| 8 |
|
| 9 |
+
# 1. 初始化官方推理客户端 (直接绕过 LangChain 不兼容的 Endpoint)
|
| 10 |
+
client = InferenceClient(
|
| 11 |
+
model="Qwen/Qwen2.5-7B-Instruct",
|
| 12 |
+
token=os.getenv("HF_TOKEN")
|
|
|
|
|
|
|
|
|
|
| 13 |
)
|
| 14 |
|
| 15 |
+
# 2. 知识库加载与向量化
|
| 16 |
+
def init_vector_db():
|
| 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 |
+
# 按照语义切分,防止回答断章取义
|
| 23 |
+
text_splitter = CharacterTextSplitter(chunk_size=500, chunk_overlap=50)
|
| 24 |
+
docs = text_splitter.split_documents(loader.load())
|
| 25 |
|
| 26 |
+
# 使用轻量级中文向量模型
|
| 27 |
embeddings = HuggingFaceEmbeddings(model_name="BAAI/bge-small-zh-v1.5")
|
| 28 |
+
return FAISS.from_documents(docs, embeddings)
|
|
|
|
| 29 |
|
| 30 |
+
vector_db = init_vector_db()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
# 3. 核心聊天逻辑
|
| 33 |
def chat_fn(message, history):
|
| 34 |
try:
|
| 35 |
+
# 第一步:在知识库中寻找最相关的片段
|
| 36 |
+
docs = vector_db.similarity_search(message, k=3)
|
| 37 |
+
context = "\n".join([doc.page_content for doc in docs])
|
| 38 |
+
|
| 39 |
+
# 第二步:构建提示词(Prompt)
|
| 40 |
+
prompt = f"你是全能私有大脑。请参考以下已知信息回答用户问题。\n\n已知信息:\n{context}\n\n问题:{message}\n回答:"
|
| 41 |
+
|
| 42 |
+
# 第三步:使用官方最新方法进行推理
|
| 43 |
+
response = ""
|
| 44 |
+
for token in client.chat_completion(
|
| 45 |
+
messages=[{"role": "user", "content": prompt}],
|
| 46 |
+
max_tokens=500,
|
| 47 |
+
stream=True
|
| 48 |
+
):
|
| 49 |
+
token_str = token.choices[0].delta.content
|
| 50 |
+
if token_str:
|
| 51 |
+
response += token_str
|
| 52 |
+
return response
|
| 53 |
+
|
| 54 |
except Exception as e:
|
| 55 |
+
return f"大脑响应异常,请尝试在 Settings 中 Factory Restart。错误详情: {str(e)}"
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
+
# 4. 界面设计
|
| 58 |
demo = gr.ChatInterface(
|
| 59 |
chat_fn,
|
| 60 |
+
title="全能私有大脑 v3.0 (终极稳定版)",
|
| 61 |
+
description="已彻底解决 InferenceClient 兼容性问题。现在可以流畅调取私有知识库了。"
|
| 62 |
)
|
| 63 |
|
| 64 |
if __name__ == "__main__":
|