Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -9,6 +9,59 @@ from langchain.memory import ConversationBufferMemory as MEM,RedisChatMessageHis
|
|
9 |
from langchain.schema import SystemMessage as SM,HumanMessage as HM, AIMessage as AM
|
10 |
from langchain import hub
|
11 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
from langchain.retrievers import WikipediaRetriever as Wiki
|
13 |
import gradio as gr
|
14 |
chatbot = gr.Chatbot(
|
@@ -41,7 +94,7 @@ def chat(message,
|
|
41 |
|
42 |
messages.append(HM(content=message))
|
43 |
memory=MEM(memory_key="history")
|
44 |
-
agent=Ex(agent=Agent(llm=llm,tools=tools),tools=tools,memory=memory,verbose=True,handle_parsing_errors=True)
|
45 |
yield agent.invoke(messages)
|
46 |
ai1=gr.ChatInterface(
|
47 |
chat,
|
|
|
9 |
from langchain.schema import SystemMessage as SM,HumanMessage as HM, AIMessage as AM
|
10 |
from langchain import hub
|
11 |
import os
|
12 |
+
from langchain_core.prompts.chat import ChatPromptTemplate, MessagesPlaceholder
|
13 |
+
system = '''Respond to the human as helpfully and accurately as possible. You have access to the following tools:
|
14 |
+
|
15 |
+
{tools}
|
16 |
+
|
17 |
+
Use a json blob to specify a tool by providing an action key (tool name) and an action_input key (tool input).
|
18 |
+
|
19 |
+
Valid "action" values: "Final Answer" or {tool_names}
|
20 |
+
|
21 |
+
Provide only ONE action per $JSON_BLOB, as shown:
|
22 |
+
|
23 |
+
```
|
24 |
+
{{
|
25 |
+
"action": $TOOL_NAME,
|
26 |
+
"action_input": $INPUT
|
27 |
+
}}
|
28 |
+
```
|
29 |
+
|
30 |
+
Follow this format:
|
31 |
+
|
32 |
+
Question: input question to answer
|
33 |
+
Thought: consider previous and subsequent steps
|
34 |
+
Action:
|
35 |
+
```
|
36 |
+
$JSON_BLOB
|
37 |
+
```
|
38 |
+
Observation: action result
|
39 |
+
... (repeat Thought/Action/Observation N times)
|
40 |
+
Thought: I know what to respond
|
41 |
+
Action:
|
42 |
+
```
|
43 |
+
{{
|
44 |
+
"action": "Final Answer",
|
45 |
+
"action_input": "Final response to human"
|
46 |
+
}}
|
47 |
+
|
48 |
+
Begin! Reminder to ALWAYS respond with a valid json blob of a single action. Use tools if necessary. Respond directly if appropriate. Format is Action:```$JSON_BLOB```then Observation'''
|
49 |
+
|
50 |
+
human = '''
|
51 |
+
|
52 |
+
{input}
|
53 |
+
|
54 |
+
{agent_scratchpad}
|
55 |
+
|
56 |
+
(reminder to respond in a JSON blob no matter what)'''
|
57 |
+
|
58 |
+
prompt = ChatPromptTemplate.from_messages(
|
59 |
+
[
|
60 |
+
("system", system),
|
61 |
+
MessagesPlaceholder("chat_history", optional=True),
|
62 |
+
("human", human),
|
63 |
+
]
|
64 |
+
)
|
65 |
from langchain.retrievers import WikipediaRetriever as Wiki
|
66 |
import gradio as gr
|
67 |
chatbot = gr.Chatbot(
|
|
|
94 |
|
95 |
messages.append(HM(content=message))
|
96 |
memory=MEM(memory_key="history")
|
97 |
+
agent=Ex(agent=Agent(llm=llm,tools=tools,prompt=prompt),tools=tools,memory=memory,verbose=True,handle_parsing_errors=True)
|
98 |
yield agent.invoke(messages)
|
99 |
ai1=gr.ChatInterface(
|
100 |
chat,
|