dAWkkkk commited on
Commit
030d5a5
·
verified ·
1 Parent(s): 5eb620d

Upload 6 files

Browse files
Files changed (6) hide show
  1. .gitattributes +35 -35
  2. Dockerfile +24 -0
  3. README.md +30 -10
  4. docker-compose.yml +15 -0
  5. package.json +17 -0
  6. server.js +408 -0
.gitattributes CHANGED
@@ -1,35 +1,35 @@
1
- *.7z filter=lfs diff=lfs merge=lfs -text
2
- *.arrow filter=lfs diff=lfs merge=lfs -text
3
- *.bin filter=lfs diff=lfs merge=lfs -text
4
- *.bz2 filter=lfs diff=lfs merge=lfs -text
5
- *.ckpt filter=lfs diff=lfs merge=lfs -text
6
- *.ftz filter=lfs diff=lfs merge=lfs -text
7
- *.gz filter=lfs diff=lfs merge=lfs -text
8
- *.h5 filter=lfs diff=lfs merge=lfs -text
9
- *.joblib filter=lfs diff=lfs merge=lfs -text
10
- *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
- *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
- *.model filter=lfs diff=lfs merge=lfs -text
13
- *.msgpack filter=lfs diff=lfs merge=lfs -text
14
- *.npy filter=lfs diff=lfs merge=lfs -text
15
- *.npz filter=lfs diff=lfs merge=lfs -text
16
- *.onnx filter=lfs diff=lfs merge=lfs -text
17
- *.ot filter=lfs diff=lfs merge=lfs -text
18
- *.parquet filter=lfs diff=lfs merge=lfs -text
19
- *.pb filter=lfs diff=lfs merge=lfs -text
20
- *.pickle filter=lfs diff=lfs merge=lfs -text
21
- *.pkl filter=lfs diff=lfs merge=lfs -text
22
- *.pt filter=lfs diff=lfs merge=lfs -text
23
- *.pth filter=lfs diff=lfs merge=lfs -text
24
- *.rar filter=lfs diff=lfs merge=lfs -text
25
- *.safetensors filter=lfs diff=lfs merge=lfs -text
26
- saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
- *.tar.* filter=lfs diff=lfs merge=lfs -text
28
- *.tar filter=lfs diff=lfs merge=lfs -text
29
- *.tflite filter=lfs diff=lfs merge=lfs -text
30
- *.tgz filter=lfs diff=lfs merge=lfs -text
31
- *.wasm filter=lfs diff=lfs merge=lfs -text
32
- *.xz filter=lfs diff=lfs merge=lfs -text
33
- *.zip filter=lfs diff=lfs merge=lfs -text
34
- *.zst filter=lfs diff=lfs merge=lfs -text
35
- *tfevents* filter=lfs diff=lfs merge=lfs -text
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tar filter=lfs diff=lfs merge=lfs -text
29
+ *.tflite filter=lfs diff=lfs merge=lfs -text
30
+ *.tgz filter=lfs diff=lfs merge=lfs -text
31
+ *.wasm filter=lfs diff=lfs merge=lfs -text
32
+ *.xz filter=lfs diff=lfs merge=lfs -text
33
+ *.zip filter=lfs diff=lfs merge=lfs -text
34
+ *.zst filter=lfs diff=lfs merge=lfs -text
35
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
Dockerfile ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 使用官方 Node.js 18 LTS 镜像作为基础
2
+ FROM node:18-alpine
3
+
4
+ # 设置工作目录
5
+ WORKDIR /usr/src/app
6
+
7
+ # 复制 package.json 和 package-lock.json (如果存在)
8
+ COPY package*.json ./
9
+
10
+ # 安装项目依赖
11
+ RUN npm install
12
+
13
+ # 复制应用源代码
14
+ COPY . .
15
+
16
+ # 暴露应用程序使用的端口
17
+ EXPOSE 3000
18
+
19
+ # 定义环境变量 (可以在 docker-compose 中覆盖)
20
+ ENV PORT=3000
21
+ # FAL_KEY 应该在运行时通过 docker-compose 传入,而不是硬编码在这里
22
+
23
+ # 运行应用程序的命令
24
+ CMD [ "npm", "start" ]
README.md CHANGED
@@ -1,10 +1,30 @@
1
- ---
2
- title: MM
3
- emoji: 🐠
4
- colorFrom: gray
5
- colorTo: purple
6
- sdk: docker
7
- pinned: false
8
- ---
9
-
10
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: fal-openai-proxy
3
+ emoji: 🐳
4
+ colorFrom: red
5
+ colorTo: red
6
+ sdk: docker
7
+ sdk_version: "{{sdkVersion}}"
8
+ app_file: app.py
9
+ pinned: false
10
+ app_port: 3000
11
+ ---
12
+
13
+
14
+
15
+ ## openai请求格式转fal
16
+ 输入限制: System prompt 和 prompt 分别最长为 5000 字符(不是 token)。
17
+
18
+ 输出长度: 测试了下输出长篇小说,出了 5W 多 token。
19
+
20
+ 上下文: 不支持。
21
+
22
+ 于是用 gemini 糊了个 openaiToFal 的服务,模拟上下文以 5000 字符为分界线,分别塞到 System prompt 和 prompt,这样可以把输入扩展到 1W 字符,太早的聊天记录会被顶掉。github 地址是一个 docker compose 包,把你的 key 填入 docker-compose.yml,一键启动 docker compose up -d 即可。默认端口 13000。
23
+
24
+ ## 部署步骤
25
+ 1、修改docker-compose.yml填入fal的api key
26
+
27
+ 2、`docker compose up -d`启动
28
+
29
+ ## 重要
30
+ 我是搭配newapi管理使用,所以**没有鉴权**,有需要自己加。
docker-compose.yml ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ services:
2
+ fal-openai-proxy:
3
+ build: . # 构建当前目录下的 Dockerfile
4
+ container_name: fal_openai_proxy
5
+ ports:
6
+ - "13000:3000" # 将主机的 3000 端口映射到容器的 3000 端口
7
+ environment:
8
+ # 在这里设置你的 Fal AI API Key
9
+ # 或者,为了安全起见,你可以创建一个 .env 文件,并在其中定义 FAL_KEY
10
+ # 然后取消下面行的注释:
11
+ # env_file:
12
+ # - .env
13
+ FAL_KEY: "" # !! 重要:替换为你的真实 Fal AI Key !!
14
+ PORT: 3000 # 确保容器内的端口与 Dockerfile 和 server.js 中一致
15
+ restart: unless-stopped # 服务失败时自动重启,除非手动停止
package.json ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "fal-openai-proxy",
3
+ "version": "1.0.0",
4
+ "description": "Proxy server to convert OpenAI requests to fal-ai format",
5
+ "main": "server.js",
6
+ "type": "module",
7
+ "scripts": {
8
+ "start": "node server.js"
9
+ },
10
+ "dependencies": {
11
+ "@fal-ai/client": "latest",
12
+ "express": "^4.19.2"
13
+ },
14
+ "engines": {
15
+ "node": ">=18.0.0"
16
+ }
17
+ }
server.js ADDED
@@ -0,0 +1,408 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import express from 'express';
2
+ import { fal } from '@fal-ai/client';
3
+
4
+ // --- Key Management ---
5
+ const FAL_KEY_STRING = process.env.FAL_KEY;
6
+ const API_KEY = process.env.API_KEY; // 自定义 API Key 环境变量保持不变
7
+
8
+ if (!FAL_KEY_STRING) {
9
+ console.error("Error: FAL_KEY environment variable is not set.");
10
+ process.exit(1);
11
+ }
12
+
13
+ // 解析 FAL_KEY 字符串为数组,去除空白并过滤空值
14
+ const falKeys = FAL_KEY_STRING.split(',')
15
+ .map(key => key.trim())
16
+ .filter(key => key.length > 0);
17
+
18
+ if (falKeys.length === 0) {
19
+ console.error("Error: FAL_KEY environment variable is set, but no valid keys were found after parsing.");
20
+ process.exit(1);
21
+ }
22
+
23
+ console.log(`Loaded ${falKeys.length} Fal AI Keys.`);
24
+
25
+ let currentFalKeyIndex = 0;
26
+
27
+ // 获取下一个 Fal Key 并循环
28
+ function getNextFalKey() {
29
+ const key = falKeys[currentFalKeyIndex];
30
+ const usedIndex = currentFalKeyIndex; // 记录本次使用的索引,用于日志
31
+ currentFalKeyIndex = (currentFalKeyIndex + 1) % falKeys.length; // 移动到下一个,如果到末尾则回到开头
32
+ console.log(`Using Fal Key at index: ${usedIndex}`);
33
+ return key;
34
+ }
35
+ // --- End Key Management ---
36
+
37
+
38
+ if (!API_KEY) {
39
+ console.error("Error: API_KEY environment variable is not set.");
40
+ process.exit(1);
41
+ }
42
+
43
+ // 注意:不再在这里全局配置 fal.config
44
+ // fal.config({
45
+ // credentials: FAL_KEY, // 移除这行
46
+ // });
47
+
48
+ const app = express();
49
+ app.use(express.json({ limit: '50mb' }));
50
+ app.use(express.urlencoded({ extended: true, limit: '50mb' }));
51
+
52
+ const PORT = process.env.PORT || 3000;
53
+
54
+ // API Key 鉴权中间件 (保持不变)
55
+ const apiKeyAuth = (req, res, next) => {
56
+ const authHeader = req.headers['authorization'];
57
+
58
+ if (!authHeader) {
59
+ console.warn('Unauthorized: No Authorization header provided');
60
+ return res.status(401).json({ error: 'Unauthorized: No API Key provided' });
61
+ }
62
+
63
+ const authParts = authHeader.split(' ');
64
+ if (authParts.length !== 2 || authParts[0].toLowerCase() !== 'bearer') {
65
+ console.warn('Unauthorized: Invalid Authorization header format');
66
+ return res.status(401).json({ error: 'Unauthorized: Invalid Authorization header format' });
67
+ }
68
+
69
+ const providedKey = authParts[1];
70
+ if (providedKey !== API_KEY) {
71
+ console.warn('Unauthorized: Invalid API Key');
72
+ return res.status(401).json({ error: 'Unauthorized: Invalid API Key' });
73
+ }
74
+
75
+ next();
76
+ };
77
+
78
+ // 应用 API Key 鉴权中间件到所有 API 路由 (保持不变)
79
+ app.use(['/v1/models', '/v1/chat/completions'], apiKeyAuth);
80
+
81
+ // === 全局定义限制 === (保持不变)
82
+ const PROMPT_LIMIT = 4800;
83
+ const SYSTEM_PROMPT_LIMIT = 4800;
84
+ // === 限制定义结束 ===
85
+
86
+ // 定义 fal-ai/any-llm 支持的模型列表 (保持不变)
87
+ const FAL_SUPPORTED_MODELS = [
88
+ "anthropic/claude-3.7-sonnet",
89
+ "anthropic/claude-3.5-sonnet",
90
+ "anthropic/claude-3-5-haiku",
91
+ "anthropic/claude-3-haiku",
92
+ "google/gemini-pro-1.5",
93
+ "google/gemini-flash-1.5",
94
+ "google/gemini-flash-1.5-8b",
95
+ "google/gemini-2.0-flash-001",
96
+ "meta-llama/llama-3.2-1b-instruct",
97
+ "meta-llama/llama-3.2-3b-instruct",
98
+ "meta-llama/llama-3.1-8b-instruct",
99
+ "meta-llama/llama-3.1-70b-instruct",
100
+ "openai/gpt-4o-mini",
101
+ "openai/gpt-4o",
102
+ "deepseek/deepseek-r1",
103
+ "meta-llama/llama-4-maverick",
104
+ "meta-llama/llama-4-scout"
105
+ ];
106
+
107
+ // Helper function to get owner from model ID (保持不变)
108
+ const getOwner = (modelId) => {
109
+ if (modelId && modelId.includes('/')) {
110
+ return modelId.split('/')[0];
111
+ }
112
+ return 'fal-ai';
113
+ }
114
+
115
+ // GET /v1/models endpoint (保持不变)
116
+ app.get('/v1/models', (req, res) => {
117
+ console.log("Received request for GET /v1/models");
118
+ try {
119
+ const modelsData = FAL_SUPPORTED_MODELS.map(modelId => ({
120
+ id: modelId, object: "model", created: 1700000000, owned_by: getOwner(modelId)
121
+ }));
122
+ res.json({ object: "list", data: modelsData });
123
+ console.log("Successfully returned model list.");
124
+ } catch (error) {
125
+ console.error("Error processing GET /v1/models:", error);
126
+ res.status(500).json({ error: "Failed to retrieve model list." });
127
+ }
128
+ });
129
+
130
+ // === convertMessagesToFalPrompt 函数 (保持不变) ===
131
+ function convertMessagesToFalPrompt(messages) {
132
+ let fixed_system_prompt_content = "";
133
+ const conversation_message_blocks = [];
134
+ console.log(`Original messages count: ${messages.length}`);
135
+
136
+ // 1. 分离 System 消息,格式化 User/Assistant 消息
137
+ for (const message of messages) {
138
+ let content = (message.content === null || message.content === undefined) ? "" : String(message.content);
139
+ switch (message.role) {
140
+ case 'system':
141
+ fixed_system_prompt_content += `System: ${content}\n\n`;
142
+ break;
143
+ case 'user':
144
+ conversation_message_blocks.push(`Human: ${content}\n\n`);
145
+ break;
146
+ case 'assistant':
147
+ conversation_message_blocks.push(`Assistant: ${content}\n\n`);
148
+ break;
149
+ default:
150
+ console.warn(`Unsupported role: ${message.role}`);
151
+ continue;
152
+ }
153
+ }
154
+
155
+ // 2. 截断合并后的 system 消息(如果超长)
156
+ if (fixed_system_prompt_content.length > SYSTEM_PROMPT_LIMIT) {
157
+ const originalLength = fixed_system_prompt_content.length;
158
+ fixed_system_prompt_content = fixed_system_prompt_content.substring(0, SYSTEM_PROMPT_LIMIT);
159
+ console.warn(`Combined system messages truncated from ${originalLength} to ${SYSTEM_PROMPT_LIMIT}`);
160
+ }
161
+ // 清理末尾可能多余的空白,以便后续判断和拼接
162
+ fixed_system_prompt_content = fixed_system_prompt_content.trim();
163
+
164
+
165
+ // 3. 计算 system_prompt 中留给对话历史的剩余空间
166
+ let space_occupied_by_fixed_system = 0;
167
+ if (fixed_system_prompt_content.length > 0) {
168
+ space_occupied_by_fixed_system = fixed_system_prompt_content.length + 4; // 预留 \n\n...\n\n 的长度
169
+ }
170
+ const remaining_system_limit = Math.max(0, SYSTEM_PROMPT_LIMIT - space_occupied_by_fixed_system);
171
+ console.log(`Trimmed fixed system prompt length: ${fixed_system_prompt_content.length}. Approx remaining system history limit: ${remaining_system_limit}`);
172
+
173
+
174
+ // 4. 反向填充 User/Assistant 对话历史
175
+ const prompt_history_blocks = [];
176
+ const system_prompt_history_blocks = [];
177
+ let current_prompt_length = 0;
178
+ let current_system_history_length = 0;
179
+ let promptFull = false;
180
+ let systemHistoryFull = (remaining_system_limit <= 0);
181
+
182
+ console.log(`Processing ${conversation_message_blocks.length} user/assistant messages for recency filling.`);
183
+ for (let i = conversation_message_blocks.length - 1; i >= 0; i--) {
184
+ const message_block = conversation_message_blocks[i];
185
+ const block_length = message_block.length;
186
+
187
+ if (promptFull && systemHistoryFull) {
188
+ console.log(`Both prompt and system history slots full. Omitting older messages from index ${i}.`);
189
+ break;
190
+ }
191
+
192
+ // 优先尝试放入 prompt
193
+ if (!promptFull) {
194
+ if (current_prompt_length + block_length <= PROMPT_LIMIT) {
195
+ prompt_history_blocks.unshift(message_block);
196
+ current_prompt_length += block_length;
197
+ continue;
198
+ } else {
199
+ promptFull = true;
200
+ console.log(`Prompt limit (${PROMPT_LIMIT}) reached. Trying system history slot.`);
201
+ }
202
+ }
203
+
204
+ // 如果 prompt 满了,尝试放入 system_prompt 的剩余空间
205
+ if (!systemHistoryFull) {
206
+ if (current_system_history_length + block_length <= remaining_system_limit) {
207
+ system_prompt_history_blocks.unshift(message_block);
208
+ current_system_history_length += block_length;
209
+ continue;
210
+ } else {
211
+ systemHistoryFull = true;
212
+ console.log(`System history limit (${remaining_system_limit}) reached.`);
213
+ }
214
+ }
215
+ }
216
+
217
+ // 5. *** 组合最终的 prompt 和 system_prompt (包含分隔符逻辑) ***
218
+ const system_prompt_history_content = system_prompt_history_blocks.join('').trim();
219
+ const final_prompt = prompt_history_blocks.join('').trim();
220
+
221
+ // 定义分隔符
222
+ const SEPARATOR = "\n\n-------下面是比较早之前的对话内容-----\n\n";
223
+
224
+ let final_system_prompt = "";
225
+
226
+ const hasFixedSystem = fixed_system_prompt_content.length > 0;
227
+ const hasSystemHistory = system_prompt_history_content.length > 0;
228
+
229
+ if (hasFixedSystem && hasSystemHistory) {
230
+ final_system_prompt = fixed_system_prompt_content + SEPARATOR + system_prompt_history_content;
231
+ console.log("Combining fixed system prompt and history with separator.");
232
+ } else if (hasFixedSystem) {
233
+ final_system_prompt = fixed_system_prompt_content;
234
+ console.log("Using only fixed system prompt.");
235
+ } else if (hasSystemHistory) {
236
+ final_system_prompt = system_prompt_history_content;
237
+ console.log("Using only history in system prompt slot.");
238
+ }
239
+
240
+ const result = {
241
+ system_prompt: final_system_prompt,
242
+ prompt: final_prompt
243
+ };
244
+
245
+ console.log(`Final system_prompt length (Sys+Separator+Hist): ${result.system_prompt.length}`);
246
+ console.log(`Final prompt length (Hist): ${result.prompt.length}`);
247
+
248
+ return result;
249
+ }
250
+ // === convertMessagesToFalPrompt 函数结束 ===
251
+
252
+
253
+ // POST /v1/chat/completions endpoint (主要修改处)
254
+ app.post('/v1/chat/completions', async (req, res) => {
255
+ const { model, messages, stream = false, reasoning = false, ...restOpenAIParams } = req.body;
256
+
257
+ console.log(`Received chat completion request for model: ${model}, stream: ${stream}`);
258
+
259
+ if (!FAL_SUPPORTED_MODELS.includes(model)) {
260
+ console.warn(`Warning: Requested model '${model}' is not in the explicitly supported list.`);
261
+ }
262
+ if (!model || !messages || !Array.isArray(messages) || messages.length === 0) {
263
+ console.error("Invalid request parameters:", { model, messages: Array.isArray(messages) ? messages.length : typeof messages });
264
+ return res.status(400).json({ error: 'Missing or invalid parameters: model and messages array are required.' });
265
+ }
266
+
267
+ try {
268
+ // *** 在处理请求前,获取下一个 Fal Key 并配置 fal 客户端 ***
269
+ const selectedFalKey = getNextFalKey();
270
+ fal.config({
271
+ credentials: selectedFalKey,
272
+ });
273
+ // *********************************************************
274
+
275
+ const { prompt, system_prompt } = convertMessagesToFalPrompt(messages);
276
+
277
+ const falInput = {
278
+ model: model,
279
+ prompt: prompt,
280
+ ...(system_prompt && { system_prompt: system_prompt }),
281
+ reasoning: !!reasoning,
282
+ };
283
+ console.log("Fal Input:", JSON.stringify(falInput, null, 2));
284
+ console.log("Forwarding request to fal-ai with system-priority + separator + recency input:");
285
+ console.log("System Prompt Length:", system_prompt?.length || 0);
286
+ console.log("Prompt Length:", prompt?.length || 0);
287
+ console.log("--- System Prompt Start ---");
288
+ console.log(system_prompt);
289
+ console.log("--- System Prompt End ---");
290
+ console.log("--- Prompt Start ---");
291
+ console.log(prompt);
292
+ console.log("--- Prompt End ---");
293
+
294
+
295
+ // --- 流式/非流式处理逻辑 (保持不变) ---
296
+ if (stream) {
297
+ res.setHeader('Content-Type', 'text/event-stream; charset=utf-8');
298
+ res.setHeader('Cache-Control', 'no-cache');
299
+ res.setHeader('Connection', 'keep-alive');
300
+ res.setHeader('Access-Control-Allow-Origin', '*');
301
+ res.flushHeaders();
302
+
303
+ let previousOutput = '';
304
+
305
+ const falStream = await fal.stream("fal-ai/any-llm", { input: falInput });
306
+
307
+ try {
308
+ for await (const event of falStream) {
309
+ const currentOutput = (event && typeof event.output === 'string') ? event.output : '';
310
+ const isPartial = (event && typeof event.partial === 'boolean') ? event.partial : true;
311
+ const errorInfo = (event && event.error) ? event.error : null;
312
+
313
+ if (errorInfo) {
314
+ console.error("Error received in fal stream event:", errorInfo);
315
+ const errorChunk = { id: `chatcmpl-${Date.now()}-error`, object: "chat.completion.chunk", created: Math.floor(Date.now() / 1000), model: model, choices: [{ index: 0, delta: {}, finish_reason: "error", message: { role: 'assistant', content: `Fal Stream Error: ${JSON.stringify(errorInfo)}` } }] };
316
+ res.write(`data: ${JSON.stringify(errorChunk)}\n\n`);
317
+ break; // Stop processing on error
318
+ }
319
+
320
+ let deltaContent = '';
321
+ if (currentOutput.startsWith(previousOutput)) {
322
+ deltaContent = currentOutput.substring(previousOutput.length);
323
+ } else if (currentOutput.length > 0) {
324
+ console.warn("Fal stream output mismatch detected. Sending full current output as delta.", { previousLength: previousOutput.length, currentLength: currentOutput.length });
325
+ deltaContent = currentOutput;
326
+ previousOutput = ''; // Reset previous if mismatch
327
+ }
328
+ previousOutput = currentOutput;
329
+
330
+ // Send chunk if there's content or if it's the final chunk (isPartial is false)
331
+ if (deltaContent || !isPartial) {
332
+ const openAIChunk = { id: `chatcmpl-${Date.now()}`, object: "chat.completion.chunk", created: Math.floor(Date.now() / 1000), model: model, choices: [{ index: 0, delta: { content: deltaContent }, finish_reason: isPartial === false ? "stop" : null }] };
333
+ res.write(`data: ${JSON.stringify(openAIChunk)}\n\n`);
334
+ }
335
+ }
336
+ // After the loop, ensure the [DONE] signal is sent if the stream finished normally
337
+ if (!res.writableEnded) {
338
+ res.write(`data: [DONE]\n\n`);
339
+ res.end();
340
+ console.log("Stream finished and [DONE] sent.");
341
+ }
342
+
343
+ } catch (streamError) {
344
+ console.error('Error during fal stream processing loop:', streamError);
345
+ try {
346
+ if (!res.writableEnded) { // Check if we can still write to the response
347
+ const errorDetails = (streamError instanceof Error) ? streamError.message : JSON.stringify(streamError);
348
+ res.write(`data: ${JSON.stringify({ error: { message: "Stream processing error", type: "proxy_error", details: errorDetails } })}\n\n`);
349
+ res.write(`data: [DONE]\n\n`); // Send DONE even after error
350
+ res.end();
351
+ } else {
352
+ console.error("Stream already ended, cannot send error message.");
353
+ }
354
+ } catch (finalError) {
355
+ console.error('Error sending stream error message to client:', finalError);
356
+ if (!res.writableEnded) { res.end(); }
357
+ }
358
+ }
359
+ } else {
360
+ // --- 非流式处理 (保持不变) ---
361
+ console.log("Executing non-stream request...");
362
+ const result = await fal.subscribe("fal-ai/any-llm", { input: falInput, logs: true });
363
+ console.log("Received non-stream result from fal-ai:", JSON.stringify(result, null, 2));
364
+
365
+ if (result && result.error) {
366
+ console.error("Fal-ai returned an error in non-stream mode:", result.error);
367
+ return res.status(500).json({ object: "error", message: `Fal-ai error: ${JSON.stringify(result.error)}`, type: "fal_ai_error", param: null, code: null });
368
+ }
369
+
370
+ const openAIResponse = {
371
+ id: `chatcmpl-${result.requestId || Date.now()}`, object: "chat.completion", created: Math.floor(Date.now() / 1000), model: model,
372
+ choices: [{ index: 0, message: { role: "assistant", content: result.output || "" }, finish_reason: "stop" }],
373
+ usage: { prompt_tokens: null, completion_tokens: null, total_tokens: null }, system_fingerprint: null,
374
+ ...(result.reasoning && { fal_reasoning: result.reasoning }),
375
+ };
376
+ res.json(openAIResponse);
377
+ console.log("Returned non-stream response.");
378
+ }
379
+
380
+ } catch (error) {
381
+ console.error('Unhandled error in /v1/chat/completions:', error);
382
+ if (!res.headersSent) {
383
+ const errorMessage = (error instanceof Error) ? error.message : JSON.stringify(error);
384
+ res.status(500).json({ error: 'Internal Server Error in Proxy', details: errorMessage });
385
+ } else if (!res.writableEnded) {
386
+ console.error("Headers already sent, ending response.");
387
+ res.end();
388
+ }
389
+ }
390
+ });
391
+
392
+ // 启动服务器 (更新启动信息)
393
+ app.listen(PORT, () => {
394
+ console.log(`===================================================`);
395
+ console.log(` Fal OpenAI Proxy Server (Key Rotation + System Top + Separator + Recency)`);
396
+ console.log(` Listening on port: ${PORT}`);
397
+ console.log(` Loaded ${falKeys.length} Fal AI Keys for rotation.`);
398
+ console.log(` Using Limits: System Prompt=${SYSTEM_PROMPT_LIMIT}, Prompt=${PROMPT_LIMIT}`);
399
+ console.log(` API Key Auth Enabled: ${API_KEY ? 'Yes' : 'No'}`);
400
+ console.log(` Chat Completions Endpoint: POST http://localhost:${PORT}/v1/chat/completions`);
401
+ console.log(` Models Endpoint: GET http://localhost:${PORT}/v1/models`);
402
+ console.log(`===================================================`);
403
+ });
404
+
405
+ // 根路径响应 (更新信息)
406
+ app.get('/', (req, res) => {
407
+ res.send('Fal OpenAI Proxy (Key Rotation + System Top + Separator + Recency Strategy) is running.');
408
+ });