Integrating Search Functionality with OpenAI Models on Azure AI Platform

Isuan Agarwal 0 Reputation points
2025-05-09T07:24:12.2+00:00

What are the steps to implement a search feature similar to ChatGPT that uses an internet search function to gather data and inform the responses based on the model's knowledge?

Azure AI Search
Azure AI Search
An Azure search service with built-in artificial intelligence capabilities that enrich information to help identify and explore relevant content at scale.
1,289 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  2. Prashanth Veeragoni 4,360 Reputation points Microsoft External Staff Moderator
    2025-05-09T08:56:36.9966667+00:00

    Hi Isuan Agarwal,

    Integrating a search functionality with Azure OpenAI models to mimic how ChatGPT Browsing works involves building a Retrieval-Augmented Generation (RAG) pipeline. This process allows your AI to fetch fresh information from the web or internal data and generate informed responses. Here’s a step-by-step implementation guide tailored to Azure:

    Step-by-Step Implementation:

    Step1: Set Up Azure OpenAI

    ·   Create an Azure OpenAI resource from Azure Portal.

    ·    Deploy a model like gpt-4, gpt-35-turbo, etc.

    ·    Get endpoint and API key for calling the model.

    Step2: Set Up the Search Functionality

    Option A: Web Search (Live Internet Data)

    ·    Use Bing Search API (via Azure Cognitive Services).

    o   Bing Search v7

    o   Get your subscription key and endpoint.

    Option B: Internal Document Search

    ·    Use Azure AI Search or OpenSearch:

    o   Index your own documents (PDFs, Word, text).

    o   Use vector search and semantic search to find relevant content.

    Step3: Create a RAG (Retrieval-Augmented Generation) Pipeline

    User Query → Search Engine (Bing or Azure Search) → Fetch Results (top 3) → Combine with Prompt → Send to Azure OpenAI → Return Response
    

    Step4: Code the Flow (Example with Bing + Azure OpenAI in Python)

    ·   Search with Bing API

    import requests
    
    def bing_search(query, subscription_key, endpoint):
        headers = {"Ocp-Apim-Subscription-Key": subscription_key}
        params = {"q": query, "textDecorations": True, "textFormat": "HTML", "count": 3}
        response = requests.get(endpoint, headers=headers, params=params)
        return [res['snippet'] for res in response.json()['webPages']['value']]
    

    ·   Combine with Prompt and Call OpenAI

    import openai
    
    def generate_answer(prompt, model, openai_endpoint, api_key):
        openai.api_base = openai_endpoint
        openai.api_key = api_key
        response = openai.ChatCompletion.create(
            model=model,
            messages=[{"role": "system", "content": "You're a helpful assistant."},
                      {"role": "user", "content": prompt}]
        )
        return response['choices'][0]['message']['content']
    

    ·   Full Pipeline

    def ask_question_with_search(query):
        search_results = bing_search(query, bing_key, bing_endpoint)
        context = "\n".join(search_results)
        prompt = f"Answer the following based on the web results:\n\n{context}\n\nQuestion: {query}"
        return generate_answer(prompt, model, openai_endpoint, openai_key)
    

    Step5: Host the Application

    ·   Use FastAPI or Flask for API endpoints.

    ·   Optionally deploy on Azure App Service or Azure Container Apps.

    Hope this helps. Do let us know if you any further queries.


    If this answers your query, do click Accept Answer and Yes for was this answer helpful. And, if you have any further query do let us know.

    Thank you! 


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.