Spaces:
Build error
Build error
| from langchain.agents import AgentExecutor, create_openai_functions_agent, create_react_agent | |
| from langchain_community.tools.tavily_search import TavilySearchResults | |
| from langchain_openai import ChatOpenAI | |
| import os | |
| from langchain_core.output_parsers import StrOutputParser, PydanticToolsParser | |
| from langchain_core.prompts import ChatPromptTemplate | |
| from serpapi import GoogleSearch | |
| from langchain.agents import initialize_agent, Tool | |
| import gradio as gr | |
| from css import * | |
| import time | |
| os.environ["OPENAI_API_KEY"] = "sb-f6c206ba7ea4df761e227eba65e65bedad1e24e73b702967" | |
| os.environ['OPENAI_BASE_URL'] = "https://api.openai-sb.com/v1/" | |
| os.environ["SERPAPI_API_KEY"] = "dcc98b22d5f7d413979a175ff7d75b721c5992a3ee1e2363020b2bbdf4f82404" | |
| # os.environ['TAVILY_API_KEY'] = "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" #you | |
| os.environ['TAVILY_API_KEY'] = "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" #zeng | |
| llm = ChatOpenAI(model="gpt-4o", temperature=0.6) | |
| def search(query) -> str: | |
| params = { | |
| "engine": "baidu_news", | |
| "q": query, | |
| "ct": "1", | |
| "api_key": "dcc98b22d5f7d413979a175ff7d75b721c5992a3ee1e2363020b2bbdf4f82404" | |
| } | |
| search = GoogleSearch(params) | |
| results = search.get_dict() | |
| organic_results = results.get("organic_results", []) | |
| if not organic_results: | |
| return "" | |
| final_output = "\n\n".join([ | |
| f"Title: {news.get('title', 'No Title')}\nSnippet: {news.get('snippet', 'No Snippet')}\nDate: {news.get('date', 'No Date')}\nSource:{news.get('source', 'No Source')}" | |
| for news in organic_results | |
| ]) | |
| return final_output | |
| search_tool = Tool( | |
| name="Baidu News Search", # 工具名称 | |
| func=search, # 引用search函数 | |
| description="备用搜索引擎,当主要搜索引擎不返回结果时调用,输入是检索query" # 工具描述 | |
| ) | |
| en_prompt = """ | |
| Remember, you are an AI called Anti-Faker, used for misinformation detection. | |
| You should asses the authenticity of every detail in the input information as thoroughly as possible. | |
| You can use the following tools: {tools} | |
| Use the following format: | |
| Question: the input information (e.g., claim, post, news) you must assess. | |
| Thought: to evaluate first detail in input information, you should always think about what to do. | |
| Action: the action to take, should be one of [{tool_names}]. | |
| Action Input: the input to the action. | |
| Observation: the result of the action. | |
| Thought: to evaluate second detail in input information, you should always think about what to do. | |
| Action: the action to take, should be one of [{tool_names}]. | |
| Action Input: the input to the action. | |
| Observation: the result of the action. | |
| (this Thought/Action/Action Input/Observation can repeat N times) | |
| Thought: I now know the final answer. | |
| Final Answer: The definitive response, including reasons on all details and a conclusion, for assessing the original input information. | |
| Additional Notes: | |
| 1. If the input information contains multiple statements or details, you'd better to assess them one by one. | |
| 2. The results of tools may also be incorrect, pay attention to conflicts and distinguishing them base on your knowledge. | |
| 3. The conclusion in the Final Answer should include the detected labels. | |
| ( For information lacking clear evidence (e.g., rumors), candidate labels are: [Highly Likely Correct, Highly Likely Incorrect, Slightly Incorrect, Slightly Correct, Neutral]; | |
| For information with clear evidence (e.g., verified by specific proof), candidate labels are: [Completely Incorrect, Mostly Incorrect, Mixed Authenticity, Mostly Correct, Completely Correct].) | |
| 4. You should answer in English. | |
| Begin! | |
| Question: {input} | |
| Thought:{agent_scratchpad} | |
| """ | |
| ch_prompt = ''' | |
| 记住, 你是一个人工智能工具, 名叫'鉴伪者', 用于虚假信息检测. | |
| 请尽可能详细地评估 输入信息 中每个细节的真实性. | |
| 您可以使用以下工具: {tools} | |
| 使用以下格式: | |
| Question: 您必须评估的 输入信息(例如, 声明、帖子、新闻). | |
| Thought: 要评估输入信息中的第一个细节, 你应该怎么做. | |
| Action: 只能是此工具列表({tool_names})之一的工具名,不能有其他内容 | |
| Action Input: 行为的输入. | |
| Observation: 行动的执行结果. | |
| Thought: 要评估输入信息中的第二个细节, 你应该怎么做. | |
| Action: 只能是此工具列表({tool_names})之一的工具名,不能有其他内容 | |
| Action Input: 动作的输入. | |
| Observation: 行动的结果. | |
| (这个 'Thought/Action/Action Input/Observation' 逻辑可以最多重复5次) | |
| Thought: 我现在知道最后的答案了. | |
| Final Answer: 对输入信息的评估结果 (包括对每个细节的推理验证或者预测, 整体评估结果以及相关URL). | |
| 补充说明: | |
| 1.如果输入信息包含多个陈述或细节, 你最好逐一评估 ,请注意对时间,地点的判断. | |
| 2.检索工具的结果也可能不正确, 请注意其中的冲突, 并根据你的知识进行区分. | |
| 3.Final Answer的评估结果应包含一个检测标签, 对于缺乏明确证据进行严格验证的信息(如谣言), 需预测标签, 候选标签为: 极有可能正确、极有可能不正确、可能错误、可能正确、中性; | |
| 对于有明确证据进行验证的信息, 候选标签为: 完全不正确、大部分不正确、真假参半、大部分正确、完全正确. | |
| 4.你的Final Answer需要用中文回答. | |
| 开始! | |
| Question: {input} | |
| Thought: {agent_scratchpad} | |
| ''' | |
| origin_prompt = ChatPromptTemplate.from_template(template) | |
| chain = origin_prompt | llm | StrOutputParser() | |
| selected_language = "ch" | |
| def SelectLanguage(option): | |
| global selected_language | |
| if option == "英文": | |
| selected_language = "en" | |
| else: | |
| selected_language = "ch" | |
| language_option = ["中文","英文"] | |
| def react(input): | |
| ori_result = chain.invoke(input) | |
| tools = [TavilySearchResults(max_result=1),search_tool] | |
| if selected_language == "en": | |
| prompt = ChatPromptTemplate.from_template(en_prompt) | |
| elif selected_language == "ch": | |
| prompt = ChatPromptTemplate.from_template(ch_prompt) | |
| agent = create_react_agent(llm, tools, prompt) | |
| agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True, handle_parsing_errors=True) | |
| result = agent_executor.invoke({"input": input}) | |
| return result['output'], ori_result | |
| title = "# 虚假信息检测" | |
| with gr.Blocks(css=css1) as demo: | |
| gr.Markdown(title, elem_id="title") | |
| with gr.Row(): | |
| with gr.Column(scale=2): | |
| chatbot = gr.Chatbot(elem_classes="gradio-output") | |
| language_select = gr.Dropdown(choices= language_option, elem_classes="gradio-input", label= "请选择要使用的语言") | |
| input_box = gr.Textbox(label="输入", elem_classes="gradio-input", placeholder="请输入要判断的新闻", lines=3) | |
| with gr.Column(scale=1): | |
| ans_box = gr.Textbox(label="gpt-4o", lines=5, elem_classes="gradio-output") | |
| react_box = gr.Textbox(label="React", lines=5, elem_classes="gradio-output", visible=False) | |
| clear = gr.Button("清空页面", elem_classes="gradio-button") | |
| submit_btn = gr.Button("提交", elem_classes="gradio-button") | |
| language_select.change(SelectLanguage, language_select) | |
| def user(user_input, history): | |
| if history is None: | |
| history = [] | |
| return "", history + [[user_input, None]] | |
| def bot(history, rag_box): | |
| if history is None or len(history) == 0: | |
| return | |
| bot_rag = str(rag_box) | |
| history[-1][1] = "" | |
| for character in bot_rag: | |
| history[-1][1] += character | |
| time.sleep(0.01) | |
| yield history | |
| submit_btn.click(react, [input_box], [react_box, ans_box] | |
| ).then( | |
| user, [input_box, chatbot], [input_box, chatbot] | |
| ).then( | |
| bot, [chatbot, react_box], chatbot | |
| ) | |
| clear.click(lambda: (None, None, []), inputs=None, outputs=[chatbot, ans_box, react_box]) | |
| if __name__ == "__main__": | |
| demo.launch() | |