smgc commited on
Commit
ea23575
1 Parent(s): 882f772

Update app.js

Browse files
Files changed (1) hide show
  1. app.js +98 -3
app.js CHANGED
@@ -10,12 +10,107 @@ const NOTDIAMOND_HEADERS = {
10
  'Content-Type': 'application/json',
11
  'next-action': '8189eb37107121e024940f588629a394a594e6a4'
12
  };
13
- const AUTH_KEY = process.env.AUTH_KEY || 'sk-linuxdo';
14
 
15
- // 默认模型,如果用户没有指定模型,就使用这个
16
  const DEFAULT_MODEL = 'gpt-4-turbo-2024-04-09';
17
 
18
- // 保留其他函数定义:createOpenAIChunk, streamNotdiamondResponse
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
  app.use(express.json());
21
 
 
10
  'Content-Type': 'application/json',
11
  'next-action': '8189eb37107121e024940f588629a394a594e6a4'
12
  };
13
+ const AUTH_KEY = process.env.AUTH_KEY;
14
 
 
15
  const DEFAULT_MODEL = 'gpt-4-turbo-2024-04-09';
16
 
17
+ function createOpenAIChunk(content, model, finishReason = null) {
18
+ return {
19
+ id: `chatcmpl-${crypto.randomUUID()}`,
20
+ object: "chat.completion.chunk",
21
+ created: Math.floor(Date.now() / 1000),
22
+ model: model,
23
+ system_fingerprint: "fp_4e2b2da518",
24
+ choices: [
25
+ {
26
+ index: 0,
27
+ delta: content ? { content: content } : {},
28
+ logprobs: null,
29
+ finish_reason: finishReason
30
+ }
31
+ ],
32
+ usage: null
33
+ };
34
+ }
35
+
36
+ async function* streamNotdiamondResponse(response, model) {
37
+ const reader = response.body.getReader();
38
+ const decoder = new TextDecoder();
39
+ let buffer = "";
40
+ let lastContent = "";
41
+
42
+ function processDollars(str) {
43
+ return str.replace(/\${2,}/, match => match.slice(1));
44
+ }
45
+
46
+ while (true) {
47
+ const { done, value } = await reader.read();
48
+ if (done) break;
49
+
50
+ buffer += decoder.decode(value, { stream: true });
51
+ let lines = buffer.split('\n');
52
+ buffer = lines.pop() || "";
53
+
54
+ for (const line of lines) {
55
+ if (line.trim() === '') continue;
56
+
57
+ try {
58
+ const jsonMatch = line.match(/\{.*\}/);
59
+ if (jsonMatch) {
60
+ const data = JSON.parse(jsonMatch[0]);
61
+ let content = '';
62
+
63
+ if (data.output?.curr !== undefined) {
64
+ content = processDollars(data.output.curr);
65
+ lastContent = content;
66
+ } else if (data.curr !== undefined) {
67
+ content = processDollars(data.curr);
68
+ lastContent = content;
69
+ } else if (data.diff !== undefined && Array.isArray(data.diff) && data.diff.length > 1) {
70
+ const newContent = processDollars(data.diff[1]);
71
+ content = lastContent + newContent;
72
+ lastContent = content;
73
+ } else if (data.diff !== undefined && Array.isArray(data.diff) && data.diff.length === 1) {
74
+ content = lastContent; // If diff is empty, use the last content
75
+ }
76
+
77
+ if (content) {
78
+ yield createOpenAIChunk(content, model);
79
+ }
80
+ }
81
+ } catch (error) {
82
+ console.error('Error processing line:', error, 'Raw line:', line);
83
+ }
84
+ }
85
+ }
86
+
87
+ if (buffer.trim()) {
88
+ try {
89
+ const jsonMatch = buffer.match(/\{.*\}/);
90
+ if (jsonMatch) {
91
+ const data = JSON.parse(jsonMatch[0]);
92
+ let content = '';
93
+ if (data.output?.curr !== undefined) {
94
+ content = processDollars(data.output.curr);
95
+ } else if (data.curr !== undefined) {
96
+ content = processDollars(data.curr);
97
+ } else if (data.diff !== undefined && Array.isArray(data.diff) && data.diff.length > 1) {
98
+ const newContent = processDollars(data.diff[1]);
99
+ content = lastContent + newContent;
100
+ } else if (data.diff !== undefined && Array.isArray(data.diff) && data.diff.length === 1) {
101
+ content = lastContent;
102
+ }
103
+ if (content) {
104
+ yield createOpenAIChunk(content, model);
105
+ }
106
+ }
107
+ } catch (error) {
108
+ console.error('Error processing final buffer:', error);
109
+ }
110
+ }
111
+
112
+ yield createOpenAIChunk('', model, 'stop');
113
+ }
114
 
115
  app.use(express.json());
116