oauth_nest_demo/src/deepseek/deepseek.controller.ts

173 lines
4.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { Body, Controller, Post } from '@nestjs/common';
import { DeepseekService } from './deepseek.service';
import { Public } from 'src/auth/decorators/public.decorator';
import { PROVIDER_TYPE } from 'src/constants/providerType';
import { ApiBody, ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
import { ErrorResponseDto } from '../common/dto/error-response.dto';
@ApiTags('DeepSeek AI 模块')
@Controller('deepSeek')
export class DeepseekController {
constructor(private readonly deepseekService: DeepseekService) {}
@Public()
@Post('chat-flow')
@ApiOperation({
summary: '与 DeepSeek Flow 模型对话',
description:
'使用 DeepSeek Flow 模型进行对话交互。Flow 模型擅长快速响应和流式输出,适合实时对话场景',
})
@ApiBody({
schema: {
type: 'object',
properties: {
message: {
type: 'string',
description: '发送给 AI 模型的消息内容',
example: '你好,请介绍一下你自己',
},
},
required: ['message'],
},
})
@ApiResponse({
status: 200,
description: '成功返回 AI 模型的响应',
schema: {
example: {
model: 'deepseek-flow',
message: '你好!我是 DeepSeek Flow一个 AI 助手。我可以帮助你回答问题、提供建议和进行对话。',
usage: {
prompt_tokens: 10,
completion_tokens: 25,
total_tokens: 35,
},
},
},
})
@ApiResponse({
status: 400,
description: '请求参数错误(消息内容为空等)',
type: ErrorResponseDto,
})
@ApiResponse({
status: 500,
description: 'AI 服务调用失败',
type: ErrorResponseDto,
})
async chatFlow(@Body() body: { message: string }) {
const response = await this.deepseekService.chatRequest(
body.message,
PROVIDER_TYPE.Flow.name,
);
return response;
}
@Public()
@Post('chat-deep')
@ApiOperation({
summary: '与 DeepSeek Deep 模型对话',
description:
'使用 DeepSeek Deep 模型进行对话交互。Deep 模型提供更深入的推理能力,适合复杂问题解答和深度分析',
})
@ApiBody({
schema: {
type: 'object',
properties: {
message: {
type: 'string',
description: '发送给 AI 模型的消息内容',
example: '请解释一下量子计算的基本原理',
},
},
required: ['message'],
},
})
@ApiResponse({
status: 200,
description: '成功返回 AI 模型的响应',
schema: {
example: {
model: 'deepseek-deep',
message:
'量子计算是一种利用量子力学原理进行信息处理的计算方式...',
usage: {
prompt_tokens: 15,
completion_tokens: 150,
total_tokens: 165,
},
},
},
})
@ApiResponse({
status: 400,
description: '请求参数错误(消息内容为空等)',
type: ErrorResponseDto,
})
@ApiResponse({
status: 500,
description: 'AI 服务调用失败',
type: ErrorResponseDto,
})
async chatDeep(@Body() body: { message: string }) {
const response = await this.deepseekService.chatRequest(
body.message,
PROVIDER_TYPE.Deep.name,
);
return response;
}
@Public()
@Post('chat-grok')
@ApiOperation({
summary: '与 Grok 模型对话',
description:
'使用 Grok 模型进行对话交互。Grok 模型具有幽默风格和实时信息获取能力,适合需要最新信息的场景',
})
@ApiBody({
schema: {
type: 'object',
properties: {
message: {
type: 'string',
description: '发送给 AI 模型的消息内容',
example: '最近有什么科技新闻?',
},
},
required: ['message'],
},
})
@ApiResponse({
status: 200,
description: '成功返回 AI 模型的响应',
schema: {
example: {
model: 'grok',
message: '最近的科技新闻包括...',
usage: {
prompt_tokens: 12,
completion_tokens: 80,
total_tokens: 92,
},
},
},
})
@ApiResponse({
status: 400,
description: '请求参数错误(消息内容为空等)',
type: ErrorResponseDto,
})
@ApiResponse({
status: 500,
description: 'AI 服务调用失败',
type: ErrorResponseDto,
})
async chatGrok(@Body() body: { message: string }) {
const response = await this.deepseekService.chatRequest(
body.message,
PROVIDER_TYPE.Grok.name,
);
return response;
}
}