blog.gopenai.com Open in urlscan Pro
162.159.152.4  Public Scan

Submitted URL: https://blog.gopenai.com/google-search-engine-with-langchain-%EF%B8%8F-bd3c1baa5a68
Effective URL: https://blog.gopenai.com/google-search-engine-with-langchain-%EF%B8%8F-bd3c1baa5a68?gi=93ddbf03c6a2
Submission: On August 23 via api from US — Scanned from US

Form analysis 0 forms found in the DOM

Text Content

Open in app

Sign up

Sign in

Write


Sign up

Sign in


Mastodon


GOOGLE SEARCH ENGINE WITH LANGCHAIN šŸ¦œļøšŸ”—

Vincent Lee

Ā·

Follow

Published in

GoPenAI

Ā·
3 min read
Ā·
Apr 1, 2024

2



Listen

Share

Keywords: Langchain, Google search engine, Python


INTRODUCTION

During my exploration of the RAG system, I realised that I needed allow my model
to access more information. The majority of my efforts are based on materials
that I find fascinating or that are well-known in the AI field.

I recently utilized LangChain and discovered that it provides a fantastic tool
that allows users to access Googleā€™s enormous amount of knowledge through
Googleā€™s search engine. This post will present and demonstrate how to use the
GoogleSearchAPIWrapper, a powerful tool discussed earlier.

Letā€™s dive in!


GOOGLESEARCHAPIWRAPPER

There are 2 essential steps

 1. First, I need to set up the proper API keys and environment variables. To
    set it up, create the GOOGLE_API_KEY in the Google Cloud credential console
    (https://console.cloud.google.com/apis/credentials)
 2. Then, I need to assign a GOOGLE_CSE_ID using the Programmable Search Engine
    (https://programmablesearchengine.google.com/controlpanel/create).

After doing those steps, I assign those keys into Secrets place.


Googleā€™s Secrets

Begin by downloading the langchain libraries, which are required for using
Google Search :

%pip install --quite --upgrade langchain-community langchain-core

Import libraries and get environment variables:

import os

from langchain_community.utilities import GoogleSearchAPIWrapper
from langchain_core.tools import Tool

from google.colab import userdata

os.environ["GOOGLE_CSE_ID"] = userdata.get('GOOGLE_CSE_ID')
os.environ["GOOGLE_API_KEY"] = userdata.get('GOOGLE_API_KEY')

Now that I can do the search. However, Iā€™d like the search to appear more like a
function than some lines of code. To do so:

def get_search(query:str="", k:int=1): # get the top-k resources with google
    search = GoogleSearchAPIWrapper(k=k)
    def search_results(query):
        return search.results(query, k)
    tool = Tool(
        name="Google Search Snippets",
        description="Search Google for recent results.",
        func=search_results,
    )
    ref_text = tool.run(query)
    if 'Result' not in ref_text[0].keys():
        return ref_text
    else:
        return None

And the result:

query = "What is prompt engineering?"
get_search(query=query, k=3)


"""
[{'title': 'What is prompt engineering? | McKinsey',
  'link': 'https://www.mckinsey.com/featured-insights/mckinsey-explainers/what-is-prompt-engineering',
  'snippet': 'Mar 22, 2024 ... Just as better ingredients can make for a better dinner, better inputs into a generative AI (gen AI) model can make for better results. These\xa0...'},
 {'title': 'What is Prompt Engineering? - AI Prompt Engineering Explained ...',
  'link': 'https://aws.amazon.com/what-is/prompt-engineering/',
  'snippet': 'In prompt engineering, you choose the most appropriate formats, phrases, words, and symbols that guide the AI to interact with your users more meaningfully.'},
 {'title': 'Prompt engineering - OpenAI API',
  'link': 'https://platform.openai.com/docs/guides/prompt-engineering',
  'snippet': 'Include details in your query to get more relevant answers Ā· Ask the model to adopt a persona Ā· Use delimiters to clearly indicate distinct parts of the input\xa0...'}]

"""


This is what the code looks like in full. Just a line of code, but its
robustness beyond infinite.




Thank you for reading this article; I hope it added something to your knowledge
bank! Just before you leave:

šŸ‘‰ Be sure to clap and follow me. It would be a great motivation for me.

šŸ‘‰The implementation refers to Notebook | Github

šŸ‘‰Follow me: Linkedin | Github




REFERENCE

 1. LangChain ā€” Google Search ā€” URL:
    https://python.langchain.com/docs/integrations/tools/google_search
 2. Stack Overflow ā€” Programmatically searching google in Python using custom
    search ā€” URL:
    https://stackoverflow.com/questions/37083058/programmatically-searching-google-in-python-using-custom-search




SIGN UP TO DISCOVER HUMAN STORIES THAT DEEPEN YOUR UNDERSTANDING OF THE WORLD.


FREE



Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.


Sign up for free


MEMBERSHIP



Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app


Try for $5/month
Langchain
Google Search
Python


2

2



Follow




WRITTEN BY VINCENT LEE

74 Followers
Ā·Writer for

GoPenAI

I write what I code, and I code what think šŸ˜Ž

Follow





MORE FROM VINCENT LEE AND GOPENAI

Vincent Lee


TEXT SUMMARIZATION WITH LLM


KEYWORDS: TEXT SUMMARIZATION, LLM, LANGCHAIN, HUGGINGFACE.

Jan 29
11
2



Elinson

in

GoPenAI


LAB #4: CHAT WITH 10M DATA RECORDS (CHATGPT, PANDASAI AND STREAMLIT)


ASK QUESTION ABOUT YOUR DATA IN NATURAL LANGUAGE


Jul 16
482
2



Paras Madan

in

GoPenAI


BUILDING A MULTI PDF RAG CHATBOT: LANGCHAIN, STREAMLIT WITH CODE


TALKING TO BIG PDFā€™S IS COOL. YOU CAN CHAT WITH YOUR NOTES, BOOKS AND DOCUMENTS
ETC. THIS BLOG POST WILL HELP YOU BUILD A MULTI RAGā€¦

Jun 6
582
2



Vincent Lee

in

GoPenAI


LETā€™S EXPLORE SCRAPEGRAPHAI


SCRAPING THE WEB LIKE A BOSS WITH SCRAPEGRAPHAI

Jul 13
6


See all from Vincent Lee
See all from GoPenAI



RECOMMENDED FROM MEDIUM

Karim Lalani


LANGCHAIN TUTORIAL: LCEL AND COMPOSING CHAINS FROM RUNNABLES


SIMPLIFY AI APP DEVELOPMENT WITH LANGCHAINā€™S LCEL. EXPLORE HOW TO COMPOSE CHAINS
WITH RUNNABLES IN THIS SHORT TUTORIAL.


Apr 10
11



Okan YenigĆ¼n

in

Artificial Intelligence in Plain English


LANGCHAIN IN CHAINS #22: CHAIN OF THOUGHT PROMPTING


ENHANCING MODEL RELIABILITY THROUGH STRUCTURED PROMPTING.


Apr 19
41




LISTS


CODING & DEVELOPMENT

11 storiesĀ·754 saves


PREDICTIVE MODELING W/ PYTHON

20 storiesĀ·1460 saves


PRACTICAL GUIDES TO MACHINE LEARNING

10 storiesĀ·1777 saves


CHATGPT

21 storiesĀ·768 saves


Okan YenigĆ¼n

in

Artificial Intelligence in Plain English


LANGCHAIN IN CHAINS #17: RETRIEVERS


A CLOSER LOOK AT LANGCHAINā€™S RETRIEVER MODULE


Mar 18
15
1



Harshit Sharma

in

Level Up Coding


HOW DOES LANGCHAIN EXPRESSION LANGUAGE (LCEL) WORKĀ ?


CLASSIC USE OF POLYMORPHISM AND OPERATOR OVERLOADING

Jun 25
44



Thuwarakesh Murallie

in

Towards Data Science


HOW TO BUILD HELPFUL RAGS WITH QUERY ROUTING.


AN LLM CAN HANDLE GENERAL ROUTING. SEMANTIC SEARCH CAN HANDLE PRIVATE DATA
BETTER. WHICH ONE SHOULD YOU PICK?


Aug 16
266
1



Vijaykumar Kartha


BEGINNERā€™S GUIDE TO CONVERSATIONAL RETRIEVAL CHAIN USING LANGCHAIN


IN THE LAST ARTICLE, WE CREATED A RETRIEVAL CHAIN THAT CAN ANSWER ONLY SINGLE
QUESTIONS. LETā€™S NOW LEARN ABOUT CONVERSATIONAL RETRIEVALā€¦

Apr 28
28


See more recommendations

Help

Status

About

Careers

Press

Blog

Privacy

Terms

Text to speech

Teams