how to set an API connection via python to 3.5 turbo model ?

Raz, Eran 0 Reputation points
2025-03-09T07:34:17.07+00:00

how to set an API connection via python to 3.5 turbo model ?

Azure AI services
Azure AI services
A group of Azure services, SDKs, and APIs designed to make apps more intelligent, engaging, and discoverable.
3,602 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Saideep Anchuri 9,425 Reputation points Microsoft External Staff Moderator
    2025-03-09T10:50:04.8966667+00:00

    Hi Raz, Eran

    To set up an API connection to the GPT-3.5 Turbo model using Python.

    Here are some steps:

    • Install the OpenAI Python client library.
    pip install openai
    
    • Go to your resource in the Azure portal. The Keys & Endpoint section can be found in the Resource Management section. Copy your endpoint and access key as you'll need both for authenticating your API calls. You can use either KEY1 or KEY2.
    • Create and assign persistent environment variables for your key and endpoint.
        setx AZURE_OPENAI_API_KEY "REPLACE_WITH_YOUR_KEY_VALUE_HERE" 
        setx AZURE_OPENAI_ENDPOINT "REPLACE_WITH_YOUR_ENDPOINT_HERE"
        
        
      
    • Create a file named quickstart.py and include the following code snippet to set up the connection:
    import os
    from openai import AzureOpenAI
    client = AzureOpenAI(
      azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT"), 
      api_key=os.getenv("AZURE_OPENAI_API_KEY"),  
      api_version="2024-02-01"
    )
    response = client.chat.completions.create(
        model="gpt-35-turbo", # model = "deployment_name".
        messages=[
            {"role": "system", "content": "You are a helpful assistant."},
            {"role": "user", "content": "Does Azure OpenAI support customer managed keys?"},
            {"role": "assistant", "content": "Yes, customer managed keys are supported by Azure OpenAI."},
            {"role": "user", "content": "Do other Azure AI services support this too?"}
        ]
    )
    print(response.choices[0].message.content) 
    
    • Run the application with the python command on your quickstart file:
        python quickstart.py
      

    Kindly refer below link: create-a-new-python-application

    Thank You.

    1 person found this answer helpful.

  2. Raz, Eran 0 Reputation points
    2025-03-11T09:10:51.1266667+00:00

    Hi , did as instructed, it still complains about the connection

    I have a "user" role for the cognitive services User's image

    Ran the playground code with my key, but still...

    https://oai-eus-sandbox-yokit.openai.azure.com/openai/deployments/gpt-35-turbo-yokit/chat/completions?api-version=2024-02-15-preview

    User's image


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.