Spaces:
Paused
Paused
netman19731
commited on
Commit
•
46127ea
1
Parent(s):
52c4598
Update app.py
Browse files
app.py
CHANGED
@@ -1,71 +1,118 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
import gradio as gr
|
4 |
-
import requests
|
5 |
-
import dashscope
|
6 |
-
from http import HTTPStatus
|
7 |
-
import json
|
8 |
-
# from langchain.llms import Tongyi
|
9 |
-
from langchain_community.llms import Tongyi,ChatGLM ,OpenAI
|
10 |
-
from langchain import hub
|
11 |
from langchain_community.tools.tavily_search import TavilySearchResults
|
12 |
-
from langchain.tools import
|
13 |
-
|
14 |
-
from
|
15 |
-
|
16 |
-
from
|
17 |
-
from
|
18 |
-
|
19 |
-
from langchain_community.vectorstores import Pinecone as Pinecone_VectorStore
|
20 |
-
from langchain.tools.retriever import create_retriever_tool
|
21 |
-
from langchain.agents import AgentExecutor,create_react_agent
|
22 |
-
from getpass import getpass
|
23 |
import os
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
#
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
)
|
61 |
|
62 |
-
tools
|
63 |
-
|
64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
|
66 |
async def predict(question):
|
67 |
-
que={"
|
68 |
-
res=
|
69 |
if res:
|
70 |
return(res["output"])
|
71 |
else:print("不好意思,出了一个小问题,请联系我的微信:13603634456")
|
@@ -74,5 +121,7 @@ async def predict(question):
|
|
74 |
gr.Interface(
|
75 |
predict,inputs="textbox",
|
76 |
outputs="textbox",
|
77 |
-
title="定制版AI专家BOT",
|
78 |
-
description="这是一个定制版的AI专家BOT,你可以通过输入问题,让AI为你回答。\n目前提供三个示例工具:\n1
|
|
|
|
|
|
1 |
+
|
2 |
+
from langchain_openai.chat_models import ChatOpenAI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
from langchain_community.tools.tavily_search import TavilySearchResults
|
4 |
+
from langchain.tools.render import format_tool_to_openai_function
|
5 |
+
from langgraph.prebuilt import ToolExecutor,ToolInvocation
|
6 |
+
from typing import TypedDict, Annotated, Sequence
|
7 |
+
import operator
|
8 |
+
from langchain_core.messages import BaseMessage,FunctionMessage,HumanMessage
|
9 |
+
from langchain.tools import ShellTool
|
10 |
+
import json
|
|
|
|
|
|
|
|
|
11 |
import os
|
12 |
+
import gradio as gr
|
13 |
+
os.environ["LANGCHAIN_TRACING_V2"] ="True"
|
14 |
+
os.environ["LANGCHAIN_API_KEY"]="ls__54e16f70b2b0455aad0f2cbf47777d30"
|
15 |
+
os.environ["OPENAI_API_KEY"]="20a79668d6113e99b35fcd541c65bfeaec497b8262c111bd328ef5f1ad8c6335"
|
16 |
+
# os.environ["OPENAI_API_KEY"]="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"
|
17 |
+
os.environ["LANGCHAIN_ENDPOINT"]="https://api.smith.langchain.com"
|
18 |
+
os.environ["LANGCHAIN_PROJECT"]="default"
|
19 |
+
|
20 |
+
class AgentState(TypedDict):
|
21 |
+
messages: Annotated[Sequence[BaseMessage], operator.add]
|
22 |
+
|
23 |
+
model = ChatOpenAI(model="gpt-3.5-turbo-1106",api_key="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")
|
24 |
+
shell_tool = ShellTool()
|
25 |
+
tools = [TavilySearchResults(max_results=1),shell_tool]
|
26 |
+
|
27 |
+
|
28 |
+
functions = [format_tool_to_openai_function(t) for t in tools]
|
29 |
+
model = model.bind_functions(functions)
|
30 |
+
|
31 |
+
tool_executor = ToolExecutor(tools)
|
32 |
+
# Define the function that determines whether to continue or not
|
33 |
+
def should_continue(state):
|
34 |
+
messages = state['messages']
|
35 |
+
last_message = messages[-1]
|
36 |
+
# If there is no function call, then we finish
|
37 |
+
if "function_call" not in last_message.additional_kwargs:
|
38 |
+
return "end"
|
39 |
+
# Otherwise if there is, we continue
|
40 |
+
else:
|
41 |
+
return "continue"
|
42 |
+
|
43 |
+
# Define the function that calls the model
|
44 |
+
def call_model(state):
|
45 |
+
messages = state['messages']
|
46 |
+
response = model.invoke(messages)
|
47 |
+
# We return a list, because this will get added to the existing list
|
48 |
+
return {"messages": [response]}
|
49 |
+
|
50 |
+
# Define the function to execute tools
|
51 |
+
def call_tool(state):
|
52 |
+
messages = state['messages']
|
53 |
+
# Based on the continue condition
|
54 |
+
# we know the last message involves a function call
|
55 |
+
last_message = messages[-1]
|
56 |
+
# We construct an ToolInvocation from the function_call
|
57 |
+
action = ToolInvocation(
|
58 |
+
tool=last_message.additional_kwargs["function_call"]["name"],
|
59 |
+
tool_input=json.loads(last_message.additional_kwargs["function_call"]["arguments"]),
|
60 |
+
)
|
61 |
+
# We call the tool_executor and get back a response
|
62 |
+
response = tool_executor.invoke(action)
|
63 |
+
# We use the response to create a FunctionMessage
|
64 |
+
function_message = FunctionMessage(content=str(response), name=action.tool)
|
65 |
+
# We return a list, because this will get added to the existing list
|
66 |
+
return {"messages": [function_message]}
|
67 |
+
|
68 |
+
from langgraph.graph import StateGraph, END
|
69 |
+
# Define a new graph
|
70 |
+
workflow = StateGraph(AgentState)
|
71 |
+
|
72 |
+
# Define the two nodes we will cycle between
|
73 |
+
workflow.add_node("agent", call_model)
|
74 |
+
workflow.add_node("action", call_tool)
|
75 |
+
|
76 |
+
# Set the entrypoint as `agent`
|
77 |
+
# This means that this node is the first one called
|
78 |
+
workflow.set_entry_point("agent")
|
79 |
+
|
80 |
+
# We now add a conditional edge
|
81 |
+
workflow.add_conditional_edges(
|
82 |
+
# First, we define the start node. We use `agent`.
|
83 |
+
# This means these are the edges taken after the `agent` node is called.
|
84 |
+
"agent",
|
85 |
+
# Next, we pass in the function that will determine which node is called next.
|
86 |
+
should_continue,
|
87 |
+
# Finally we pass in a mapping.
|
88 |
+
# The keys are strings, and the values are other nodes.
|
89 |
+
# END is a special node marking that the graph should finish.
|
90 |
+
# What will happen is we will call `should_continue`, and then the output of that
|
91 |
+
# will be matched against the keys in this mapping.
|
92 |
+
# Based on which one it matches, that node will then be called.
|
93 |
+
{
|
94 |
+
# If `tools`, then we call the tool node.
|
95 |
+
"continue": "action",
|
96 |
+
# Otherwise we finish.
|
97 |
+
"end": END
|
98 |
+
}
|
99 |
)
|
100 |
|
101 |
+
# We now add a normal edge from `tools` to `agent`.
|
102 |
+
# This means that after `tools` is called, `agent` node is called next.
|
103 |
+
workflow.add_edge('action', 'agent')
|
104 |
+
|
105 |
+
# Finally, we compile it!
|
106 |
+
# This compiles it into a LangChain Runnable,
|
107 |
+
# meaning you can use it as you would any other runnable
|
108 |
+
app = workflow.compile()
|
109 |
+
|
110 |
+
# inputs = {"messages": [HumanMessage(content="查询你的cast命令版本")]}
|
111 |
+
# app.invoke(inputs)
|
112 |
|
113 |
async def predict(question):
|
114 |
+
que={"messages": [HumanMessage(content=question)]}
|
115 |
+
res=app.invoke(que)
|
116 |
if res:
|
117 |
return(res["output"])
|
118 |
else:print("不好意思,出了一个小问题,请联系我的微信:13603634456")
|
|
|
121 |
gr.Interface(
|
122 |
predict,inputs="textbox",
|
123 |
outputs="textbox",
|
124 |
+
title="定制版AI专家BOT-0.1版",
|
125 |
+
description="这是一个定制版的AI专家BOT,你可以通过输入问题,让AI为你回答。\n目前提供三个示例工具:\n1.bash命令行执行工具,可以将人类语言转化为bash命令,然后执行。\n2.搜索引擎").launch()
|
126 |
+
|
127 |
+
|