Adiciona controller do Ollama e reestrutura arquitetura

This commit is contained in:
LeoMortari
2025-09-30 20:30:22 -03:00
parent 76b7670fba
commit f7857fdcbc
31 changed files with 267 additions and 105 deletions

View File

@@ -33,6 +33,7 @@
"cross-env": "10.0.0", "cross-env": "10.0.0",
"dayjs": "1.11.13", "dayjs": "1.11.13",
"jwks-rsa": "3.2.0", "jwks-rsa": "3.2.0",
"ollama": "0.6.0",
"passport": "0.7.0", "passport": "0.7.0",
"passport-jwt": "4.0.1", "passport-jwt": "4.0.1",
"reflect-metadata": "0.2.2", "reflect-metadata": "0.2.2",

View File

@@ -3,12 +3,13 @@ import { ConfigModule } from '@nestjs/config';
import { PrismaModule } from './prisma/prisma.module'; import { PrismaModule } from './prisma/prisma.module';
import { AppController } from './app.controller'; import { AppController } from './app.controller';
import { AppService } from './app.service'; import { AppService } from './app.service';
import { VideosModule } from './videos/videos.module'; import { VideosModule } from './modules/videos/videos.module';
import { AuthModule } from './auth/auth.module'; import { AuthModule } from './modules/auth/auth.module';
import { VideosController } from './videos/videos.controller'; import { VideosController } from './modules/videos/videos.controller';
import { UsuariosModule } from './usuarios/usuarios.module'; import { UsuariosModule } from './modules/usuarios/usuarios.module';
import { LoggerMiddleware } from './middleware/logger.middleware'; 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({ @Module({
imports: [ imports: [
@@ -17,6 +18,7 @@ import { RolesGuard } from './auth/roles.guard';
VideosModule, VideosModule,
AuthModule, AuthModule,
UsuariosModule, UsuariosModule,
OllamaModule,
], ],
controllers: [AppController], controllers: [AppController],
providers: [AppService, RolesGuard], providers: [AppService, RolesGuard],

View File

@@ -0,0 +1,10 @@
import { IsNotEmpty, IsString } from 'class-validator';
export class ChatDto {
@IsString()
@IsNotEmpty()
message: string;
@IsString()
model: string;
}

View 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();
});
});

View 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,
});
}
}

View 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 {}

View 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();
});
});

View 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);
}
}
}

View File

@@ -1,7 +1,7 @@
import { Module } from '@nestjs/common'; import { Module } from '@nestjs/common';
import { UsuariosService } from './usuarios.service'; import { UsuariosService } from './usuarios.service';
import { UsuariosController } from './usuarios.controller'; import { UsuariosController } from './usuarios.controller';
import { PrismaModule } from '../prisma/prisma.module'; import { PrismaModule } from '@prisma/prisma.module';
@Module({ @Module({
imports: [PrismaModule], imports: [PrismaModule],

View File

@@ -4,8 +4,8 @@ import { plainToInstance } from 'class-transformer';
import { Prisma, Usuario } from 'generated/prisma'; import { Prisma, Usuario } from 'generated/prisma';
import { PrismaService } from '../prisma/prisma.service'; import { PrismaService } from '@prisma/prisma.service';
import { PaginatedResponse } from '../shared/dto/paginated'; import { PaginatedResponse } from '@shared/dto/paginated';
import { UsuariosResponseDto } from './dto/usuarios.response'; import { UsuariosResponseDto } from './dto/usuarios.response';
import { CreateUsuarioDto } from './dto/create-usuario-dto'; import { CreateUsuarioDto } from './dto/create-usuario-dto';
@@ -24,7 +24,7 @@ const SELECT = {
bloqueado_ate: true, bloqueado_ate: true,
criado_em: true, criado_em: true,
atualizado_em: true, atualizado_em: true,
}; } as const;
@Injectable() @Injectable()
export class UsuariosService { export class UsuariosService {

View File

@@ -7,7 +7,7 @@ import {
IsUrl, IsUrl,
MaxLength, MaxLength,
} from 'class-validator'; } from 'class-validator';
import { video_situation } from '../../../generated/prisma'; import { video_situation } from '@root/generated/prisma';
export class CreateVideoDto { export class CreateVideoDto {
@IsUrl() @IsUrl()

View File

@@ -1,8 +1,8 @@
import { Module } from '@nestjs/common'; import { Module } from '@nestjs/common';
import { VideosService } from './videos.service'; import { VideosService } from './videos.service';
import { VideosController } from './videos.controller'; import { VideosController } from './videos.controller';
import { PrismaModule } from '../prisma/prisma.module'; import { PrismaModule } from '@prisma/prisma.module';
import { KeycloakAuthGuard } from '../auth/keycloak-auth.guard'; import { KeycloakAuthGuard } from '@modules/auth/keycloak-auth.guard';
@Module({ @Module({
imports: [PrismaModule], imports: [PrismaModule],

View File

@@ -4,11 +4,11 @@ import { plainToInstance } from 'class-transformer';
import { Prisma, videos, video_situation } from 'generated/prisma'; 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 { 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 { ListVideosQueryDto } from './dto/list-videos-query.dto';
import { VideoMetadataDto } from 'src/shared/dto/video-metadata'; import { VideoMetadataDto } from '@shared/dto/video-metadata';
@Injectable() @Injectable()
export class VideosService { export class VideosService {
@@ -115,7 +115,6 @@ export class VideosService {
} }
async getVideoMetadata(url: string): Promise<VideoMetadataDto> { async getVideoMetadata(url: string): Promise<VideoMetadataDto> {
console.log(url);
try { try {
const { data } = await axios.get<VideoMetadataDto>( const { data } = await axios.get<VideoMetadataDto>(
`${process.env.YOUTUBE_API_URL}/get-video-metadata`, `${process.env.YOUTUBE_API_URL}/get-video-metadata`,
@@ -129,8 +128,7 @@ export class VideosService {
return plainToInstance(VideoMetadataDto, data, { return plainToInstance(VideoMetadataDto, data, {
excludeExtraneousValues: true, excludeExtraneousValues: true,
}); });
} catch (error) { } catch {
console.log(error);
throw new Error('Erro ao obter metadados do vídeo'); throw new Error('Erro ao obter metadados do vídeo');
} }
} }

View File

@@ -20,6 +20,13 @@
"forceConsistentCasingInFileNames": true, "forceConsistentCasingInFileNames": true,
"noImplicitAny": false, "noImplicitAny": false,
"strictBindCallApply": false, "strictBindCallApply": false,
"noFallthroughCasesInSwitch": false "noFallthroughCasesInSwitch": false,
"paths": {
"@/*": ["./src/*"],
"@modules/*": ["./src/modules/*"],
"@shared/*": ["./src/shared/*"],
"@prisma/*": ["./src/prisma/*"],
"@root/*": ["./*"]
}
} }
} }