Skip to content

Commit

Permalink
unchi
Browse files Browse the repository at this point in the history
  • Loading branch information
dino3616 committed Mar 16, 2024
1 parent e0a89de commit c09319e
Show file tree
Hide file tree
Showing 20 changed files with 199 additions and 50 deletions.
4 changes: 3 additions & 1 deletion apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"test": "bun test"
},
"dependencies": {
"@as-integrations/fastify": "2.1.1",
"@distube/ytdl-core": "4.13.3",
"@fastify/compress": "7.0.0",
"@fastify/cors": "9.0.1",
Expand All @@ -32,8 +31,10 @@
"@nestjs/common": "10.3.3",
"@nestjs/config": "3.2.0",
"@nestjs/core": "10.3.3",
"@nestjs/platform-express": "10.3.3",
"@nestjs/platform-fastify": "10.3.3",
"@nestjs/testing": "10.3.3",
"ai": "3.0.12",
"fluent-ffmpeg": "2.1.2",
"langchain": "0.1.27",
"openai": "4.29.1",
Expand All @@ -45,6 +46,7 @@
"@hanjaemeo-api/tsconfig": "workspace:*",
"@hanjaemeo-api/type": "workspace:*",
"@nestjs/schematics": "10.1.1",
"@types/express": "4.17.21",
"@types/fluent-ffmpeg": "2.1.24",
"@types/uuid": "9.0.8"
}
Expand Down
3 changes: 1 addition & 2 deletions apps/api/src/app/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { Module } from '@nestjs/common';
import { EnvModule } from '#api/common/service/env/env.module';
import { LangchainModule } from '#api/infra/langchain/langchain.module';
import { Modules } from '#api/module';

@Module({
imports: [EnvModule, LangchainModule, ...Modules],
imports: [EnvModule, LangchainModule],
})
export class AppModule {}
12 changes: 11 additions & 1 deletion apps/api/src/infra/langchain/langchain.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Controller, Get, Inject, Logger, Param } from '@nestjs/common';
import { Controller, Get, Inject, Logger, Param, Post, Res } from '@nestjs/common';
import type { Response } from 'express';
import { OpenaiService } from '#api/infra/openai/openai.service';
import { YoutubeService } from '../youtube/youtube.service';

Expand All @@ -24,4 +25,13 @@ export class LangchainController {

return transcription;
}

@Post('/call')
async predict(@Res() res: Response) {
this.logger.log(`${this.predict.name} called`);

await this.openaiService.llmCall('Why do people meet?', res);

res.end();
}
}
26 changes: 26 additions & 0 deletions apps/api/src/infra/openai/openai.service.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
import fs from 'node:fs';
import { ChatOpenAI } from '@langchain/openai';
import { Inject, Injectable } from '@nestjs/common';
import type { Response } from 'express';
import { OpenAI } from 'openai';
import { EnvService } from '#api/common/service/env/env.service';

@Injectable()
export class OpenaiService {
private readonly openai: OpenAI;

private readonly llm: ChatOpenAI;

constructor(@Inject(EnvService) private readonly envService: EnvService) {
this.openai = new OpenAI({ apiKey: this.envService.OpenaiApiKey });

this.llm = new ChatOpenAI({
openAIApiKey: this.envService.OpenaiApiKey,
modelName: 'gpt-3.5-turbo',
maxTokens: 4096,
temperature: 0.2,
streaming: true,
});
}

async transcribeFromFile(path: string): Promise<string> {
Expand All @@ -22,4 +34,18 @@ export class OpenaiService {

return res.text;
}

async llmCall(prompt: string, res: Response) {
const stream = await this.llm.invoke(prompt, {
callbacks: [
{
async handleLLMNewToken(token) {
await res.write(token);
},
},
],
});

return stream;
}
}
49 changes: 22 additions & 27 deletions apps/api/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,34 @@
import { fastifyCompress } from '@fastify/compress';
import { fastifyCors } from '@fastify/cors';
import { fastifyHelmet } from '@fastify/helmet';
import { fastifyRateLimit } from '@fastify/rate-limit';
import { Logger } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { FastifyAdapter, type NestFastifyApplication } from '@nestjs/platform-fastify';
import { AppModule } from './app/app.module';
import { EnvService } from './common/service/env/env.service';

const bootstrap = async () => {
const logger = new Logger(bootstrap.name);

const app = await NestFactory.create<NestFastifyApplication>(AppModule, new FastifyAdapter());
const app = await NestFactory.create(AppModule, { cors: true });

app.register(fastifyCompress, {
encodings: ['gzip', 'deflate'],
});
app.register(fastifyCors, {
origin: '*',
});
app.register(fastifyHelmet, {
contentSecurityPolicy: {
directives: {
imgSrc: [`'self'`, 'data:', 'apollo-server-landing-page.cdn.apollographql.com'],
scriptSrc: [`'self'`, `https: 'unsafe-inline'`],
manifestSrc: [`'self'`, 'apollo-server-landing-page.cdn.apollographql.com'],
frameSrc: [`'self'`, 'sandbox.embed.apollographql.com'],
},
},
crossOriginEmbedderPolicy: false,
});
app.register(fastifyRateLimit, {
max: 100,
timeWindow: '1 minute',
});
// app.register(fastifyCompress, {
// encodings: ['gzip', 'deflate'],
// });
// app.register(fastifyCors, {
// origin: '*',
// });
// app.register(fastifyHelmet, {
// contentSecurityPolicy: {
// directives: {
// imgSrc: [`'self'`, 'data:', 'apollo-server-landing-page.cdn.apollographql.com'],
// scriptSrc: [`'self'`, `https: 'unsafe-inline'`],
// manifestSrc: [`'self'`, 'apollo-server-landing-page.cdn.apollographql.com'],
// frameSrc: [`'self'`, 'sandbox.embed.apollographql.com'],
// },
// },
// crossOriginEmbedderPolicy: false,
// });
// app.register(fastifyRateLimit, {
// max: 100,
// timeWindow: '1 minute',
// });

const port = app.get(EnvService).Port;

Expand Down
3 changes: 0 additions & 3 deletions apps/api/src/module/index.ts

This file was deleted.

9 changes: 0 additions & 9 deletions apps/api/src/module/user/controller/user.controller.ts

This file was deleted.

7 changes: 0 additions & 7 deletions apps/api/src/module/user/user.module.ts

This file was deleted.

3 changes: 3 additions & 0 deletions apps/website/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# auto generated folder
generated/
.vercel
5 changes: 5 additions & 0 deletions apps/website/next-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/// <reference types="next" />
/// <reference types="next/image-types/global" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.
7 changes: 7 additions & 0 deletions apps/website/next.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/** @type {import('next').NextConfig} */
const config = {
reactStrictMode: true,
transpilePackages: ['@hayabusa/ui'],
};

export default config;
33 changes: 33 additions & 0 deletions apps/website/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "@hanjaemeo-api/website",
"version": "0.1.0",
"repository": "https://github.com/HanJaemEo/hanjaemeo-api.git",
"license": "MIT",
"private": true,
"type": "module",
"module": "nodenext",
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"fmt": "biome format .",
"fmt:fix": "biome format --write",
"lint": "biome lint .",
"lint:fix": "biome lint --apply",
"test": "bun test"
},
"dependencies": {
"ai": "3.0.12",
"next": "14.1.3",
"react": "18.2.0",
"react-dom": "18.2.0",
"sharp": "0.33.2",
"ts-pattern": "5.0.8"
},
"devDependencies": {
"@hanjaemeo-api/tsconfig": "workspace:*",
"@hanjaemeo-api/type": "workspace:*",
"@types/react": "18.2.65",
"@types/react-dom": "18.2.22"
}
}
Empty file added apps/website/public/.gitkeep
Empty file.
39 changes: 39 additions & 0 deletions apps/website/src/app/chat.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use client';

import { useChat } from 'ai/react';

type ChatProps = {
api: NonNullable<Parameters<typeof useChat>['0']>['api'];
};

export const Chat = ({ api }: ChatProps) => {
const { messages, input, isLoading, handleInputChange, handleSubmit } = useChat({
api,
onResponse: response => {
// biome-ignore lint/suspicious/noConsoleLog:
console.log('response', response);
},
});

return (
<div>
<ul>
{messages.map((m, index) => (
// biome-ignore lint/suspicious/noArrayIndexKey:
<li key={index}>
{m.role === 'user' ? 'User: ' : 'AI: '}
{m.content}
</li>
))}
</ul>
<form onSubmit={handleSubmit}>
<label>
Say something...
<input value={input} onChange={handleInputChange} />
</label>
<button type='submit'>Send</button>
{isLoading && <span>...</span>}
</form>
</div>
);
};
17 changes: 17 additions & 0 deletions apps/website/src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type { NextPage } from 'next';
import type { ReactNode } from 'react';

type RootLayoutProps = {
children: ReactNode;
};

const RootLayout: NextPage<RootLayoutProps> = ({ children }) => {
return (
<html lang='en' suppressHydrationWarning>
<head />
<body>{children}</body>
</html>
);
};

export default RootLayout;
15 changes: 15 additions & 0 deletions apps/website/src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { NextPage } from 'next';
import { Chat } from './chat';

const RootPage: NextPage = () => (
<div className='flex flex-col gap-20'>
<div className='flex items-center gap-10'>
<h1 className='text-xl text-sage-12'>GPT</h1>
<Chat api='https://hanjaemeo-production.up.railway.app/call' />
</div>
</div>
);

export default RootPage;

export const runtime = 'edge';
4 changes: 4 additions & 0 deletions apps/website/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "./tsconfig.json",
"exclude": ["**/test/**/*", "**/*.*js", "**/*.test.ts", "**/*.story.tsx"]
}
12 changes: 12 additions & 0 deletions apps/website/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"extends": "@hanjaemeo-api/tsconfig/tsconfig.nextjs.json",
"compilerOptions": {
"baseUrl": "./",
"paths": {
"#website/*": ["./src/*"]
}
},
"include": ["./src/**/*", "./*.*js", "./*.*ts", ".next/types/**/*.*ts", ".next/types/**/*.ts"],
"exclude": ["**/node_modules/**/*"]
}
Binary file modified bun.lockb
Binary file not shown.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"scripts": {
"prepare": "lefthook install",
"api": "bun --cwd ./apps/api",
"website": "bun --cwd ./apps/website",
"tsconfig": "bun --cwd ./packages/tsconfig",
"type": "bun --cwd ./packages/type"
},
Expand Down

0 comments on commit c09319e

Please sign in to comment.