Skip to content

Commit

Permalink
Improve proxy agent
Browse files Browse the repository at this point in the history
  • Loading branch information
yuezk committed Mar 6, 2023
1 parent fc0bbc4 commit b78724a
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/chatgpt.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ import { Observable } from 'rxjs';
@Injectable()
export class ChatGPTService implements OnModuleInit {
private api: ChatGPTAPI;
private httpProxy: string;
private proxyAgent: unknown;

onModuleInit() {
this.httpProxy = process.env.HTTP_PROXY;
const { HTTP_PROXY } = process.env;
if (HTTP_PROXY) {
this.proxyAgent = new ProxyAgent(HTTP_PROXY);
}

this.api = new ChatGPTAPI({
apiKey: process.env.OPENAI_API_KEY,
Expand All @@ -21,7 +24,7 @@ export class ChatGPTService implements OnModuleInit {
private proxyFetch = (url: string, options?: any) => {
return fetch(url, {
...options,
agent: this.httpProxy ? new ProxyAgent(this.httpProxy) : undefined,
agent: this.proxyAgent,
});
};

Expand Down

0 comments on commit b78724a

Please sign in to comment.