Skip to content

Commit

Permalink
add google api proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
windmemory committed Jan 9, 2025
1 parent 4536205 commit 6b9b484
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import configuration from './config/configuration';
import { OpenaiProxyModule } from './openai/openai-proxy.module';
import { NestjsFormDataModule } from 'nestjs-form-data';
import { AnthropicProxyModule } from './anthropic/anthropic-proxy.module';
import { GoogleProxyModule } from './google/google-proxy.module';

@Module({
imports: [
Expand All @@ -14,6 +15,7 @@ import { AnthropicProxyModule } from './anthropic/anthropic-proxy.module';
NestjsFormDataModule.config({ isGlobal: true }),
OpenaiProxyModule,
AnthropicProxyModule,
GoogleProxyModule,
],
})
export class AppModule {}
52 changes: 52 additions & 0 deletions src/google/google-proxy.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import {
Body,
Controller,
Headers,
HttpCode,
HttpException,
Inject,
Post,
Query,
Request,
} from '@nestjs/common';
import { Request as ExpressRequest } from 'express';
import { GoogleProxyService } from './google-proxy.service';

@Controller()
export class GoogleProxyController {
@Inject()
private readonly service: GoogleProxyService;

@Post('/google/v1beta/models/:reqParams')
@HttpCode(200)
async generateContent(
@Body() body: any,
@Headers() headers: any,
@Query() query: any,
@Request() req: ExpressRequest,
) {
const params = req.params;
const reqParams = params.reqParams;
const [model, method] = reqParams.split(':');

if (method === 'generateContent') {
const result = await this.service.generateContent(
body,
headers,
query,
model,
);
return result;
} else if (method === 'streamGenerateContent') {
const result = await this.service.streamGenerateContent(
body,
headers,
query,
model,
);
return result;
} else {
throw new HttpException('Method not found', 404);
}
}
}
9 changes: 9 additions & 0 deletions src/google/google-proxy.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Module } from '@nestjs/common';
import { GoogleProxyService } from './google-proxy.service';
import { GoogleProxyController } from './google-proxy.controller';

@Module({
providers: [GoogleProxyService],
controllers: [GoogleProxyController],
})
export class GoogleProxyModule {}
92 changes: 92 additions & 0 deletions src/google/google-proxy.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { HttpException, Inject, Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import axios from 'axios';
import { Agent as HttpAgent } from 'http';
import { Agent as HttpsAgent } from 'https';
import { SocksProxyAgent } from 'socks-proxy-agent';
import { MINUTE } from 'src/common/time';

@Injectable()
export class GoogleProxyService {
@Inject()
private readonly configService: ConfigService;

async generateContent(body: any, headers: any, query: any, model: string) {
const url = `https://generativelanguage.googleapis.com/v1beta/models/${model}:generateContent`;

return this.makeRequest(url, headers, body, query, false);
}

async streamGenerateContent(
body: any,
headers: any,
query: any,
model: string,
) {
const url = `https://generativelanguage.googleapis.com/v1beta/models/${model}:streamGenerateContent`;

return this.makeRequest(url, headers, body, query, true);
}

private async makeRequest(
url: string,
headers: any,
body: any,
query: any,
stream?: boolean,
) {
const { httpAgent, httpsAgent } = this.getAgents();
let response: any;
try {
response = await axios(url, {
httpAgent,
httpsAgent,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'anthropic-version': headers['anthropic-version'],
'x-api-key': headers['x-api-key'],
},
params: query,
responseType: stream ? 'stream' : 'json',
data: body,
timeout: 10 * MINUTE,
});
} catch (e) {
if (e.response) {
if (body.stream) {
return e.response.data;
}
throw new HttpException(e.response.data, e.response.status);
} else if (e.request) {
console.log(e.message);
throw new Error(
`Failed to send message. error message: ${e.message}, request: ${e.request}`,
);
} else {
throw e;
}
}

if (response.status !== 200) {
const error = new HttpException(response.data, response.status);
throw error;
}
return response.data;
}

private getAgents() {
const socksHost = this.configService.get<string | undefined>('socksHost');
let httpAgent: HttpAgent | undefined;
let httpsAgent: HttpsAgent | undefined;
if (socksHost) {
httpAgent = new SocksProxyAgent(socksHost);
httpsAgent = new SocksProxyAgent(socksHost);
httpsAgent.options.rejectUnauthorized = false;
}
return {
httpAgent,
httpsAgent,
};
}
}

0 comments on commit 6b9b484

Please sign in to comment.