Pseudo Code Generator | JavaScript

Asynchronous OpenAI Chat Application Pseudo Code

This pseudo code illustrates the structure of an asynchronous chat application that utilizes the OpenAI API to facilitate interactions between two personas, Elon and Warren, while managing message history efficiently.


Empty image or helper icon

Prompt

import os
import chainlit as cl
import asyncio

from openai import AsyncClient

os.environ["OPENAI_API_KEY"] = "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"

# Attempt to get the API key from the environment variable
api_key = os.getenv("OPENAI_API_KEY")

if not api_key:
    print("API key not found in environment variables.")
    api_key = input("Please enter your OpenAI API key: ")


openai_client = AsyncClient(api_key=os.environ.get("OPENAI_API_KEY"))

model_name = "gpt-4o"
settings = {
    "temperature": 0.3,
    "max_tokens": 500,
    "top_p": 1,
    "frequency_penalty": 0,
    "presence_penalty": 0,
}


@cl.on_chat_start
async def start_chat():
    cl.user_session.set(
        "message_history",
        [
            {
                "role": "system",
                "content": "Two helpful assistants checking each others work. Elon is a master business mogul. Warren is an investing genius",
            }
        ],
    )

async def answer_as(name):
    message_history = cl.user_session.get("message_history")
    msg = cl.Message(author=name, content="")

    stream = await openai_client.chat.completions.create(
        model=model_name,
        messages=message_history + [{"role": "user", "content": f"speak as {name}"}],
        stream=True,
        **settings,
    )
    async for part in stream:
        if token := part.choices[0].delta.content or "":
            await msg.stream_token(token)

    # Need to add the information that it was the author who answered but OpenAI only allows assistant.
    # simplified for the purpose of the demo.
    message_history.append({"role": "assistant", "content": msg.content})
    await msg.send()


@cl.on_message
async def main(message: cl.Message):
    message_history = cl.user_session.get("message_history")
    message_history.append({"role": "user", "content": message.content})

    await asyncio.gather(answer_as("Elon"), answer_as("Warren"))

Answer

Pseudo Code for OpenAI Chat Application

Overview

The following pseudo code demonstrates the logic and structure of an asynchronous chat application that utilizes the OpenAI API. The application facilitates interaction between two personas (Elon and Warren), retrieving responses from OpenAI's language model while maintaining message history.

Main Components

  1. Environment Setup: Load the OpenAI API key and handle cases where it is not available.
  2. Client Initialization: Initialize the OpenAI asynchronous client with the API key.
  3. Chat Session Management: Manage chat session through message history.
  4. Asynchronous Response Handling: Generate responses from the AI model based on user input.
  5. Message Processing: Handle incoming messages and invoke response generation from both personas.

Pseudo Code

SET OPENAI_API_KEY to environment variable "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"

# Attempt to get API key from environment variable
SET api_key to get environment variable "OPENAI_API_KEY"

IF api_key is empty THEN
    PRINT "API key not found in environment variables."
    SET api_key to user input for "Please enter your OpenAI API key:"

# Initialize OpenAI asynchronous client
SET openai_client to AsyncClient with api_key

# Define model settings
SET model_name to "gpt-4o"
SET settings to {
    "temperature": 0.3,
    "max_tokens": 500,
    "top_p": 1,
    "frequency_penalty": 0,
    "presence_penalty": 0,
}

# Define behavior on chat start
FUNC start_chat():
    SET user_session message_history to initial system message:
        {
            "role": "system",
            "content": "Two helpful assistants checking each others work. Elon is a master business mogul. Warren is an investing genius"
        }

# Define function for generating answers from the AI
ASYNC FUNC answer_as(name):
    SET message_history to user_session.get("message_history")
    CREATE new message object msg with author name and empty content

    # Request completion from OpenAI's chat with the user's message history
    SET stream to await openai_client chat.completions.create with model_name, message_history, and settings

    # Process the response stream
    ASYNC for each part in stream:
        IF token exists in part's choices THEN
            Stream token to msg

    # Append assistant's message to history
    ADD {"role": "assistant", "content": msg.content} to message_history
    SEND msg

# Define behavior on receiving a message
ASYNC FUNC main(message):
    SET message_history to user_session.get("message_history")
    
    # Append user's message to history
    ADD {"role": "user", "content": message.content} to message_history

    # Generate responses asynchronously from both personas
    AWAIT asyncio.gather(answer_as("Elon"), answer_as("Warren"))

Explanation of Logic

  1. Environment Handling: The application checks if the OpenAI API key is available; prompts the user for input if it is not set.
  2. Model Configuration: The settings for the language model are defined to control the behavior of the model during interactions.
  3. Session Management: A chat session is initiated where the message history is preserved, allowing the model to have context for generating responses.
  4. Asynchronous Processing: The application's design employs asynchronous methods to handle API requests efficiently, ensuring smooth user interactions.
  5. Response Generation: Upon receiving a message, both personas (Elon and Warren) generate responses simultaneously, enhancing the user experience.

This pseudo code effectively outlines the underlying logic and functionality of the given Python code snippet, providing a clear framework for documentation or further development.

Create your Thread using our flexible tools, share it with friends and colleagues.

Your current query will become the main foundation for the thread, which you can expand with other tools presented on our platform. We will help you choose tools so that your thread is structured and logically built.

Description

This pseudo code illustrates the structure of an asynchronous chat application that utilizes the OpenAI API to facilitate interactions between two personas, Elon and Warren, while managing message history efficiently.