Function that handles sampling requests
Properly typed ADK sampling handler
import { createSamplingHandler, Gemini } from "@iqai/adk";
const llm = new Gemini("gemini-2.0-flash-exp");
// Example 1: Return full LlmResponse
const samplingHandler1 = createSamplingHandler(async (request) => {
const responses = [];
for await (const response of llm.generateContentAsync(request)) {
responses.push(response);
}
return responses[responses.length - 1];
});
// Example 2: Return simple string
const samplingHandler2 = createSamplingHandler(async (request) => {
const lastMessage = request.contents[request.contents.length - 1].parts[0].text;
return await runner.ask(lastMessage);
});
// Example 3: Direct function reference
const samplingHandler3 = createSamplingHandler(runner.ask);
Helper function to create a sampling handler with proper TypeScript types.