Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
muxiangqiu authored and yangsubo committed Apr 11, 2023
0 parents commit ce1808f
Show file tree
Hide file tree
Showing 10 changed files with 2,214 additions and 0 deletions.
22 changes: 22 additions & 0 deletions .gitignore
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
23 changes: 23 additions & 0 deletions .vscode/launch.json
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"
}
]
}
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# OPEN AI 的 API 代理项目

在国内直接访问 OPEN AI 的 API 存在被封账号的风险,本项目可以快速搭建一个 OPEN AI 的接口代理服务。然后一键部署到腾讯云云函数的国外节点上,比如新加坡地域,不需要特意买一台云服务器,按量付费,成本低,且无需运维。

[![Cloud Studio Template](https://cs-res.codehub.cn/common/assets/icon-badge.svg)](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`,配置允许跨域请求策略。
12 changes: 12 additions & 0 deletions malagu-remote.yml
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 added malagu.yml
Empty file.
30 changes: 30 additions & 0 deletions package.json
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"
}
}
3 changes: 3 additions & 0 deletions src/module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import './proxy';
import { autoBind } from '@malagu/core';
export default autoBind();
38 changes: 38 additions & 0 deletions src/proxy.ts
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;

}
29 changes: 29 additions & 0 deletions tsconfig.json
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"
]
}
Loading

0 comments on commit ce1808f

Please sign in to comment.