Intro to LLMs and LangChain module

# Intro to LLMs and LangChain module
use cases and determine the optimal approach for each scenario, shedding light on the most suitable strategies. In particular, we will examine the fundamental distinctions between prompting the Large Language Models (LLMs) and their Chat model counterparts. Finally, introducing open-source models empowers individuals to run the models locally, eliminating associated costs and enabling further development on top of them.


FableForge: Creating Picture Books with OpenAI, Replicate and Deep Lake

# FableForge: Creating Picture Books with OpenAI, Replicate, and Deep Lake
In this lesson, we are going to delve into a use case of AI technology in the creative domain of children's picture book creation in a project called "FableForge", leveraging both OpenAI GPT-3.5 LLM for writing the story and Stable Diffusion for generating images for it. ### Introduction This lesson’s project, FableForge, is an application that generates picture books from a single text prompt. It utilizes the power of OpenAI's language model GPT-3.5 to write the story. Then, the text is transformed into visual prompts for Stable Diffusion, an AI that creates corresponding images, resulting in a complete picture book. The data, both text and images, are then stored in a Deep Lake dataset for easy analysis. The article guides us through the steps of building FableForge, detailing the challenges, successes, and methodologies adopted. You will learn how the team leveraged the “function calling” feature newly introduced by OpenAI, which is used in this project specifically to structure text data suitable for Stable Diffusion, a task that initially proved difficult due to the model's tendency to include non-visual content in the prompts. We’ll see how to overcome this by using a function providing structured, actionable output for external tools. We'll delve into each component of FableForge, including the generation of text and images, combining them into a book format, storing the data into Deep Lake, and finally presenting it all through a user-friendly interface with Streamlit. We'll explore the process of text generation, extracting visual prompts, assembling PDFs, and uploading the data to Deep Lake. By the end of this lesson, you'll gain a comprehensive understanding of how various AI tools and methodologies can be effectively integrated to overcome challenges and open new frontiers in creative domains. [https://www.activeloop.ai/resources/ai-story-generator-open-ai-function-calling-lang-chain-stable-diffusion/](https://www.activeloop.ai/resources/ai-story-generator-open-ai-function-calling-lang-chain-stable-diffusion/) Congratulations on finishing this module! You can now test your new knowledge with the module quizzes. The next module will be about chains, which are the concept that gives the name to LangChain.


Managing Outputs with Output Parsers

# Managing Outputs with Output Parsers
### Introduction While the language models can only generate textual outputs, a predictable data structure is always preferred in a production environment. For example, imagine you are creating a thesaurus application and want to generate a list of possible substitute words based on the context. The LLMs are powerful enough to generate many suggestions easily. Here is a sample output from the ChatGPT for several words with close meaning to the term “behavior.” ``` Here are some substitute words for "behavior": Conduct Manner Demeanor Attitude Disposition Deportment Etiquette Protocol Performance Actions ``` The problem is the lack of a method to extract relevant information from the mentioned string dynamically. You might say we can split the response by a new line and ignore the first two lines. However, there is no guarantee that the response have the same format every time. The list might be numbered, or there could be no introduction line. The Output Parsers help create a data structure to define the expectations from the output precisely. We can ask for a list of words in case of the word suggestion application or a combination of different variables like a word and the explanation of why it fits. The parser can extract the expected information for you. This lesson covers the different types of parsing objects and the troubleshooting processing. ---


Managing Outputs with Output Parsers

# Managing Outputs with Output Parsers
## 1. Output Parsers
There are three classes that we will introduce in this section. While the Pydrantic parser is the most powerful and flexible wrapper, knowing the other options for less complicated problems is beneficial. We will implement the thesaurus application in each section to better understand the details of each approach. ### 1-1. PydanticOutputParser This class instructs the model to generate its output in a JSON format and then extract the information from the response. You will be able to treat the parser’s output as a list, meaning it will be possible to index through the results without worrying about formatting. <aside> 💡 It is important to note that not all models have the same capability in terms of generating JSON outputs. So, it would be best to use a more powerful model (like OpenAI’s DaVinci instead of Curie) to get the most satisfactory result. </aside> This class uses the Pydantic library, which helps define and validate data structures in Python. It enables us to characterize the expected output with a name, type, and description. We need a variable that can store multiple suggestions in the thesaurus example. It can be easily done by defining a class that inherits from the Pydantic’s BaseModel class. Remember to install the required packages with the following command: `pip install langchain==0.0.208 deeplake openai tiktoken`. ```python from langchain.output_parsers import PydanticOutputParser from pydantic import BaseModel, Field, validator from typing import List # Define your desired data structure. class Suggestions(BaseModel): words: List[str] = Field(description="list of substitue words based on context") # Throw error in case of receiving a numbered-list from API @validator('words') def not_start_with_number(cls, field): for item in field: if item[0].isnumeric(): raise ValueError("The word can not start with numbers!") return field parser = PydanticOutputParser(pydantic_object=Suggestions) ``` We always import and follow the necessary libraries by creating the `Suggestions` schema class. There are two essential parts to this class: 1. **Expected Outputs:** Each output is defined by declaring a variable with desired type, like a list of strings (`: List[str]`) in the sample code, or it could be a single string (`: str`) if you are expecting just one word/sentence as the response. Also, It is required to write a simple explanation using the `Field` function’s `description` attribute to help the model during inference. (We will see an example of having multiple outputs later in the lesson) 2. **Validators:** It is possible to declare functions to validate the formatting. We ensure that the first character is not a number in the sample code. The function’s name is unimportant, but the `@validator` decorator must receive the same name as the variable you want to approve. (like `@validator(’words’)`) It is worth noting that the `field` variable inside the validator function will be a list if you specify it as one. We will pass the created class to the `PydanticOutputParser` wrapper to make it a LangChain parser object. The next step is to prepare the prompt. ```python from langchain.prompts import PromptTemplate template = """ Offer a list of suggestions to substitue the specified target_word based the presented context. {format_instructions} target_word={target_word} context={context} """ prompt = PromptTemplate( template=template, input_variables=["target_word", "context"], partial_variables={"format_instructions": parser.get_format_instructions()} ) model_input = prompt.format_prompt( target_word="behaviour", context="The behaviour of the students in the classroom was disruptive and made it difficult for the teacher to conduct the lesson." ) ``` As discussed in previous lessons, the `template` variable is a string that can have named index placeholders using the following `{variable_name}` format. The template outlines our expectations for the model, including the expected formatting from the parser and the inputs. The `PromptTemplate` receives the template string with the details of each placeholder’s type. They could either


Managing Outputs with Output Parsers

# Managing Outputs with Output Parsers
## 1. Output Parsers
be 1) `input_variables` whose value is initialized later on using the `.format_prompt()` function, or 2) `partial_variables` to be initialized instantly. The prompt can send the query to models like GPT using LangChain’s OpenAI wrapper. (Remember to set the `OPENAI_API_KEY` environment variables with your API key from OpenAI) We are using the Davinci model, one of the more powerful options to get the best results, and set the temperature value to 0, making the results reproducible. <aside> 💡 The temperature value could be anything between 0 to 1, where a higher number means the model is more creative. Using larger value in production is a good practice if you deal with tasks requiring creative output. </aside> ```python from langchain.llms import OpenAI # Before executing the following code, make sure to have # your OpenAI key saved in the “OPENAI_API_KEY” environment variable. model = OpenAI(model_name='text-davinci-003', temperature=0.0) output = model(model_input.to_string()) parser.parse(output) ``` ```python Suggestions(words=['conduct', 'manner', 'action', 'demeanor', 'attitude', 'activity']) ``` The parser object’s `parse()` function will convert the model’s string response to the format we specified. There is a list of words that you can index through and use in your applications. **Multiple Outputs Example** Here is a sample code for Pydantic class to process multiple outputs. It requests the model to suggest a list of words and present the reasoning behind each proposition. Replace the `template` variable and `Suggestion` class with the following codes to run this example. The template changes will ask the model to present its reasoning, and the suggestion class declares a new output named `reasons`. Also, the validator function manipulates the output to ensure every reasoning ends with a dot. Another use case of the validator function could be output manipulation. ```python template = """ Offer a list of suggestions to substitute the specified target_word based on the presented context and the reasoning for each word. {format_instructions} target_word={target_word} context={context} """ ``` ```python class Suggestions(BaseModel): words: List[str] = Field(description="list of substitue words based on context") reasons: List[str] = Field(description="the reasoning of why this word fits the context") @validator('words') def not_start_with_number(cls, field): for item in field: if item[0].isnumeric(): raise ValueError("The word can not start with numbers!") return field @validator('reasons') def end_with_dot(cls, field): for idx, item in enumerate( field ): if item[-1] != ".": field[idx] += "." return field ``` ```python Suggestions(words=['conduct', 'manner', 'demeanor', 'comportment'], reasons=['refers to the way someone acts in a particular situation.', 'refers to the way someone behaves in a particular situation.', 'refers to the way someone behaves in a particular situation.', 'refers to the way someone behaves in a particular situation.']) ``` ### 1-2. CommaSeparatedOutputParser It is evident from the name of this class that it manages comma-separated outputs. It handles one specific case: anytime you want to receive a list of outputs from the model. Let’s start by importing the necessary module. ```python from langchain.output_parsers import CommaSeparatedListOutputParser parser = CommaSeparatedListOutputParser() ``` The parser does not require a setting up step. Therefore it is less flexible. We can create the object by calling the class. The rest of the process for writing the prompt, initializing the model, and parsing the output is as follows. ```python from langchain.llms import OpenAI from langchain.prompts import PromptTemplate # Prepare the Prompt template = """ Offer a list of suggestions to substitute the word '{target_word}' based the presented the following text: {context}. {format_instructions} """ prompt = PromptTemplate( template=template, input_variables=["target_word", "context"], partial_variables={"format_instructions": parser.get_format_instructions()} ) model_input = prompt.format( target_word="behaviour", context="The behaviour of the students in the classroom was disruptive and made it difficult for the teacher to conduct the lesson." ) # Loading OpenAI API model = OpenAI(model_name='text-davinci-003', temperature=0.0) # Send the Request output


Managing Outputs with Output Parsers

# Managing Outputs with Output Parsers
## 1. Output Parsers
= model(model_input) parser.parse(output) ``` ```python ['Conduct', 'Actions', 'Demeanor', 'Mannerisms', 'Attitude', 'Performance', 'Reactions', 'Interactions', 'Habits', 'Repertoire', 'Disposition', 'Bearing', 'Posture', 'Deportment', 'Comportment'] ``` Although most of the sample code has been explained in the previous subsection, two parts might need attention. Firstly, we tried a new format for the prompt’s template to show different ways to write a prompt. Secondly, the use of `.format()` instead of `.format_prompt()` to generate the model’s input. The main difference compared to the previous subsection’s code is that we no longer need to call the `.to_string()` object since the prompt is already in string type. As you can see, the final output is a list of words that has some overlaps with the `PydanticOutputParser` approach with more variety. However, requesting additional reasoning information using the `CommaSeparatedOutputParser` class is impossible. ### 1-3. StructuredOutputParser This is the first output parser implemented by the LangChain team. While it can process multiple outputs, it only supports texts and does not provide options for other data types, such as lists or integers. It can be used when you want to receive one response from the model. For example, only one substitute word in the thesaurus application. ```python from langchain.output_parsers import StructuredOutputParser, ResponseSchema response_schemas = [ ResponseSchema(name="words", description="A substitue word based on context"), ResponseSchema(name="reasons", description="the reasoning of why this word fits the context.") ] parser = StructuredOutputParser.from_response_schemas(response_schemas) ``` The above code demonstrates how to define a schema. However, we are not going to go into details. This class has no advantage since the `PydanticOutputParser` class provides validation and more flexibility for more complex tasks, and the `CommaSeparatedOutputParser` option covers more straightforward applications. ---


Managing Outputs with Output Parsers

# Managing Outputs with Output Parsers
## 2. Fixing Errors
The parsers are powerful tools to dynamically extract the information from the prompt and validate it to some extent. Still, they do not guarantee a response. Imagine a situation where you deployed your application, and the model’s response [to a user’s request] is incomplete, causing the parser to throw an error. It is not ideal! In the following subsections, we will introduce two classes acting as fail-safe. They add a layer on top of the model’s response to help fix the errors. <aside> 💡 The following approaches work with the `PydanticOutputParser` class since it is the only one with a validation method. </aside> ### 2-1. OutputFixingParser This method tries to fix the parsing error by looking at the model’s response and the previous parser. It uses a Large Language Model (LLM) to solve the issue. We will use GPT-3 to be consistent with the rest of the lesson, but it is possible to pass any supported model. Let’s start by defining the Pydantic data schema and show a sample error that could occur. ```python from langchain.output_parsers import PydanticOutputParser from pydantic import BaseModel, Field from typing import List # Define your desired data structure. class Suggestions(BaseModel): words: List[str] = Field(description="list of substitue words based on context") reasons: List[str] = Field(description="the reasoning of why this word fits the context") parser = PydanticOutputParser(pydantic_object=Suggestions) missformatted_output = '{"words": ["conduct", "manner"], "reasoning": ["refers to the way someone acts in a particular situation.", "refers to the way someone behaves in a particular situation."]}' parser.parse(missformatted_output) ``` ![The output.](Managing%20Outputs%20with%20Output%20Parsers%206bfed140f72147a984ade0fa5d6ad931/Untitled.png) The output. As you can see in the error message, the parser correctly identified an error in our sample response (`missformatted_output`) since we used the word `reasoning` instead of the expected `reasons` key. The `OutputFixingParser` class could easily fix this error. ```python from langchain.llms import OpenAI from langchain.output_parsers import OutputFixingParser model = OpenAI(model_name='text-davinci-003', temperature=0.0) outputfixing_parser = OutputFixingParser.from_llm(parser=parser, llm=model) outputfixing_parser.parse(missformatted_output) ``` ```python Suggestions(words=['conduct', 'manner'], reasons=['refers to the way someone acts in a particular situation.', 'refers to the way someone behaves in a particular situation.']) ``` The `from_llm()` function takes the old parser and a language model as input parameters. Then, It initializes a new parser for you that has the ability to fix output errors. In this case, it successfully identified the misnamed key and changed it to what we defined. However, fixing the issues using this class is not always possible. Here is an example of using `OutputFixingParser` class to resolve an error with a missing key. ```python missformatted_output = '{"words": ["conduct", "manner"]}' outputfixing_parser = OutputFixingParser.from_llm(parser=parser, llm=model) outputfixing_parser.parse(missformatted_output) ``` ```python Suggestions(words=['conduct', 'manner'], reasons=["The word 'conduct' implies a certain behavior or action, while 'manner' implies a polite or respectful way of behaving."]) ``` Looking at the output, it is evident that the model understood the key `reasons` missing from the response but didn’t have the context of the desired outcome. It created a list with one entry, while we expect one reason per word. This is why we sometimes need to use the `RetryOutputParser` class. ### 2-2. RetryOutputParser In some cases, the parser needs access to both the output and the prompt to process the full context, as demonstrated in the previous section. We first need to define the mentioned variables. The following codes initialize the LLM model, parser, and prompt, which were explained in more detail earlier. ```python from langchain.llms import OpenAI from langchain.prompts import PromptTemplate from langchain.output_parsers import PydanticOutputParser from pydantic import BaseModel, Field from typing import List # Define data structure. class Suggestions(BaseModel): words: List[str] = Field(description="list of substitue words based on context") reasons: List[str] = Field(description="the reasoning of why this word fits the context") parser = PydanticOutputParser(pydantic_object=Suggestions)


Managing Outputs with Output Parsers

# Managing Outputs with Output Parsers
## 2. Fixing Errors
# Define prompt template = """ Offer a list of suggestions to substitue the specified target_word based the presented context and the reasoning for each word. {format_instructions} target_word={target_word} context={context} """ prompt = PromptTemplate( template=template, input_variables=["target_word", "context"], partial_variables={"format_instructions": parser.get_format_instructions()} ) model_input = prompt.format_prompt(target_word="behaviour", context="The behaviour of the students in the classroom was disruptive and made it difficult for the teacher to conduct the lesson.") # Define Model model = OpenAI(model_name='text-davinci-003', temperature=0.0) ``` Now, we can fix the same `missformatted_output` using the `RetryWithErrorOutputParser` class. It receives the old parser and a model to declare the new parser object, as we saw in the previous section. However, the `parse_with_prompt` function is responsible for fixing the parsing issue while requiring the output and the prompt. ```python from langchain.output_parsers import RetryWithErrorOutputParser missformatted_output = '{"words": ["conduct", "manner"]}' retry_parser = RetryWithErrorOutputParser.from_llm(parser=parser, llm=model) retry_parser.parse_with_prompt(missformatted_output, model_input) ``` ```python Suggestions(words=['conduct', 'manner'], reasons=["The behaviour of the students in the classroom was disruptive and made it difficult for the teacher to conduct the lesson, so 'conduct' is a suitable substitute.", "The students' behaviour was inappropriate, so 'manner' is a suitable substitute."]) ``` The outputs show that the `RetryOuputParser` has the ability to fix the issue where the `OuputFixingParser` was not able to. The parser correctly guided the model to generate one reason for each word. The best practice to incorporate these techniques in production is to catch the parsing error using a `try: ... except: ...` method. It means we can capture the errors in the `except` section and attempt to fix them using the mentioned classes. It will limit the number of API calls and avoid unnecessary costs that are associated with it. ### Conclusion We learned how to validate and extract the information in an easy-to-use format from the language models’ responses which are always a string. Additionally, we reviewed LangChain’s fail-safe procedures to guarantee the consistency of the output. Combining these approaches will help us write more reliable applications in production environments. In the next lesson, we will learn how to build a knowledge graph and capture useful information or entities from texts. In the next lesson we’ll modify the news summarizer we built in the previous module by improving how we manage the prompts. --- You can find the code of this lesson in this online [notebook](https://colab.research.google.com/drive/1j7Kv1qKsXmIhWXlvZ4Jm_6K9DJ1pBgHp?usp=sharing).


# Exploring Conversational Capabilities with GPT-4 and ChatGPT
### Introduction In this lesson, we will explore the benefits of using GPT-4 and ChatGPT, focusing on their ability to maintain context in conversations. We will demonstrate how these advanced language models can remember conversation history and respond accordingly, making them ideal for chat applications. Additionally, we will briefly discuss the improvements in GPT-4, such as longer context length and better generalization. By the end of this lesson, you should be able to understand how GPT-4 and ChatGPT can be used for context-aware chat applications via the API, as opposed to just using the OpenAI ChatGPT webpage. As mentioned before, **OpenAI's GPT-4** represents a significant advancement in the field of large language models. Among its many improvements are enhanced creativity, the ability to process visual input, and an extended contextual understanding. In the realm of conversational AI, both GPT-4 and ChatGPT use the Transformers architecture at their core and are fine-tuned to hold natural dialogue with a user. While the free version of ChatGPT relies on GPT-3, the premium offering, ChatGPT Plus, gives access to the more advanced GPT-4 model. The benefits of employing ChatGPT and GPT-4 in chat format are numerous. For instance, GPT-4's short-term memory capacity of 64,000 words greatly surpasses GPT-3.5's 8,000-word limit, enabling it to maintain context more effectively in prolonged conversations. Furthermore, GPT-4 is highly multilingual, accurately handling up to 26 languages, and boasts improved steering capabilities, allowing users to tailor responses with a custom "personality." The new model is considerably safer to use, boasting a 40% increase in factual responses and an 82% reduction in disallowed content responses. It can also interpret images as a foundation for interaction. While this functionality has not yet been incorporated into ChatGPT, its potential to revolutionize context-aware chat applications is immense. ### Setting up the API To use GPT-4 or ChatGPT in your application, you must obtain API keys from OpenAI. You'll need to sign up for an account and submit a request to access the latest model. At the time of writing this lesson, there is a waitlist to get your hands on GPT-4. Then, set the `OPENAI_API_KEY` key in your environment variables so the LangChain library can access them. The following example demonstrates how to create a chatbot using the GPT-4 model from OpenAI. After importing the necessary classes, we declare a set of messages. It starts by setting the context for the model (`SystemMessage`) that it is an assistant, followed by the user’s query (`HumanMessage`), and finishes by defining a sample response from the AI model (`AIMessage`). Remember to install the required packages with the following command: `pip install langchain==0.0.208 deeplake openai tiktoken`. ```python from langchain.chat_models import ChatOpenAI from langchain.schema import ( SystemMessage, HumanMessage, AIMessage ) messages = [ SystemMessage(content="You are a helpful assistant."), HumanMessage(content="What is the capital of France?"), AIMessage(content="The capital of France is Paris.") ] ``` When the user posed the question about the capital of France, the model confidently answered with "Paris.” Next up, we test if the model can leverage these discussions as a reference to delve further into details about the city without us explicitly mentioning the name (referring to Paris). The code below adds a new message which requires the model to understand and find the “city you just mentioned” reference from previous conversations. ```python prompt = HumanMessage( content="I'd like to know more about the city you just mentioned." ) # add to messages messages.append(prompt) llm = ChatOpenAI(model_name="gpt-4") response = llm(messages) ``` ```python AIMessage(content='Paris, the capital of France, is one of the most famous and visited cities in the world. It is located in the north-central part of the


# Exploring Conversational Capabilities with GPT-4 and ChatGPT
country, along the Seine River. With a population of over 2 million people, it is a bustling metropolis known for its art, culture, history, and gastronomy.\n\nParis is often referred to as the "City of Light" (La Ville Lumière) due to its role in the Age of Enlightenment and its early adoption of street lighting. The city is home to numerous iconic landmarks, including the Eiffel Tower, the Louvre Museum, Notre-Dame Cathedral, and the Arc de Triomphe. \n\nParis is also known for its charming neighborhoods, such as Montmartre, Le Marais, and Saint-Germain-des-Prés, each with its own unique character and attractions. The city has an extensive public transportation system, including buses, trams, and the Métro, making it easy to explore its various districts.\n\nAs a global center for art, fashion, and culture, Paris hosts numerous events and exhibitions throughout the year, such as Paris Fashion Week and the Paris Air Show. The city is also renowned for its culinary scene, featuring a wide array of restaurants, cafés, patisseries, and food markets that offer both traditional French cuisine and international flavors.\n\nIn addition to its historical and cultural attractions, Paris is an important international business hub and the headquarters for many multinational corporations and organizations, including UNESCO, the OECD, and the International Chamber of Commerce.', additional_kwargs={}, example=False) ``` As you can see, the model successfully extracted the information from previous conversations and explained more details about Paris. It shows that the chat models are capable of referring to the chat history and understanding the context. To recap, the `ChatOpenAI` class is used to create a chat-based application that can handle user inputs and generate responses using the GPT-4 language model. The conversation is initiated with a series of messages, including system, human, and AI messages. The `SystemMessage` provides context for the conversation, while `HumanMessage` and `AIMessage` represent the user and the AI's messages, respectively. The LangChain’s Chat API offers several advantages: - **Context preservation**: By maintaining a list of messages in the conversation, the API ensures that the context is preserved throughout the interaction. This allows the GPT-4 model to generate relevant and coherent responses based on the provided information. - **Memory**: The class’s message history acts as a short-term memory for the chatbot, allowing it to refer back to previous messages and provide more accurate and contextual responses. - **Modularity**: The combination of MessageTemplate and ChatOpenAI classes offers a modular approach to designing conversation applications. This makes it easier to develop, maintain, and extend the functionality of the chatbot. - **Improved performance**: GPT-4, as an advanced language model, is more adept at understanding complex prompts and generating better responses than its predecessors. It can handle tasks that require deeper reasoning and context awareness, which leads to a more engaging and useful conversation experience. - **Flexibility**: The Chat API can be adapted to different domains and tasks, making it a versatile solution for various chatbot applications. In this example, the chatbot specializes in French culture but could be easily modified to focus on other subjects or industries. Moreover, as newer and more powerful language models become available, the API can be updated to utilize those models, allowing for continuous improvements in chatbot capabilities. ### ****Conclusion**** In this lesson, we learned that GPT-4 boasts remarkable advancements in context length and generalization, paving the way for more sophisticated language processing. By accommodating a more extensive context, GPT-4 can generate lengthier text pieces, analyze more massive documents, and engage in longer conversations without compromising the context's integrity. ChatGPT and GPT-4 models are tailor-made for conversational interfaces, which require input to be formatted in a specific chat-like transcript format.


# Exploring Conversational Capabilities with GPT-4 and ChatGPT
This format empowers the models to retain conversation history and furnish contextually relevant responses. This attribute is especially advantageous for multi-turn conversations and can also prove useful in non-chat scenarios. These potent models have diverse applications, including customer support chatbots that manage intricate inquiries and dispense pertinent responses based on previous interactions. They can also function as virtual personal assistants that preserve context across various tasks and requests. They also serve as natural language interfaces for databases and search engines, enabling them to better understand user queries and provide more accurate results. In the next lesson, you’ll do the first project of the course, that is a news summarizer leveraging LangChain. --- You can find the code of this lesson in this online [Notebook](https://colab.research.google.com/drive/1l3m7gHPWI5yX5Tfx90hLJcd8vFahIgDO?usp=sharing).


LangChain 101: from Zero to Hero

# LangChain 101: from Zero to Hero
### Introduction Welcome to our quick guide on how to leverage the power of LangChain! This lesson will introduce you to various concepts and tools that can help you build your AI projects more effectively. We will cover a range of topics, including library installation, obtaining OpenAI credentials, generating predictions using a language model, constructing chains, incorporating memory, and utilizing vector databases. Additionally, we will explore how to use Deep Lake as vector store and provide a practical example. Furthermore, we will discuss how to use tools and agents such as the Vector store Agent. With concise explanations and code snippets, this guide will serve as a valuable resource for individuals getting started with LangChain or looking to enhance their AI workflow. Moreover, we will explore the use of different tools, including the Google Search tool, and discuss how to combine them with appropriate agents to achieve desired outcomes. We will demonstrate how to initiate and manage these agents, which act as the orchestrators, coordinating the usage of tools based on the input they receive. ### Installation and API keys Let's install the required packages with the following command: `pip install langchain==0.0.208 deeplake openai tiktoken`. In order to access OpenAI's services, you must first obtain credentials by signing up on their website, completing the registration process, and creating an `API key` from your dashboard. This enables you to leverage OpenAI's powerful capabilities in your projects. 1. If you don't have an account yet, create one by going to **[https://platform.openai.com/](https://platform.openai.com/)**. If you already have an account, skip to step 5. 2. Fill out the registration form with your name, email address, and desired password. 3. OpenAI will send you a confirmation email with a link. Click on the link to confirm your account. 4. Please note that you'll need to verify your email account and provide a phone number for verification. 5. Log in to **[https://platform.openai.com/](https://platform.openai.com/)**. 6. Navigate to the API key section at **[https://platform.openai.com/account/api-keys](https://platform.openai.com/account/api-keys)**. 7. Click "Create new secret key" and give the key a recognizable name or ID. ### The LLMs The fundamental component of LangChain involves invoking an LLM with a specific input. To illustrate this, we'll explore a simple example. Let's imagine we are building a service that suggests personalized workout routines based on an individual's fitness goals and preferences. To accomplish this, we will first need to import the LLM wrapper. ```python from langchain.llms import OpenAI ``` The temperature parameter in OpenAI models manages the randomness of the output. When set to 0, the output is mostly predetermined and suitable for tasks requiring stability and the most probable result. At a setting of 1.0, the output can be inconsistent and interesting but isn't generally advised for most tasks. For creative tasks, a temperature between 0.70 and 0.90 offers a balance of reliability and creativity. The best setting should be determined by experimenting with different values for each specific use case. The code initializes the GPT-3 model’s Davinci variant. We will learn more about the various models and their differences later on. Before executing the following code, save your OpenAI key in the environment variable using the following key `OPENAI_API_KEY`. ```python # Before executing the following code, make sure to have # your OpenAI key saved in the “OPENAI_API_KEY” environment variable. llm = OpenAI(model="text-davinci-003", temperature=0.9) ``` Now we can call it on some input! ```python text = "Suggest a personalized workout routine for someone looking to improve cardiovascular endurance and prefers outdoor activities." print(llm(text)) ``` This code will generate a personalized workout routine based on the user's fitness goals and preferences using the LLM model: ```python 1)


LangChain 101: from Zero to Hero

# LangChain 101: from Zero to Hero
Monday: Jog for 30 minutes outside 2) Tuesday: 30 minutes of jumping jacks 3) Wednesday: 30 minutes of running stairs 4) Thursday: 30 minutes of sprints 5) Friday: 30 minutes of jogging on an inclined treadmill 6) Saturday: 30 minutes of interval training 7) Sunday: Walk for 30 minutes ``` --- ### The Chains In LangChain, a chain is an end-to-end wrapper around multiple individual components, providing a way to accomplish a common use case by combining these components in a specific sequence. The most commonly used type of chain is the `LLMChain`, which consists of a `PromptTemplate`, a model (either an LLM or a ChatModel), and an optional output parser. The `LLMChain` works as follows: 1. Takes (multiple) input variables. 2. Uses the `PromptTemplate` to format the input variables into a prompt. 3. Passes the formatted prompt to the model (LLM or ChatModel). 4. If an output parser is provided, it uses the `OutputParser` to parse the output of the LLM into a final format. In the next example, we demonstrate how to create a chain that generates a possible name for a company that produces eco-friendly water bottles. By using LangChain's `LLMChain`, `PromptTemplate`, and `OpenAI`classes, we can easily define our prompt, set the input variables, and generate creative outputs. ```python from langchain.prompts import PromptTemplate from langchain.llms import OpenAI from langchain.chains import LLMChain llm = OpenAI(model="text-davinci-003", temperature=0.9) prompt = PromptTemplate( input_variables=["product"], template="What is a good name for a company that makes {product}?", ) chain = LLMChain(llm=llm, prompt=prompt) # Run the chain only specifying the input variable. print(chain.run("eco-friendly water bottles")) ``` ``` Eco-Pure Water Bottles. ``` This example showcases the flexibility and ease of using LangChain to create custom chains for various language generation tasks. --- ### The Memory In LangChain, Memory refers to the mechanism that stores and manages the conversation history between a user and the AI. It helps maintain context and coherency throughout the interaction, enabling the AI to generate more relevant and accurate responses. Memory, such as `ConversationBufferMemory`, acts as a wrapper around `ChatMessageHistory`, extracting the messages and providing them to the chain for better context-aware generation. ```python from langchain.llms import OpenAI from langchain.chains import ConversationChain from langchain.memory import ConversationBufferMemory llm = OpenAI(model="text-davinci-003", temperature=0) conversation = ConversationChain( llm=llm, verbose=True, memory=ConversationBufferMemory() ) # Start the conversation conversation.predict(input="Tell me about yourself.") # Continue the conversation conversation.predict(input="What can you do?") conversation.predict(input="How can you help me with data analysis?") # Display the conversation print(conversation) ``` The output you will observe resembles the following, although the specifics may be different. ``` **> Entering new ConversationChain chain...** Prompt after formatting: ***The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know. Current conversation: Human: Tell me about yourself. AI: Hi there! I'm an AI created to help people with their daily tasks. I'm programmed to understand natural language and respond to questions and commands. I'm also able to learn from my interactions with people, so I'm constantly growing and improving. I'm excited to help you out! Human: What can you do? AI: I can help you with a variety of tasks, such as scheduling appointments, setting reminders, and providing information. I'm also able to answer questions about topics like current events, sports, and entertainment. I'm always learning new things, so I'm sure I can help you with whatever you need. Human: How can you help me with data analysis? AI:* I'm not familiar with data analysis, but I'm sure I


LangChain 101: from Zero to Hero

# LangChain 101: from Zero to Hero
can help you find the information you need. I can search the web for articles and resources related to data analysis, and I can also provide you with links to helpful websites.** **> Finished chain.** ``` In this output, you can see the memory being used by observing the "Current conversation" section. After each input from the user, the conversation is updated with both the user's input and the AI's response. This way, the memory maintains a record of the entire conversation. When the AI generates its next response, it will use this conversation history as context, making its responses more coherent and relevant. --- ### Deep Lake VectorStore Deep Lake provides storage for embeddings and their corresponding metadata in the context of LLM apps. It enables hybrid searches on these embeddings and their attributes for efficient data retrieval. It also integrates with LangChain, facilitating the development and deployment of applications. Deep Lake provides several advantages over the typical vector store: - It’s **multimodal**, which means that it can be used to store items of diverse modalities, such as texts, images, audio, and video, along with their vector representations. - It’s **serverless**, which means that we can create and manage cloud datasets without creating and managing a database instance. This aspect gives a great speedup to new projects. - Last, it’s possible to easily create a **data loader** out of the data loaded into a Deep Lake dataset. It is convenient for fine-tuning machine learning models using common frameworks like PyTorch and TensorFlow. In order to use Deep Lake, you first have to register on the Activeloop website and redeem your API token. Here are the steps for doing it: 1. Sign up for an account on Activeloop's platform. You can sign up at [Activeloop's website](https://app.activeloop.ai/register). After specifying your username, click on the “Sign up” button. You should now see your homepage. 2. You should now see a “Create API token” button at the top of your homepage. Click on it, and you’ll get redirected to the “API tokens” page. This is where you can generate, manage, and revoke your API keys for accessing Deep Lake. 3. Click on the "Create API token" button. Then, you should see a popup asking for a token name and an expiration date. By default, the token expiration date is set so that the token expires after one day from its creation, but you can set it further in the future if you want to keep using the same token for the whole duration of the course. Once you’ve set the token name and its expiration date, click on the “Create API token” button. 4. You should now see a green banner saying that the token has been successfully generated, along with your new API token, on the “API tokens” page. To copy your token to your clipboard, click on the square icon on its right. Now that you have your API token, you can conveniently store under the `ACTIVELOOP_TOKEN` key in the environment variable to retrieve it automatically by the Deep Lake libraries whenever needed. You can also save the token to its environment variable with Python, like in the following code snippet. ```python import os os.environ["ACTIVELOOP_TOKEN"] = "<YOUR-ACTIVELOOP-TOKEN>" ``` Please notice that the previous code snippet adds the environment variable only in the context of the current code execution. Therefore, you’d need to run it again for each new Python program you want to run. <aside> 💡 If you set your API keys/tokens in your code explicitly, please be careful in doing so, because if another person gets your API key (e.g.,


LangChain 101: from Zero to Hero

# LangChain 101: from Zero to Hero
by seeing it in a GitHub repository where you uploaded it by mistake), then they can do actions on your behalf, as the API key is associated to your account. Exposing these sensitive credentials directly in the code poses a security risk. In case of a key compromise or a need to revoke access, developers would have to modify and redeploy the code, which can be time-consuming and error-prone. </aside> It's best to store API keys in secure configuration files or use environment variables to keep them separate from the codebase, reducing the risk of accidental exposure and simplifying key management processes. Let’s install the `deeplake` library. ```python !pip install deeplake ``` Set up the environment and load the data. Before executing the following code, save your Activeloop key in the “ACTIVELOOP_TOKEN” environment variable. ```python from langchain.embeddings.openai import OpenAIEmbeddings from langchain.vectorstores import DeepLake from langchain.text_splitter import RecursiveCharacterTextSplitter from langchain.llms import OpenAI from langchain.chains import RetrievalQA # Before executing the following code, make sure to have your # Activeloop key saved in the “ACTIVELOOP_TOKEN” environment variable. # instantiate the LLM and embeddings models llm = OpenAI(model="text-davinci-003", temperature=0) embeddings = OpenAIEmbeddings(model="text-embedding-ada-002") # create our documents texts = [ "Napoleon Bonaparte was born in 15 August 1769", "Louis XIV was born in 5 September 1638" ] text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=0) docs = text_splitter.create_documents(texts) # create Deep Lake dataset # TODO: use your organization id here. (by default, org id is your username) my_activeloop_org_id = "<YOUR-ACTIVELOOP-ORG-ID>" my_activeloop_dataset_name = "langchain_course_from_zero_to_hero" dataset_path = f"hub://{my_activeloop_org_id}/{my_activeloop_dataset_name}" db = DeepLake(dataset_path=dataset_path, embedding_function=embeddings) # add documents to our Deep Lake dataset db.add_documents(docs) ``` If everything works correctly, you should see a printed output like this: ```jsx Your Deep Lake dataset has been successfully created! The dataset is private so make sure you are logged in! ``` If so, you’ve just created your first Deep Lake dataset! Now, let's create a `RetrievalQA` chain: ```python retrieval_qa = RetrievalQA.from_chain_type( llm=llm, chain_type="stuff", retriever=db.as_retriever() ) ``` Next, let's create an agent that uses the `RetrievalQA` chain as a tool: ```python from langchain.agents import initialize_agent, Tool from langchain.agents import AgentType tools = [ Tool( name="Retrieval QA System", func=retrieval_qa.run, description="Useful for answering questions." ), ] agent = initialize_agent( tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True ) ``` Finally, we can use the agent to ask a question: ```python response = agent.run("When was Napoleone born?") print(response) ``` You should see something similar to the following printed output. Here, the agent used the “Retrieval QA System” tool with the query “When was Napoleone born?” which is then run on our new Deep Lake dataset, returning the most similar document (i.e., the document containing the date of birth of Napoleon). This document is eventually used to generate the final output. ```jsx **> Entering new AgentExecutor chain...** ***I need to find out when Napoleone was born. Action: Retrieval QA System Action Input: When was Napoleone born?*** Observation: ***Napoleon Bonaparte was born on 15 August 1769.*** Thought: ***I now know the final answer. Final Answer: Napoleon Bonaparte was born on 15 August 1769.*** **> Finished chain.** Napoleon Bonaparte was born on 15 August 1769. ``` This example demonstrates how to use Deep Lake as a vector database and create an agent with a `RetrievalQA` chain as a tool to answer questions based on the given document. Let’s add an example of reloading an existing vector store and adding more data. We first reload an existing vector store from Deep Lake that's located at a specified dataset path. Then, we load new textual data and split it into manageable chunks. Finally, we add these chunks to the existing dataset, creating and storing corresponding embeddings for each added


LangChain 101: from Zero to Hero

# LangChain 101: from Zero to Hero
text segment: ```python # load the existing Deep Lake dataset and specify the embedding function db = DeepLake(dataset_path=dataset_path, embedding_function=embeddings) # create new documents texts = [ "Lady Gaga was born in 28 March 1986", "Michael Jeffrey Jordan was born in 17 February 1963" ] text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=0) docs = text_splitter.create_documents(texts) # add documents to our Deep Lake dataset db.add_documents(docs) ``` We then recreate our previous agent and ask a question that can be answered only by the last documents added. ```python # instantiate the wrapper class for GPT3 llm = OpenAI(model="text-davinci-003", temperature=0) # create a retriever from the db retrieval_qa = RetrievalQA.from_chain_type( llm=llm, chain_type="stuff", retriever=db.as_retriever() ) # instantiate a tool that uses the retriever tools = [ Tool( name="Retrieval QA System", func=retrieval_qa.run, description="Useful for answering questions." ), ] # create an agent that uses the tool agent = initialize_agent( tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True ) ``` Let’s now test our agent with a new question. ```python response = agent.run("When was Michael Jordan born?") print(response) ``` The printed output will look like the following. ```python > Entering new AgentExecutor chain... I need to find out when Michael Jordan was born. Action: Retrieval QA System Action Input: When was Michael Jordan born? Observation: Michael Jordan was born on February 17, 1963. Thought: I now know the final answer. Final Answer: Michael Jordan was born on February 17, 1963. > Finished chain. Michael Jordan was born on February 17, 1963. ``` The LLM successfully retrieves accurate information by using the power of Deep Lake as a vector store and the OpenAI language model. ### Agents in LangChain In LangChain, agents are high-level components that use language models (LLMs) to determine which actions to take and in what order. An action can either be using a tool and observing its output or returning it to the user. Tools are functions that perform specific duties, such as Google Search, database lookups, or Python REPL. Agents involve an LLM making decisions about which Actions to take, taking that Action, seeing an Observation, and repeating that until done. Several types of agents are available in LangChain: - The `zero-shot-react-description` agent uses the ReAct framework to decide which tool to employ based purely on the tool's description. It necessitates a description of each tool. - The `react-docstore` agent engages with a docstore through the ReAct framework. It needs two tools: a Search tool and a Lookup tool. The Search tool finds a document, and the Lookup tool searches for a term in the most recently discovered document. - The `self-ask-with-search` agent employs a single tool named Intermediate Answer, which is capable of looking up factual responses to queries. It is identical to the original self-ask with the search paper, where a Google search API was provided as the tool. - The `conversational-react-description` agent is designed for conversational situations. It uses the ReAct framework to select a tool and uses memory to remember past conversation interactions. In our example, the Agent will use the Google Search tool to look up recent information about the Mars rover and generates a response based on this information. First, you want to set the environment variables “GOOGLE_API_KEY” and “GOOGLE_CSE_ID” to be able to use Google Search via API. Refer to this article for a guide on how to get them. [Google Search | 🦜🔗 Langchain](https://python.langchain.com/docs/modules/agents/tools/integrations/google_search) Then, let’s import the necessary modules: - `langchain.llms.OpenAI`: This is used to create an instance of the OpenAI language model, which can generate human-like text based on the input it's given. - `langchain.agents.load_tools`: This function is used to load a list of tools that an AI agent can


LangChain 101: from Zero to Hero

# LangChain 101: from Zero to Hero
use. - `langchain.agents.initialize_agent`: This function initializes an AI agent that can use a given set of tools and a language model to interact with users. - `langchain.agents.Tool`: This is a class used to define a tool that an AI agent can use. A tool is defined by its name, a function that performs the tool's action, and a description of the tool. - `langchain.utilities.GoogleSearchAPIWrapper`: This class is a wrapper for the Google Search API, allowing it to be used as a tool by an AI agent. It likely contains a method that sends a search query to Google and retrieves the results. ```python from langchain.llms import OpenAI from langchain.agents import AgentType from langchain.agents import load_tools from langchain.agents import initialize_agent from langchain.agents import Tool from langchain.utilities import GoogleSearchAPIWrapper ``` We’ll initialize the LLM and set the temperature to 0 for the precise answer. ```python llm = OpenAI(model="text-davinci-003", temperature=0) ``` We can now define the Google search wrapper as follow. ```python # remember to set the environment variables # “GOOGLE_API_KEY” and “GOOGLE_CSE_ID” to be able to use # Google Search via API. search = GoogleSearchAPIWrapper() ``` The `Tool` object represents a specific capability or function the system can use. In this case, it's a tool for performing Google searches. It is initialized with three parameters: - `name` parameter: This is a string that serves as a unique identifier for the tool. In this case, the name of the tool is "google-search.” - `func` parameter: This parameter is assigned the function that the tool will execute when called. In this case, it's the `run` method of the `search` object, which presumably performs a Google search. - `description` parameter: This is a string that briefly explains what the tool does. The description explains that this tool is helpful when you need to use Google to answer questions about current events. ```python tools = [ Tool( name = "google-search", func=search.run, description="useful for when you need to search google to answer questions about current events" ) ] ``` Next, we create an agent that uses our Google Search tool: - `initialize_agent()`: This function call creates and initializes an agent. An agent is a component that determines which actions to take based on user input. These actions can be using a tool, returning a response to the user, or something else. - `tools`: represents the list of `Tool` objects that the agent can use. - `agent="zero-shot-react-description"`: The "zero-shot-react-description" type of an Agent uses the ReAct framework to decide which tool to use based only on the tool's description. - `verbose=True`: when set to True, it will cause the Agent to print more detailed information about what it's doing. This is useful for debugging and understanding what's happening under the hood. - `max_iterations=6`: sets a limit on the number of iterations the Agent can perform before stopping. It's a way of preventing the agent from running indefinitely in some cases, which may have unwanted monetary costs. ```python agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True, max_iterations=6) ``` And now, we can check out the response: ```python response = agent("What's the latest news about the Mars rover?") print(response['output']) ``` You’ll see an output like the following. ```python > Entering new AgentExecutor chain... I need to find out the latest news about the Mars rover Action: google-search Action Input: "latest news Mars rover" Observation: The mission has concluded that the solar-powered lander has run out of energy after more than four years on the Red Planet. Dec 15, 2021 ... Mars Sample Return is going to have great stuff to choose from!” Get the Latest JPL News. SUBSCRIBE TO THE NEWSLETTER. NASA's


LangChain 101: from Zero to Hero

# LangChain 101: from Zero to Hero
Curiosity rover is discovering odd rocks halfway into a region called the "clay-bearing unit." ... This panorama of a location called "Teal Ridge" was ... 2 days ago ... LEGO Technic 42158 NASA Mars Rover Perseverance officially revealed as latest real-life space set [News]. Today LEGO has officially revealed ... Oct 19, 2022 ... NASA's Curiosity Mars rover used its Mast Camera, or Mastcam, to capture this panorama of a hill nicknamed ... Get the Latest JPL News. Latest Updates · Curiosity rover finds water-carved 'book' rock on Mars (photo) · Curiosity rover on Mars gets a brain boost to think (and move) faster · Curiosity ... Mar 7, 2023 ... 27, helps provide scientists with information about the particle sizes within the clouds. Sign up for National Breaking News Alerts. Sep 16, 2022 ... Since July, NASA's Perseverance rover has drilled and collected four slim cores of sedimentary rock, formed in what was once a lake on Mars. Nasa Mars lander study reveals 'main source of heat' on Red Planet · NASA/ESA/A. Simon (Goddard Space Flight Center) and M.H. Wong. All the latest content about Nasa Perseverance Mars rover from the BBC. ... James Tytko presents science news and we're putting tuberculosis under the ... Thought: I now know the final answer Final Answer: The latest news about the Mars rover is that the mission has concluded that the solar-powered lander has run out of energy after more than four years on the Red Planet. NASA's Curiosity rover is discovering odd rocks halfway into a region called the "clay-bearing unit." LEGO Technic 42158 NASA Mars Rover Perseverance has been officially revealed as the latest real-life space set. NASA's Curiosity Mars rover used its Mast Camera, or Mastcam, to capture a panorama of a hill nicknamed "Teal Ridge." NASA's Perseverance rover has drilled and collected four slim cores of sedimentary rock, formed in what was once a lake on Mars. A study of the Mars lander has revealed the 'main source of heat' on the Red Planet. > Finished chain. The latest news about the Mars rover is that the mission has concluded that the solar-powered lander has run out of energy after more than four years on the Red Planet. NASA's Curiosity rover is discovering odd rocks halfway into a region called the "clay-bearing unit." LEGO Technic 42158 NASA Mars Rover Perseverance has been officially revealed as the latest real-life space set. NASA's Curiosity Mars rover used its Mast Camera, or Mastcam, to capture a panorama of a hill nicknamed "Teal Ridge." NASA's Perseverance rover has drilled and collected four slim cores of sedimentary rock, formed in what was once a lake on Mars. A study of the Mars lander has revealed the 'main source of heat' on the Red Planet. ``` In summary, Agents in LangChain help decide which actions to take based on user input. The example demonstrates initializing and using a "zero-shot-react-description" agent with a Google search tool. --- ### Tools in LangChain LangChain provides a variety of tools for agents to interact with the outside world. These tools can be used to create custom agents that perform various tasks, such as searching the web, answering questions, or running Python code. In this section, we will discuss the different tool types available in LangChain and provide examples of creating and using them. In our example, two tools are being defined for use within a LangChain agent: a Google Search tool and a Language Model tool acting specifically as a text summarizer. The Google Search tool, using the GoogleSearchAPIWrapper, will handle queries that involve finding recent event


LangChain 101: from Zero to Hero

# LangChain 101: from Zero to Hero
information. The Language Model tool leverages the capabilities of a language model to summarize texts. These tools are designed to be used interchangeably by the agent, depending on the nature of the user's query. Let’s import the necessary libraries. ```python from langchain.llms import OpenAI from langchain.agents import Tool from langchain.utilities import GoogleSearchAPIWrapper from langchain.prompts import PromptTemplate from langchain.chains import LLMChain from langchain.agents import initialize_agent, AgentType ``` We then instantiate a `LLMChain` specifically for text summarization. ```python llm = OpenAI(model="text-davinci-003", temperature=0) prompt = PromptTemplate( input_variables=["query"], template="Write a summary of the following text: {query}" ) summarize_chain = LLMChain(llm=llm, prompt=prompt) ``` Next, we create the tools that our agent will use. ```python # remember to set the environment variables # “GOOGLE_API_KEY” and “GOOGLE_CSE_ID” to be able to use # Google Search via API. search = GoogleSearchAPIWrapper() tools = [ Tool( name="Search", func=search.run, description="useful for finding information about recent events" ), Tool( name='Summarizer', func=summarize_chain.run, description='useful for summarizing texts' ) ] ``` We are now ready to create our agent that leverages two tools. ```python agent = initialize_agent( tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True ) ``` Let’s run the agent with a question about summarizing the latest news about the Mars rover. ```python response = agent("What's the latest news about the Mars rover? Then please summarize the results.") print(response['output']) ``` You should see an output like the following. ```python > Entering new AgentExecutor chain... I should search for recent news about the Mars rover and then summarize the results. Action: Search Action Input: Latest news about the Mars rover Observation: Mars 2020 Perseverance Rover · The largest and most capable rover ever sent to Mars. ... Curiosity Rover · Measures Mars' atmosphere to understand its climate ... Dec 15, 2021 ... Mars Sample Return is going to have great stuff to choose from!” Get the Latest JPL News. SUBSCRIBE TO THE NEWSLETTER. The mission has concluded that the solar-powered lander has run out of energy after more than four years on the Red Planet. Oct 19, 2022 ... NASA's Curiosity Mars rover used its Mast Camera, or Mastcam, to capture this panorama of a hill nicknamed ... Get the Latest JPL News. NASA's Mars 2020 Perseverance rover will look for signs of past microbial life, cache rock and soil samples, and prepare for future human exploration. Latest Updates · Curiosity rover finds water-carved 'book' rock on Mars (photo) · Curiosity rover on Mars gets a brain boost to think (and move) faster · Curiosity ... Feb 8, 2023 ... Curiosity Rover Finds New Clues to Mars' Watery Past ... at Gediz Vallis Ridge twice last year but could only survey it from a distance. Sep 16, 2022 ... Since July, NASA's Perseverance rover has drilled and collected four slim cores of sedimentary rock, formed in what was once a lake on Mars. Mar 7, 2023 ... 27, helps provide scientists with information about the particle sizes within the clouds. Sign up for National Breaking News Alerts. All the latest content about Nasa Perseverance Mars rover from the BBC. ... James Tytko presents science news and we're putting tuberculosis under the ... Thought: I should summarize the results of the search. Action: Summarizer Action Input: Mars 2020 Perseverance Rover is the largest and most capable rover ever sent to Mars. It measures Mars' atmosphere to understand its climate and has run out of energy after more than four years on the Red Planet. NASA's Curiosity Mars rover used its Mast Camera to capture a panorama of a hill nicknamed "Book Rock". NASA's Mars 2020 Perseverance rover will look for signs of past microbial life, cache rock and soil


LangChain 101: from Zero to Hero

# LangChain 101: from Zero to Hero
samples, and prepare for future human exploration. Curiosity rover finds water-carved 'book' rock on Mars, Curiosity rover on Mars gets a brain boost to think (and move) faster, Curiosity Rover Finds New Clues to Mars' Watery Past, and NASA's Perseverance rover has drilled and collected four slim cores of sedimentary rock. Observation: NASA's Mars 2020 Perseverance rover is the largest and most capable rover ever sent to Mars. It has been on the Red Planet for more than four years, measuring the atmosphere to understand its climate and searching for signs of past microbial life. The Curiosity rover has captured a panorama of a hill nicknamed "Book Rock" and has been given a brain boost to think and move faster. It has also found new clues to Mars' watery past. The Perseverance rover has drilled and collected four slim cores of sedimentary rock, which will be used to cache rock and soil samples and prepare for future human exploration. Thought: I now know the final answer. Final Answer: NASA's Mars 2020 Perseverance rover is the largest and most capable rover ever sent to Mars. It has been on the Red Planet for more than four years, measuring the atmosphere to understand its climate and searching for signs of past microbial life. The Curiosity rover has captured a panorama of a hill nicknamed "Book Rock" and has been given a brain boost to think and move faster. It has also found new clues to Mars' watery past. The Perseverance rover has drilled and collected four slim cores of sedimentary rock, which will be used to cache rock and soil samples and prepare for future human exploration. > Finished chain. NASA's Mars 2020 Perseverance rover is the largest and most capable rover ever sent to Mars. It has been on the Red Planet for more than four years, measuring the atmosphere to understand its climate and searching for signs of past microbial life. The Curiosity rover has captured a panorama of a hill nicknamed "Book Rock" and has been given a brain boost to think and move faster. It has also found new clues to Mars' watery past. The Perseverance rover has drilled and collected four slim cores of sedimentary rock, which will be used to cache rock and soil samples and prepare for future human exploration. ``` Notice how the agents used at first the “Search” tool to look for recent information about the Mars rover and then used the “Summarizer” tool for writing a summary. LangChain provides an expansive toolkit that integrates various functions to improve the functionality of conversational agents. Here are some examples: - `SerpAPI`: This tool is an interface for the SerpAPI search engine, allowing the agent to perform robust online searches to pull in relevant data for a conversation or task. - `PythonREPLTool`: This unique tool enables the writing and execution of Python code within an agent. This opens up a wide range of possibilities for advanced computations and interactions within the conversation. If you wish to add more specialized capabilities to your LangChain conversational agent, the platform offers the flexibility to create `custom tools`. By following the general tool creation guidelines provided in the LangChain documentation, you can develop tools tailored to the specific needs of your application. ### Conclusion As we conclude our comprehensive exploration of LangChain, it's clear that this toolset presents a remarkable opportunity for both novice and seasoned AI developers. Through our journey, we've unpacked key concepts, from library installation to configuring OpenAI credentials, and dove into the intricacies of generating predictions using language models. Whether you're just starting with LangChain


LangChain 101: from Zero to Hero

# LangChain 101: from Zero to Hero
or looking to enhance your existing AI workflow, we hope this exploration has provided you with a solid foundation to build upon. For a more in-depth explanation of all the LangChain concepts and several projects examples with it, consider taking the full course, starting with the lesson on large language models. Happy coding!


# Conversation Intelligence: Gong.io Open-Source Alternative AI Sales Assistant
In this lesson we will explore how LangChain, Deep Lake, and GPT-4 can be used to develop a sales assistant able to give advice to salesman, taking into considerations internal guidelines. ### Introduction This particular article provides an in-depth view of a sales call assistant that connects you to a chatbot that understands the context of your conversation. A great feature of SalesCopilot is its ability to detect potential objections from customers and deliver recommendations on how best to address them. The article is a journey that reveals the challenges faced and the solutions discovered during the creation of a the project. You'll learn about the two distinct text-splitting approaches that didn't work and how these failures paved the way for an effective solution. Firstly, the authors tried to rely solely on the LLM, but they encountered issues such as response inconsistency and slow response times with GPT-4. Secondly, they naively split the custom knowledge base into chunks, but this approach led to context misalignment and inefficient results. After these unsuccessful attempts, a more intelligent way of splitting the knowledge base based on its structure was adopted. This change greatly improved the quality of responses and ensured better context grounding for LLM responses. This process is explained in detail, helping you grasp how to navigate similar challenges in your own AI projects. Next, the article explores how SalesCopilot was integrated with Deep Lake. This integration enhanced SalesCopilot's capabilities by retrieving the most relevant responses from a custom knowledge base, thereby creating a persistent, efficient, and highly adaptable solution for handling customer objections. By the end of this lesson, you'll learn how to utilize LLMs, how to intelligently split your knowledge base, and integrate it with a vector database like Deep Lake for optimal performance. [https://www.activeloop.ai/resources/conversation-intelligence-gong-io-open-source-alternative-ai-sales-assistant/](https://www.activeloop.ai/resources/conversation-intelligence-gong-io-open-source-alternative-ai-sales-assistant/) In the next lesson, we’ll see how to leverage both LLMs and image generation models to write a creative children’s picture book.


Recreating the Bing Chatbot

# Recreating the Bing Chatbot
### Introduction While the Large Language Models (LLMs) possess impressive capabilities, they have certain limitations that can present challenges when deploying them in a production environment. The hallucination problem makes them answer certain questions wrongly with high confidence. This issue can be attributed to various factors, one of which is that their training process has a cut-off date. So, these models do not have access to events preceding that date. A workaround approach is to present the required information to the model and leverage its reasoning capability to find/extract the answer. Furthermore, it is possible to present the top-matched results a search engine returns as the context for a user’s query. This lesson will explore the idea of finding the best articles from the Internet as the context for a chatbot to find the correct answer. We will use LangChain’s integration with Google Search API and the Newspaper library to extract the stories from search results. This is followed by choosing and using the most relevant options in the prompt. Notice that the same pipeline could be done with the Bing API, but we’ll use the Google Search API in this project because it is used in other lessons of this course, thus avoiding creating several keys for the same functionality. Please refer to the following [tutorial](https://levelup.gitconnected.com/api-tutorial-how-to-use-bing-web-search-api-in-python-4165d5592a7e) (or [Bing Web Search API](https://www.microsoft.com/en-us/bing/apis/bing-web-search-api) for direct access) on obtaining the Bing Subscription Key and using the LangChain Bing search [wrapper](https://python.langchain.com/en/latest/modules/agents/tools/examples/bing_search.html?highlight=Bing). What we are going to do is explained in the following diagram. ![Screenshot 2023-06-09 at 13.39.38.png](Recreating%20the%20Bing%20Chatbot%20c546f88539d34fbe8dcccd8a1f010669/Screenshot_2023-06-09_at_13.39.38.png) The user query is used to extract relevant articles using a search engine (e.g. Bing or Google Search), which are then split into chunks. We then compute the embeddings of each chunk, rank them by cosine similarity with respect to the embedding of the query, and put the most relevant chunks into a prompt to generate the final answer, while also keeping track of the sources. ### Ask Trending Questions Let’s start this lesson by seeing an example. The following piece must be familiar by now. It uses the OpenAI `GPT-3.5-turbo` model to create an assistant to answer questions. We will ask the model to name the latest Fast & Furious movie, released recently. Therefore, the model couldn’t have seen the answer during the training. Remember to install the required packages with the following command: `pip install langchain==0.0.208 deeplake openai tiktoken`. ```python from langchain import LLMChain, PromptTemplate from langchain.llms import OpenAI llm = OpenAI(temperature=0) template = """You are an assistant that answers the following question correctly and honestly: {question}\n\n""" prompt_template = PromptTemplate(input_variables=["question"], template=template) question_chain = LLMChain(llm=llm, prompt=prompt_template) question_chain.run("what is the latest fast and furious movie?") ``` ``` The latest Fast and Furious movie is Fast & Furious 9, which is set to be released in May 2021. ``` The response shows that the model references the previous movie title as the answer. This is because the new movie (10th sequel) has yet to be released in its fictional universe! Let’s fix the problem.


Recreating the Bing Chatbot

# Recreating the Bing Chatbot
## Google API
Before we start, let’s set up the API Key and a custom search engine. If you don’t have the keys from the previous lesson, head to the [Google Cloud console](https://console.cloud.google.com/apis/credentials) and generate the key by pressing the CREATE CREDENTIALS buttons from the top and choosing API KEY. Then, head to the [Programmable Search Engine](https://programmablesearchengine.google.com/controlpanel/create) dashboard and remember to select the “Search the entire web” option. The Search engine ID will be visible in the details. You might also need to enable the “Custom Search API” service under the Enable APIs and services. (You will receive the instruction from API if required) Now we can set the environment variables for both Google and OpenAI APIs. ```python import os os.environ["GOOGLE_CSE_ID"] = "<Custom_Search_Engine_ID>" os.environ["GOOGLE_API_KEY"] = "<Google_API_Key>" os.environ["OPENAI_API_KEY"] = "<OpenAI_Key>" ```


Recreating the Bing Chatbot

# Recreating the Bing Chatbot
## Get Search Results
This section uses LangChain’s `GoogleSearchAPIWrapper` class to receive search results. It works in combination with the `Tool` class that presents the utilities for agents to help them interact with the outside world. In this case, creating a tool out of any function, like `top_n_results` is possible. The API will return the page’s title, URL, and a short description. ```python from langchain.tools import Tool from langchain.utilities import GoogleSearchAPIWrapper search = GoogleSearchAPIWrapper() TOP_N_RESULTS = 10 def top_n_results(query): return search.results(query, TOP_N_RESULTS) tool = Tool( name = "Google Search", description="Search Google for recent results.", func=top_n_results ) query = "What is the latest fast and furious movie?" results = tool.run(query) for result in results: print(result["title"]) print(result["link"]) print(result["snippet"]) print("-"*50) ``` ``` Fast & Furious movies in order | chronological and release order ... https://www.radiotimes.com/movies/fast-and-furious-order/ Mar 22, 2023 ... Fast & Furious Presents: Hobbs & Shaw (2019); F9 (2021); Fast and Furious 10 (2023). Tokyo Drift also marks the first appearance of Han Lue, a ... -------------------------------------------------- FAST X | Official Trailer 2 - YouTube https://www.youtube.com/watch?v=aOb15GVFZxU Apr 19, 2023 ... Fast X, the tenth film in the Fast & Furious Saga, launches the final ... witnessed it all and has spent the last 12 years masterminding a ... -------------------------------------------------- Fast & Furious 10: Release date, cast, plot and latest news on Fast X https://www.radiotimes.com/movies/fast-and-furious-10-release-date/ Apr 17, 2023 ... Fast X is out in cinemas on 19th May 2023 – find out how to rewatch all the Fast & Furious movies in order, and read our Fast & Furious 9 review ... -------------------------------------------------- Fast & Furious - Wikipedia https://en.wikipedia.org/wiki/Fast_%26_Furious The main films are known as The Fast Saga. Universal expanded the series to include the spin-off film Fast & Furious Presents: Hobbs & Shaw (2019), ... -------------------------------------------------- How many 'Fast & Furious' movies are there? Here's the list in order. https://www.usatoday.com/story/entertainment/movies/2022/07/29/fast-and-furious-movies-order-of-release/10062943002/ Jul 29, 2022 ... There are currently nine films in the main "Fast and Furious" franchise, with the 10th, "Fast X," set to release on May 19, 2023. There are ... -------------------------------------------------- How to Watch Fast and Furious Movies in Chronological Order - IGN https://www.ign.com/articles/fast-and-furious-movies-in-order Apr 6, 2023 ... Looking to go on a Fast and Furious binge before the next movie comes out? ... This is the last Fast film with Paul Walker's Brian O'Conner, ... -------------------------------------------------- 'Fast and Furious 10': Everything We Know So Far https://www.usmagazine.com/entertainment/pictures/fast-and-furious-10-everything-we-know-so-far/ 7 days ago ... Fast X will be the second-to-last film in the blockbuster franchise, and Dominic Toretto's next adventure is set to be one of the biggest so far ... -------------------------------------------------- Latest 'Fast & Furious' Movie Leads Weekend Box Office - WSJ https://www.wsj.com/articles/latest-fast-furious-movie-leads-weekend-box-office-11624815451 Jun 27, 2021 ... “F9,” however, offers the clearest test yet on the post-pandemic habits of moviegoers. The movie is the most-anticipated title released since ... -------------------------------------------------- Fast & Furious Movies In Order: How to Watch Fast Saga ... https://editorial.rottentomatoes.com/guide/fast-furious-movies-in-order/ After that, hop to franchise best Furious 7. Follow it up with The Fate of the Furious and spin-off Hobbs & Shaw and then the latest: F9 and Fast X. See below ... -------------------------------------------------- The looming release of the latest "Fast and Furious" movie heightens ... https://www.cbsnews.com/losangeles/news/looming-release-of-latest-fast-and-furious-movie-heightens-concerns-over-street-racing-takeovers/ 17 hours ago ... With the latest installment of the "Fast and Furious" franchise set to hit theaters this weekend, local law enforcement is banding together ... -------------------------------------------------- ``` Now, we use the `results` variable’s `link` key to download and parse the contents. The newspaper library takes care of everything. However, it might be unable to capture some contents in certain situations, like anti-bot mechanisms or having a file as a result. ```python import newspaper pages_content = []


Recreating the Bing Chatbot

# Recreating the Bing Chatbot
## Get Search Results
for result in results: try: article = newspaper.Article(result["link"]) article.download() article.parse() if len(article.text) > 0: pages_content.append({ "url": result["link"], "text": article.text }) except: continue ```


Recreating the Bing Chatbot

# Recreating the Bing Chatbot
## Process the Search Results
We now have the top 10 results from the Google search. (Honestly, who looks at Google’s second page?) However, it is not efficient to pass all the contents to the model because of the following reasons: - The model’s context length is limited. - It will significantly increase the cost if we process all the search results. - In almost all cases, they share similar pieces of information. So, let’s find the most relevant results, Incorporating the LLMs embedding generation capability will enable us to find contextually similar content. It means converting the text to a high-dimensionality tensor that captures meaning. The cosine similarity function can find the closest article with respect to the user’s question. It starts by splitting the texts using the `RecursiveCharacterTextSplitter` class to ensure the content lengths are inside the model’s input length. The `Document` class will create a data structure from each chunk that enables saving metadata like `URL` as the source. The model can later use this data to know the content’s location. ```python from langchain.text_splitter import RecursiveCharacterTextSplitter from langchain.docstore.document import Document text_splitter = RecursiveCharacterTextSplitter(chunk_size=4000, chunk_overlap=100) docs = [] for d in pages_content: chunks = text_splitter.split_text(d["text"]) for chunk in chunks: new_doc = Document(page_content=chunk, metadata={ "source": d["url"] }) docs.append(new_doc) ``` The subsequent step involves utilizing the OpenAI API's `OpenAIEmbeddings` class, specifically the `.embed_documents()` method for search results and the `.embed_query()` method for the user's question, to generate embeddings. ```python from langchain.embeddings import OpenAIEmbeddings embeddings = OpenAIEmbeddings(model="text-embedding-ada-002") docs_embeddings = embeddings.embed_documents([doc.page_content for doc in docs]) query_embedding = embeddings.embed_query(query) ``` Lastly, the `get_top_k_indices` function accepts the content and query embedding vectors and returns the index of top K candidates with the highest cosine similarities to the user's request. Later, we use the indexes to retrieve the best-fit documents. ```python import numpy as np from sklearn.metrics.pairwise import cosine_similarity def get_top_k_indices(list_of_doc_vectors, query_vector, top_k): # convert the lists of vectors to numpy arrays list_of_doc_vectors = np.array(list_of_doc_vectors) query_vector = np.array(query_vector) # compute cosine similarities similarities = cosine_similarity(query_vector.reshape(1, -1), list_of_doc_vectors).flatten() # sort the vectors based on cosine similarity sorted_indices = np.argsort(similarities)[::-1] # retrieve the top K indices from the sorted list top_k_indices = sorted_indices[:top_k] return top_k_indices top_k = 2 best_indexes = get_top_k_indices(docs_embeddings, query_embedding, top_k) best_k_documents = [doc for i, doc in enumerate(docs) if i in best_indexes] ```


Recreating the Bing Chatbot

# Recreating the Bing Chatbot
## Chain with Source
Finally, we used the selected articles in our prompt (using the `stuff` method) to assist the model in finding the correct answer. LangChain provides the `load_qa_with_sources_chain()` chain, which is designed to accept a list of `input_documents` as a source of information and a `question` argument which is the user’s question. The final part involves preprocessing the model’s response to extract its answer and the sources it utilized. ```python from langchain.chains.qa_with_sources import load_qa_with_sources_chain from langchain.llms import OpenAI chain = load_qa_with_sources_chain(OpenAI(temperature=0), chain_type="stuff") response = chain({"input_documents": best_k_documents, "question": query}, return_only_outputs=True) response_text, response_sources = response["output_text"].split("SOURCES:") response_text = response_text.strip() response_sources = response_sources.strip() print(f"Answer: {response_text}") print(f"Sources: {response_sources}") ``` ``` Answer: The latest Fast and Furious movie is Fast X, scheduled for release on May 19, 2023. Sources: https://www.radiotimes.com/movies/fast-and-furious-10-release-date/, https://en.wikipedia.org/wiki/Fast_%26_Furious ``` The use of search results helped the model find the correct answer, even though it never saw it before during the training stage. The question and answering chain with source also provides information regarding the sources utilized by the model to derive the answer. --- ### Conclusion In this lesson, we learned how to utilize external knowledge from a search engine to make a robust application. The context can be presented from various sources such as PDFs, text documents, CSV files, or even the Internet! We used Google search results as the source of information, and it enabled the model to respond to the question it previously couldn’t answer correctly. In the next lesson, we’ll see how to build a bot that leverages multiple tools to answer questions. --- You can find the code of this lesson in this online [Notebook](https://drive.google.com/file/d/1qURJ78Kfg1Xq8mo6UU6TPBS5MZrXniSi/view?usp=sharing).


Introduction to Agents

# Introduction to Agents
In this module, we will have a series of lessons that explore the power of tools in LangChain and demonstrate how they can be combined to tackle different challenges and enhance information retrieval. The agents are intelligent entities and components interacting with the language model and other tools to fulfill a task. Agents act as the unifying force that combines the concepts we explored in previous lessons, including chains, prompts, memory, and tools. They have the capability to select the appropriate tools from a given list or formulate a plan and incorporate suitable tools at each step to get the job done. It enables agents to behave dynamically with respect to the user’s query instead of using a fixed prompt for every interaction. We will learn how. to create autonomous chatbots that can plan and reason. Here are the lessons of this module, along with a brief description of them: - **What are Agents: Agents as Content Generators and Reasoning Engines:** In the dynamic world of artificial intelligence, LangChain and Large Language Models (LLMs) revolutionize data analysis, information synthesis, and content generation. Central to their functionality are intelligent systems called Agents, which leverage LLMs to perform complex tasks and make informed decisions. These agents act as reasoning engines or planners, utilizing LLMs to determine actions and facilitate a wide range of objectives. - **Exploring the Fascinating World of Autonomous Agents: A Closer Look at AutoGPT and BabyAGI:** The exciting developments in the realm of autonomous agents further propel the AI landscape forward. Two remarkable projects, AutoGPT and BabyAGI, gain substantial attention and acclaim. AutoGPT, an open-source initiative utilizing GPT-4, employs a structured approach to navigate the internet, formulate subtasks, and initiate new agents. This project has gained significant popularity among the GitHub community, indicating its potential impact. Similarly, BabyAGI integrates GPT-4, a vector store, and LangChain to create tasks based on prior outcomes and set goals. These advancements mark a glimpse into the future of autonomous agents. - **Using AutoGPT with LangChain:** AutoGPT, in particular, becomes a subject of exploration in subsequent lessons. These lessons delve into the mechanics of AutoGPT, demonstrating how it utilizes multiple tools and memory systems to perform and manage tasks autonomously. This detailed examination provides insights into the inner workings of this innovative autonomous agent, shedding light on its capabilities and potential applications. - **Agent Simulation Projects: CAMEL and Generative Agents:** The LangChain framework plays a central role in the development of autonomous agents. Projects like CAMEL and Generative Agents showcase the paradigm shift from traditional frameworks. CAMEL brings unique agents with distinct personalities into a harmonious ecosystem, fostering collaboration and synergy among them. On the other hand, Generative Agents encapsulate the essence of human behavior, integrating LLMs with computational agents to create dynamic and interactive simulations. These projects represent strides toward enabling compelling simulations of human behavior, pushing the boundaries of what autonomous agents can achieve. - **Building Autonomous Agents to Create Analysis Reports:** Within the LangChain framework, the concept of "Plan and Execute" agents emerges as a powerful approach for complex long-term planning. By separating high-level planning from immediate execution, these agents are capable of generating insightful analysis reports based on retrieved documents. Exploring the fundamentals and implementation details of the "Plan and Execute" framework equips developers with the skills to create autonomous agents and generate analysis reports effectively. - **Current Insights and Trends of Agents:** As the AI landscape evolves, various trends and developments shape the future of AI agents. The emergence of AutoGPT, with its pursuit of full autonomy, captures attention and popularity. Additionally, the separation of high-level planning and execution


Introduction to Agents

# Introduction to Agents
in "Plan-and-Execute" agents opens doors for improved efficiency and performance. The integration of plug-ins and code interpreters in GPT-4 enhances its abilities, enabling tasks such as data analysis, visualization, and internet interaction. Happy learning!


# Exploring the Fascinating World of Autonomous Agents: A Closer Look at AutoGPT and BabyAGI
### Introduction AutoGPT and BabyAGI are two exciting developments in the world of autonomous agents, which are AI systems designed to carry out tasks without needing constant human guidance. These innovative agents are making waves due to their ability to work independently to achieve a specific objective. Their introduction has led to unreal hype with over 100k stars on GitHub, and they've been heralded as a peek into the future. AutoGPT, an open-source initiative, employs GPT-4 to sift through the internet in a structured manner, formulate subtasks, and initiate new agents. This project has quickly become a sensation, marked by its rapid growth in popularity among the GitHub community. On the other hand, BabyAGI functions similarly via the integration of GPT-4, a vector store, and LangChain. It creates tasks based on prior outcomes and a set goal. While Auto GPT and similar technologies are rapidly evolving, developers are also building and improving on these models. The intrigue surrounding these autonomous agents stems from a few key factors: - **Limited human involvement**: Unlike traditional systems like ChatGPT that require human prompts, autonomous agents such as AutoGPT and BabyAGI require minimal human intervention. - **Diverse applications**: The potential use cases for these autonomous agents are vast, spanning from personal assistants and problem solvers to automated aids for tasks like email management and prospecting. - **Swift progress**: The rapid pace of growth and interest in these projects highlights the significant potential of autonomous agents to revolutionize the AI landscape and beyond. To effectively utilize these agents, we need to start by setting long-term goals tailored to the project's specific needs. These goals might encompass generating high-quality natural language text, answering questions with accuracy and context, and learning from user interactions for continuous performance improvement. ### **What is AutoGPT?** AutoGPT, a type of autonomous AI agent, is designed to carry out tasks until they are solved. It brings three key features to the table: - Firstly, it's connected to the internet, allowing for real-time research and information retrieval. - Secondly, it can self-prompt, generating a list of sub-tasks to accomplish a given task. - Lastly, it can execute tasks, including spinning up other AI agents. While the first two features have been successful, the execution aspect has met with some challenges, including getting caught in loops or wrongly assuming a task has been completed. The initial conception of AutoGPT was as a general autonomous agent capable of doing anything. However, this wide breadth of application seemed to dilute its effectiveness. As a result, a shift has been observed in the AutoGPT space, with developers starting to build specialized agents. These agents are designed to perform specific tasks effectively and efficiently, making them more practically useful. ### How AutoGPT work? The concept behind AutoGPT is simple yet profound. Rather than only generating text in response to prompts like plain ChatGPT and GPT-4, AutoGPT is designed to generate, prioritize, and execute tasks. These tasks can range in complexity and are not confined to mere text generation. > **AutoGPT can understand the overall goal, break it down into subtasks, execute those tasks, and dynamically adjust its actions based on the ongoing context.** > AutoGPT uses plugins for internet browsing and other forms of access to gather necessary information. The outside memory serves as its context-aware module, enabling it to evaluate its current situation, generate new tasks, self-correct if needed, and add new tasks to its queue. This allows for a dynamic flow of operations where tasks are executed and constantly reprioritized based on the context and situation. This understanding of the task, the environment, and the goal


# Exploring the Fascinating World of Autonomous Agents: A Closer Look at AutoGPT and BabyAGI
at each point in the process transforms AutoGPT from a passive text generator into an active, goal-oriented agent. While this could open up new vistas of AI-powered productivity and problem-solving, it also ushers in new challenges regarding control, misuse, and unforeseen consequences. ### **What is BabyAGI?** **Baby AGI** works similarly to autoGPT. It operates in an infinite loop, pulling tasks from a list, executing them, enriching the results, and creating new tasks based on the previous task's objective and results. The concept is similar, but the specific implementation is different. Let’s see it in more detail. ### How BabyAGI works BabyAGI operates in a loop that revolves around four main sub-agents: the Execution Agent, the Task Creation Agent, the Prioritization Agent, and the Context Agent. 1. **Execution Agent**: This is the agent that executes the tasks. It takes an objective and a task as parameters, constructs a prompt based on these inputs, and feeds it to a LLM (e.g. GPT4). The LLM then returns a result, which is the outcome of executing the task. 2. **Task Creation Agent**: Here, the system creates new tasks based on the previously executed task's objective and result. The agent uses a prompt that includes the task description and the current task list and feeds this prompt to the LLM, which generates a list of new tasks. These tasks are returned as a list of dictionaries, each dictionary representing a new task. 3. **Prioritization Agent**: This function is responsible for prioritizing the tasks in the tasks list. 4. **Context Agent**: The scope of this agent is to collect the results from the Execution Agent and merge them with all the other intermediate results from the previous executions of the Execution Agent. ![ Image Credit: [https://github.com/yoheinakajima/babyagi/tree/main](https://github.com/yoheinakajima/babyagi/tree/main)](Exploring%20the%20Fascinating%20World%20of%20Autonomous%20Agen%20af06870e912348fbb851027d4d857c9f/babyagi.png) Image Credit: [https://github.com/yoheinakajima/babyagi/tree/main](https://github.com/yoheinakajima/babyagi/tree/main) **We can conclude the following about BabyAGI** 1. BabyAGI is an autonomous AI agent designed to execute tasks, generate new tasks based on previous task results, and re-prioritize tasks in real time. This showcases the potential of AI-powered language models to perform tasks autonomously within various constraints and contexts. 2. The system utilizes the power of GPT-4 for task execution, a vector database for efficient search and storage of task-related data, and the LangChain framework to enhance the decision-making processes. The integration of these technologies allows BabyAGI to interact with its environment and perform tasks efficiently. 3. A key feature of the system is its task management. BabyAGI maintains a task list for managing and prioritizing tasks. The system autonomously generates new tasks based on completed results and dynamically re-prioritizes the task list, highlighting the adaptability of AI-powered language models. 4. By using GPT-4 and LangChain's capabilities, BabyAGI cannot only complete tasks but also enrich and store results in the database. The agent thus **becomes a learning system** that can adapt and respond to new information and priorities. ### A Code Example of Using BabyAGI Although BabyAGI uses specific vector stores and model providers, one of the benefits of implementing it with LangChain is that you can easily swap those out for different options. In this implementation, we use a FAISS vector store. <aside> 💡 Langchain has recently (as of August 2023) moved some classes from "langchain.experimental" to another library called "library_experimental", in an attempt to make the "langchain" library smaller. If you try the following code with the suggested version “langchain==0.0.208” it should work fine, but if you want to run it with the latest langchain version then you have to (1) install the experimental library with “pip install langchain-experimental” and (2) replace all the occurrences of “langchain.experimental” with “langchain_experimental”. </aside> Let’s set up the API