github-actions[bot] commited on
Commit ·
740a4ac
1
Parent(s): 396d368
Update from GitHub Actions
Browse files
server.js
CHANGED
|
@@ -14,6 +14,12 @@ const PROMPT_LIMIT = 4800;
|
|
| 14 |
const SYSTEM_PROMPT_LIMIT = 4800;
|
| 15 |
// === 限制定义结束 ===
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
// 定义 fal-ai/any-llm 支持的模型列表
|
| 18 |
const FAL_SUPPORTED_MODELS = [
|
| 19 |
"anthropic/claude-3.7-sonnet",
|
|
@@ -58,183 +64,193 @@ app.get('/v1/models', (req, res) => {
|
|
| 58 |
}
|
| 59 |
});
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
//
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 236 |
}
|
| 237 |
-
// === convertMessagesToFal 函数结�� ===
|
| 238 |
|
| 239 |
|
| 240 |
// POST /v1/chat/completions endpoint (保持不变)
|
|
|
|
| 14 |
const SYSTEM_PROMPT_LIMIT = 4800;
|
| 15 |
// === 限制定义结束 ===
|
| 16 |
|
| 17 |
+
|
| 18 |
+
export interface OpenAIMessage {
|
| 19 |
+
role: 'system' | 'user' | 'assistant';
|
| 20 |
+
content: string | null;
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
// 定义 fal-ai/any-llm 支持的模型列表
|
| 24 |
const FAL_SUPPORTED_MODELS = [
|
| 25 |
"anthropic/claude-3.7-sonnet",
|
|
|
|
| 64 |
}
|
| 65 |
});
|
| 66 |
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
/**
|
| 70 |
+
* 将 OpenAI 格式的消息转换为 Fal AI 格式的 prompt 和 system_prompt
|
| 71 |
+
*
|
| 72 |
+
* 核心逻辑:倒序遍历 messages,至多取 3 条 user/assistant 消息放到 prompt 部分,
|
| 73 |
+
* chat_history 最多包含 2 条消息(user + assistant),最后一个用户消息是最新提问,不属于对话历史
|
| 74 |
+
*
|
| 75 |
+
* @param messages - OpenAI 格式的消息数组
|
| 76 |
+
* @returns 包含 system_prompt、prompt 和可选错误信息的对象
|
| 77 |
+
*
|
| 78 |
+
* @example
|
| 79 |
+
* // 基本用法:系统消息 + 用户消息
|
| 80 |
+
* const messages = [
|
| 81 |
+
* { role: 'system', content: 'You are a helpful assistant.' },
|
| 82 |
+
* { role: 'user', content: 'Hello, how are you?' }
|
| 83 |
+
* ];
|
| 84 |
+
* const result = convertMessagesToFalPrompt(messages);
|
| 85 |
+
* // result.system_prompt: 'You are a helpful assistant.'
|
| 86 |
+
* // result.prompt: 'Hello, how are you?'
|
| 87 |
+
*
|
| 88 |
+
* @example
|
| 89 |
+
* // 多轮对话:最后一条是用户消息
|
| 90 |
+
* const messages = [
|
| 91 |
+
* { role: 'system', content: 'You are helpful.' },
|
| 92 |
+
* { role: 'user', content: 'What is AI?' },
|
| 93 |
+
* { role: 'assistant', content: 'AI is artificial intelligence.' },
|
| 94 |
+
* { role: 'user', content: 'Tell me more.' }
|
| 95 |
+
* ];
|
| 96 |
+
* const result = convertMessagesToFalPrompt(messages);
|
| 97 |
+
* // result.system_prompt: 'You are helpful.\n<chat_history>'
|
| 98 |
+
* // result.prompt: 'What is AI?\nAssistant: AI is artificial intelligence.\n</chat_history>\nTell me more.'
|
| 99 |
+
*
|
| 100 |
+
* @example
|
| 101 |
+
* // 多轮对话:最后一条不是用户消息
|
| 102 |
+
* const messages = [
|
| 103 |
+
* { role: 'user', content: 'Hello' },
|
| 104 |
+
* { role: 'assistant', content: 'Hi there!' }
|
| 105 |
+
* ];
|
| 106 |
+
* const result = convertMessagesToFalPrompt(messages);
|
| 107 |
+
* // result.system_prompt: '<chat_history>\nHuman: Hello\nAssistant: Hi there!\n</chat_history>'
|
| 108 |
+
* // result.prompt: ''
|
| 109 |
+
*
|
| 110 |
+
* @description
|
| 111 |
+
* 实现逻辑:
|
| 112 |
+
* 1. **系统消息处理**:只使用最后一个非空系统消息,如果超出 SYSTEM_PROMPT_LIMIT 则返回错误
|
| 113 |
+
* 2. **消息过滤**:自动过滤空内容消息(null、undefined、空字符串或纯空格)
|
| 114 |
+
* 3. **倒序遍历**:取最后 3 条消息,根据最后一条消息类型分两种情况:
|
| 115 |
+
*
|
| 116 |
+
* **情况 A - 最后一条是用户消息**:
|
| 117 |
+
* - 取倒数第 3、第 2 条作为 chat_history(最多 2 条:user + assistant)
|
| 118 |
+
* - system_prompt: `系统消息\n<chat_history>`
|
| 119 |
+
* - prompt: `<user message>\nAssistant: <assistant message>\n</chat_history>\n<最新用户消息>`
|
| 120 |
+
*
|
| 121 |
+
* **情况 B - 最后一条不是用户消息**:
|
| 122 |
+
* - 取最后 2 条消息作为 chat_history,放在 system_prompt 中
|
| 123 |
+
* - system_prompt: `系统消息\n<chat_history>\nHuman: <user message>\nAssistant: <assistant message>\n</chat_history>`
|
| 124 |
+
* - prompt: `""`(空字符串)
|
| 125 |
+
*
|
| 126 |
+
* 4. **格式约定**:
|
| 127 |
+
* - prompt 中会自动拼接 Human 消息,所以 user 消息不需要 "Human:" 前缀
|
| 128 |
+
* - system_prompt 中的 user 消息需要 "Human:" 前缀
|
| 129 |
+
* - assistant 消息始终使用 "Assistant:" 前缀
|
| 130 |
+
*
|
| 131 |
+
* @note
|
| 132 |
+
* - 字符限制:系统消息长度不能超过 SYSTEM_PROMPT_LIMIT (4800) 字符
|
| 133 |
+
* - 消息数量:最多处理最近的 3 条对话消息(倒数第 1、2、3 条)
|
| 134 |
+
* - 历史限制:chat_history 最多包含 2 条消息,避免 prompt 过长
|
| 135 |
+
* - 错误处理:系统消息超限时返回错误,其他情况尽力处理
|
| 136 |
+
*/
|
| 137 |
+
export function convertMessagesToFalPrompt(messages: OpenAIMessage[]): { system_prompt: string; prompt: string; error?: string } {
|
| 138 |
+
// 第一步:过滤空内容消息,分离系统消息和对话消息
|
| 139 |
+
const filtered_messages: OpenAIMessage[] = [];
|
| 140 |
+
let system_message_content = "";
|
| 141 |
+
|
| 142 |
+
for (const message of messages) {
|
| 143 |
+
const content = (message.content === null || message.content === undefined) ? "" : String(message.content).trim();
|
| 144 |
+
if (content.length > 0) {
|
| 145 |
+
if (message.role === 'system') {
|
| 146 |
+
system_message_content = content; // 只保留最后一个非空系统消息
|
| 147 |
+
} else {
|
| 148 |
+
filtered_messages.push({
|
| 149 |
+
...message,
|
| 150 |
+
content: content
|
| 151 |
+
});
|
| 152 |
+
}
|
| 153 |
+
}
|
| 154 |
+
}
|
| 155 |
+
|
| 156 |
+
// 检查系统消息长度限制
|
| 157 |
+
if (system_message_content.length > SYSTEM_PROMPT_LIMIT) {
|
| 158 |
+
return {
|
| 159 |
+
system_prompt: "",
|
| 160 |
+
prompt: "",
|
| 161 |
+
error: `System message too long: ${system_message_content.length} characters exceeds limit of ${SYSTEM_PROMPT_LIMIT} characters`
|
| 162 |
+
};
|
| 163 |
+
}
|
| 164 |
+
|
| 165 |
+
// 如果没有对话消息,直接返回
|
| 166 |
+
if (filtered_messages.length === 0) {
|
| 167 |
+
return {
|
| 168 |
+
system_prompt: system_message_content,
|
| 169 |
+
prompt: ""
|
| 170 |
+
};
|
| 171 |
+
}
|
| 172 |
+
|
| 173 |
+
// 第二步:倒序遍历messages,至多取3条user/assistant消息放到prompt部分
|
| 174 |
+
const prompt_messages = filtered_messages.slice(-3); // 取最后3条消息
|
| 175 |
+
const remaining_messages = filtered_messages.slice(0, -3); // 剩余的消息
|
| 176 |
+
|
| 177 |
+
// 第三步:构建prompt部分
|
| 178 |
+
let prompt_parts: string[] = [];
|
| 179 |
+
|
| 180 |
+
for (const message of prompt_messages) {
|
| 181 |
+
if (message.role === 'user') {
|
| 182 |
+
prompt_parts.push(String(message.content));
|
| 183 |
+
} else if (message.role === 'assistant') {
|
| 184 |
+
prompt_parts.push(`Assistant: ${String(message.content)}`);
|
| 185 |
+
}
|
| 186 |
+
}
|
| 187 |
+
|
| 188 |
+
const final_prompt = prompt_parts.join('\n');
|
| 189 |
+
|
| 190 |
+
// 第四步:构建system_prompt部分
|
| 191 |
+
let system_prompt_parts: string[] = [];
|
| 192 |
+
|
| 193 |
+
// 添加系统消息(如果存在)
|
| 194 |
+
if (system_message_content.length > 0) {
|
| 195 |
+
system_prompt_parts.push(system_message_content);
|
| 196 |
+
}
|
| 197 |
+
|
| 198 |
+
// 添加剩余的对话消息
|
| 199 |
+
for (const message of remaining_messages) {
|
| 200 |
+
if (message.role === 'user') {
|
| 201 |
+
system_prompt_parts.push(`Human: ${String(message.content)}`);
|
| 202 |
+
} else if (message.role === 'assistant') {
|
| 203 |
+
system_prompt_parts.push(`Assistant: ${String(message.content)}`);
|
| 204 |
+
}
|
| 205 |
+
}
|
| 206 |
+
|
| 207 |
+
let final_system_prompt = system_prompt_parts.join('\n');
|
| 208 |
+
|
| 209 |
+
// 第五步:检查system_prompt字符限制并截断
|
| 210 |
+
if (final_system_prompt.length > SYSTEM_PROMPT_LIMIT) {
|
| 211 |
+
// 优先保留系统消息,然后从最新的对话开始截断
|
| 212 |
+
const system_part = system_message_content;
|
| 213 |
+
let remaining_space = SYSTEM_PROMPT_LIMIT - system_part.length - 1; // -1 for newline
|
| 214 |
+
|
| 215 |
+
if (remaining_space <= 0) {
|
| 216 |
+
final_system_prompt = system_part;
|
| 217 |
+
} else {
|
| 218 |
+
const conversation_parts: string[] = [];
|
| 219 |
+
|
| 220 |
+
// 倒序添加剩余对话,确保不超过字符限制
|
| 221 |
+
for (let i = remaining_messages.length - 1; i >= 0; i--) {
|
| 222 |
+
const message = remaining_messages[i];
|
| 223 |
+
let message_text = "";
|
| 224 |
+
|
| 225 |
+
if (message.role === 'user') {
|
| 226 |
+
message_text = `Human: ${String(message.content)}`;
|
| 227 |
+
} else if (message.role === 'assistant') {
|
| 228 |
+
message_text = `Assistant: ${String(message.content)}`;
|
| 229 |
+
}
|
| 230 |
+
|
| 231 |
+
if (message_text.length + 1 <= remaining_space) { // +1 for newline
|
| 232 |
+
conversation_parts.unshift(message_text);
|
| 233 |
+
remaining_space -= (message_text.length + 1);
|
| 234 |
+
} else {
|
| 235 |
+
break; // 无法添加更多消息
|
| 236 |
+
}
|
| 237 |
+
}
|
| 238 |
+
|
| 239 |
+
if (system_part.length > 0 && conversation_parts.length > 0) {
|
| 240 |
+
final_system_prompt = system_part + '\n' + conversation_parts.join('\n');
|
| 241 |
+
} else if (system_part.length > 0) {
|
| 242 |
+
final_system_prompt = system_part;
|
| 243 |
+
} else {
|
| 244 |
+
final_system_prompt = conversation_parts.join('\n');
|
| 245 |
+
}
|
| 246 |
+
}
|
| 247 |
+
}
|
| 248 |
+
|
| 249 |
+
return {
|
| 250 |
+
system_prompt: final_system_prompt,
|
| 251 |
+
prompt: final_prompt
|
| 252 |
+
};
|
| 253 |
}
|
|
|
|
| 254 |
|
| 255 |
|
| 256 |
// POST /v1/chat/completions endpoint (保持不变)
|