Adiciona controller do Ollama e reestrutura arquitetura
This commit is contained in:
@@ -33,6 +33,7 @@
|
||||
"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",
|
||||
|
||||
@@ -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