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 all commits
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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
.azure
.azure
.env
__pycache__
3 changes: 3 additions & 0 deletions src/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
OPENAI_API_VERSION=
AZURE_OPENAI_ENDPOINT=
AZURE_OPENAI_DEPLOYMENT=
65 changes: 65 additions & 0 deletions src/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import uuid

from azure.identity import DefaultAzureCredential, get_bearer_token_provider
from openai import AzureOpenAI
from quart import Quart, g, request
from stateStore import StateStore
from config import Config

token_provider = get_bearer_token_provider(
DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default")

app = Quart(__name__)

config = Config()
state_store = StateStore()


@app.before_request
async def load_history():
data = await request.get_json()
messages = data.get('messages', [])
session_state = data.get('sessionState', None)

if session_state:
try:
history = state_store.read(session_state)
messages = history + messages
except ValueError:
pass
else:
session_state = str(uuid.uuid4())

g.messages = [
{"role": "system", "content": config.system_prompt}] + messages
g.session_state = session_state


@app.route("/api/chat", methods=['POST'])
async def chat():
messages = g.messages
session_state = g.session_state

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?

api_version=config.azure_api_version,
azure_endpoint=config.azure_endpoint,
azure_ad_token_provider=token_provider,
)

completion = client.chat.completions.create(
model=config.azure_deployment,
messages=messages,
max_tokens=1024
)
choice = completion.choices[0]
responseMessage = {
"role": choice.message.role,
"content": choice.message.content
}

state_store.save(session_state, messages + [responseMessage])

return {"messages": responseMessage, "sessionState": session_state}

if __name__ == "__main__":
app.run()
28 changes: 28 additions & 0 deletions src/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import os
from dotenv import load_dotenv


class Config:
def __init__(self):
load_dotenv()
self._azure_endpoint = os.environ.get("AZURE_OPENAI_ENDPOINT")
self._azure_deployment = os.environ.get("AZURE_OPENAI_DEPLOYMENT")
self._azure_api_version = os.environ.get("OPENAI_API_VERSION")
self._system_prompt = os.environ.get(
"SYSTEM_PROMPT", "You are a helpful assistant that responds succintly to questions.")

@property
def azure_endpoint(self):
return self._azure_endpoint

@property
def azure_deployment(self):
return self._azure_deployment

@property
def azure_api_version(self):
return self._azure_api_version

@property
def system_prompt(self):
return self._system_prompt
4 changes: 4 additions & 0 deletions src/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
quart
openai
python-dotenv
azure-identity
12 changes: 12 additions & 0 deletions src/stateStore.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class StateStore:
def __init__(self):
self.store = {}

def read(self, key):
state = self.store.get(key)
if state is None:
raise ValueError("Not found.")
return state

def save(self, key, state):
self.store[key] = state