• Emergence
  • Posts
  • Building An AI Agent That Has Access To The Internet Using Python And ChatGPT

Building An AI Agent That Has Access To The Internet Using Python And ChatGPT

In the rapidly evolving world of artificial intelligence, ChatGPT has emerged as a groundbreaking language model, offering unparalleled capabilities for natural language understanding and generation. In this article, we delve into the exciting prospect of building an AI agent that harnesses the power of ChatGPT while also having access to the internet.

As we embark on the journey to build our AI agent, we will utilize Langchain, a cutting-edge library specifically designed to facilitate the integration of large language models (LLMs) with additional computational resources and knowledge sources. Langchain empowers developers to create more sophisticated, context-aware applications by seamlessly combining the capabilities of LLMs like ChatGPT with other tools, such as web-based APIs, databases, or custom algorithms.

By leveraging Langchain, our AI agent will not only benefit from the advanced natural language understanding and generation abilities of ChatGPT, but also gain access to a wealth of external information and processing capabilities. This potent combination enables the creation of AI agents that can tackle complex tasks, provide in-depth insights, and offer highly personalized user experiences.

In this example, you’ll need to install the SerpAPI Python package as well. To do so, use the following command:

pip install google-search-results

Next, set the necessary environment variables in your Python script:

import os
os.environ["SERPAPI_API_KEY"] = "your_api_key_here"

You can alternatively do that on the CLI:

export SERPAPI_API_KEY="your_api_key_here"      

Make sure to replace "your_api_key_here" with your actual SerpAPI API key. Your can get a key by registering on https://serpapi.com/

First, we will import the necessary components from the Langchain library to build our AI agent. We need to load the tools and resources that will work alongside the large language model (LLM) to create a more powerful and context-aware AI agent. To accomplish this, we import the load_toolsfunction:

from langchain.agents import load_tools

Next, we need to set up the AI agent with the specified LLM and tools. To do this, we import the initialize_agent function:

from langchain.agents import initialize_agent

This function takes care of configuring the agent to work seamlessly with the LLM and the additional resources, making it easy for developers to create sophisticated AI applications.

Finally, we’ll import the OpenAI class from the Langchain library to work with OpenAI's LLMs, such as ChatGPT:

from langchain.llms import OpenAI

The OpenAI class provides a high-level interface for integrating ChatGPT's natural language understanding and generation capabilities into our AI agent.

With these components in place, we are now ready to move forward with creating an AI agent that leverages the power of ChatGPT and external resources for enhanced functionality and context-awareness.

Now, let’s configure the AI agent using the Langchain library, with ChatGPT as the LLM and additional tools to enhance the agent’s capabilities. We will follow these steps:

  1. Load the language model: First, we’ll create an instance of the OpenAIclass, which represents the ChatGPT language model. We set the temperature parameter to 0 for deterministic output:

llm = OpenAI(temperature=0)

2. Load the tools: Next, we’ll load the external tools we want our AI agent to use alongside ChatGPT. In this case, we’re loading the serpapi and llm-mathtools. The llm-math tool requires the LLM instance, so we pass it using the llm parameter:

tools = load_tools(["serpapi", "llm-math"], llm=llm)

3. Initialize the agent: Finally, we’ll initialize the AI agent by providing the loaded tools, the ChatGPT instance (llm), and specifying the agent type as zero-shot-react-description. The verbose parameter set to True will enable additional log output during the agent's operation:

agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True)

By completing these steps, we have successfully set up an AI agent that utilizes ChatGPT and additional tools to provide enhanced functionality and context-aware responses.

Now let’s test our powerful AI agent ! We can do it like this:

agent.run("What was the high temperature in Paris, France yesterday in Celsius?
 What is that number raised to the 0.023 power?")

Here is the response log in our console:

Amazing right ? Here is the step-by-step process the AI agent followed to find the high temperature in Paris, France yesterday in Celsius and then raise that number to the 0.023 power.

  1. Entering new AgentExecutor chain: The agent begins its execution chain, aiming to find the required information and perform the necessary calculations.

  2. Search: The agent performs a search to find the high temperature in Paris, France yesterday.

  3. Observation: The agent finds that the maximum temperature was 63 °F.

  4. Thought: The agent realizes it needs to convert the temperature from Fahrenheit to Celsius.

  5. Calculator: The agent uses a calculator to perform the conversion.

  6. Observation: The agent finds that the temperature in Celsius was 17.22222222222222.

  7. Thought: The agent now needs to raise the temperature to the 0.023 power.

  8. Calculator: The agent uses the calculator again to perform the power operation.

  9. Observation: The agent finds that the result of the operation is 1.0676528196018449.

  10. Thought: The agent now knows the final answer and is ready to share it.

  11. Final Answer: The high temperature in Paris, France yesterday in Celsius was 17.22222222222222, and when raised to the 0.023 power, the result is 1.0676528196018449.