feat: 添加grok支持
This commit is contained in:
parent
0b2ac82f5c
commit
49a38f7bce
3
.env
3
.env
|
|
@ -18,3 +18,6 @@ FEISHU_APP_SECRET=s106GAbbCZk66OcHN69Rng5TaLK6fiH2
|
||||||
# 深寻
|
# 深寻
|
||||||
SILICONFLOW_API_KEY=sk-ojmkkuyrfigcmdlijbirbjaqmuwhhfsddcqdvpvayspqrupc
|
SILICONFLOW_API_KEY=sk-ojmkkuyrfigcmdlijbirbjaqmuwhhfsddcqdvpvayspqrupc
|
||||||
DEEPSEEK_API_KEY=sk-e8941dc2bf1d4d099001b161db6f4317
|
DEEPSEEK_API_KEY=sk-e8941dc2bf1d4d099001b161db6f4317
|
||||||
|
|
||||||
|
# Grok
|
||||||
|
GROK_API_KEY=xai-hKcnlbz5N2AWSdWYvV29G7iBmiI8FFU3lQLDG25Xw6BGjjhkYGGqPSSjiaxnAySViM3AYiYTeJwFFjJK
|
||||||
|
|
|
||||||
|
|
@ -36,6 +36,7 @@
|
||||||
"axios": "^1.7.9",
|
"axios": "^1.7.9",
|
||||||
"bcrypt": "^5.1.1",
|
"bcrypt": "^5.1.1",
|
||||||
"bcryptjs": "^2.4.3",
|
"bcryptjs": "^2.4.3",
|
||||||
|
"body-parser": "^1.20.3",
|
||||||
"cache-manager": "4.1.0",
|
"cache-manager": "4.1.0",
|
||||||
"dotenv": "^16.4.7",
|
"dotenv": "^16.4.7",
|
||||||
"install": "^0.13.0",
|
"install": "^0.13.0",
|
||||||
|
|
|
||||||
8269
pnpm-lock.yaml
8269
pnpm-lock.yaml
File diff suppressed because it is too large
Load Diff
|
|
@ -9,4 +9,9 @@ export const PROVIDER_TYPE = {
|
||||||
baseUrl: 'https://api.siliconflow.cn/v1/chat/completions',
|
baseUrl: 'https://api.siliconflow.cn/v1/chat/completions',
|
||||||
model: 'deepseek-ai/DeepSeek-V3',
|
model: 'deepseek-ai/DeepSeek-V3',
|
||||||
},
|
},
|
||||||
|
Grok: {
|
||||||
|
name: 'Grok',
|
||||||
|
baseUrl: 'https://api.x.ai/v1/chat/completions',
|
||||||
|
model: 'grok-2-latest',
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ import { PROVIDER_TYPE } from 'src/constants/providerType';
|
||||||
|
|
||||||
@Controller('deepSeek')
|
@Controller('deepSeek')
|
||||||
export class DeepseekController {
|
export class DeepseekController {
|
||||||
constructor(private readonly deepseekService: DeepseekService) {}
|
constructor(private readonly deepseekService: DeepseekService) { }
|
||||||
|
|
||||||
@Public()
|
@Public()
|
||||||
@Post('chat-flow')
|
@Post('chat-flow')
|
||||||
|
|
@ -26,4 +26,14 @@ export class DeepseekController {
|
||||||
);
|
);
|
||||||
return response;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,16 +6,32 @@ import { PROVIDER_TYPE } from 'src/constants/providerType';
|
||||||
export class DeepseekService {
|
export class DeepseekService {
|
||||||
async chatRequest(message: string, provider = PROVIDER_TYPE.Flow.name) {
|
async chatRequest(message: string, provider = PROVIDER_TYPE.Flow.name) {
|
||||||
const { baseUrl, model } = PROVIDER_TYPE[provider];
|
const { baseUrl, model } = PROVIDER_TYPE[provider];
|
||||||
const apiKey =
|
const apiKeys = {
|
||||||
provider === PROVIDER_TYPE.Flow.name
|
[PROVIDER_TYPE.Flow.name]: process.env.SILICONFLOW_API_KEY,
|
||||||
? process.env.SILICONFLOW_API_KEY
|
[PROVIDER_TYPE.Deep.name]: process.env.DEEPSEEK_API_KEY,
|
||||||
: process.env.DEEPSEEK_API_KEY;
|
[PROVIDER_TYPE.Grok.name]: process.env.GROK_API_KEY,
|
||||||
|
};
|
||||||
|
|
||||||
|
const apiKey = apiKeys[provider];
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await axios.post(
|
const response = await axios.post(
|
||||||
baseUrl,
|
baseUrl,
|
||||||
{
|
{
|
||||||
messages: [{ content: message, role: 'user' }],
|
messages: [
|
||||||
|
{
|
||||||
|
role: "system",
|
||||||
|
content: "你是一个虚拟货币分析师,擅长分析虚拟货币的统计文件,并且给用户提供建议,应该做空还是做多以及具体的杠杆倍数,还有应该在什么点位入场以及具体的止盈止损点位"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
role: "user",
|
||||||
|
content: "帮我分析下这份虚拟货币的统计文件,并且给我提供建议,应该做空还是做多以及具体的杠杆倍数,还有应该在什么点位入场以及具体的止盈止损点位"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
role: "user",
|
||||||
|
content: message
|
||||||
|
}
|
||||||
|
],
|
||||||
model,
|
model,
|
||||||
stream: false,
|
stream: false,
|
||||||
},
|
},
|
||||||
|
|
|
||||||
11
src/main.ts
11
src/main.ts
|
|
@ -5,6 +5,7 @@ import { ConfigService } from '@nestjs/config';
|
||||||
import * as dotenv from 'dotenv';
|
import * as dotenv from 'dotenv';
|
||||||
import { HttpExceptionFilter } from './common/filters/http-exception.filter';
|
import { HttpExceptionFilter } from './common/filters/http-exception.filter';
|
||||||
import { TransformInterceptor } from './common/interceptors/transform.interceptor';
|
import { TransformInterceptor } from './common/interceptors/transform.interceptor';
|
||||||
|
import * as bodyParser from 'body-parser';
|
||||||
|
|
||||||
dotenv.config();
|
dotenv.config();
|
||||||
|
|
||||||
|
|
@ -24,8 +25,10 @@ async function bootstrap() {
|
||||||
await connection.end();
|
await connection.end();
|
||||||
|
|
||||||
app.enableCors({
|
app.enableCors({
|
||||||
origin: 'http://localhost:8000', // 只允许该来源访问
|
origin: ['http://108.171.193.155:8000', 'http://localhost:8000'], // 指定允许的前端地址
|
||||||
credentials: true, // 允许带有凭证(cookies)的请求
|
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());
|
app.useGlobalInterceptors(new TransformInterceptor());
|
||||||
|
|
||||||
|
// 使用 NestJS 内置方法设置限制
|
||||||
|
app.use(bodyParser.json({ limit: '50mb' }));
|
||||||
|
app.use(bodyParser.urlencoded({ limit: '50mb', extended: true }));
|
||||||
|
|
||||||
await app.listen(3030);
|
await app.listen(3030);
|
||||||
}
|
}
|
||||||
bootstrap();
|
bootstrap();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue