UNPKG

@mintplex-labs/openai-assistant-swarm

Version:

A simple library the extends the OpenAI NodeJS SDK so you can automatically delegate any task to any assistants you create in OpenAi through one united interface and manager. Now you can delegate work to a swarm of assistant all specialized with specific

104 lines (99 loc) 3.43 kB
import OpenAI from 'openai'; import EventEmitter from 'events'; import { Assistant } from 'openai/resources/beta/assistants/assistants'; import { Run, RunCreateParams } from 'openai/resources/beta/threads/runs/runs'; type EventTypes = "debug" | "child_assistants_complete" | "parent_assistant_complete" | "parent_text_response" | "poll_event"; type ManagerOptions = { debug: boolean; managerAssistantOptions?: { name: string; model: string | "gpt-3.5-turbo"; instructions?: string; }; }; type DelegationResponse = { concludedPrimaryRun: ParentResponseEvent["data"]["parentRun"]; subRuns: DelegateRun[]; }; type DelegateRun = { status: "success" | "failed"; assistant: Assistant | null; parentRun: OpenAI.Beta.Threads.Run; parentToolCallId: OpenAI.Beta.Threads.Runs.FunctionToolCall["id"]; thread: OpenAI.Beta.Thread | null; run: OpenAI.Beta.Threads.Run | null; abortReason: string | null; textResponse: string | null; playground: string | null; }; type ParentResponseEvent = { data: { parentRun: OpenAI.Beta.Threads.Run & { playground: string | null; textResponse: string | null; }; }; }; type SubRunResponseEvent = { data: { subRuns: DelegateRun[]; }; }; type PollEvent = { data: object; }; declare class SwarmManager { emitter: EventEmitter; client: OpenAI; knownAssistantIds?: string[]; assistants: OpenAI["beta"]["assistants"]; log: (_: any) => void; logGroup: (_: any, __?: any) => void; private ready; private _mgrBotName; private mgrAssistant?; private options; constructor(client: OpenAI, options?: ManagerOptions); private isReady; private findOrUpsertManager; private delegateTaskToChildren; /** * Initializes your account with the primary assistant to execute and delegate to existing assistants */ init(): Promise<void>; /** * Returns a list of all assistants connected to this OpenAI apiKey. */ allAssistants(): Promise<Assistant[]>; /** * Returns multiple assistants at once from multiple ids. */ getAssistants(assistantIds?: string[]): Promise<Assistant[]>; /** * Cleanup and remove primary swarm assistant manager from your account. * Only removes the one created with this script using the params defined during creation of class. */ cleanup(): Promise<void>; /** * Emit informative event from the swarm process running. */ emitEvent(event: EventTypes, args: ParentResponseEvent | SubRunResponseEvent | PollEvent): void; /** * Generate the Playground link for an assistant and thread for visualization in the browser. */ playgroundLink(run?: Run | null): string | null; /** * Given a single prompt we will create a thread and then find the best option * for assistant execution to complete or fulfill the task. */ delegateWithPrompt(prompt: string, assistantIds?: string[], runOpts?: RunCreateParams): Promise<DelegationResponse>; } interface OpenAIExtended extends OpenAI { beta: OpenAI["beta"] & { assistants: OpenAI["beta"]["assistants"] & { swarm: SwarmManager; }; }; } declare function EnableSwarmAbilities(client: OpenAI, options?: SwarmManager["options"]): OpenAIExtended; export { EnableSwarmAbilities, OpenAIExtended };