Skip to main content
Version: v5.0

Custom agent

You can create your own custom agent class and invoke it when you create an agent.

Creating a custom agent class#

Do create a custom agent class, do the following:

  1. Import Agent from '@dtplatform/agent-core'.

    import { Agent } from '@dtplatform/agent-core';
  2. Extend the Agent class to create your own agent class.

    export default class MyFormatterAgent extends Agent {  constructor(options: any) {    super(options);  }  ...}
  3. Add a method called processRequest with the following parameters and structure:

      async processRequest(    type: string,    state?: any,    prompt?: string,    tools?: any[],    userId?: string,    sessionId?: string,    chatHistory?: [],    additionalParams?: Record<string, string>,  ): Promise<any> {
      }
  4. Inside this method, add your agent's logic. Make a call to the LLM using this.callLLM:

      async processRequest(    type: string,    state?: any,    prompt?: string,    tools?: any[],    userId?: string,    sessionId?: string,    chatHistory?: [],    additionalParams?: Record<string, string>,  ): Promise<any> {    //your code here    const res: any = await this.callLLM(type, state, prompt, tools);    //your code here  }
  5. Upload the script using IafAISvc.uploadAgentSourceCode. Either upload the class as string content or as a file:

    const sourceCode = { _content: 'your script content' };await IafAISvc.uploadAgentSourceCode(agentId, sourceCode, ctx);
    //orconst sourceFile = { _file: .ts file };await IafAISvc.uploadAgentSourceCode(agentId, sourceFile, ctx);

Custom agent class invocation#

To invoke a custom agent class, create an agent with Iaf, then set the agent's _agentClass property to your custom agent class:

  const formatterAgent = await IafAISvc.createAgents([{    _name: "Asset Info Agent",    _background: "Use the GetAssetsTool and return its output",    _userType: "asset_info_agent",    _type: "user_agent",    _namespaces: [      "IPUTTheExchange_EleSnXBw"    ],    _config: {      "_model": "gpt-4o",      "_provider": "openai"    },    _tools: [      "GetAssetsTool"    ],    _agentClass: "MyFormatterAgent" //references custom agent class  }]);