-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2_Chatbot_Agent.py
130 lines (112 loc) · 4.43 KB
/
2_Chatbot_Agent.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import utils
import streamlit as st
from langchain.agents import AgentType
from langchain.chat_models import ChatOpenAI
from langchain.tools import DuckDuckGoSearchRun
from langchain.agents import initialize_agent, Tool
from langchain.callbacks import StreamlitCallbackHandler
from langchain.tools import WikipediaQueryRun
from langchain.utilities import WikipediaAPIWrapper
from langchain.tools import PythonREPLToo
st.set_page_config(page_title="ChatWeb", page_icon="🌐")
st.header('Chatbot with Web Browser Access')
st.write('Equipped with internet agent, enables users to ask questions about recent events')
class ChatbotTools:
def __init__(self):
utils.configure_openai_api_key()
self.openai_model = "gpt-3.5-turbo"
# def setup_agent(self):
# # Define tool
# ddg_search = DuckDuckGoSearchRun()
# tools = [
# Tool(
# name="DuckDuckGoSearch",
# func=ddg_search.run,
# description="Useful for when you need to answer questions about current events. You should ask targeted questions",
# )
# ]
# # 1 a)
# def setup_agent(self):
# # Define tool
# ddg_search = DuckDuckGoSearchRun()
# wiki_agent = WikipediaQueryRun(api_wrapper =
# WikipediaAPIWrapper())
# tools = [
# Tool(
# name="DuckDuckGoSearch",
# func=ddg_search.run,
# description="Useful for when you need to answer questions about current events. You should ask targeted questions",),
# Tool(
# name="Wikipedia",
# func=wiki_agent.run,
# description="Useful for when you need to query about a specific topic, person, or event. You should ask targeted questions",)
# ]
# # Setup LLM and Agent
# llm = ChatOpenAI(model_name=self.openai_model, streaming=True)
# agent = initialize_agent(
# tools=tools,
# llm=llm,
# agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
# handle_parsing_errors=True,
# verbose=True
# )
# return agent
# # Setup LLM and Agent
# llm = ChatOpenAI(model_name=self.openai_model, streaming=True)
# agent = initialize_agent(
# tools=tools,
# llm=llm,
# agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
# handle_parsing_errors=True,
# verbose=True
# )
# return agent
# 1 b) Second Shiba
def setup_agent(self):
# Define tool
ddg_search = DuckDuckGoSearchRun()
wiki_agent = WikipediaQueryRun(api_wrapper =
WikipediaAPIWrapper())
python_repl = PythonREPLTool()
tools = [
Tool(
name="DuckDuckGoSearch",
func=ddg_search.run,
description="Useful for when you need to answer questions about current events. You should ask targeted questions",),
Tool(
name="Wikipedia",
func=wiki_agent.run,
description="Useful for when you need to query about a specific topic, person, or event. You should ask targeted questions",),
Tool(
name="Python REPL",
func=python_repl.run,
description="A Python shell. Use this to execute python commands. Input should be a valid python command. If you expect output it should be printed out.",)
]
# Setup LLM and Agent
llm = ChatOpenAI(model_name=self.openai_model, streaming=True)
agent = initialize_agent(
tools=tools,
llm=llm,
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
handle_parsing_errors=True,
verbose=True
)
return agent
@utils.enable_chat_history
def main(self):
agent = self.setup_agent()
user_query = st.chat_input(placeholder="Ask me anything!")
if user_query:
utils.display_msg(user_query, 'user')
with st.chat_message("assistant"):
try:
st_cb = StreamlitCallbackHandler(st.container())
response = agent.run(user_query, callbacks=[st_cb])
st.session_state.messages.append(
{"role": "assistant", "content": response})
st.write(response)
except Exception as e:
print(e)
if __name__ == "__main__":
obj = ChatbotTools()
obj.main()