agents-js v1.0.5: Can I manually execute an LLM to...
# ask-ai
f
agents-js v1.0.5: Can I manually execute an LLM tool (with known parameters) ?
t
Thanks for reaching out, Steve. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Absolutely! You can manually execute an LLM tool with known parameters in agents-js v1.0.5. The framework is designed to let you directly call a tool's
execute
method using valid arguments that match the tool's defined schema, without needing to rely on LLM-triggered execution. Here’s a friendly example to guide you:
Copy code
import { z } from 'zod';
import { llm } from '@livekit/agents';

// Define a tool to get the weather
const getWeather = llm.tool({
  description: 'Get the weather for a given location',
  parameters: z.object({
    location: z.string().describe('The city and state, e.g. San Francisco, CA'),
    unit: z.enum(['celsius', 'fahrenheit']).describe('The temperature unit to use'),
  }),
  execute: async ({ location, unit }, { ctx }) => {
    // Simulate external service call
    return `The weather in ${location} is 25 degrees ${unit}.`;
  },
});

// Manually execute the tool with known parameters
const result = await getWeather.execute(
  { location: 'Paris', unit: 'celsius' },
  { ctx: { userData: {} }, toolCallId: 'test-call-123' }
);

console.log(result); // Output: "The weather in Paris is 25 degrees celsius."
To make sure everything runs smoothly, here are a couple of important points to keep in mind: • Schema Validation: Make sure the arguments you provide match the tool's parameter schema exactly. If something’s off, a validation error will let you know. • ToolOptions Context: The second argument to
execute
should be a
ToolOptions
object, which must include
ctx
(a
RunContext
), and can optionally have
toolCallId
and
abortSignal
. Using the
createToolOptions
helper can make this easier. This method is fully supported and dependable in agents-js v1.0.5, so you can confidently use it in your projects. If you have any more questions or need further assistance, I’m here to help! Sources: README.md