Adiciona controller do Ollama e reestrutura arquitetura
This commit is contained in:
173
package.json
173
package.json
@@ -1,86 +1,87 @@
|
||||
{
|
||||
"name": "clipperia-api",
|
||||
"version": "0.0.1",
|
||||
"description": "",
|
||||
"author": "Leonardo Mortari",
|
||||
"private": true,
|
||||
"license": "UNLICENSED",
|
||||
"scripts": {
|
||||
"build": "nest build",
|
||||
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
|
||||
"start": "nest start",
|
||||
"start:dev": "cross-env NODE_ENV=development nest start --watch",
|
||||
"start:debug": "cross-env NODE_ENV=development 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": "11.0.1",
|
||||
"@nestjs/config": "4.0.2",
|
||||
"@nestjs/core": "11.0.1",
|
||||
"@nestjs/passport": "11.0.5",
|
||||
"@nestjs/platform-express": "11.0.1",
|
||||
"@prisma/client": "6.14.0",
|
||||
"axios": "1.12.0",
|
||||
"bcrypt": "6.0.0",
|
||||
"class-transformer": "0.5.1",
|
||||
"class-validator": "0.14.2",
|
||||
"cross-env": "10.0.0",
|
||||
"dayjs": "1.11.13",
|
||||
"jwks-rsa": "3.2.0",
|
||||
"passport": "0.7.0",
|
||||
"passport-jwt": "4.0.1",
|
||||
"reflect-metadata": "0.2.2",
|
||||
"rxjs": "7.8.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@eslint/eslintrc": "3.2.0",
|
||||
"@eslint/js": "9.18.0",
|
||||
"@nestjs/cli": "11.0.0",
|
||||
"@nestjs/schematics": "11.0.0",
|
||||
"@nestjs/testing": "11.0.1",
|
||||
"@types/bcrypt": "6.0.0",
|
||||
"@types/express": "5.0.0",
|
||||
"@types/jest": "30.0.0",
|
||||
"@types/node": "22.10.7",
|
||||
"@types/passport-jwt": "4.0.1",
|
||||
"@types/supertest": "6.0.2",
|
||||
"eslint": "9.18.0",
|
||||
"eslint-config-prettier": "10.0.1",
|
||||
"eslint-plugin-prettier": "5.2.2",
|
||||
"globals": "16.0.0",
|
||||
"jest": "30.0.0",
|
||||
"prettier": "3.4.2",
|
||||
"prisma": "6.14.0",
|
||||
"source-map-support": "0.5.21",
|
||||
"supertest": "7.0.0",
|
||||
"ts-jest": "29.2.5",
|
||||
"ts-loader": "9.5.2",
|
||||
"ts-node": "10.9.2",
|
||||
"tsconfig-paths": "4.2.0",
|
||||
"typescript": "5.7.3",
|
||||
"typescript-eslint": "8.20.0"
|
||||
},
|
||||
"jest": {
|
||||
"moduleFileExtensions": [
|
||||
"js",
|
||||
"json",
|
||||
"ts"
|
||||
],
|
||||
"rootDir": "src",
|
||||
"testRegex": ".*\\.spec\\.ts$",
|
||||
"transform": {
|
||||
"^.+\\.(t|j)s$": "ts-jest"
|
||||
},
|
||||
"collectCoverageFrom": [
|
||||
"**/*.(t|j)s"
|
||||
],
|
||||
"coverageDirectory": "../coverage",
|
||||
"testEnvironment": "node"
|
||||
}
|
||||
}
|
||||
{
|
||||
"name": "clipperia-api",
|
||||
"version": "0.0.1",
|
||||
"description": "",
|
||||
"author": "Leonardo Mortari",
|
||||
"private": true,
|
||||
"license": "UNLICENSED",
|
||||
"scripts": {
|
||||
"build": "nest build",
|
||||
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
|
||||
"start": "nest start",
|
||||
"start:dev": "cross-env NODE_ENV=development nest start --watch",
|
||||
"start:debug": "cross-env NODE_ENV=development 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": "11.0.1",
|
||||
"@nestjs/config": "4.0.2",
|
||||
"@nestjs/core": "11.0.1",
|
||||
"@nestjs/passport": "11.0.5",
|
||||
"@nestjs/platform-express": "11.0.1",
|
||||
"@prisma/client": "6.14.0",
|
||||
"axios": "1.12.0",
|
||||
"bcrypt": "6.0.0",
|
||||
"class-transformer": "0.5.1",
|
||||
"class-validator": "0.14.2",
|
||||
"cross-env": "10.0.0",
|
||||
"dayjs": "1.11.13",
|
||||
"jwks-rsa": "3.2.0",
|
||||
"ollama": "0.6.0",
|
||||
"passport": "0.7.0",
|
||||
"passport-jwt": "4.0.1",
|
||||
"reflect-metadata": "0.2.2",
|
||||
"rxjs": "7.8.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@eslint/eslintrc": "3.2.0",
|
||||
"@eslint/js": "9.18.0",
|
||||
"@nestjs/cli": "11.0.0",
|
||||
"@nestjs/schematics": "11.0.0",
|
||||
"@nestjs/testing": "11.0.1",
|
||||
"@types/bcrypt": "6.0.0",
|
||||
"@types/express": "5.0.0",
|
||||
"@types/jest": "30.0.0",
|
||||
"@types/node": "22.10.7",
|
||||
"@types/passport-jwt": "4.0.1",
|
||||
"@types/supertest": "6.0.2",
|
||||
"eslint": "9.18.0",
|
||||
"eslint-config-prettier": "10.0.1",
|
||||
"eslint-plugin-prettier": "5.2.2",
|
||||
"globals": "16.0.0",
|
||||
"jest": "30.0.0",
|
||||
"prettier": "3.4.2",
|
||||
"prisma": "6.14.0",
|
||||
"source-map-support": "0.5.21",
|
||||
"supertest": "7.0.0",
|
||||
"ts-jest": "29.2.5",
|
||||
"ts-loader": "9.5.2",
|
||||
"ts-node": "10.9.2",
|
||||
"tsconfig-paths": "4.2.0",
|
||||
"typescript": "5.7.3",
|
||||
"typescript-eslint": "8.20.0"
|
||||
},
|
||||
"jest": {
|
||||
"moduleFileExtensions": [
|
||||
"js",
|
||||
"json",
|
||||
"ts"
|
||||
],
|
||||
"rootDir": "src",
|
||||
"testRegex": ".*\\.spec\\.ts$",
|
||||
"transform": {
|
||||
"^.+\\.(t|j)s$": "ts-jest"
|
||||
},
|
||||
"collectCoverageFrom": [
|
||||
"**/*.(t|j)s"
|
||||
],
|
||||
"coverageDirectory": "../coverage",
|
||||
"testEnvironment": "node"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,12 +3,13 @@ import { ConfigModule } from '@nestjs/config';
|
||||
import { PrismaModule } from './prisma/prisma.module';
|
||||
import { AppController } from './app.controller';
|
||||
import { AppService } from './app.service';
|
||||
import { VideosModule } from './videos/videos.module';
|
||||
import { AuthModule } from './auth/auth.module';
|
||||
import { VideosController } from './videos/videos.controller';
|
||||
import { UsuariosModule } from './usuarios/usuarios.module';
|
||||
import { VideosModule } from './modules/videos/videos.module';
|
||||
import { AuthModule } from './modules/auth/auth.module';
|
||||
import { VideosController } from './modules/videos/videos.controller';
|
||||
import { UsuariosModule } from './modules/usuarios/usuarios.module';
|
||||
import { LoggerMiddleware } from './middleware/logger.middleware';
|
||||
import { RolesGuard } from './auth/roles.guard';
|
||||
import { RolesGuard } from './modules/auth/roles.guard';
|
||||
import { OllamaModule } from './modules/ollama/ollama.module';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
@@ -17,6 +18,7 @@ import { RolesGuard } from './auth/roles.guard';
|
||||
VideosModule,
|
||||
AuthModule,
|
||||
UsuariosModule,
|
||||
OllamaModule,
|
||||
],
|
||||
controllers: [AppController],
|
||||
providers: [AppService, RolesGuard],
|
||||
|
||||
10
src/modules/ollama/dto/chat.dto.ts
Normal file
10
src/modules/ollama/dto/chat.dto.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import { IsNotEmpty, IsString } from 'class-validator';
|
||||
|
||||
export class ChatDto {
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
message: string;
|
||||
|
||||
@IsString()
|
||||
model: string;
|
||||
}
|
||||
18
src/modules/ollama/ollama.controller.spec.ts
Normal file
18
src/modules/ollama/ollama.controller.spec.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { OllamaController } from './ollama.controller';
|
||||
|
||||
describe('OllamaController', () => {
|
||||
let controller: OllamaController;
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
controllers: [OllamaController],
|
||||
}).compile();
|
||||
|
||||
controller = module.get<OllamaController>(OllamaController);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(controller).toBeDefined();
|
||||
});
|
||||
});
|
||||
44
src/modules/ollama/ollama.controller.ts
Normal file
44
src/modules/ollama/ollama.controller.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { Body, Controller, Get, Post, Query } from '@nestjs/common';
|
||||
import { OllamaService } from './ollama.service';
|
||||
import { ChatDto } from './dto/chat.dto';
|
||||
|
||||
@Controller('ollama')
|
||||
export class OllamaController {
|
||||
constructor(private readonly ollamaService: OllamaService) {}
|
||||
|
||||
@Get('models')
|
||||
public async getModels(
|
||||
@Query('onlyChats') { onlyChats }: { onlyChats: boolean },
|
||||
) {
|
||||
return await this.ollamaService.getModels({ onlyChats });
|
||||
}
|
||||
|
||||
@Post('chat')
|
||||
public async chat(@Body() body: ChatDto) {
|
||||
const { message, model } = body;
|
||||
|
||||
if (!message) {
|
||||
throw new Error('O atributo message é obrigatório');
|
||||
}
|
||||
|
||||
if (!model) {
|
||||
const [defaultModel] = await this.ollamaService.getModels({
|
||||
onlyChats: true,
|
||||
});
|
||||
|
||||
if (!defaultModel) {
|
||||
throw new Error('Nenhum modelo encontrado');
|
||||
}
|
||||
|
||||
return await this.ollamaService.generateChat({
|
||||
model: defaultModel.name,
|
||||
message,
|
||||
});
|
||||
}
|
||||
|
||||
return await this.ollamaService.generateChat({
|
||||
model,
|
||||
message,
|
||||
});
|
||||
}
|
||||
}
|
||||
9
src/modules/ollama/ollama.module.ts
Normal file
9
src/modules/ollama/ollama.module.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { OllamaController } from './ollama.controller';
|
||||
import { OllamaService } from './ollama.service';
|
||||
|
||||
@Module({
|
||||
controllers: [OllamaController],
|
||||
providers: [OllamaService],
|
||||
})
|
||||
export class OllamaModule {}
|
||||
18
src/modules/ollama/ollama.service.spec.ts
Normal file
18
src/modules/ollama/ollama.service.spec.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { OllamaService } from './ollama.service';
|
||||
|
||||
describe('OllamaService', () => {
|
||||
let service: OllamaService;
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [OllamaService],
|
||||
}).compile();
|
||||
|
||||
service = module.get<OllamaService>(OllamaService);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
});
|
||||
55
src/modules/ollama/ollama.service.ts
Normal file
55
src/modules/ollama/ollama.service.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
import { Ollama } from 'ollama';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import type { ListResponse, ModelResponse } from 'ollama';
|
||||
|
||||
@Injectable()
|
||||
export class OllamaService {
|
||||
private readonly ollama: Ollama;
|
||||
|
||||
constructor() {
|
||||
this.ollama = new Ollama({ host: 'http://154.12.229.181:11434' });
|
||||
}
|
||||
|
||||
public async getModels({ onlyChats }: { onlyChats: boolean }) {
|
||||
try {
|
||||
const modelsData = await this.ollama.list();
|
||||
|
||||
if (onlyChats) {
|
||||
return this.getChatModels(modelsData);
|
||||
}
|
||||
|
||||
return modelsData.models;
|
||||
} catch (error) {
|
||||
throw new Error(error as string);
|
||||
}
|
||||
}
|
||||
|
||||
public getChatModels(modelsData: ListResponse): ModelResponse[] {
|
||||
const excludeFamilies = ['nomic-bert', 'embed', 'embedding'];
|
||||
|
||||
return modelsData.models.filter((model) => {
|
||||
const families = model.details?.families || [];
|
||||
|
||||
return !families.some((f) => excludeFamilies.includes(f.toLowerCase()));
|
||||
});
|
||||
}
|
||||
|
||||
public async generateChat({
|
||||
model,
|
||||
message,
|
||||
}: {
|
||||
model: string;
|
||||
message: string;
|
||||
}) {
|
||||
try {
|
||||
const response = await this.ollama.chat({
|
||||
model,
|
||||
messages: [{ role: 'user', content: message }],
|
||||
});
|
||||
|
||||
return response.message.content;
|
||||
} catch (error) {
|
||||
throw new Error(error as string);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { UsuariosService } from './usuarios.service';
|
||||
import { UsuariosController } from './usuarios.controller';
|
||||
import { PrismaModule } from '../prisma/prisma.module';
|
||||
import { PrismaModule } from '@prisma/prisma.module';
|
||||
|
||||
@Module({
|
||||
imports: [PrismaModule],
|
||||
@@ -4,8 +4,8 @@ import { plainToInstance } from 'class-transformer';
|
||||
|
||||
import { Prisma, Usuario } from 'generated/prisma';
|
||||
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { PaginatedResponse } from '../shared/dto/paginated';
|
||||
import { PrismaService } from '@prisma/prisma.service';
|
||||
import { PaginatedResponse } from '@shared/dto/paginated';
|
||||
import { UsuariosResponseDto } from './dto/usuarios.response';
|
||||
import { CreateUsuarioDto } from './dto/create-usuario-dto';
|
||||
|
||||
@@ -24,7 +24,7 @@ const SELECT = {
|
||||
bloqueado_ate: true,
|
||||
criado_em: true,
|
||||
atualizado_em: true,
|
||||
};
|
||||
} as const;
|
||||
|
||||
@Injectable()
|
||||
export class UsuariosService {
|
||||
@@ -7,7 +7,7 @@ import {
|
||||
IsUrl,
|
||||
MaxLength,
|
||||
} from 'class-validator';
|
||||
import { video_situation } from '../../../generated/prisma';
|
||||
import { video_situation } from '@root/generated/prisma';
|
||||
|
||||
export class CreateVideoDto {
|
||||
@IsUrl()
|
||||
@@ -1,8 +1,8 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { VideosService } from './videos.service';
|
||||
import { VideosController } from './videos.controller';
|
||||
import { PrismaModule } from '../prisma/prisma.module';
|
||||
import { KeycloakAuthGuard } from '../auth/keycloak-auth.guard';
|
||||
import { PrismaModule } from '@prisma/prisma.module';
|
||||
import { KeycloakAuthGuard } from '@modules/auth/keycloak-auth.guard';
|
||||
|
||||
@Module({
|
||||
imports: [PrismaModule],
|
||||
@@ -4,11 +4,11 @@ import { plainToInstance } from 'class-transformer';
|
||||
|
||||
import { Prisma, videos, video_situation } from 'generated/prisma';
|
||||
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { PrismaService } from '@prisma/prisma.service';
|
||||
import { VideoResponseDto } from './dto/video-response.dto';
|
||||
import { PaginatedResponse } from '../shared/dto/paginated';
|
||||
import { PaginatedResponse } from '@shared/dto/paginated';
|
||||
import { ListVideosQueryDto } from './dto/list-videos-query.dto';
|
||||
import { VideoMetadataDto } from 'src/shared/dto/video-metadata';
|
||||
import { VideoMetadataDto } from '@shared/dto/video-metadata';
|
||||
|
||||
@Injectable()
|
||||
export class VideosService {
|
||||
@@ -115,7 +115,6 @@ export class VideosService {
|
||||
}
|
||||
|
||||
async getVideoMetadata(url: string): Promise<VideoMetadataDto> {
|
||||
console.log(url);
|
||||
try {
|
||||
const { data } = await axios.get<VideoMetadataDto>(
|
||||
`${process.env.YOUTUBE_API_URL}/get-video-metadata`,
|
||||
@@ -129,8 +128,7 @@ export class VideosService {
|
||||
return plainToInstance(VideoMetadataDto, data, {
|
||||
excludeExtraneousValues: true,
|
||||
});
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
} catch {
|
||||
throw new Error('Erro ao obter metadados do vídeo');
|
||||
}
|
||||
}
|
||||
@@ -20,6 +20,13 @@
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"noImplicitAny": false,
|
||||
"strictBindCallApply": false,
|
||||
"noFallthroughCasesInSwitch": false
|
||||
"noFallthroughCasesInSwitch": false,
|
||||
"paths": {
|
||||
"@/*": ["./src/*"],
|
||||
"@modules/*": ["./src/modules/*"],
|
||||
"@shared/*": ["./src/shared/*"],
|
||||
"@prisma/*": ["./src/prisma/*"],
|
||||
"@root/*": ["./*"]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user