feat: 添加grok支持

This commit is contained in:
lichao 2025-03-17 03:04:17 +00:00
parent 0b2ac82f5c
commit 49a38f7bce
7 changed files with 4711 additions and 3616 deletions

3
.env
View File

@ -18,3 +18,6 @@ FEISHU_APP_SECRET=s106GAbbCZk66OcHN69Rng5TaLK6fiH2
# 深寻
SILICONFLOW_API_KEY=sk-ojmkkuyrfigcmdlijbirbjaqmuwhhfsddcqdvpvayspqrupc
DEEPSEEK_API_KEY=sk-e8941dc2bf1d4d099001b161db6f4317
# Grok
GROK_API_KEY=xai-hKcnlbz5N2AWSdWYvV29G7iBmiI8FFU3lQLDG25Xw6BGjjhkYGGqPSSjiaxnAySViM3AYiYTeJwFFjJK

View File

@ -36,6 +36,7 @@
"axios": "^1.7.9",
"bcrypt": "^5.1.1",
"bcryptjs": "^2.4.3",
"body-parser": "^1.20.3",
"cache-manager": "4.1.0",
"dotenv": "^16.4.7",
"install": "^0.13.0",

File diff suppressed because it is too large Load Diff

View File

@ -9,4 +9,9 @@ export const PROVIDER_TYPE = {
baseUrl: 'https://api.siliconflow.cn/v1/chat/completions',
model: 'deepseek-ai/DeepSeek-V3',
},
Grok: {
name: 'Grok',
baseUrl: 'https://api.x.ai/v1/chat/completions',
model: 'grok-2-latest',
},
};

View File

@ -5,7 +5,7 @@ import { PROVIDER_TYPE } from 'src/constants/providerType';
@Controller('deepSeek')
export class DeepseekController {
constructor(private readonly deepseekService: DeepseekService) {}
constructor(private readonly deepseekService: DeepseekService) { }
@Public()
@Post('chat-flow')
@ -26,4 +26,14 @@ export class DeepseekController {
);
return response;
}
@Public()
@Post('chat-grok')
async chatGrok(@Body() body: { message: string }) {
const response = await this.deepseekService.chatRequest(
body.message,
PROVIDER_TYPE.Grok.name,
);
return response;
}
}

View File

@ -6,16 +6,32 @@ import { PROVIDER_TYPE } from 'src/constants/providerType';
export class DeepseekService {
async chatRequest(message: string, provider = PROVIDER_TYPE.Flow.name) {
const { baseUrl, model } = PROVIDER_TYPE[provider];
const apiKey =
provider === PROVIDER_TYPE.Flow.name
? process.env.SILICONFLOW_API_KEY
: process.env.DEEPSEEK_API_KEY;
const apiKeys = {
[PROVIDER_TYPE.Flow.name]: process.env.SILICONFLOW_API_KEY,
[PROVIDER_TYPE.Deep.name]: process.env.DEEPSEEK_API_KEY,
[PROVIDER_TYPE.Grok.name]: process.env.GROK_API_KEY,
};
const apiKey = apiKeys[provider];
try {
const response = await axios.post(
baseUrl,
{
messages: [{ content: message, role: 'user' }],
messages: [
{
role: "system",
content: "你是一个虚拟货币分析师,擅长分析虚拟货币的统计文件,并且给用户提供建议,应该做空还是做多以及具体的杠杆倍数,还有应该在什么点位入场以及具体的止盈止损点位"
},
{
role: "user",
content: "帮我分析下这份虚拟货币的统计文件,并且给我提供建议,应该做空还是做多以及具体的杠杆倍数,还有应该在什么点位入场以及具体的止盈止损点位"
},
{
role: "user",
content: message
}
],
model,
stream: false,
},

View File

@ -5,6 +5,7 @@ import { ConfigService } from '@nestjs/config';
import * as dotenv from 'dotenv';
import { HttpExceptionFilter } from './common/filters/http-exception.filter';
import { TransformInterceptor } from './common/interceptors/transform.interceptor';
import * as bodyParser from 'body-parser';
dotenv.config();
@ -24,8 +25,10 @@ async function bootstrap() {
await connection.end();
app.enableCors({
origin: 'http://localhost:8000', // 只允许该来源访问
credentials: true, // 允许带有凭证cookies的请求
origin: ['http://108.171.193.155:8000', 'http://localhost:8000'], // 指定允许的前端地址
credentials: true, // 允许携带凭证
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS', 'PATCH'], // 允许的 HTTP 方法
allowedHeaders: ['Content-Type', 'Authorization', 'Accept', 'Origin', 'Referer', 'User-Agent'], // 允许的请求头
});
// 注册全局异常过滤器
@ -34,6 +37,10 @@ async function bootstrap() {
// 注册全局响应转换拦截器
app.useGlobalInterceptors(new TransformInterceptor());
// 使用 NestJS 内置方法设置限制
app.use(bodyParser.json({ limit: '50mb' }));
app.use(bodyParser.urlencoded({ limit: '50mb', extended: true }));
await app.listen(3030);
}
bootstrap();