Skip to content

Commit

Permalink
feat: ✨ unchi2
Browse files Browse the repository at this point in the history
  • Loading branch information
dino3616 committed Mar 16, 2024
1 parent c09319e commit 6310ecb
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 38 deletions.
3 changes: 1 addition & 2 deletions apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +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",
"fastify": "4.26.0",
"fluent-ffmpeg": "2.1.2",
"langchain": "0.1.27",
"openai": "4.29.1",
Expand All @@ -46,7 +46,6 @@
"@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
20 changes: 16 additions & 4 deletions apps/api/src/infra/langchain/langchain.controller.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { Controller, Get, Inject, Logger, Param, Post, Res } from '@nestjs/common';
import type { Response } from 'express';
import { Body, Controller, Get, Inject, Logger, Param, Post, Res } from '@nestjs/common';
import type { FastifyReply } from 'fastify';
import { OpenaiService } from '#api/infra/openai/openai.service';
import { YoutubeService } from '../youtube/youtube.service';

class Message {
role!: 'user' | 'assistant';
content!: string;
}

@Controller()
export class LangchainController {
private readonly logger = new Logger(LangchainController.name);
Expand All @@ -27,10 +32,17 @@ export class LangchainController {
}

@Post('/call')
async predict(@Res() res: Response) {
async predict(@Res() { raw: res }: FastifyReply, @Body('messages') messages: Message[]) {
this.logger.log(`${this.predict.name} called`);

await this.openaiService.llmCall('Why do people meet?', res);
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
res.writeHead(200, { 'Content-Type': 'text/plain' });

await this.openaiService.llmCall(messages.at(-1)!.content, token => {
res.write(token);
});

res.end();
}
Expand Down
11 changes: 2 additions & 9 deletions apps/api/src/infra/openai/openai.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
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';

Expand Down Expand Up @@ -35,15 +34,9 @@ export class OpenaiService {
return res.text;
}

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

return stream;
Expand Down
39 changes: 17 additions & 22 deletions apps/api/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,29 @@
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(AppModule, { cors: true });
const app = await NestFactory.create<NestFastifyApplication>(AppModule, new FastifyAdapter());

// 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: true,
});
app.register(fastifyHelmet);
app.register(fastifyRateLimit, {
max: 100,
timeWindow: '1 minute',
});

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

Expand Down
2 changes: 1 addition & 1 deletion apps/website/src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ 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' />
<Chat api='http://localhost:4000/call' />
</div>
</div>
);
Expand Down
Binary file modified bun.lockb
Binary file not shown.

0 comments on commit 6310ecb

Please sign in to comment.