@rhyizm/ai-enbod
TypeScript icon, indicating that this package has built-in type declarations

0.1.7 • Public • Published

npm version AGPL-3.0 License

AI Enbod

AI Enbod is an API wrapper / AI Embodi Framework designed to simplify the usage of the OpenAI Assistant API (Beta). This framework provides automated handling of Run and Thread management, as well as Function Calling (tool invocation).
The framework abstracts synchronous function calling for multiple functions and workflows that involve invoking functions and assistants, enabling AI specialized in different tasks to work collaboratively. This allows for the realization of more complex workflows with simple implementations.
It maps functions implemented by the user on the server side to functions registered in Function Calling, handling the execution of functions and returning results to OpenAI. Confidential arguments or those obtained server-side can be configured using the argumentMap feature.



AI Enbodは、OpenAI Assistant API(ベータ版)の使用を簡素化するために設計された API wrapper / AI Embodi Framework です。このフレームワークは、RunやThreadの管理、さらにFunction Calling(ツール呼び出し)の自動処理を提供します。
このフレームワークは、複数の関数を同期的に呼び出すFunction Callingと、関数やアシスタントを呼び出すワークフローを抽象化しており、異なるタスクに特化したAIが連携して動作できるようにします。これにより、シンプルな実装でより複雑なワークフローを実現することが可能になります。
サーバーサイドでユーザーが実装した関数をFunction Callingに登録された関数とマッピングし、関数の実行結果をOpenAI APIへ返却します。非公開にしたい引数やサーバーサイドで取得する引数は、argumentMap機能を使用して設定することができます。

Table of Contents


Features

1. Abstraction of Complex Flows in the OpenAI Beta API

In OpenAI Beta, very complex interactions are required, such as creating threads, generating Runs, and making tool calls when the status becomes requires_action.
AI Enbod abstracts these flows with classes and methods, so that chat and tool calls can be completed with simple method calls.

2. Automatic Handling of Function Calling (Tool Calling)

Implementing the function calling feature proposed by OpenAI on your own can lead to cumbersome code involving mapping functions, formatting arguments, and returning results.
AI Enbod uses mechanisms called functionMap and argumentMap to register "function name → implementation function", and if instructed by the assistant, it automatically executes the corresponding function and returns the result to OpenAI.
Arguments that you want to set in advance on the server side (such as user IDs) can be set with argumentMap.

3. Workflow for Calling Multiple Assistants

It supports not only a single assistant but also flows that link assistants together.
You can call assistants with other assistantIds as needed, allowing AI specialized in different tasks to collaborate, enabling the implementation of advanced scenarios where multiple AIs work together.


Operating Environment

  • Node.js v18 or higher
  • Any of npm / yarn / pnpm
  • TypeScript 4.0 or higher recommended

Note: To use OpenAI Beta features, you need an environment that can access the latest beta version of OpenAI's API and an API Key.


Installation

Below is an example using npm. Any preferred package manager is also fine.

npm install @rhyizm/AI Enbod

Usage

Below is sample code that chats with an assistant using AI Enbod.

import { Assistant } from '@rhyizm/ai-enbod';
import functionMap from "../src/functionMap";
import getAuthorInfo from "./getAuthorInfo";

// Set your API key in the environment variable
// No need to explicitly set the API key here if you have already set it in the environment variable

// Set your assistant ID in the environment variable
const assistantId = process.env.ASSISTANT_ID;

// Register your function to the functionMap if you want to use it in the chat
const myFunctionMap = {
  ...functionMap,
  getAuthorInfo
};

const assistant = new Assistant({
  assistantId: assistantId,
  functionMap: myFunctionMap,
  argumentMap: {
    getAuthorInfo: {
      name: "rhyizm"
    }
  },
});

const response = await assistant.chat({
  userMessage: "Tell me about your author",
});

console.log(response.assistant.message.text);
// => "My author is rhyizm. He is a software engineer."

Call Another Assistant

By registering the callAssistant function in the Assistant of the OpenAI Assistant API, you can call other Assistants. As needed, the callAssistant function can be used to call other Assistants, allowing the conversation to continue.

Below is an example definition of the callAssistant function.

{
  "name": "callAssistant",
  "description": "{'Your Role':'Depending on the content of the conversation, an appropriate assistant will be called upon to continue the conversation.','Assistants':[{'Name':'Translator','Role':'Translates into English','ID':'asst_HjpL3IVhC4UxM2ydiAVSBL6T'}]}",
  "strict": false,
  "parameters": {
    "type": "object",
    "properties": {
      "assistantId": {
        "type": "string",
        "description": "Assistant ID to be called."
      }
    },
    "required": [
      "assistantId"
    ]
  }
}

License

AGPL-3.0

Package Sidebar

Install

npm i @rhyizm/ai-enbod

Weekly Downloads

4

Version

0.1.7

License

AGPL-3.0

Unpacked Size

105 kB

Total Files

48

Last publish

Collaborators

  • rhyizm