本页还没有译文
本页 6 段还没译成한국어,下面显示的是英文原文。Intercepting Fetch Requests
还没翻译译自英文(ai-sdk.dev)本页 6 段都还没译
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
Example
import { generateText, createGateway } from 'ai';
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-6-astra'),
prompt: 'Why is the sky blue?',
});AI 翻译查看英文原文