Commit 25dcfb2d authored by nanahira's avatar nanahira

first

parents
# compiled output
/dist
/node_modules
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
# OS
.DS_Store
# Tests
/coverage
/.nyc_output
# IDEs and editors
/.idea
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace
# IDE - VSCode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
/data
/output
/config.yaml
.git*
Dockerfile
.dockerignore
module.exports = {
parser: '@typescript-eslint/parser',
parserOptions: {
project: 'tsconfig.json',
tsconfigRootDir : __dirname,
sourceType: 'module',
},
plugins: ['@typescript-eslint/eslint-plugin'],
extends: [
'plugin:@typescript-eslint/recommended',
'plugin:prettier/recommended',
],
root: true,
env: {
node: true,
jest: true,
},
ignorePatterns: ['.eslintrc.js'],
rules: {
'@typescript-eslint/interface-name-prefix': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-explicit-any': 'off',
},
};
# compiled output
/dist
/node_modules
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
# OS
.DS_Store
# Tests
/coverage
/.nyc_output
# IDEs and editors
/.idea
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace
# IDE - VSCode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
/data
/output
/config.yaml
\ No newline at end of file
stages:
- build
- deploy
variables:
GIT_DEPTH: "1"
before_script:
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
.build-image:
stage: build
script:
- docker build --pull -t $TARGET_IMAGE .
- docker push $TARGET_IMAGE
build-x86:
extends: .build-image
tags:
- docker
variables:
TARGET_IMAGE: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG
.deploy:
stage: deploy
tags:
- docker
script:
- docker pull $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG
- docker manifest push $TARGET_IMAGE
deploy_latest:
extends: .deploy
variables:
TARGET_IMAGE: $CI_REGISTRY_IMAGE:latest
only:
- master
/install-npm.sh
.git*
/data
/output
/config.yaml
.idea
.dockerignore
Dockerfile
/src
{
"singleQuote": true,
"trailingComma": "all"
}
\ No newline at end of file
FROM node:lts-bullseye-slim as base
LABEL Author="Nanahira <nanahira@momobako.com>"
RUN echo 'deb https://dl.google.com/linux/chrome/deb/ stable main' > /etc/apt/sources.list.d/google-chrome.list && \
apt update && \
apt -y install python3 build-essential git google-chrome-stable libnss3 libfreetype6-dev libharfbuzz-bin libharfbuzz-dev ca-certificates fonts-freefont-otf fonts-freefont-ttf fonts-noto-cjk fonts-noto-cjk-extra fonts-wqy-microhei fonts-wqy-zenhei xvfb && \
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /var/log/*
WORKDIR /usr/src/app
COPY ./package*.json ./
FROM base as builder
RUN npm ci && npm cache clean --force
COPY . ./
RUN npm run build
FROM base
ENV NODE_ENV production
RUN npm ci && npm cache clean --force
COPY --from=builder /usr/src/app/dist ./dist
COPY ./config.example.yaml ./config.yaml
EXPOSE 3000
ENTRYPOINT [ "xvfb-run" ]
CMD [ "npm", "run", "start:prod" ]
This diff is collapsed.
# chatgpt-forwarder
Forwards ChatGPT messages to an API.
## Installation
```bash
$ npm install
```
## Config
Make a copy of `config.example.yaml` to `config.yaml`.
## Running the app
```bash
# development
$ npm run start
# watch mode
$ npm run start:dev
# production mode
$ npm run start:prod
```
## License
AGPLv3
host: '::'
port: 3000
OPENAI_EMAIL: ''
OPENAI_PASSWORD: ''
OPENAI_LOGIN_TYPE: 'default'
REDIS_URL: ''
OPENAI_PRO: false
TOKEN: ''
\ No newline at end of file
#!/bin/bash
npm install --save \
class-validator \
class-transformer \
@nestjs/swagger \
@nestjs/config \
yaml
npm install --save-dev \
@types/express
npm i --save-exact --save-dev eslint@8.22.0
{
"collection": "@nestjs/schematics",
"sourceRoot": "src",
"compilerOptions": {
"plugins": ["@nestjs/swagger"]
}
}
This diff is collapsed.
{
"name": "chatgpt-forwarder",
"version": "0.0.1",
"description": "",
"author": "",
"private": true,
"license": "UNLICENSED",
"scripts": {
"prebuild": "rimraf dist && sed -i -e 's/delay(50)/delay(200)/g' -e 's/text-davinci-002-render-next/text-davinci-002-render/' node_modules/chatgpt3/build/index.js",
"build": "nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "nest start",
"start:dev": "nest start --watch",
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "jest",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json"
},
"dependencies": {
"@nestjs/common": "^9.0.0",
"@nestjs/config": "^2.3.1",
"@nestjs/core": "^9.0.0",
"@nestjs/platform-express": "^9.0.0",
"@nestjs/swagger": "^6.2.1",
"aragami": "^1.1.2",
"chatgpt3": "npm:chatgpt@^3.5.2",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.0",
"nestjs-aragami": "^1.0.0",
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2",
"rxjs": "^7.2.0",
"uuid": "^9.0.0",
"yaml": "^2.2.1"
},
"devDependencies": {
"@nestjs/cli": "^9.0.0",
"@nestjs/schematics": "^9.0.0",
"@nestjs/testing": "^9.0.0",
"@types/express": "^4.17.17",
"@types/jest": "28.1.8",
"@types/node": "^16.0.0",
"@types/supertest": "^2.0.11",
"@types/uuid": "^9.0.0",
"@typescript-eslint/eslint-plugin": "^5.0.0",
"@typescript-eslint/parser": "^5.0.0",
"eslint": "8.22.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^4.0.0",
"jest": "28.1.3",
"prettier": "^2.3.2",
"source-map-support": "^0.5.20",
"supertest": "^6.1.3",
"ts-jest": "28.0.8",
"ts-loader": "^9.2.3",
"ts-node": "^10.0.0",
"tsconfig-paths": "4.1.0",
"typescript": "^4.7.4"
},
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "src",
"testRegex": ".*\\.spec\\.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"collectCoverageFrom": [
"**/*.(t|j)s"
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
}
import { Test, TestingModule } from '@nestjs/testing';
import { AppController } from './app.controller';
import { AppService } from './app.service';
describe('AppController', () => {
let appController: AppController;
beforeEach(async () => {
const app: TestingModule = await Test.createTestingModule({
controllers: [AppController],
providers: [AppService],
}).compile();
appController = app.get<AppController>(AppController);
});
describe('root', () => {
it('should be defined', () => {
expect(appController).toBeDefined();
});
});
});
import {
Body,
Controller,
Header,
Headers,
Post,
ValidationPipe,
} from '@nestjs/common';
import { ChatgptService } from './chatgpt/chatgpt.service';
import {
BlankReturnMessageDto,
ReturnMessageDto,
} from './dto/ReturnMessage.dto';
import { ConversationDto } from './conversation/conversation.dto';
import {
ApiBody,
ApiCreatedResponse,
ApiHeader,
ApiOperation,
} from '@nestjs/swagger';
import { TalkDto } from './chatgpt/talk.dto';
import { ResetConversationDto } from './conversation/reset-conversation.dto';
import { ConversationService } from './conversation/conversation.service';
import { AuthService } from './auth/auth.service';
const ResponseDto = ReturnMessageDto(ConversationDto);
@ApiHeader({ name: 'Authorization', required: false })
@Controller()
export class AppController {
constructor(
private readonly chatgpt: ChatgptService,
private readonly conversation: ConversationService,
private readonly authService: AuthService,
) {}
@Post('chat')
@ApiOperation({ summary: 'Chat with ChatGPT' })
@ApiBody({ type: TalkDto })
@ApiCreatedResponse({ type: ResponseDto })
async chat(
@Body(
new ValidationPipe({
transform: true,
transformOptions: { enableImplicitConversion: true },
}),
)
dto: TalkDto,
@Headers('Authorization') header: string,
) {
await this.authService.auth(header);
return new ResponseDto(201, 'success', await this.chatgpt.chat(dto));
}
@Post('reset-conversation')
@ApiOperation({ summary: 'Reset conversation' })
@ApiBody({ type: ResetConversationDto })
@ApiCreatedResponse({ type: BlankReturnMessageDto })
async resetConversation(
@Body(
new ValidationPipe({
transform: true,
transformOptions: { enableImplicitConversion: true },
}),
)
dto: ResetConversationDto,
@Headers('Authorization') header: string,
) {
await this.authService.auth(header);
await this.conversation.resetConversation(dto.session);
return new BlankReturnMessageDto(201, 'success');
}
}
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { loadConfig } from './utility/config';
import { AragamiModule } from 'nestjs-aragami';
import { ChatgptService } from './chatgpt/chatgpt.service';
import { ConversationService } from './conversation/conversation.service';
import { AuthService } from './auth/auth.service';
@Module({
imports: [
ConfigModule.forRoot({
load: [loadConfig],
isGlobal: true,
ignoreEnvVars: true,
ignoreEnvFile: true,
}),
AragamiModule.registerAsync({
inject: [ConfigService],
useFactory: (config: ConfigService) => {
const redisUrl = config.get<string>('REDIS_URL');
if (redisUrl) {
return {
redis: {
uri: redisUrl,
},
};
}
return {};
},
}),
],
controllers: [AppController],
providers: [ChatgptService, ConversationService, AuthService],
})
export class AppModule {}
import { Test, TestingModule } from '@nestjs/testing';
import { AuthService } from './auth.service';
describe('AuthService', () => {
let service: AuthService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [AuthService],
}).compile();
service = module.get<AuthService>(AuthService);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
});
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { BlankReturnMessageDto } from '../dto/ReturnMessage.dto';
@Injectable()
export class AuthService {
constructor(private config: ConfigService) {}
async auth(header: string) {
const token = this.config.get<string>('TOKEN');
if (!token) {
return true;
}
if (!header) {
throw new BlankReturnMessageDto(401, 'Token required').toException();
}
if (header.startsWith('Bearer ')) {
header = header.slice(7);
}
if (header !== token) {
throw new BlankReturnMessageDto(403, 'Invalid token').toException();
}
return true;
}
}
import { Test, TestingModule } from '@nestjs/testing';
import { ChatgptService } from './chatgpt.service';
describe('ChatgptService', () => {
let service: ChatgptService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [ChatgptService],
}).compile();
service = module.get<ChatgptService>(ChatgptService);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
});
import {
ConsoleLogger,
Injectable,
OnModuleDestroy,
OnModuleInit,
} from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import type { ChatGPTAPIBrowser } from 'chatgpt3';
import { TalkDto } from './talk.dto';
import { ConversationService } from '../conversation/conversation.service';
import { v4 as uuid } from 'uuid';
import { ConversationDto } from '../conversation/conversation.dto';
@Injectable()
export class ChatgptService
extends ConsoleLogger
implements OnModuleInit, OnModuleDestroy
{
constructor(
private config: ConfigService,
private conversationService: ConversationService,
) {
super('ChatGPT');
}
private ChatGPTAPIBrowserConstructor: typeof ChatGPTAPIBrowser;
private api: ChatGPTAPIBrowser;
async onModuleInit() {
this.ChatGPTAPIBrowserConstructor = (
await eval("import('chatgpt3')")
).ChatGPTAPIBrowser;
this.api = new this.ChatGPTAPIBrowserConstructor({
email: this.config.get('OPENAI_EMAIL'),
password: this.config.get('OPENAI_PASSWORD'),
isProAccount: !!this.config.get('OPENAI_PRO'),
isGoogleLogin: this.config.get('OPENAI_LOGIN_TYPE') === 'google',
isMicrosoftLogin: this.config.get('OPENAI_LOGIN_TYPE') === 'microsoft',
});
this.log('Initializing ChatGPT API');
await this.api.initSession();
}
async onModuleDestroy() {
await this.api.closeSession();
}
getAPI() {
return this.api;
}
async chat(question: TalkDto) {
const session = question.session || uuid();
const previousConversation = await this.conversationService.getConversation(
session,
);
const result = await this.api.sendMessage(question.text, {
...(previousConversation
? {
conversationId: previousConversation.conversationId,
parentMessageId: previousConversation.messageId,
}
: {}),
timeoutMs: 300000,
});
await this.conversationService.saveConversation(session, result);
const dto = new ConversationDto();
dto.session = session;
dto.conversationId = result.conversationId;
dto.messageId = result.messageId;
dto.text = result.response.replace(/^<!--(.*)-->$/gm, '');
return dto;
}
}
import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty, IsOptional, IsString } from 'class-validator';
export class TalkDto {
@IsOptional()
@IsString()
@ApiProperty({ description: 'Session identifier.', required: false })
session: string;
@IsString()
@IsNotEmpty()
@ApiProperty({ description: 'Message from user.' })
text: string;
}
import { CacheKey } from 'aragami';
import { ApiProperty } from '@nestjs/swagger';
export class ConversationDto {
@CacheKey()
@ApiProperty({ description: 'Session identifier.' })
session: string;
@ApiProperty({ description: 'Conversation ID from ChatGPT.' })
conversationId: string;
@ApiProperty({ description: 'Message ID from ChatGPT.' })
messageId: string;
@ApiProperty({ description: 'Message from ChatGPT.' })
text: string;
}
import { Test, TestingModule } from '@nestjs/testing';
import { ConversationService } from './conversation.service';
describe('ConversationService', () => {
let service: ConversationService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [ConversationService],
}).compile();
service = module.get<ConversationService>(ConversationService);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
});
import { Injectable } from '@nestjs/common';
import { InjectAragami } from 'nestjs-aragami';
import { Aragami } from 'aragami';
import { ConversationDto } from './conversation.dto';
import type { ChatResponse } from 'chatgpt3';
@Injectable()
export class ConversationService {
constructor(@InjectAragami() private readonly aragami: Aragami) {}
async getConversation(session: string) {
return this.aragami.get(ConversationDto, session);
}
async saveConversation(session: string, response: ChatResponse) {
return this.aragami.set(ConversationDto, {
session,
conversationId: response.conversationId,
messageId: response.messageId,
});
}
async resetConversation(session: string) {
return this.aragami.del(ConversationDto, session);
}
}
import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty, IsString } from 'class-validator';
export class ResetConversationDto {
@IsString()
@IsNotEmpty()
@ApiProperty({ description: 'Session identifier.' })
session: string;
}
import { ApiProperty } from '@nestjs/swagger';
import { HttpException } from '@nestjs/common';
export interface BlankReturnMessage {
statusCode: number;
message: string;
success: boolean;
}
export interface ReturnMessage<T> extends BlankReturnMessage {
data?: T;
}
export class BlankReturnMessageDto implements BlankReturnMessage {
@ApiProperty({ description: 'Return code' })
statusCode: number;
@ApiProperty({ description: 'Return message' })
message: string;
@ApiProperty({ description: 'Whether success.' })
success: boolean;
constructor(statusCode: number, message?: string) {
this.statusCode = statusCode;
this.message = message || 'success';
this.success = statusCode < 400;
}
toException() {
return new HttpException(this, this.statusCode);
}
}
type AnyClass = new (...args: any[]) => any;
type ClassOrArray = AnyClass | [AnyClass];
type TypeFromClass<T> = T extends new (...args: any[]) => infer U ? U : never;
export type ParseType<T extends ClassOrArray> = T extends [infer U]
? TypeFromClass<U>[]
: TypeFromClass<T>;
function getClass(o: ClassOrArray) {
return o instanceof Array ? o[0] : o;
}
export function ReturnMessageDto<T extends ClassOrArray>(type: T) {
const cl = class SpecificReturnMessage extends BlankReturnMessageDto {
data?: ParseType<T>;
constructor(statusCode: number, message?: string, data?: ParseType<T>) {
super(statusCode, message);
this.data = data;
}
};
ApiProperty({ description: 'Return data.', type, required: false })(
cl.prototype,
'data',
);
Object.defineProperty(cl, 'name', {
value: `${getClass(type).name}ReturnMessageDto`,
});
return cl;
}
export class StringReturnMessageDto
extends BlankReturnMessageDto
implements ReturnMessage<string>
{
@ApiProperty({ description: 'Return data.' })
data?: string;
constructor(statusCode: number, message?: string, data?: string) {
super(statusCode, message);
this.data = data;
}
}
import { NestFactory } from '@nestjs/core';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
import { NestExpressApplication } from '@nestjs/platform-express';
import { AppModule } from './app.module';
import { ConfigService } from '@nestjs/config';
async function bootstrap() {
const app = await NestFactory.create<NestExpressApplication>(AppModule);
app.setGlobalPrefix('api');
app.enableCors();
app.set('trust proxy', ['172.16.0.0/12', 'loopback']);
const documentConfig = new DocumentBuilder()
.setTitle('chatgpt-forwarder')
.setDescription('Forwards ChatGPT messages to an API.')
.setVersion('1.0')
.build();
const document = SwaggerModule.createDocument(app, documentConfig);
SwaggerModule.setup('docs', app, document);
const config = app.get(ConfigService);
await app.listen(
config.get<number>('port') || 3000,
config.get<string>('host') || '::',
);
}
bootstrap();
import yaml from 'yaml';
import * as fs from 'fs';
const defaultConfig = {
host: '::',
port: 3000,
OPENAI_EMAIL: '',
OPENAI_PASSWORD: '',
REDIS_URL: '',
OPENAI_LOGIN_TYPE: 'default',
OPENAI_PRO: false,
};
export type Config = typeof defaultConfig;
export async function loadConfig(): Promise<Config> {
let readConfig: Partial<Config> = {};
try {
const configText = await fs.promises.readFile('./config.yaml', 'utf-8');
readConfig = yaml.parse(configText);
} catch (e) {
console.error(`Failed to read config: ${e.toString()}`);
}
return {
...defaultConfig,
...readConfig,
...process.env,
};
}
import { Test, TestingModule } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import * as request from 'supertest';
import { AppModule } from './../src/app.module';
describe('AppController (e2e)', () => {
let app: INestApplication;
beforeEach(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
}).compile();
app = moduleFixture.createNestApplication();
await app.init();
});
/* it('/ (GET)', () => {
return request(app.getHttpServer())
.get('/')
.expect(200)
.expect('Hello World!');
}); */
});
{
"moduleFileExtensions": ["js", "json", "ts"],
"rootDir": ".",
"testEnvironment": "node",
"testRegex": ".e2e-spec.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
}
}
{
"extends": "./tsconfig.json",
"exclude": ["node_modules", "test", "dist", "**/*spec.ts"]
}
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"target": "es2021",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
"incremental": true,
"esModuleInterop": true
},
"compileOnSave": true,
"allowJs": true
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment