Vercelのai-sdkを使用しようとしたとき、プロキシの設定にハマりました。
こうするといけます。
proxyFetch.ts
import { ProxyAgent } from 'proxy-agent'
import fetch from 'node-fetch';
// プロキシの設定
const proxyUrl = process.env.HTTP_PROXY || '';
const agent = new ProxyAgent(proxyUrl);
// カスタム fetch 実装
export const proxyFetch = async (input: RequestInfo, init?: RequestInit): Promise<Response> => {
const response = await fetch(input, { ...init, agent });
const text = await response.text();
return new Response(text, {
status: response.status,
statusText: response.statusText,
headers: response.headers,
});
};
mode.ts
import { createOpenAI } from '@ai-sdk/openai';
import { createGoogleGenerativeAI } from '@ai-sdk/google';
import { proxyFetch } from '../../utils/proxyFetch';
export const openai = createOpenAI({
apiKey: process.env.OPENAI_API_KEY || "",
fetch: proxyFetch,
});
export const google = createGoogleGenerativeAI({
apiKey: process.env.GOOGLE_API_KEY || "",
fetch: proxyFetch,
});