Skip to content

Instantly share code, notes, and snippets.

@MahrRah
Last active March 25, 2024 08:02
Show Gist options
  • Save MahrRah/d0ca1a746e5e28e53dbca3658ce5c835 to your computer and use it in GitHub Desktop.
Save MahrRah/d0ca1a746e5e28e53dbca3658ce5c835 to your computer and use it in GitHub Desktop.
import logging
import os
import chainlit as cl
from dotenv import load_dotenv
from openai import AzureOpenAI
load_dotenv()
SYSTEM_MESSAGE = """
You are a skilled Python engineer who excels in writing code.
Your job is to generate application code based on a user input.
Before you generate code, make sure the user is providing you with the right set of requirements for the use-case they have requested.
"""
chat_history = []
def get_chat_completion(messages, stream=False):
try:
client = AzureOpenAI(
api_key=os.environ["AZURE_OPENAI_API_KEY"],
api_version=os.environ["AZURE_OPENAI_API_VERSION"],
azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT"],
)
messages = (
[{"role": "system", "content": SYSTEM_MESSAGE}]
+ chat_history
+ [{"role": "user", "content": messages}]
)
completion = client.chat.completions.create(
model=os.environ["AZURE_OPENAI_DEPLOYMENT_NAME"],
messages=messages,
stream=stream,
)
return completion.choices[0].message.content
except Exception as e:
logging.error(e)
@cl.on_chat_end
def on_chat_end():
chat_history.clear()
@cl.on_message
async def main(message: cl.Message):
chat_history.append({"role": "user", "content": message.content})
logging.info(f"Incoming message:\n {message.content}")
response = get_chat_completion(message.content)
chat_history.append({"role": "assistant", "content": str(response)})
logging.info(f"Response:\n {response}")
await cl.Message(content=response).send()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment