0% found this document useful (0 votes)
17 views33 pages

Module#3_L17_Information Retrieval Using Agents & Tools

Uploaded by

shettyravi341
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views33 pages

Module#3_L17_Information Retrieval Using Agents & Tools

Uploaded by

shettyravi341
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

MODULE #3 :

Information retrieval using


Agents & Tools

Module #3 - LangChain 1
Module 3: Introduction to Langchain. [L-8 P-10 Total:
16 sessions] [Apply]

Introduction to Lang chain: Types, Components, Information retrieval using agents and tools in Lang
chain, Retrieval Augmented Language Models (RALM): Understanding Retrieval and vectors:
Embeddings, Vector storage, Vector indexing, Vector Libraries, Vector Databases, Chatbot using
memory and conversation buffer
17. Introduction to Information retrieval using agents Co3 PPT/
Langchain and tools in Langchain Chalk and
L01: Generalize the concept of Talk
information retrieval L1
methodologies.
L02: Estimate the impact of
agents that influence the L2
information retrieval and tools that
demonstrate the impact.

Module #3 - LangChain 2
An Agent…
In LangChain,
An “Agent” is an AI entity that is able to perceive its environment, act on its
environment, and make intelligent decisions about how to reach a goal, as
well as the ability to learn as it goes”.

“An Agent interacts with various “Tools” to perform tasks or answer


queries.

Module #3 - LangChain 3
• LLMs have capabilities beyond simply predicting the next word.

• They break the task into steps or sub-tasks, execute each step,
monitor the outcomes, reason through successes or failures, and
adapt the plan accordingly.

• They adjust their actions based on feedback. Such systems are


known as Autonomous Agents.

• These intelligent systems can think and act independently.

4
• They use reasoning, which we can reinforce with prompts and
instructions.

• LangChain build agents that use an LLM to decide on actions to


take.

• These actions are typically done using tools.

• Agents also require an executor, which is the runtime for the agent
and helps execute it.

• This is the legacy syntax for building agents

5
The brains of a LangChain agent are an
LLM.
So, at a basic level, an agent needs
• an LLM to act as its brain, and to give
it its reasoning abilities
• as tools can interact with the
environment around it and achieve its
goals

6
What are Tools?

Tools are essentially methods/classes the agent has access to


perform specific actions.

E.g., Interacting with a Stock Market index over an API, update a


Google Calendar event, or run a query against a database.

A collection of Tools in LangChain are called a Toolkit.

Module #3 - LangChain 7
• Tools act as a interface that an
agent, a chain, or a chat model /
LLM can use to interact with the
world. For e.g., Tools can be Search
tool like SerpAPI, Google search etc.,.
• Given a list of tools and prompt, an
LLM can request that one or more
tools be invoked with appropriate
arguments.

8
9
AI Agents with LangChain

10
AI Agent(bot) An example…

11
Create Agent’s in Langchain…
• The basic code to create an agent in LangChain involves defining
tools, loading a prompt template, and initializing a language model.

• The agent is then executed using an AgentExecutor , which


manages the interaction between the agent and the tools.

• This setup allows for dynamic and context-aware responses,


enabling the AI to reason and take actions in a structured manner.

12
Steps involved to Create Agent’s in Langchain…
• Import the necessary library and modules for Agent
• Define Tool function
• Define Tools
• Prompt Template
• Initialize OpenAI Model
• Initialize the Agent (ReAct Agent)
• Create Agent Executor
• Run the Agent
• Print the response

13
Import Statements: This block imports necessary libraries and modules:

• load_dotenv : This function loads environment variables from a .env file


into the system’s environment variables.
• hub : Allows interaction with the LangChain hub for pulling templates and
other assets.

14
Agent’s in Langchain…

• AgentExecutor and create_react_agent : Classes and functions used to


create and manage agents in LangChain.
• Tool : A class from LangChain that represents a tool the agent can use. In
this case, it wraps the get_current_time() function.
• ChatOpenAI : A class for interfacing with OpenAI's GPT models.

15
Define a Tool function…
• This function get_current_time returns the current time formatted as H:MM
AM/PM.

• The function imports the datetime module, gets the current time, and then
formats it.

16
Define a Tools…
The Tools class requires three parameters:

1. Name: Specify a unique name for the tool.

2. Func: Assign the function that you want to associate with this
tool.

3. Description: Provide a description to indicate when you want the


agent to execute this tool.

• By utilizing the Tools class, developers can effectively organize and


manage their functions, allowing the Langchain agent to execute
the specified tool at the appropriate times.

17
Define a Tools…
This creates a list of tools available to the agent.

18
• combine all the tools you can simple create a array

19
Pull Prompt Template…
This pulls a prompt template named “react” from the LangChain hub, which is
designed to help the agent reason and take actions.

20
Initialize OpenAI Model…

• LLM is an essential component required by the agent for execution.

• You can create and utilize any LLM of your choice.

• For instance, you can employ the OpenAI LLM, which provides powerful
language processing capabilities.

21
Initializing the ReAct Agent…
• Once the tools are created, you can initialize the agent to in executing the
defined tools.

• This creates a ReAct agent using the specified model (llm), tools (tools), and
prompt template (prompt).

• The stop_sequence=True parameter ensures that the agent stops processing


when it reaches a predefined stopping point in the conversation.

22
Create Agent Executor…
• This creates an AgentExecutor that manages the interaction between the agent
and the tools.

• The verbose =True parameter allows detailed logging of the agent’s actions..

23
Run the Agent …
• This runs the agent with a test query: “What time is it?”.

• The agent will use the Time tool to get the current time and respond.

24
Print the Response …
Finally, this prints the response from the agent, which should contain the

current time as per the tool’s output.

25
Benefits of Langchain Agents
1.Flexibility in Integration: Easily integrate with popular LLMs like OpenAI’s
GPT-3, ensuring your applications are powered by the latest in AI.

2.Contextual Understanding: Agents maintain context in conversations,


leading to more meaningful and coherent interactions.

3.Simplified Development: Abstract away the complexities of prompt


engineering and API management, focusing on what truly matters —
solving real-world problems.

Module #3 - LangChain 26
Various other Latest Agent Frameworks…
CrewAI – A Platform for Multi Agent Purpose
https://www.crewai.com/?source=post_page-----90d09a9343db

• Crew AI is a framework which helps us build the agents, where the multiple
agents can work on a different task individually and communicate with each
other.

• Each agents have their own expertise, instructions, and responsibilities. We


can use CrewAI to automate stock-picking, planning vacations, or booking a
movie-ticket.

27
CrewAI – an example…
For example, if we ask the Agent to book a movie ticket, it will start by retrieving the user’s
location and searching for nearby theaters.

• It will then check the available movies in those theaters and sort them based on their
preferences.

• Next, it will present options for the selected movies according to their availability, which it can
also determine by accessing the user’s calendar. Finally, it will book the ticket based on their
seat and location preferences.

• Building agents with Crew AI is incredibly fast and reliable.

• For instance, if you mention that you are on a diet, the agent will avoid booking seats at a fast
food restaurant.

28
Various Latest Agent Frameworks…
Autogen– Multi Agent Conversational Framework
https://microsoft.github.io/autogen/?source=post_page-----90d09a9343db

• Autogen is a robust open-source framework created by Microsoft Research for


developing advanced AI applications through a multi-agent conversation
approach.
• It provides a high-level abstraction and customizable agents to integrate
large language models (LLMs), tools, and human input to address complex
tasks.

29
References:
1.https://python.langchain.com/v0.1/docs/modules/agents/
2. https://brandonhancock.io/langchain-master-class
3. https://python.langchain.com/v0.1/docs/modules/tools/

30
Complete Code

31
Complete Code

32
Complete Code

33

You might also like