Spaces:
No application file
No application file
ENUGANDHULA NILESH
commited on
Update search.py
Browse files
search.py
CHANGED
|
@@ -1,23 +1,26 @@
|
|
| 1 |
import os
|
| 2 |
import streamlit as st
|
| 3 |
from langchain_groq import ChatGroq
|
| 4 |
-
from langchain_community.tools import ArxivQueryRun,WikipediaQueryRun,DuckDuckGoSearchRun
|
| 5 |
-
from langchain_community.utilities import WikipediaAPIWrapper,ArxivAPIWrapper
|
| 6 |
from langchain.agents import initialize_agent, AgentType
|
| 7 |
from langchain.callbacks import StreamlitCallbackHandler
|
| 8 |
from dotenv import load_dotenv
|
| 9 |
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
| 11 |
## Arxiv and Wikipedia Tools
|
| 12 |
-
arxiv_wrapper=ArxivAPIWrapper(top_k_results=1, doc_content_chars_max=200)
|
| 13 |
-
arxiv=ArxivQueryRun(api_wrapper=arxiv_wrapper)
|
| 14 |
|
| 15 |
-
api_wrapper=WikipediaAPIWrapper(top_k_results=1,doc_content_chars_max=200)
|
| 16 |
-
wiki=WikipediaQueryRun(api_wrapper=api_wrapper)
|
| 17 |
|
| 18 |
-
search=DuckDuckGoSearchRun(name="Search")
|
| 19 |
|
| 20 |
-
##NILESH
|
| 21 |
st.title("🤖 NileAI - Your AI Search Companion")
|
| 22 |
"""
|
| 23 |
Welcome to NileSearch, your AI-powered chat agent for real-time web search and insights.
|
|
@@ -25,33 +28,29 @@ This app uses `StreamlitCallbackHandler` to display the agent's thoughts and act
|
|
| 25 |
Explore more LangChain 🧠 Streamlit Agent examples at [github.com/langchain-ai/streamlit-agent](https://github.com/langchain-ai/streamlit-agent).
|
| 26 |
"""
|
| 27 |
|
| 28 |
-
|
| 29 |
## Sidebar for Settings
|
| 30 |
# st.sidebar.title("Settings")
|
| 31 |
# api_key = st.sidebar.text_input("Enter your Groq API key:", type="password")
|
| 32 |
|
| 33 |
-
api_key="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"
|
| 34 |
-
|
| 35 |
if "messages" not in st.session_state:
|
| 36 |
st.session_state["messages"] = [
|
| 37 |
{"role": "assistant", "content": "Hi! How can I assist you today?"}
|
| 38 |
]
|
| 39 |
|
| 40 |
-
|
| 41 |
for msg in st.session_state.messages:
|
| 42 |
st.chat_message(msg["role"]).write(msg["content"])
|
| 43 |
-
|
| 44 |
-
if prompt:=st.chat_input(placeholder="What is machine learning?"):
|
| 45 |
-
st.session_state.messages.append({"role":"user", "content":prompt})
|
| 46 |
st.chat_message("user").write(prompt)
|
| 47 |
-
|
| 48 |
llm = ChatGroq(groq_api_key=api_key, model_name="Llama3-8b-8192", streaming=True)
|
| 49 |
tools = [search, arxiv, wiki]
|
| 50 |
-
|
| 51 |
-
search_agent = initialize_agent(tools, llm, agent
|
| 52 |
-
|
| 53 |
with st.chat_message("assistant"):
|
| 54 |
st_cb = StreamlitCallbackHandler(st.container(), expand_new_thoughts=False)
|
| 55 |
response = search_agent.run(st.session_state.messages, callbacks=[st_cb])
|
| 56 |
-
st.session_state.messages.append({'role':'assistant', 'content':response})
|
| 57 |
st.write(response)
|
|
|
|
| 1 |
import os
|
| 2 |
import streamlit as st
|
| 3 |
from langchain_groq import ChatGroq
|
| 4 |
+
from langchain_community.tools import ArxivQueryRun, WikipediaQueryRun, DuckDuckGoSearchRun
|
| 5 |
+
from langchain_community.utilities import WikipediaAPIWrapper, ArxivAPIWrapper
|
| 6 |
from langchain.agents import initialize_agent, AgentType
|
| 7 |
from langchain.callbacks import StreamlitCallbackHandler
|
| 8 |
from dotenv import load_dotenv
|
| 9 |
|
| 10 |
+
# Load environment variables from .env file
|
| 11 |
+
load_dotenv()
|
| 12 |
+
api_key = os.getenv("GROQ_API_KEY") # Get the API key from the environment variables
|
| 13 |
+
|
| 14 |
## Arxiv and Wikipedia Tools
|
| 15 |
+
arxiv_wrapper = ArxivAPIWrapper(top_k_results=1, doc_content_chars_max=200)
|
| 16 |
+
arxiv = ArxivQueryRun(api_wrapper=arxiv_wrapper)
|
| 17 |
|
| 18 |
+
api_wrapper = WikipediaAPIWrapper(top_k_results=1, doc_content_chars_max=200)
|
| 19 |
+
wiki = WikipediaQueryRun(api_wrapper=api_wrapper)
|
| 20 |
|
| 21 |
+
search = DuckDuckGoSearchRun(name="Search")
|
| 22 |
|
| 23 |
+
## NILESH
|
| 24 |
st.title("🤖 NileAI - Your AI Search Companion")
|
| 25 |
"""
|
| 26 |
Welcome to NileSearch, your AI-powered chat agent for real-time web search and insights.
|
|
|
|
| 28 |
Explore more LangChain 🧠 Streamlit Agent examples at [github.com/langchain-ai/streamlit-agent](https://github.com/langchain-ai/streamlit-agent).
|
| 29 |
"""
|
| 30 |
|
|
|
|
| 31 |
## Sidebar for Settings
|
| 32 |
# st.sidebar.title("Settings")
|
| 33 |
# api_key = st.sidebar.text_input("Enter your Groq API key:", type="password")
|
| 34 |
|
|
|
|
|
|
|
| 35 |
if "messages" not in st.session_state:
|
| 36 |
st.session_state["messages"] = [
|
| 37 |
{"role": "assistant", "content": "Hi! How can I assist you today?"}
|
| 38 |
]
|
| 39 |
|
|
|
|
| 40 |
for msg in st.session_state.messages:
|
| 41 |
st.chat_message(msg["role"]).write(msg["content"])
|
| 42 |
+
|
| 43 |
+
if prompt := st.chat_input(placeholder="What is machine learning?"):
|
| 44 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 45 |
st.chat_message("user").write(prompt)
|
| 46 |
+
|
| 47 |
llm = ChatGroq(groq_api_key=api_key, model_name="Llama3-8b-8192", streaming=True)
|
| 48 |
tools = [search, arxiv, wiki]
|
| 49 |
+
|
| 50 |
+
search_agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, handle_parsing_errors=True)
|
| 51 |
+
|
| 52 |
with st.chat_message("assistant"):
|
| 53 |
st_cb = StreamlitCallbackHandler(st.container(), expand_new_thoughts=False)
|
| 54 |
response = search_agent.run(st.session_state.messages, callbacks=[st_cb])
|
| 55 |
+
st.session_state.messages.append({'role': 'assistant', 'content': response})
|
| 56 |
st.write(response)
|