NazaninMn commited on
Commit
e73458f
Β·
unverified Β·
1 Parent(s): 388a9cf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -30
app.py CHANGED
@@ -2,9 +2,9 @@ import streamlit as st
2
  from langchain_groq import ChatGroq
3
  from langchain_community.utilities import ArxivAPIWrapper, WikipediaAPIWrapper
4
  from langchain_community.tools import ArxivQueryRun, WikipediaQueryRun, DuckDuckGoSearchRun
5
- from langchain.agents import AgentExecutor, create_tool_calling_agent
6
- from langchain_core.prompts import ChatPromptTemplate
7
  from langchain.callbacks import StreamlitCallbackHandler
 
 
8
 
9
  ## Arxiv and Wikipedia Tools
10
  arxiv_wrapper = ArxivAPIWrapper(top_k_results=1, doc_content_chars_max=200)
@@ -42,6 +42,21 @@ if "messages" not in st.session_state:
42
  for msg in st.session_state.messages:
43
  st.chat_message(msg["role"]).write(msg['content'])
44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  # Chat input
46
  if prompt := st.chat_input(placeholder="What is machine learning?"):
47
  # Check if API key is provided
@@ -53,40 +68,71 @@ if prompt := st.chat_input(placeholder="What is machine learning?"):
53
  st.session_state.messages.append({"role": "user", "content": prompt})
54
  st.chat_message("user").write(prompt)
55
 
56
- # Initialize LLM and tools
57
  llm = ChatGroq(groq_api_key=api_key, model_name="Llama3-8b-8192", streaming=True)
58
- tools = [search, arxiv, wiki]
59
-
60
- # Create prompt template
61
- prompt_template = ChatPromptTemplate.from_messages([
62
- ("system", "You are a helpful assistant. Use the available tools to answer questions."),
63
- ("human", "{input}"),
64
- ("placeholder", "{agent_scratchpad}"),
65
- ])
66
-
67
- # Create agent
68
- agent = create_tool_calling_agent(llm, tools, prompt_template)
69
- agent_executor = AgentExecutor(
70
- agent=agent,
71
- tools=tools,
72
- verbose=True,
73
- handle_parsing_errors=True,
74
- max_iterations=5
75
- )
76
 
77
  # Generate response
78
  with st.chat_message("assistant"):
79
- st_cb = StreamlitCallbackHandler(st.container(), expand_new_thoughts=False)
 
80
  try:
81
- response = agent_executor.invoke(
82
- {"input": prompt},
83
- {"callbacks": [st_cb]}
84
- )
85
- answer = response['output']
86
- st.session_state.messages.append({'role': 'assistant', "content": answer})
87
- st.write(answer)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88
  except Exception as e:
89
- st.error(f"An error occurred: {str(e)}")
 
90
  st.session_state.messages.append({
91
  'role': 'assistant',
92
  "content": f"Sorry, I encountered an error: {str(e)}"
 
2
  from langchain_groq import ChatGroq
3
  from langchain_community.utilities import ArxivAPIWrapper, WikipediaAPIWrapper
4
  from langchain_community.tools import ArxivQueryRun, WikipediaQueryRun, DuckDuckGoSearchRun
 
 
5
  from langchain.callbacks import StreamlitCallbackHandler
6
+ from langchain_core.messages import HumanMessage, AIMessage
7
+ import traceback
8
 
9
  ## Arxiv and Wikipedia Tools
10
  arxiv_wrapper = ArxivAPIWrapper(top_k_results=1, doc_content_chars_max=200)
 
42
  for msg in st.session_state.messages:
43
  st.chat_message(msg["role"]).write(msg['content'])
44
 
45
+ # Simple tool execution function
46
+ def use_tool(tool_name, query):
47
+ """Execute a tool based on its name"""
48
+ try:
49
+ if "search" in tool_name.lower() or "duckduckgo" in tool_name.lower():
50
+ return search.run(query)
51
+ elif "arxiv" in tool_name.lower():
52
+ return arxiv.run(query)
53
+ elif "wiki" in tool_name.lower():
54
+ return wiki.run(query)
55
+ else:
56
+ return "Tool not found"
57
+ except Exception as e:
58
+ return f"Error using tool: {str(e)}"
59
+
60
  # Chat input
61
  if prompt := st.chat_input(placeholder="What is machine learning?"):
62
  # Check if API key is provided
 
68
  st.session_state.messages.append({"role": "user", "content": prompt})
69
  st.chat_message("user").write(prompt)
70
 
71
+ # Initialize LLM
72
  llm = ChatGroq(groq_api_key=api_key, model_name="Llama3-8b-8192", streaming=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
 
74
  # Generate response
75
  with st.chat_message("assistant"):
76
+ st_container = st.container()
77
+
78
  try:
79
+ # Create a system message explaining available tools
80
+ system_prompt = """You are a helpful assistant with access to the following tools:
81
+ 1. Search (DuckDuckGo) - for web searches
82
+ 2. ArXiv - for searching academic papers
83
+ 3. Wikipedia - for encyclopedia information
84
+
85
+ When you need information, think about which tool to use and tell me. I'll execute it for you.
86
+ Answer questions directly when you can, or suggest which tool to use for more information."""
87
+
88
+ # Simple approach: Ask LLM if it needs tools
89
+ messages = [
90
+ {"role": "system", "content": system_prompt},
91
+ {"role": "user", "content": prompt}
92
+ ]
93
+
94
+ with st_container:
95
+ response = llm.invoke(messages)
96
+ answer = response.content
97
+
98
+ # Check if the response suggests using a tool
99
+ if any(keyword in answer.lower() for keyword in ["search", "arxiv", "wikipedia", "look up", "find"]):
100
+ st.info("πŸ” Searching for information...")
101
+
102
+ # Try to use relevant tools
103
+ search_results = []
104
+ if "arxiv" in answer.lower() or "paper" in answer.lower() or "research" in answer.lower():
105
+ st.write("πŸ“š Searching ArXiv...")
106
+ result = use_tool("arxiv", prompt)
107
+ search_results.append(("ArXiv", result))
108
+
109
+ if "wikipedia" in answer.lower() or "wiki" in answer.lower():
110
+ st.write("πŸ“– Searching Wikipedia...")
111
+ result = use_tool("wiki", prompt)
112
+ search_results.append(("Wikipedia", result))
113
+
114
+ # Default to web search
115
+ if not search_results or "search" in answer.lower():
116
+ st.write("🌐 Searching the web...")
117
+ result = use_tool("search", prompt)
118
+ search_results.append(("Web Search", result))
119
+
120
+ # Synthesize answer with search results
121
+ if search_results:
122
+ context = "\n\n".join([f"{name}: {result[:500]}" for name, result in search_results])
123
+ final_messages = [
124
+ {"role": "system", "content": "You are a helpful assistant. Use the following search results to answer the user's question."},
125
+ {"role": "user", "content": f"Question: {prompt}\n\nSearch Results:\n{context}\n\nProvide a comprehensive answer based on these results."}
126
+ ]
127
+ final_response = llm.invoke(final_messages)
128
+ answer = final_response.content
129
+
130
+ st.session_state.messages.append({'role': 'assistant', "content": answer})
131
+ st.write(answer)
132
+
133
  except Exception as e:
134
+ error_msg = f"An error occurred: {str(e)}\n\n{traceback.format_exc()}"
135
+ st.error(error_msg)
136
  st.session_state.messages.append({
137
  'role': 'assistant',
138
  "content": f"Sorry, I encountered an error: {str(e)}"