We are 50+ professional Software Developers with more than 5+ years of experience in delivering solutions.

Contacts

B-1001, PNTC, Prahalad Nagar, Ahmedabad

+91 63548-55130 | +1 707-901-7072

Technology
Discover the Future of Search with ChatGPT and Bing Precise Developers

Discover the Future of Search with ChatGPT and Bing

ChatGPT is a natural language processing (NLP) model developed by OpenAI that can be used to build chatbots and other conversational AI applications. In this blog post, we will show you how to use ChatGPT to build a simple search engine that allows users to search the web using Bing.

To get started, you will need to sign up for a Bing Web Search API key. You can do this by visiting the Microsoft Azure portal and creating a new Cognitive Services API key. You can sign up for a ChatGPT account at https://www.chatgpt.com/ and obtain a Bing Search API key by following the instructions at https://docs.microsoft.com/en-us/azure/cognitive-services/bing-web-search/getting-started. Once you have your API key, you will need to install the azure-cognitiveservices-search-websearch package using pip:

pip install azure-cognitiveservices-search-websearch

Next, you will need to install ChatGPT and its dependencies. You can do this by running the following command:

pip install chatgpt

Now, you are ready to start using ChatGPT to build your search engine. Here is some sample code that shows you how to use the Bing Web Search API to search the web and return the results to the user:

import openai_secret_manager

assert "bing" in openai_secret_manager.get_services()
secrets = openai_secret_manager.get_secrets("bing")

print(secrets)

from azure.cognitiveservices.search.websearch import WebSearchClient
from msrest.authentication import CognitiveServicesCredentials

client = WebSearchClient(
    base_url=secrets["endpoint"],
    credentials=CognitiveServicesCredentials(secrets["api_key"])
)

def search(query):
    web_data = client.web.search(query=query)
    return web_data.web_pages.value

results = search("chatbot")
for result in results:
    print(f'{result.name}: {result.url}')

To use ChatGPT to build your search engine, you will need to create a function that takes the user’s query and passes it to the search function. You can then use ChatGPT to generate a response based on the search results. Here is an example of how you might do this:

import openai

openai.api_key = secrets["api_key"]

def generate_response(query):
    results = search(query)
    if not results:
        return "Sorry, I could not find any results for your query."
    response = "Here are some search results:\n"
    for result in results:
        response += f'{result.name}: {result.url}\n'
    return response

def chatbot(prompt):
    completions = openai.Completion.create(
        engine="text-davinci-002",
        prompt=prompt,
        max_tokens=1024,
        n=1,
       stop=None,
        temperature=0.7,
    )
    message = completions.choices[0].text
    return message.strip()

while True:
    query = input("Enter your search query: ")
    response = chatbot(generate_response(query))
    print(response)

or you can use the following code to search the web with Bing and ChatGPT:

import openai

# Replace YOUR_CHATGPT_API_KEY with your ChatGPT API key
openai.api_key = "YOUR_CHATGPT_API_KEY"

# Replace YOUR_BING_SEARCH_API_KEY with your Bing Search API key
bing_search_api_key = "YOUR_BING_SEARCH_API_KEY"

def search(query):
  # Use the ChatGPT API to generate a response to the user's query
  response = openai.Completion.create(
    engine="chatgpt",
    prompt=f"What do you want to know about {query}?",
    max_tokens=1024,
    n=1,
    stop=None,
    temperature=0.5,
  )
  
  # Extract the search query from the ChatGPT response
  search_query = response["choices"][0]["text"].strip()
  
  # Use the Bing Search API to search the web
  search_results = requests.get(
    "https://api.cognitive.microsoft.com/bing/v7.0/search",
    headers={"Ocp-Apim-Subscription-Key": bing_search_api_key},
    params={"q": search_query, "count": 10},
  ).json()
  
  # Extract the top 10 search results
  search_results = search_results["webPages"]["value"]
  
  # Return the search results to the user
  return "\n".join([result["name"] for result in search_results])

With this code, you can use ChatGPT to generate a response to the user’s query and use the Bing Search API to search the web for the information the user is looking for. You can then return the top 10 search results to the user as a list of links.

You may also like: What’s New in Ruby 3.2.0

If you like this post then you may also like to share the same with your colleagues. Let us know your thoughts on our blogs and on social media posts on InstagramFacebookLinkedIn, and Twitter.

Leave a comment

Your email address will not be published. Required fields are marked *