Back to prompt library
Integrating Local Tool Functionality
Linked challenge: Local Multi-LLM Chat Agent
Prompt source
implementation39 lines8 sectionsNo variables1 code block
Create a client-side tool (e.g., `getLocalTime` which returns the current local time) and integrate it with your AI SDK agent. The agent should be able to call this tool when appropriate (e.g., if the user asks 'What time is it?'). Show how to define the tool and pass it to `useChat` or `streamText`.
```typescript
// lib/tools.ts (example tool definition)
export async function getLocalTime() {
return new Date().toLocaleTimeString();
}
export const tools = {
getLocalTime: {
description: 'Gets the current local time.',
parameters: { type: 'object', properties: {} },
execute: getLocalTime,
},
};
// app/page.tsx (client component, using useChat with tools)
// ...
import { useChat } from 'ai/react';
import { tools } from '../lib/tools';
export default function Chat() {
const { messages, input, handleInputChange, handleSubmit } = useChat({
api: '/api/chat', // Your API route
tools: tools, // Pass your tools here
});
// ... rest of your component
}
// api/chat/route.ts (server-side, to enable tool calling on the model)
// ...
import { experimental_streamText } from 'ai'; // Use experimental for tool calling
// ...
const result = await experimental_streamText({
model: modelToUse,
messages,
tools: {
getLocalTime: {
description: 'Gets the current local time.',
parameters: { type: 'object', properties: {} },
},
},
});
```