-
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
0 parents
commit ce1808f
Showing
10 changed files
with
2,214 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
.DS_Store | ||
node_modules | ||
dist | ||
lib | ||
.malagu | ||
.env | ||
*.log | ||
.idea | ||
.metadata | ||
*.iml | ||
jdt.ls-java-project | ||
lerna-debug.log | ||
.nyc_output | ||
coverage | ||
errorShots | ||
.browser_modules | ||
**/docs/api | ||
package-backup.json | ||
.history | ||
.Trash-* | ||
packages/plugin/typedoc | ||
plugins |
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,23 @@ | ||
{ | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "Malagu Debug", | ||
"request": "launch", | ||
"runtimeArgs": [ | ||
"malagu", | ||
"serve" | ||
], | ||
"runtimeExecutable": "npx", | ||
"skipFiles": [ | ||
"<node_internals>/**" | ||
], | ||
"env": { | ||
"NODE_ENV": "development" | ||
}, | ||
"internalConsoleOptions": "openOnSessionStart", | ||
"outputCapture": "std", | ||
"type": "pwa-node" | ||
} | ||
] | ||
} |
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,29 @@ | ||
# OPEN AI 的 API 代理项目 | ||
|
||
在国内直接访问 OPEN AI 的 API 存在被封账号的风险,本项目可以快速搭建一个 OPEN AI 的接口代理服务。然后一键部署到腾讯云云函数的国外节点上,比如新加坡地域,不需要特意买一台云服务器,按量付费,成本低,且无需运维。 | ||
|
||
[](https://cloudstudio.net/#https://github.com/cellbang/openai-proxy.git) | ||
|
||
## 本地运行 | ||
|
||
```bash | ||
yarn start | ||
``` | ||
|
||
## 配置 AKSK 和地域 | ||
|
||
```bash | ||
malagu config -m scf | ||
``` | ||
|
||
## 一键部署 | ||
|
||
```bash | ||
malagu deploy -m scf | ||
``` | ||
|
||
## 配置 | ||
|
||
1. 环境变量 `OPENAI_PROXY_DEFAULT_API_KEY`,配置访问被代理的 OPEN AI API 的默认 API Key。当前客户端请求没有携带认证 API KEY 时使用。 | ||
|
||
1. 环境变量 `OPENAI_PROXY_CORS_ORIGIN`,配置允许跨域请求策略。 |
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,12 @@ | ||
openAIProxy: | ||
defaultAPIKey: ${{env.OPENAI_PROXY_DEFAULT_API_KEY}} | ||
malagu: | ||
web: | ||
cors: | ||
origin: ${{env.OPENAI_PROXY_CORS_ORIGIN}} | ||
cloud: | ||
function: | ||
timeout: 900 | ||
apiGateway: | ||
api: | ||
serviceTimeout: 900 |
Empty file.
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,30 @@ | ||
{ | ||
"name": "openai-proxy", | ||
"keywords": [ | ||
"malagu-component" | ||
], | ||
"version": "0.0.0", | ||
"license": "MIT", | ||
"files": [ | ||
"lib", | ||
"src" | ||
], | ||
"dependencies": { | ||
"@malagu/core": "2.45.2", | ||
"@malagu/mvc": "2.45.2", | ||
"http-proxy": "^1.18.1" | ||
}, | ||
"devDependencies": { | ||
"@malagu/cli": "2.45.2", | ||
"@types/http-proxy": "^1.17.8" | ||
}, | ||
"scripts": { | ||
"clean": "rimraf lib dist .malagu", | ||
"build": "malagu build -m server", | ||
"start": "malagu serve", | ||
"deploy": "malagu deploy -m test", | ||
"deploy:test": "malagu deploy -m test", | ||
"deploy:pre": "malagu deploy -m pre", | ||
"deploy:prod": "malagu deploy -m prod" | ||
} | ||
} |
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,3 @@ | ||
import './proxy'; | ||
import { autoBind } from '@malagu/core'; | ||
export default autoBind(); |
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,38 @@ | ||
import * as Server from 'http-proxy'; | ||
import { HttpHeaders } from '@malagu/web'; | ||
import { Middleware, CORS_MIDDLEWARE_PRIORITY, Context } from '@malagu/web/lib/node'; | ||
import { Component, PostConstruct, Value } from '@malagu/core'; | ||
|
||
interface OpenAIProxyOptions { | ||
defaultAPIKey?: string; | ||
} | ||
|
||
@Component(Middleware) | ||
export class CodingProxyMiddleware implements Middleware { | ||
|
||
protected proxy: Server; | ||
|
||
@Value('openAIProxy') | ||
protected readonly options?: OpenAIProxyOptions; | ||
|
||
@PostConstruct() | ||
protected init(): void { | ||
this.proxy = Server.createProxy(); | ||
this.proxy.on('proxyReq', proxyReq => { | ||
if (!proxyReq.hasHeader(HttpHeaders.AUTHORIZATION) && this.options?.defaultAPIKey) { | ||
proxyReq.setHeader(HttpHeaders.AUTHORIZATION, `Bearer ${this.options.defaultAPIKey}`); | ||
} | ||
}); | ||
} | ||
|
||
async handle(ctx: Context, next: () => Promise<void>): Promise<void> { | ||
const { request, response } = ctx; | ||
this.proxy.web(request, response, { | ||
target: 'https://api.openai.com', | ||
changeOrigin: true, | ||
}); | ||
return; | ||
} | ||
priority = CORS_MIDDLEWARE_PRIORITY - 10; | ||
|
||
} |
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,29 @@ | ||
{ | ||
"compilerOptions": { | ||
"skipLibCheck": true, | ||
"declaration": true, | ||
"declarationMap": true, | ||
"noImplicitAny": true, | ||
"noEmitOnError": false, | ||
"noImplicitThis": true, | ||
"noUnusedLocals": true, | ||
"strictNullChecks": true, | ||
"experimentalDecorators": true, | ||
"emitDecoratorMetadata": true, | ||
"downlevelIteration": true, | ||
"module": "commonjs", | ||
"moduleResolution": "node", | ||
"jsx": "react", | ||
"target": "ES2017", | ||
"lib": [ | ||
"ES2017", | ||
"dom" | ||
], | ||
"sourceMap": true, | ||
"rootDir": "src", | ||
"outDir": "lib" | ||
}, | ||
"include": [ | ||
"src" | ||
] | ||
} |
Oops, something went wrong.