Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add sample code in Python #9

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.azure
.azure
.env
hund030 marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 4 additions & 0 deletions src/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
OPENAI_API_VERSION=
AZURE_OPENAI_ENDPOINT=
AZURE_OPENAI_DEPLOYMENT=
AZURE_OPENAI_API_KEY=
hund030 marked this conversation as resolved.
Show resolved Hide resolved
36 changes: 36 additions & 0 deletions src/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from quart import Quart, request
from openai import AzureOpenAI
from dotenv import load_dotenv
import os
hund030 marked this conversation as resolved.
Show resolved Hide resolved

load_dotenv()
azure_endpoint = os.environ["AZURE_OPENAI_ENDPOINT"]
azure_deployment = os.environ["AZURE_OPENAI_DEPLOYMENT"]

app = Quart(__name__)

@app.route("/api/chat", methods=['POST'])
async def chat():
# Get the message from the request data
data = await request.get_json()
message = data["message"]

# gets the API Key from environment variable AZURE_OPENAI_API_KEY
hund030 marked this conversation as resolved.
Show resolved Hide resolved
client = AzureOpenAI(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we're using Quart, we should take advantage of the Async client instead, I think?

azure_endpoint=azure_endpoint,
)

completion = client.chat.completions.create(
model=azure_deployment,
messages=[
{
"role": "user",
"content": message,
},
],
)

return completion.choices[0].message.content

if __name__ == "__main__":
app.run()
3 changes: 3 additions & 0 deletions src/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
quart
openai
python-dotenv