datasense/leanvalue_qa
0
1import gradio as gr2import random3import time4 5from typing import List6 7from langchain.embeddings.openai import OpenAIEmbeddings8from langchain.vectorstores import FAISS9from langchain.chains import RetrievalQA10from langchain.chat_models import ChatOpenAI11 12enable_chat = False # 初始化为False13 14def toggle_enable_chat():15 global enable_chat16 enable_chat = not enable_chat17 return f"Enable Chat set to {enable_chat}"18 19 20def initialize_aliyun_qa_bot(vector_store_dir: str="aliyun_qa"):21 print(vector_store_dir)22 db = FAISS.load_local(vector_store_dir, OpenAIEmbeddings())23 print(db)24 llm = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0)25 26 global ALIYUN_BOT 27 ALIYUN_BOT = RetrievalQA.from_chain_type(llm,28 retriever=db.as_retriever(search_type="similarity_score_threshold",29 search_kwargs={"score_threshold": 0.8}))30 # 返回向量数据库的检索结果31 ALIYUN_BOT.return_source_documents = True32 33 return ALIYUN_BOT34 35def aliyun_chat(message, history):36 print(f"[message]{message}")37 print(f"[history]{history}")38 # TODO: 从命令行参数中获取39 global enable_chat40 41 ans = ALIYUN_BOT({"query": message})42 # 如果检索出结果,或者开了大模型聊天模式43 # 返回 RetrievalQA combine_documents_chain 整合的结果44 if ans["source_documents"] or enable_chat:45 print(f"[result]{ans['result']}")46 print(f"[source_documents]{ans['source_documents']}")47 return ans["result"]48 # 否则输出套路话术49 else:50 return "基于现有文档数据,无法回答你的问题,请您联系对应的阿里云企业咨询顾问。"51 52 53def launch_gradio():54 # 创建 ChatInterface 和其他组件55 chat_interface = gr.ChatInterface(56 fn=aliyun_chat,57 title="企业数字化转型咨询顾问(aliyun)问答机器人",58 examples=["阿里云有没有成熟的方法或者模板能够供我们去把业务价值和底座组件对应起来?","我们要建设中交二航局的数字化底座,阿里云有什么建议","公共云上,EMR和MaxCompute的使用量多少?","阿里公共云上能用maxcompute吗?"],59 description='<div style="font-family: \'KaiTi\', \'楷体\', serif; font-size: 18px; color: red;text-align: center;">问答数据基于<a href="https://kdocs.cn/l/cszrR7raahdS" style="font-family: \'KaiTi\', \'楷体\', serif; font-size: 18px; color: blue;">阿里云咨询QA文档</a> 请以官方文档为准。</div>'+'<div style="text-align: center;">\60 <img src="https://i.postimg.cc/y8T53gjr/logo.png" alt="精益价值logo" style="height: 80px; margin:0px auto;"></div>',61 # cache_examples=True,62 # retry_btn=None,63 # undo_btn=None,64 chatbot=gr.Chatbot(height=450),65 )66 67 # 创建一个简单的按钮界面来触发 toggle_enable_chat 函数68 toggle_chat_interface = gr.Interface(69 fn=toggle_enable_chat, 70 inputs=gr.Button("Toggle Chat"), 71 outputs="text"72 )73 74 # 使用 TabbedInterface 来组合聊天界面和设置界面75 demo = gr.TabbedInterface([chat_interface, toggle_chat_interface], 76 ["Chat", "Settings"])77 78 demo.launch(auth=("admin", "leanvalue"))79 demo.deploy()80 81if __name__ == "__main__":82 # 初始化机器人83 initialize_aliyun_qa_bot("aliyun_qa")84 # 启动 Gradio 服务85 launch_gradio()86 