-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
39 lines (31 loc) · 1.15 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
from flask import Flask, request, jsonify, render_template
from azure.identity import DefaultAzureCredential, EnvironmentCredential, get_bearer_token_provider
from openai import AzureOpenAI
import os
app = Flask(__name__)
@app.route('/')
def home():
return render_template('index.html')
@app.route('/ask', methods=['POST'])
def ask():
question = request.form['question']
token_provider = get_bearer_token_provider(
DefaultAzureCredential(),
"https://cognitiveservices.azure.com/.default"
)
client = AzureOpenAI(
azure_endpoint = os.getenv("AZURE_OPENAI_API_BASE"),
azure_ad_token_provider = token_provider,
api_version = os.getenv("AZURE_OPENAI_API_VERSION")
)
response = client.chat.completions.create(
model = os.getenv("AZURE_OPENAI_API_DEPLOY"),
messages = [
{"role": "system", "content": "You are a friendly chatbot"},
{"role": "user", "content": question}
]
)
answer = response.choices[0].message.content
return jsonify({'answer': answer})
if __name__ == '__main__':
app.run(debug=True)