-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4536205
commit 6b9b484
Showing
4 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
} | ||
} |