textObject()

Generates a JSON response using the configured AI model and tools

const agent = new Agent({...});
 
// Simple textObject generation
const textObjectGeneration = await agent.textObject({
  messages: ["What's the weather like?"],
  structuredOutput: {
    weather: {
      type: "object",
      items: {
        temperature: {
          type: "string"
        },
        humidity: {
          type: "string"
        },
        description: {
          type: "string"
        }
      }
    }
  }
});
 
// With step tracking
const stepTrackResult = await agent.textObject({
  messages: ["Send an email to john@example.com"],
  structuredOutput: {
    recipient: {
      type: "string"
    },
    emailContent: {
      type: "string"
    }
  }
  onStepFinish: (step) => {
  console.log(`Step completed: ${step}`)
  },
  maxSteps: 3
});

API Signature

Parameters

messages:

Array<string>
An array of messages to generate process

structuredOutput:

JSON schema
JSON structure for the model to use as guide in its response

onStepFinish?:

(step: string) => void;
Callback function called after each step. Receives step details as a JSON string

maxSteps?:

number
Maximum number of tool execution steps allowed. (Defaults to 5)

Returns

object?:

object
The generated structured response (typed according to the structuredOuput)

toolCalls?:

Array<ToolCall>
The tool calls made during the text generation process
ToolCall

toolName:

string
The name of the tool

args:

any
The arguments passed to the tool

error?:

string
Error message if the tool execution fails

MIT 2024 © Nextra.