Skip to content

Commit

Permalink
Implementing setup function in UI
Browse files Browse the repository at this point in the history
  • Loading branch information
dividor committed Jul 10, 2024
1 parent 701c2e6 commit ee7bee5
Showing 1 changed file with 27 additions and 15 deletions.
42 changes: 27 additions & 15 deletions ui/chat-chainlit-assistant/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,32 @@
user = os.environ.get("USER_LOGIN")
password = os.environ.get("USER_PWD")

if os.environ.get("ASSISTANTS_API_TYPE") == "openai":
async_openai_client = AsyncOpenAI(api_key=os.environ.get("ASSISTANTS_API_KEY"))
sync_openai_client = OpenAI(api_key=os.environ.get("ASSISTANTS_API_KEY"))
else:
async_openai_client = AsyncAzureOpenAI(
azure_endpoint=os.getenv("ASSISTANTS_BASE_URL"),
api_key=os.getenv("ASSISTANTS_API_KEY"),
api_version=os.getenv("ASSISTANTS_API_VERSION"),
)
sync_openai_client = AzureOpenAI(
azure_endpoint=os.getenv("ASSISTANTS_BASE_URL"),
api_key=os.getenv("ASSISTANTS_API_KEY"),
api_version=os.getenv("ASSISTANTS_API_VERSION"),
)

def setup(cl):
"""
Sets up the assistant and OpenAI API clients based on the environment variables.
Args:
cl: The ChatLabs instance.
Returns:
tuple: A tuple containing the assistant, async OpenAI API client, and sync OpenAI API client.
"""

if os.environ.get("ASSISTANTS_API_TYPE") == "openai":
async_openai_client = AsyncOpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
sync_openai_client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
else:
async_openai_client = AsyncAzureOpenAI(
azure_endpoint=os.getenv("ASSISTANTS_BASE_URL"),
api_key=os.getenv("ASSISTANTS_API_KEY"),
api_version=os.getenv("ASSISTANTS_API_VERSION"),
)
sync_openai_client = AzureOpenAI(
azure_endpoint=os.getenv("ASSISTANTS_BASE_URL"),
api_key=os.getenv("ASSISTANTS_API_KEY"),
api_version=os.getenv("ASSISTANTS_API_VERSION"),
)

cl.instrument_openai() # Instrument the OpenAI API client

Expand Down Expand Up @@ -782,4 +794,4 @@ async def process_message(message: cl.Message):
) as stream:
stream.until_done()

cl.user_session.set("chat_history", chat_history)
cl.user_session.set("chat_history", chat_history)

0 comments on commit ee7bee5

Please sign in to comment.