0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ai-sdkをプロキシ環境化で使用する方法

Posted at

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,
});
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?