Giter Club home page Giter Club logo

openai-assistant-api-chat's Introduction

Code Refactor 90% done. ----> Branch ---> Code_refactor

OpenAI Assistant API Chat

Deploy with Vercel Live Demo

Introduction

Welcome to the OpenAI Assistant API Chat repository! This innovative chat application allows users to interact with an AI assistant powered by OpenAI's latest "gpt-4-1106-preview" model. It's an exciting space where technology meets conversation, offering a unique experience of AI interaction.

Agent42

Beta & Work in Progress

Please note that this application is currently in the beta phase and is continuously evolving. We are working diligently to enhance the user experience and add new features. During this phase, you may encounter some hiccups or unexpected behavior.

Deployment

This application is ready to be deployed with Vercel, a cloud platform for static sites and Serverless Functions. Vercel provides an easy way to deploy your applications directly from your repository.

To deploy this application with Vercel, click on the "Deploy with Vercel" button below. This will take you to the Vercel platform where you'll be guided through the deployment process.

Please note that you'll need to provide your OpenAI API key during the deployment process. This key is used to authenticate your application's requests to the OpenAI API.

Deploy with Vercel

In addition to the OpenAI API key, you can also specify a default Assistant ID during the deployment process. This ID determines which AI assistant is used in the chat application. If you set this ID, the application will use this assistant for the chat. If you do not set this ID, the application will prompt the user to enter the assistant details.

To deploy the application with both the OpenAI API key and a hardcoded Assistant ID, click on the "Deploy with Vercel" button below. You will be prompted to enter both your OpenAI API key and your Assistant ID.

Deploy with Vercel

Features

  • Personalized AI Assistant: Customize the assistant's name, model, and description for a unique chat experience.

  • Interactive Chat Experience: Engage in dynamic conversations with the AI assistant.

  • Robust AI Responses: Leveraging OpenAI's "gpt-4-1106-preview" model (128k context) for intelligent, context-aware chat responses.

  • File Upload: Users can upload files for the assistant to analyze.

  • GPT-4 Vision Integration: Send pictures to the AI, and it will describe what it sees, providing insights and understanding of the visual content.(improoved version soon)

  • Function Calls: (Coming Soon) Experience interactive functionalities such as API calls based on chat context.

  • Code Interpretation: (Coming Soon) The assistant can execute Pytho code.

Getting Started

Prerequisites

  • Node.js installed on your machine.
  • An active OpenAI API key.

Installation

  1. Clone the Repository:
    git clone https://github.com/admineral/OpenAI-Assistant-API-Chat.git
    
  2. Install Dependencies: Navigate to the project directory and run:
    npm install
    
  3. Environment Setup: Create a .env file in the root directory and add your OpenAI API key:
    OPENAI_API_KEY=your_openai_api_key
    
  4. Run the Application: Start the server with:
    npm run dev
    

Contributing

Your contributions make this project thrive. Whether it's reporting bugs, suggesting features, or submitting code changes, every bit of help is greatly appreciated.

  • Report Issues: If you encounter any problems, please open an issue on our GitHub page.
  • Feature Requests: Have an idea? Share it with us by opening an issue.
  • Pull Requests: Want to make a direct impact? Fork the repository, make your changes, and submit a pull request.

We look forward to growing this project with the community's support and creativity!

Application Architecture Overview

ChatManager (ChatManager.ts)

  • Role: Central component for managing chat state and operations.
  • Functions:
    • startAssistant: Initializes the chat assistant, manages file uploads, and handles thread creation.
    • sendMessage: Sends user messages to the assistant and updates the chat.
    • getChatState: Retrieves the current state of the chat, including messages and assistant status.

API Layer (api.js)

  • Purpose: Acts as an intermediary between the front-end and various API routes.
  • Key Functions:
    • uploadImageAndGetDescription: Uploads images and gets descriptions using the GPT-4 Vision API.
    • createAssistant, createThread, runAssistant: Handles assistant creation, thread management, and assistant operations.

Assistant Modules (assistantModules.ts)

  • Role: Manages tasks related to the chat assistant, such as file preparation and assistant initialization.
  • Key Functions:
    • prepareUploadFile: Prepares and uploads files for the chat assistant.
    • initializeAssistant: Initializes a chat assistant with specific details.
    • createChatThread: Creates a chat thread with an initial message.

Chat Modules (chatModules.ts)

  • Purpose: Manages chat-related functionalities.
  • Key Functions:
    • submitUserMessage: Submits user messages to the chat.
    • fetchAssistantResponse: Fetches the latest messages from the assistant.
    • updateChatState: Updates the chat state with new messages.

Detailed Code Explanation

ChatManager Implementation (ChatManager.ts)

  • Singleton Pattern: Ensures a single instance of ChatManager manages the chat state and operations.
  • State Management: Handles chat state, including messages, thread IDs, assistant status, and loading states.
  • Error Handling: Robust error handling during chat operations.
  • API Integration: Integrates with API layer for message sending/receiving and chat thread management.

API Layer (api.js)

  • Central API Management: Simplifies front-end interactions with a clean API interface.
  • Error Handling: Ensures smooth application operation with error handling in API requests.

Front-End Interaction

  • React Hooks: Utilizes hooks in useChatState.ts for state management.
  • User Interface: InputForm and MessageList interact with ChatManager for displaying messages and handling user inputs.

Main Components and Flow

  • ChatManager (ChatManager.ts): Central component managing the chat state and operations.
  • API Layer (api.js): Intermediary for API interactions.
  • Assistant Modules (assistantModules.ts): Handles tasks related to the chat assistant.
  • Chat Modules (chatModules.ts): Manages chat functionalities.

Detailed Breakdown

ChatManager.ts

This is the core class managing the chat's state and operations.

class ChatManager {
  private state: ChatState;
  private static instance: ChatManager | null = null;

  // Singleton pattern to ensure a single ChatManager instance
  private constructor(setChatMessages: (messages: any[]) => void, setStatusMessage: (message: string) => void) {
    this.state = {
      /* State initialization */
    };
    console.log('ChatManager initialized');
  }

  // Method to get the current instance of ChatManager
  public static getInstance(setChatMessages: (messages: any[]) => void, setStatusMessage: (message: string) => void): ChatManager {
    if (this.instance === null) {
      this.instance = new ChatManager(setChatMessages, setStatusMessage);
    }
    return this.instance;
  }

  // Method to start the assistant
  async startAssistant(assistantDetails: any, file: File | null, initialMessage: string): Promise<void> {
    // ... Function logic including API calls to initialize assistant and create chat thread
  }

  // Method to send a message
  async sendMessage(input: string): Promise<void> {
    // ... Function logic to handle message sending
  }

  // Method to get the current chat state
  getChatState(): ChatState {
    console.log('Getting chat state');
    return this.state;
  }
}
  • Key Features:
    • Singleton pattern ensures only one instance of ChatManager is created.
    • Manages the chat's state, including messages, assistant's ID, thread ID, and loading states.
    • startAssistant: Initiates the assistant and sets up the chat thread.
    • sendMessage: Handles sending messages to the assistant.
    • getChatState: Retrieves the current state of the chat.

api.js

This module contains functions for various API interactions required by the chat application.

// Example of an API function
export const uploadImageAndGetDescription = async (base64Image) => {
  // Code to upload an image and get a description using the OpenAI API
};

export const createAssistant = async (assistantDetails) => {
  // Code to create an assistant
};

// Other API functions like 'createThread', 'runAssistant', etc.
  • Purpose: Provides a centralized and clean interface for API interactions.
  • Key Functions:
    • uploadImageAndGetDescription: Uploads a base64 encoded image and gets a description.
    • createAssistant: Creates a new assistant instance.
    • Other functions for managing threads, running assistants, etc.

assistantModules.ts

Contains functions related to preparing and managing the chat assistant.

export const prepareUploadFile = async (file: File, setStatusMessage: (message: string) => void): Promise<string> => {
  // Logic to prepare and upload a file for the chat assistant
};

export const initializeAssistant = async (assistantDetails, fileId): Promise<string> => {
  // Logic to initialize an assistant with given details
};

export const createChatThread = async (inputMessage: string): Promise<string> => {
  // Logic to create a chat thread
};
  • Purpose: Handles assistant-related tasks such as file preparation and assistant initialization.

chatModules.ts

Manages chat-related functionalities, primarily dealing with messages.

export const submitUserMessage = async (input: string, threadId: string): Promise<void> => {
  // Logic to submit a user's message to the chat
};

export const fetchAssistantResponse = async (runId: string, threadId: string): Promise<string> => {
  // Logic to fetch the latest messages from the assistant
};

export const updateChatState = (prevMessages: Message[], newMessages: Message[], setChatMessages: (messages: any[]) => void): Promise<void> => {
  // Logic to update the chat state with new messages
};
  • Purpose: Manages sending user messages, fetching assistant responses, and updating the chat state.

React Components

  • WelcomeForm, InputForm, and MessageList are React components that build the user interface of the chat application.

They use hooks and states to manage user interactions and display chat messages.

API Routes (/api/*.ts)

These files define various API routes for handling tasks like creating assistants, listing messages, checking run status, etc. They interact with the OpenAI API and provide endpoints for the frontend to call.

openai-assistant-api-chat's People

Contributors

admineral avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

openai-assistant-api-chat's Issues

File ID is undefined

got this error

472-dcc473fb6ec49a48.js:1 Error uploading file: Error: File ID is undefined

image image

Failed to run assistant

Hi when I deploy to vercel with my OPENAI_API_KEY and ASSISTANT_ID I got the error "Failed to run assistant"

Failed to run assistant 500

I see runAssistant() just post a request but how do I fix this error ?

  export const runAssistant = async (assistantId, threadId) => {
    console.log('Running assistant...');
    const response = await fetch('/api/runAssistant', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ assistantId, threadId }),
    });
    if (!response.ok) {
      console.error('Failed to run assistant');
      throw new Error('Failed to run assistant');
    }

From the console I saw Thread ID: undefined, maybe this is the reason.

Chat thread created successfully. Thread ID: undefined

Chat introduction

Would you consider changing the chat introduction from Pirate to Professional or adding a choice for the user to specify as a parameter?

报错

image
这个shish是什么问题

Wish to see a CLI version of this

Would love to see a CLI version for this, im not really in to the upload file, just send a question with a specific Assistan role. but to not use a webbrowser and use the CLI instead would be perfect if possible :)

Availability to support

Great repo, thanks! Do you take on private clients to implement the above and other projects?

Don

Not an issue, just don't know where to ask. I really could use the TTS from eleven labs and Whisper put in this, dunno if you'd be willing to add it for a fee or something but we have some folks here in that don't type too well and it would sure be cool. Please shoot me an email and let me know [email protected] I really hope it's possible cuz the last proton in the universe would decay before I could even think of being able to do that lol. Sorry for the intrusion.

Pirate?!

i have an assistant ID than i added in the .env and when i ask assistant "who are you" it is correct. But When i start/load the promp it always state in the terminal text: { value: 'You are a Pirate! introduce yourself', annotations: [] } i have looked at OpenAI, Assistant and what i can see/know i have nothing naming a pirate. Is there something im missing or how can i remove this and make the prompt act like it should when loading?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.