Intercepting Fetch Requests
Learn how to intercept fetch requests using the AI TOOLKIT and Node
Many providers support setting a custom fetch function using the fetch argument in their factory function. A custom fetch function can be used to intercept and modify requests before they are sent to the provider's API, and to intercept and modify responses before they are returned to the caller. Use cases for intercepting requests include: - Logging requests and responses - Adding authentication headers - Modifying request bodies - Caching responses - Using a custom HTTP client
Run it locally
$ npm install aiExample
import { generateText, createGateway } from 'ai-toolkit';
const gateway = createGateway({
// example fetch wrapper that logs the input to the API call:
fetch: async (url, options) => {
console.log('URL', url);
console.log('Headers', JSON.stringify(options!.headers, null, 2));
console.log(
Body ${JSON.stringify(JSON.parse(options!.body! as string), null, 2)},
);
return await fetch(url, options);
},
});
const { text } = await generateText({
model: gateway('openai/gpt-4o'),
prompt: 'Why is the sky blue?',
});
- 1import { generateText, createGateway } from 'ai-toolkit';
- 2const gateway = createGateway({
- 3 // example fetch wrapper that logs the input to the API call:
- 4 fetch: async (url, options) => {
- 5 console.log('URL', url);
- 6 console.log('Headers', JSON.stringify(options!.headers, null, 2));
- 7 console.log(
- 8 `Body ${JSON.stringify(JSON.parse(options!.body! as string), null, 2)}`,
- 9 );
- 10 return await fetch(url, options);
- 11 },
- 12});
- 13const { text } = await generateText({
- 14 model: gateway('openai/gpt-4o'),
- 15 prompt: 'Why is the sky blue?',
- 16});