letterm commited on
Commit
a293648
·
verified ·
1 Parent(s): 4e15216

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +15 -6
index.js CHANGED
@@ -13,7 +13,7 @@ dotenv.config();
13
  // 配置管理
14
  const CONFIG = {
15
  SERVER: {
16
- PORT: process.env.PORT || 7860,
17
  BODY_LIMIT: '5mb',
18
  CORS_OPTIONS: {
19
  origin: '*',
@@ -40,6 +40,7 @@ const CONFIG = {
40
  "grok-3-imageGen": "grok-3",
41
  },
42
  SIGNATURE_INDEX: 0,
 
43
  IS_IMG_GEN: false,
44
  IS_THINKING: false
45
  };
@@ -281,6 +282,11 @@ class TwitterGrokApiClient {
281
 
282
  return await Utils.handleApiResponse(response, '图片上传失败');
283
  }
 
 
 
 
 
284
 
285
  async transformMessages(messages) {
286
  if (messages[0].role === 'assistant') {
@@ -331,10 +337,10 @@ class TwitterGrokApiClient {
331
  let fileAttachments = [];
332
 
333
  if (typeof content === 'string') {
334
- message = content;
335
  } else if (Array.isArray(content) || typeof content === 'object') {
336
  const { text, imageAttachments } = await this.processComplexContent(content, isLastTwoMessages);
337
- message = text;
338
  fileAttachments = imageAttachments;
339
  }
340
 
@@ -476,6 +482,7 @@ class ResponseHandler {
476
 
477
  switch (model) {
478
  case "grok-3-reasoning":
 
479
  if (!CONFIG.IS_THINKING && jsonData.result?.isThinking) {
480
  message = "<think>" + message;
481
  CONFIG.IS_THINKING = true;
@@ -555,6 +562,8 @@ class ResponseHandler {
555
  static processReasoningMessage(jsonData, isThinking) {
556
  let result = { text: '', isThinking };
557
 
 
 
558
  if (jsonData.result?.isThinking && !isThinking) {
559
  result.text = "<think>" + jsonData.result.message;
560
  result.isThinking = true;
@@ -598,7 +607,7 @@ app.use(express.json({ limit: CONFIG.SERVER.BODY_LIMIT }));
598
  app.use(express.urlencoded({ extended: true, limit: CONFIG.SERVER.BODY_LIMIT }));
599
 
600
  // API 路由
601
- app.get('/hf/v1/models', (req, res) => {
602
  res.json({
603
  object: "list",
604
  data: Object.keys(CONFIG.MODELS).map(model => ({
@@ -609,7 +618,7 @@ app.get('/hf/v1/models', (req, res) => {
609
  }))
610
  });
611
  });
612
- app.post('/hf/v1/chat/completions', async (req, res) => {
613
  try {
614
  const authToken = req.headers.authorization?.replace('Bearer ', '');
615
  if (authToken !== CONFIG.API.API_KEY) {
@@ -680,7 +689,7 @@ app.post('/hf/v1/chat/completions', async (req, res) => {
680
 
681
  // 404 处理
682
  app.use((req, res) => {
683
- res.status(404).json({ error: '请求路径不存在' });
684
  });
685
 
686
  // 启动服务器
 
13
  // 配置管理
14
  const CONFIG = {
15
  SERVER: {
16
+ PORT: process.env.PORT || 25526,
17
  BODY_LIMIT: '5mb',
18
  CORS_OPTIONS: {
19
  origin: '*',
 
40
  "grok-3-imageGen": "grok-3",
41
  },
42
  SIGNATURE_INDEX: 0,
43
+ ISSHOW_SEARCH_RESULTS: process.env.ISSHOW_SEARCH_RESULTS === 'true',
44
  IS_IMG_GEN: false,
45
  IS_THINKING: false
46
  };
 
282
 
283
  return await Utils.handleApiResponse(response, '图片上传失败');
284
  }
285
+ removeThinkTags(text) {
286
+ text = text.replace(/<think>[\s\S]*?<\/think>/g, '').trim();
287
+ text = text.replace(/!\[image\]\(data:.*?base64,.*?\)/g, '[图片]');
288
+ return text;
289
+ };
290
 
291
  async transformMessages(messages) {
292
  if (messages[0].role === 'assistant') {
 
337
  let fileAttachments = [];
338
 
339
  if (typeof content === 'string') {
340
+ message = this.removeThinkTags(content);
341
  } else if (Array.isArray(content) || typeof content === 'object') {
342
  const { text, imageAttachments } = await this.processComplexContent(content, isLastTwoMessages);
343
+ message = this.removeThinkTags(text);
344
  fileAttachments = imageAttachments;
345
  }
346
 
 
482
 
483
  switch (model) {
484
  case "grok-3-reasoning":
485
+ if(!CONFIG.ISSHOW_SEARCH_RESULTS && jsonData.result?.isThinking)return;
486
  if (!CONFIG.IS_THINKING && jsonData.result?.isThinking) {
487
  message = "<think>" + message;
488
  CONFIG.IS_THINKING = true;
 
562
  static processReasoningMessage(jsonData, isThinking) {
563
  let result = { text: '', isThinking };
564
 
565
+ if(!CONFIG.ISSHOW_SEARCH_RESULTS && jsonData.result?.isThinking)return result;
566
+
567
  if (jsonData.result?.isThinking && !isThinking) {
568
  result.text = "<think>" + jsonData.result.message;
569
  result.isThinking = true;
 
607
  app.use(express.urlencoded({ extended: true, limit: CONFIG.SERVER.BODY_LIMIT }));
608
 
609
  // API 路由
610
+ app.get('/v1/models', (req, res) => {
611
  res.json({
612
  object: "list",
613
  data: Object.keys(CONFIG.MODELS).map(model => ({
 
618
  }))
619
  });
620
  });
621
+ app.post('/v1/chat/completions', async (req, res) => {
622
  try {
623
  const authToken = req.headers.authorization?.replace('Bearer ', '');
624
  if (authToken !== CONFIG.API.API_KEY) {
 
689
 
690
  // 404 处理
691
  app.use((req, res) => {
692
+ res.status(404).send('api运行正常');
693
  });
694
 
695
  // 启动服务器