episyche logo

Episyche

Open Ai/Langchain/

How to connect Google search to ChatGPT using Langchain?

Published on

How to connect Google search to ChatGPT using Langchain?
Open Ai's knowledge database is limited to 2021. If you want to know information or news after 2021 it's not possible. To overcome this problem we use the Langchain python library to connect open ai text generative AI to Google search using SerpAPI.
Introduction :

Since the introduction of chatGPT text-based generative AI developed by Open Ai uses pre-trained neural networks to generate more personalized responses. The Ai model has been trained on a vast amount of data including text, articles, news articles, and websites.

Although it’s trained on huge amounts of data its knowledge database is limited to 2021. No recent or latest data is not updated on the Ai model. Thus I can’t ask a question happening after 2021 simply it says the data it can access is limited to 2021.

In conclusion, ChatGPT is an impressive technology that has the potential to revolutionize the way we interact with AI systems. With its ability to understand natural language, personalize responses, and continually learn and improve, it has opened up exciting opportunities for businesses and individuals alike.


Flow Diagram :



Steps :

Step 1: Generate Open ai API key

Step 2: Generate SerpAPI API key

We can’t access Google Search directly using API because they don’t provide API access. SerpAPI is a third API service that provides access to the Google search engine via API.

  • Navigate to https://serpapi.com/

  • Signup for an account.

  • Purchase an API key.

  • copy the API key and store it in a secure place.

Step 3: Install Python packages

  • Install the following Python packages

  • Open AI

pip install openai
  • Langchain

pip install langchain

Step 4: Search snippet setup

  • Create new Python file chat_gpt_search.py

  • Imported the required packages

from langchain.agents import load_tools from langchain.agents import initialize_agent from langchain.agents import AgentType from langchain.llms import OpenAI
  • Python code to connect Open Ai API with Google search.

  • Paste your Open Ai key and SerpAPI key

llm = OpenAI(openai_api_key="", temperature=0.9) tools = load_tools(["serpapi", "llm-math"], llm=llm, serpapi_api_key="") agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True) question = "Give me details about ceo of episyche" a = agent.run(question) print(a)

 

Result:

Write your queries on the question variable and run the Python file.


Comments