GithubHelp home page GithubHelp logo

abhinandshibu / remix-openai-assistants-event-streaming Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 309 KB

Keep the OpenAI Assistants thread consistent with a single react state variable. Aiming to reduce bloat and save time when developing web applications with assistants.

License: MIT License

JavaScript 13.75% TypeScript 86.25%

remix-openai-assistants-event-streaming's Introduction

Remix OpenAI Event Streaming with OpenAI Assistants API

This example aims to keep a single state variable that stores the message of the thread and is up to date. In that, the every streamed token is updated in this thread state variable, leading to automatic UI updates. So when creating your UI, you only need to use the thread state variable as you wish.

This is done through three resource routes, with three corresponding functions:

  • api.add-message.ts and addMessage(thread_id, message, setThread)
  • api.load-thread.ts and loadThread(thread_id, setThread)
  • api.run.ts and run(thread_id, assistant_id, setThread)

Though as a developer just copy in the resource routes, and place the corresponding functions in a file, and now you should be able to utilise them in your application as you wish.

Create and populate your thread on initial render:

const [thread, setThread] = useState<any[]>([]);
useEffect(() => { loadThread(thread_id, setThread) }, []);

Run with your OPENAI_API_KEY in .env file.

Install npm i remix-utils, which is used to create the eventStream.


Design Aims:

  • A single state variable that has all the messages on the thread and is up to date.
  • It should be up to date in that it is updated by every token that is streamed.
  • The format of this state variable is a list of messages, namely in the format that the openai threads/{thread_id}/messages gives

Method:

  • Initially => Load openai thread into thread state var
  • User sends message => Add message to openai thread & append result onto thread state var
  • Response => Trigger message run & when event thread.message.created is given, its return value is the message which should be appended to the thread state var & when events thread.message.delta are given, its return value should be appended to the specific message in the thread state var & when event thread.message.completed is given, its return value is the message which should overwrite the message

Backend & Frontend

  • The backend will essentially act as a proxy
  • The frontend will create a few functions to handle the logic, which can be stored in seperate files to simplify the view
  • The frontend development experience will then consist of 4 elements
    • loadThread()
    • addMessage()
    • run()
    • the state variable that holds the up-to-date context (note, it is in desc order of time)

remix-openai-assistants-event-streaming's People

Contributors

abhinandshibu avatar

Stargazers

 avatar

Watchers

 avatar

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.