Agents in LangChain
Agents in LangChain
Agents
Agents in LangChain
Introduction to Agents
An Agent is a program component that dynamically decides how to respond to a user's query
by selecting the best tool for the task. This enables flexibility and adaptability, allowing the
program to tackle a wide variety of questions and tasks.
Agents don’t just follow preset instructions; instead, they analyze the user’s query and
determine:
Example
● Math Calculations
● Looking up facts online
● Language Translation
Scenario:
Agents decide on the right tool based on the context of the question.
Types of Agents in LangChain
1. Zero-Shot Agents
2. Few-Shot Agents
● Description: These agents perform better with a few examples of how to solve a
problem.
● Use Case: Useful for tasks where context or format is essential.
● Example:
○ Question: "Translate words into French."
○ Provided Examples:
■ "Hello -> Bonjour"
■ "Goodbye -> Au revoir"
○ Response: Translates based on the provided examples.
3. Custom Agents
Agents become powerful by connecting to tools. Tools act as the agent’s “abilities.”
Example
Link the tools to your agent and specify its type (e.g., Zero-shot).
Ask various questions to verify that the agent selects and uses tools correctly.
model_name = "gemini-1.5-flash"
4. google_api_key:
○ The API key for authenticating with Google Generative AI.
○ Replace "your_google_api_key" with your actual API key from Google.
5. model_name = "gemini-1.5-flash":
○ Specifies the Google Generative AI model to use. gemini-1.5-flash is an
example of a fast-response model.
○ You can replace it with another supported model name if required.
6. temperature = 0:
○ Controls the randomness of the language model's output.
○ A lower value (e.g., 0) makes responses more deterministic, while a higher value
(e.g., 1) makes responses more creative.
llm = ChatGoogleGenerativeAI(
google_api_key=google_api_key,
model=model_name,
temperature=temperature
7. llm = ChatGoogleGenerativeAI(...):
○ Initializes the Google Generative AI language model.
○ Parameters:
■ google_api_key: Your API key for authentication.
■ model: Specifies which model to use (gemini-1.5-flash).
■ temperature: Sets the creativity level of responses.
tools = load_tools(["serpapi"],
serpapi_api_key="your_serpapi_api_key")
tools=tools,
llm=llm,
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
verbose=True
9. agent = initialize_agent(...):
○ Combines the tools (serpapi) and the language model (llm) to create an
agent.
○ Parameters:
■ tools: Passes the loaded tools (in this case, SerpAPI).
■ llm: The initialized language model (Google Generative AI).
■ agent: Specifies the type of agent to use. Here,
AgentType.ZERO_SHOT_REACT_DESCRIPTION:
■ A Zero-Shot Agent reacts to queries without requiring prior
examples.
■ It interprets the query, decides on the required tool, and generates
a response.
■ verbose=True: Enables detailed logging of the agent’s internal
decision-making process. Useful for debugging.
1. Setup:
○ Configures a Google Generative AI model and SerpAPI tool.
○ Combines them into an agent.
2. Functionality:
○ Allows the agent to:
■ Analyze the user's query.
■ Use the SerpAPI tool to retrieve real-time information from the web.
■ Generate a final, coherent response using the Google Generative AI
model.
3. Example Execution:
○ If you ask for the "latest news about artificial intelligence," the agent:
■ Uses SerpAPI to search the web for relevant news.
■ Processes the search results and formulates a response using the
language model.
■ Outputs the result.
This makes the agent dynamic, capable of answering a wide variety of queries by leveraging
both its tools and the language model.
Key Takeaways
By leveraging LangChain agents, you can build intelligent and adaptable applications that mimic
the decision-making processes of a real assistant.