OvidijusParsiunas / deep-chat

Fully customizable AI chatbot component for your website
https://deepchat.dev
MIT License
1.43k stars 218 forks source link

Ability to create a new OpenAI assistant #97

Closed bruffridge closed 8 months ago

bruffridge commented 8 months ago

Feature request

Currently you have to specify the ID of an already existing Assistant. I would like the ability to either specify an ID to an existing Assistant or an object that defines the configuration of a New Assistant. The object option would allow for an assistant to be created. Here's an example of what this could look like.

Existing Assistant

<deep-chat
  directConnection='{
    "openAI": {
      "key": "placeholder key",
      "assistant": {"assistant_id": "placeholder-id"}
  }}'
  mixedFiles="true"
></deep-chat>

New Assistant

<deep-chat
  directConnection='{
    "openAI": {
      "key": "placeholder key",
      "assistant": {"assistant_id": {
        "instructions": "You are a personal math tutor. When asked a question, write and run Python code to answer the question.",
        "name": "Math Tutor",
        "tools": [{ "type": "code_interpreter" }],
        "model": "gpt-4",
      }}
  }}'
  mixedFiles="true"
></deep-chat>

Similar to the OpenAI key, please make the newly created Assistant ID accessible (perhaps in deepChatRef._activeService). My desire is to save the assistant ID in localstorage, and use that in subsequent visits by the same user, so duplicate Assistants won't be created in their account.

Context

I recently learned that API keys only allow access to Assistants within the account. I want to have users pay their own OpenAI fees, this would allow them to create the Assistant within their own account. This will also help ensure users can't see or modify other users' threads, runs, messages, and files.

OvidijusParsiunas commented 8 months ago

Hi @bruffridge.

This would be an interesting feature. The problem is I can see that there would need to be quite a few code enhancements for this to work (the component and the playground). I am committed to adding this feature, but it will take some time and I will likely have a working prototype by the end of this week/early next week as there are few other features that I am working on and am also maintaining other projects.

I will keep you posted on any progress. Thanks.

OvidijusParsiunas commented 8 months ago

If you do need this feature immediately I can work via a sponsorship But I will be adding the change to the component later anyway, so patience is bliss :)

bruffridge commented 8 months ago

I wish I could sponsor this project and you. Your responsiveness has been outstanding. However, the project I'm using this in is open source and free to use as well, so no budget for it unfortunately.

OvidijusParsiunas commented 8 months ago

Don't worry about it. I mentioned it just in-case you need it asap. This is a rather large feature hence I just wanted to give you a heads up that it will take longer than my previous smaller updates.

I forgot to mention that you can always fork/clone the project and design it the way you want it. It's actually pretty easy and we detail everything in the Create your own Deep Chat component section, so the setup is very simple.

Ofcourse, this is a feature I will be adding in the near future anyway. So waiting can be the easiest option :)

OvidijusParsiunas commented 8 months ago

Hi, you are now able to create OpenAI Assistants in deep-chat-dev and deep-chat-react-dev packages version 9.0.117.

To do this, you can emit the use of the assistant_id property, which will automatically create a new assistant when the user attempts to send a message. It is important to note that the assistant will only be created upon attempting to send a new message and not on chat render because it would otherwise be creating assistants that never get used as some users may navigate to a website without wanting to use the chat, and may go there repetitively - hence creating multiple empty assistants, therefore creating an assistant is most appropriate when it actually needs to be used.

Ofcourse, you can also define the kind of assistant you want to create, this is the new TypeScript interface:

export interface OpenAIAssistant {
  assistant_id?: string;
  new_assistant?: OpenAINewAssistant;
  function_handler?: AssistantFunctionHandler;
}

Where the new OpenAINewAssistant interface is the following:

export interface OpenAINewAssistant {
  model?: string;
  name?: string;
  description?: string;
  instructions?: string;
  tools?: {
    type: 'code_interpreter' | 'retrieval' | 'function';
    function?: {name: string; description?: string; parameters?: object};
  }[];
  file_ids?: string[];
}

The API will use the gpt-4 model by default.

You can access the new assistant_id from the Deep Chat element reference via: chatRef._activeService.rawBody.assistant_id

I will include this in the next release.

Let me know if you have any questions.

bruffridge commented 8 months ago

Thanks! Excited to try this out.

OvidijusParsiunas commented 8 months ago

This has now been released in main packages version 1.4.10. Checkout the new Assistants documentation for more.

I will close this issue, nevertheless feel free to comment below.