aasherkamal216 commited on
Commit
da2a98d
·
unverified ·
1 Parent(s): 42b1bb6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -10
app.py CHANGED
@@ -1,9 +1,11 @@
1
  import os, dotenv
2
  import streamlit as st
3
  from langchain_groq import ChatGroq
 
4
  from langchain_community.utilities import ArxivAPIWrapper, WikipediaAPIWrapper
5
  from langchain_community.tools import ArxivQueryRun, WikipediaQueryRun, DuckDuckGoSearchRun
6
- from langchain.agents import initialize_agent, AgentType
 
7
  from langchain_community.callbacks.streamlit import StreamlitCallbackHandler
8
  dotenv.load_dotenv()
9
 
@@ -16,6 +18,43 @@ arxiv_tool = ArxivQueryRun(api_wrapper=arxiv_wrapper)
16
  # DuckDuckGo Search Tool
17
  search = DuckDuckGoSearchRun(name="Internet Search")
18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  # Streamlit Code
20
  st.set_page_config(page_icon=":mag:", page_title="Tools & Agent")
21
  st.title(":green[Langchain] Search Agent")
@@ -43,20 +82,18 @@ for message in st.session_state.messages:
43
  st.chat_message("assistant", avatar="robot.png").write(message['content'])
44
 
45
  if api_key:
46
- if prompt := st.chat_input("What is Generative AI?"):
47
- st.session_state.messages.append({"role": "user", "content": prompt})
48
- st.chat_message("user", avatar="boss.png").write(prompt)
49
 
50
- llm = ChatGroq(model="llama-3.1-70b-versatile", api_key=api_key, streaming=True)
51
  tools = [wiki_tool, arxiv_tool, search]
52
 
53
- search_agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
54
- agent_executor_kwargs={"handle_parsing_errors": True})
55
  try:
56
  with st.chat_message("assistant", avatar="robot.png"):
57
- st_callback = StreamlitCallbackHandler(st.container(), expand_new_thoughts=True)
58
- response = search_agent.run(st.session_state.messages, callbacks=[st_callback])
59
- st.write(response)
60
  st.session_state.messages.append({"role": "assistant", "content": response})
61
  except Exception as e:
62
  st.error(f"An error occurred: {e}")
 
1
  import os, dotenv
2
  import streamlit as st
3
  from langchain_groq import ChatGroq
4
+ from langchain_core.prompts import ChatPromptTemplate
5
  from langchain_community.utilities import ArxivAPIWrapper, WikipediaAPIWrapper
6
  from langchain_community.tools import ArxivQueryRun, WikipediaQueryRun, DuckDuckGoSearchRun
7
+ from langchain.agents import create_react_agent
8
+ from langchain.agents import AgentExecutor
9
  from langchain_community.callbacks.streamlit import StreamlitCallbackHandler
10
  dotenv.load_dotenv()
11
 
 
18
  # DuckDuckGo Search Tool
19
  search = DuckDuckGoSearchRun(name="Internet Search")
20
 
21
+
22
+ prompt = ChatPromptTemplate.from_template("""
23
+ Answer the following user questions as best you can. Use the available tools to find the answer.
24
+ You have access to the following tools:\n
25
+ {tools}\n\n
26
+ To use a tool, please use the following format:
27
+ ```
28
+ Thought: Do I need to use a tool? Yes
29
+ Action: the action to take, should be one of [{tool_names}]
30
+ Action Input: the input to the action
31
+ Observation: the result of the action
32
+ ```
33
+ If one tool doesn't give the relavant information, use another tool.
34
+ When you have a response to say to the Human, or if you do not need to use a tool, you MUST use the format:
35
+
36
+ ```
37
+ Thought: Do I need to use a tool? No
38
+ Final Answer: [your response here]
39
+ ```
40
+ Begin!
41
+
42
+ Previous conversation history:
43
+ {chat_history}
44
+ New input: {input}
45
+
46
+ {agent_scratchpad}
47
+ """)
48
+
49
+ def create_groq_agent(llm, api_key, tools, question, chat_history):
50
+ agent = create_react_agent(llm, tools, prompt)
51
+
52
+ agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True, handle_parsing_errors=True, max_iterations=7)
53
+ st_callback = StreamlitCallbackHandler(st.container(), expand_new_thoughts=True)
54
+
55
+ response = agent_executor.invoke({"input":question, "chat_history":chat_history}, {"callbacks": [st_callback]})
56
+ return response['output']
57
+
58
  # Streamlit Code
59
  st.set_page_config(page_icon=":mag:", page_title="Tools & Agent")
60
  st.title(":green[Langchain] Search Agent")
 
82
  st.chat_message("assistant", avatar="robot.png").write(message['content'])
83
 
84
  if api_key:
85
+ if question := st.chat_input("What is Generative AI?"):
86
+ st.session_state.messages.append({"role": "user", "content": question})
87
+ st.chat_message("user", avatar="boss.png").write(question)
88
 
89
+ llm = ChatGroq(model="llama-3.1-70b-versatile", api_key=api_key)
90
  tools = [wiki_tool, arxiv_tool, search]
91
 
 
 
92
  try:
93
  with st.chat_message("assistant", avatar="robot.png"):
94
+ response = create_groq_agent(llm, api_key, tools, question, st.session_state.messages)
95
+ st.markdown(response)
96
+
97
  st.session_state.messages.append({"role": "assistant", "content": response})
98
  except Exception as e:
99
  st.error(f"An error occurred: {e}")