generateText Method

The generateText method allows you to generate text using the AI model configured in your W-Kits Agent. This method provides a flexible way to interact with the AI, with support for various parameters and tools.

Method Signature

async generateText(options: {
  prompt?: string;
  system?: string;
  temperature?: number;
  maxTokens?: number;
  topP?: number;
  topK?: number;
  presencePenalty?: number;
  frequencyPenalty?: number;
  stopSequences?: string[];
  seed?: number;
  toolChoice?: 'auto' | 'none' | 'required' | { type: 'tool'; toolName: string };
  maxSteps?: number;
}): Promise<GenerateTextResult>

Parameters

The generateText method accepts an options object with the following properties:

  • prompt (optional): The input prompt for the AI model.
  • system (optional): A system message to set the behavior of the AI.
  • temperature (optional): Controls the randomness of the output (0 to 1).
  • maxTokens (optional): The maximum number of tokens to generate.
  • topP (optional): Nucleus sampling parameter (0 to 1).
  • topK (optional): Top-k sampling parameter.
  • presencePenalty (optional): Penalizes new tokens based on their presence in the text.
  • frequencyPenalty (optional): Penalizes new tokens based on their frequency in the text.
  • stopSequences (optional): Array of sequences that will stop the generation when encountered.
  • seed (optional): Seed for deterministic generation.
  • toolChoice (optional): Specifies how tools should be used ('auto', 'none', 'required', or a specific tool).
  • maxSteps (optional): Maximum number of steps for multi-step generation.

Usage Example

Here's an example of how to use the generateText method:

import { Agent } from 'w-kits';
import { google } from 'w-kits/models';

const agent = new Agent({
  model: google('gemini-2.0-flash-exp'),
  chain: 'testnet',
  privateKey: process.env.PRIVATE_KEY as `0x${string}`,
});

async function generateResponse() {
  const result = await agent.generateText({
    prompt: "What's the current gas price on WhiteChain?",
    temperature: 0.7,
    maxTokens: 100,
    toolChoice: 'auto',
  });

  console.log(result.text);
  console.log(result.toolCalls);
  console.log(result.toolResults);
}

Return Value

The generateText method returns a Promise that resolves to an object containing:

  • text: The generated text response.
  • toolCalls: An array of tool calls made during the generation process.
  • toolResults: An array of results from the tool calls.

Best Practices

  • Use clear and specific prompts to guide the AI's response.
  • Experiment with different temperature settings to find the right balance between creativity and coherence.
  • Utilize the toolChoice parameter to control when and how tools are used in the generation process.
  • Consider using stopSequences to prevent the AI from generating unwanted content.
  • For complex tasks, use the maxSteps parameter to allow for multi-step reasoning and tool usage.