Trouble with Azure OpenAI Service, how do I connect to OpenAI Service?

Elias Barthelemy 20 Reputation points
2024-01-11T15:36:07.4866667+00:00

Hey, I am currently trying to develop a chatbot with the Azure OpenAI service and Python; however, I want to use the Azure OpenAI API as well as the endpoint and not the direct AI intersection. The first code block (maintest.py) shows the working method. However, I would like to program it like in the second (main.py) and third (functions.py) code file. Thanks for your help already :) maintest.py:

from dotenv import load_dotenv
load_dotenv("secrets.env")

from flask_cors import CORS
import json
import osimport time
import requests
import openai

app = Flask(__name__)
CORS(app)
    
chatgpt_model_name = os.environ['AZURE_OPENAI_MODEL']
openai.api_type = "azure"
openai.api_key = os.environ['AZURE_OPENAI_API']
openai.api_base = os.environ['AZURE_OPENAI_ENDPOINT']
openai.api_version = os.environ['AZURE_OPENAI_API_VERSION']
openai.gpt_type = os.environ['AZURE_GPT_TYPE']

def generate_response(prompt):  
    model_engine = chatgpt_model_name  
    response = openai.Completion.create(  
        engine=model_engine,  
        prompt=prompt, 
        temperature=0.4, 
        max_tokens=500,
        top_p=0.45,  
        frequency_penalty=0,
        presence_penalty=0,
        stop=None           
    )  
    return response.choices[0].text.strip()  

@app.route('/', methods=['GET', 'POST'])  
def index():  
    if request.method == 'POST':  
        user_question = request.form['question']  
        generated_response = generate_response(user_question)  
        return render_template('index.html', response=generated_response)  
    return render_template('index.html')  
  

	app.run(debug=False, host='0.0.0.0', port=8000)

main.py

from dotenv import load_dotenv
load_dotenv("secrets.env")

from flask_cors import CORS
import json
import os
import time
import functions
from functions 
import get_openai_response
import requests
import openai

app = Flask(__name__)
CORS(app)

AZURE_OPENAI_ENDPOINT = os.environ['AZURE_OPENAI_ENDPOINT']

headers = {
    "Content-Type": "application/json",
    "Authorization": f"Bearer {AZURE_OPENAI_API}"
}


@app.route('/')
def index():
    return render_template('chat.html')

@app.route('/chat', methods=['GET', 'POST'])
def chat():
    if request.method == 'POST':
        user_input = request.form.get('message')
        response = get_openai_response(user_input)
        return render_template('chat.html', response=response)
    return redirect(url_for('index'))

port = os.getenv('PORT', '8000')
app.run(debug=False, host='0.0.0.0', port=int(port))

functions.py

from dotenv import load_dotenv
load_dotenv("secrets.env")

import os
import time
import requests
import openai
from airtable import airtable

AZURE_OPENAI_API = os.getenv("AZURE_OPENAI_API")
AZURE_OPENAI_ENDPOINT = os.getenv("AZURE_OPENAI_ENDPOINT")

def get_openai_response(prompt):
    headers = {
        'Content-Type': 'application/json',
        'Authorization': f'Bearer {AZURE_OPENAI_API}'
    }
    data = {
        'prompt': prompt,
        'max_tokens': 500
    }

    try:
        url = f"{AZURE_OPENAI_ENDPOINT}/completions"

        response = requests.post(url, headers=headers, json=data)

        if response.status_code == 200:
            response_data = response.json()
            return response_data.get('choices')[0].get('text')
        else:
            print(f"Error: Received status code {response.status_code}")
            return f"Error: Unable to get response from OpenAI. Status Code: {response.status_code}"
    except Exception as e:
        print(f"Exception occurred: {str(e)}")
        return "Error: An exception occurred while querying OpenAI."
Azure OpenAI Service
Azure OpenAI Service
An Azure service that provides access to OpenAI’s GPT-3 models with enterprise capabilities.
3,680 questions
Azure AI services
Azure AI services
A group of Azure services, SDKs, and APIs designed to make apps more intelligent, engaging, and discoverable.
3,134 questions
{count} votes

Accepted answer
  1. YutongTie-MSFT 53,891 Reputation points
    2024-01-15T23:31:00.0333333+00:00

    Thanks for your confirmation Elias! Glad to know you have solved the issue and sorry for the delay during the weekend.

    May I know what is the root cause of the issue? Please do let us know if you see any issue again and hope you enjoy Azure OpenAI.

    Regards, Yutong

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Elias Barthelemy 20 Reputation points
    2024-01-14T18:21:47.6533333+00:00

    Problem Solved

    0 comments No comments

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.