skip to Main Content

I got "Error code: 404 – {‘error’: {‘code’: ‘404’, ‘message’: ‘Resource not found’}}" even if I have provided everything correctly. The problem for sure is not related with providing wrong Endpoints or Keys etc. I have installed "pip install openai==1.13.3"

and I have tried previous versions too, but nothing worked. I would be grateful if somebody can help.

import os
import json
from dotenv import load_dotenv

# Add OpenAI import
from openai import AzureOpenAI

def main(): 
        
    try:
        # Flag to show citations
        show_citations = False

        # Get configuration settings 
        load_dotenv()
        azure_oai_endpoint = os.getenv("AZURE_OAI_ENDPOINT")
        azure_oai_key = os.getenv("AZURE_OAI_KEY")
        azure_oai_deployment = os.getenv("AZURE_OAI_DEPLOYMENT")
        azure_search_endpoint = os.getenv("AZURE_SEARCH_ENDPOINT")
        azure_search_key = os.getenv("AZURE_SEARCH_KEY")
        azure_search_index = os.getenv("AZURE_SEARCH_INDEX")
        
        # Initialize the Azure OpenAI client
        client = AzureOpenAI(
            base_url=f"{azure_oai_endpoint}/openai/deployments/{azure_oai_deployment}/extensions",
            #base_url = f"{azure_oai_endpoint}/openai/deployments/{azure_oai_deployment}?api-version=2022-12-01",
            api_key=azure_oai_key,
            api_version="2024-02-01")

        # Get the prompt
        text = input('nEnter a question:n')

        # Configure your data source
        extension_config = dict(dataSources = [  
                { 
                    "type": "AzureCognitiveSearch", 
                    "parameters": { 
                        "endpoint":azure_search_endpoint, 
                        "key": azure_search_key, 
                        "indexName": azure_search_index,
                    }
                }]
            )


        # Send request to Azure OpenAI model
        print("...Sending the following request to Azure OpenAI endpoint...")
        print("Request: " + text + "n")

        response = client.chat.completions.create(
            model = azure_oai_deployment,
            temperature = 0.5,
            max_tokens = 1000,
            messages = [
                {"role": "system", "content": "You are a helpful customer assistant"},
                {"role": "user", "content": text}
            ],
            extra_body = extension_config
        )

        # Print response
        print("Response: " + response.choices[0].message.content + "n")

        if (show_citations):
            # Print citations
            print("Citations:")
            citations = response.choices[0].message.context["messages"][0]["content"]
            citation_json = json.loads(citations)
            for c in citation_json["citations"]:
                print("  Title: " + c['title'] + "n    URL: " + c['url'])


        
    except Exception as ex:
        print(ex)


if __name__ == '__main__': 
    main()

2

Answers


  1. Chosen as BEST ANSWER

    Thank you for answers but non of them worked for me.


  2. I updated the base_url and the api_version as below in your code and able to run the application without any issues.

            base_url = f"{azure_oai_endpoint}/openai/deployments/{azure_oai_deployment}/chat/completions?api-version=2024-02-15-preview"
            client = AzureOpenAI(
                base_url=base_url,
                api_key=azure_oai_key,
                api_version="2024-02-15-preview")
    

    Below is the complete code.

    import os
    import json
    from dotenv import load_dotenv
    from openai import AzureOpenAI
    
    def main(): 
        try:
            show_citations = False
            load_dotenv()
            azure_oai_endpoint = os.getenv("AZURE_OAI_ENDPOINT")
            azure_oai_key = os.getenv("AZURE_OAI_KEY")
            azure_oai_deployment = os.getenv("AZURE_OAI_DEPLOYMENT")
            azure_search_endpoint = os.getenv("AZURE_SEARCH_ENDPOINT")
            azure_search_key = os.getenv("AZURE_SEARCH_KEY")
            azure_search_index = os.getenv("AZURE_SEARCH_INDEX")
            
            base_url = f"{azure_oai_endpoint}/openai/deployments/{azure_oai_deployment}/chat/completions?api-version=2024-02-15-preview"
            client = AzureOpenAI(
                base_url=base_url,
                api_key=azure_oai_key,
                api_version="2024-02-15-preview")
    
            text = input('nEnter a question:n')
            extension_config = dict(dataSources = [  
                    { 
                        "type": "AzureCognitiveSearch", 
                        "parameters": { 
                            "endpoint":azure_search_endpoint, 
                            "key": azure_search_key, 
                            "indexName": azure_search_index,
                        }
                    }]
                )
    
            print("...Sending the following request to Azure OpenAI endpoint...")
            print("Request: " + text + "n")
    
            response = client.chat.completions.create(
                model = azure_oai_deployment,
                temperature = 0.5,
                max_tokens = 1000,
                messages = [
                    {"role": "system", "content": "You are a helpful customer assistant"},
                    {"role": "user", "content": text}
                ],
                extra_body = extension_config
            )
            print("Response: " + response.choices[0].message.content + "n")
    
            if (show_citations):
                print("Citations:")
                citations = response.choices[0].message.context["messages"][0]["content"]
                citation_json = json.loads(citations)
                for c in citation_json["citations"]:
                    print("  Title: " + c['title'] + "n    URL: " + c['url'])
        except Exception as ex:
            print(ex)
    
    if __name__ == '__main__': 
        main()
    

    Output :

    The following code ran successfully as below.

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search