Creating AI agents that can interact with the real world is a great area of research and development. One useful application is building agents capable of searching the web to gather information and complete tasks. This blog post will guide you through the process of creating such an agent using LangChain, a framework for developing LLM-powered applications, and Llama 3.3, a state-of-the-art large language model.
This article was published as a part of the Data Science Blogathon.
Llama 3.3 is a 70-billion parameter, instruction-tuned large language model developed by Meta. It is optimized to perform better on text-based tasks, including following instructions, coding, and multilingual processing. In comparison, it outperforms its forerunners, including Llama 3.1 70B and Llama 3.2 90B. It can even compete with larger models, such as Llama 3.1 405B in some areas, while saving costs.
LangChain is an open-source framework, useful for building applications powered by large language models (LLMs). The suite of tools and abstractions simplifies the integration of LLMs in various applications, enabling developers to create sophisticated AI-driven solutions with ease.
Key Features of LangChain
Our web-searching agent will consist of the following key components:
This workflow outlines the process of integrating multiple tools and models to create an interactive system using Arxiv, Wikipedia, and ChatGroq, powered by Streamlit. It starts by loading necessary API keys and setting up the required tools. The user is then prompted to input their query, which is stored in the session state. The ChatGroq model processes this input while search tools gather relevant information from Arxiv and Wikipedia. The search agent combines the responses and displays a relevant answer to the user.
The process ensures seamless user interaction by initializing the tools, managing user input, and providing accurate responses. In case of errors, the system handles them gracefully, ensuring a smooth experience. Overall, this workflow allows the user to efficiently receive responses based on real-time data, leveraging powerful search and AI models for a rich and informative conversation.
Setting up the base is the first crucial step in preparing your environment for efficient processing and interaction with the tools and models required for the task.
Environment setup involves configuring the necessary tools, API keys, and settings to ensure a smooth workflow for the project.
# Create a Environment
python -m venv env
# Activate it on Windows
.\env\Scripts\activate
# Activate in MacOS/Linux
source env/bin/activate
pip install -r https://raw.githubusercontent.com/Gouravlohar/Search-Agent/refs/heads/master/requirements.txt
Make a .env file in your project and visit Groq for API Key.
After getting your API Key paste it in your .env file
GROQ_API_KEY="Your API KEY PASTE HERE"
import streamlit as st
from langchain_groq import ChatGroq
from langchain_community.utilities import ArxivAPIWrapper, WikipediaAPIWrapper
from langchain_community.tools import ArxivQueryRun, WikipediaQueryRun
from langchain.agents import initialize_agent, AgentType
from langchain_community.callbacks.streamlit import StreamlitCallbackHandler # Updated import
import os
from dotenv import load_dotenv
Import libraries to build the Webapp, interact with Llama 3.3, query ArXiv and Wikipedia tools, initialize agents, handle callbacks, and manage environment variables.
load_dotenv()
api_key = os.getenv("GROQ_API_KEY")
arxiv_wrapper = ArxivAPIWrapper(top_k_results=1, doc_content_chars_max=200)
arxiv = ArxivQueryRun(api_wrapper=arxiv_wrapper)
api_wrapper = WikipediaAPIWrapper(top_k_results=1, doc_content_chars_max=200)
wiki = WikipediaQueryRun(api_wrapper=api_wrapper)
st.title("🔎Search Web with Llama 3.3")
Set a user-friendly title for the web app to indicate its purpose.
if "messages" not in st.session_state:
st.session_state["messages"] = [
{"role": "assistant", "content": "Hi, I can search the web. How can I help you?"}
]
Set up a session state to store messages and ensure the chat history persists throughout the user session.
for msg in st.session_state.messages:
st.chat_message(msg["role"]).write(msg['content'])
if prompt := st.chat_input(placeholder="Enter Your Question Here"):
st.session_state.messages.append({"role": "user", "content": prompt})
st.chat_message("user").write(prompt)
llm = ChatGroq(groq_api_key=api_key, model_name="llama-3.3-70b-versatile", streaming=True)
tools = [arxiv, wiki]
search_agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, handle_parsing_errors=True)
Combine both the tools and the language model to create a zero-shot agent capable of performing web searches and providing answers.
with st.chat_message("assistant"):
st_cb = StreamlitCallbackHandler(st.container(), expand_new_thoughts=False)
try:
response = search_agent.run([{"role": "user", "content": prompt}], callbacks=[st_cb])
st.session_state.messages.append({'role': 'assistant', "content": response})
st.write(response)
except ValueError as e:
st.error(f"An error occurred: {e}")
Ensures the app handles issues (e.g., invalid responses) gracefully, displaying an error message whennecessary.
Testing the Webapp
The Agent is providing thought output between both the tools
Get the Full Code in GitHub Repo Here
Building a web-searching agent with LangChain and Llama 3.3 demonstrates how the combination of cutting-edge AI with external knowledge sources such as ArXiv and Wikipedia can power real-world applications that bridge the gap between conversational AI and real-world applications. Users can effortlessly retrieve accurate, context-aware information with this approach. The project uses tools like Streamlit for user interaction and environment variables for security to provide a seamless and secure experience for users.
The modular design allows easy scoping, adapting the entire system to domains and even use cases. This modularity, especially in our AI-driven agents like this example, is how we create tremendous steps toward more intelligent systems that augment human capacities for capabilities in research and education; in fact, way further than that. This research serves as the base platform for building even smarter interactive tools that seize the high potential of AI in seeking knowledge.
A. Llama 3.3 is a versatile language model capable of processing and understanding complex queries. It is used for its advanced reasoning abilities and natural language generation.
A. These platforms provide access to research papers and Data, making them ideal for a knowledge-enhanced web-searching agent.
A. Streamlit offers an intuitive framework to create a chat interface, enabling seamless interaction between the user and the AI agent.
A. Thanks to the modular nature of LangChain, we can integrate other tools to expand the agent’s capabilities.
A. We manage errors using a try-except block, ensuring the app provides meaningful feedback instead of crashing.
The media shown in this article is not owned by Analytics Vidhya and is used at the Author’s discretion.