-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Update worker.mjs #65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
简单来说用过url中的模型名来替换 请求体的模型,好处可以强制模型名 你的代码片段意图是: 用正则 /^\/([\w.-]+)\/v1\/(chat\/completions|embeddings)$/ 匹配 /模型名/v1/chat/completions 或 /模型名/v1/embeddings 这样的路径, 并从中提取模型名(如 gemini-1.5-pro), 如果不匹配则抛出 404。 这段代码的作用和你刚才的需求完全一致,并且更通用(支持 chat/completions 和 embeddings 两种 endpoint)。
✅ Deploy Preview for gemini-pro ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
你可以看下,代码整体熟悉程度不如大佬,你看下这种方式可不可行。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR updates the API routing logic to extract model names from URL paths and enforce them in request bodies. Instead of matching endpoints by suffix, it now uses a regex pattern to match routes like /<model-name>/v1/chat/completions
and /<model-name>/v1/embeddings
, forcing the URL-specified model to override any model in the request body.
Key changes:
- Replaced switch-case routing with regex-based path matching
- Added model name extraction from URL paths
- Implemented forced model override in request bodies
const match = pathname.match(routeRegex); | ||
|
||
if (!match) { | ||
throw new HttpError("404 Not Found. Use format: /<model-name>/v1/chat/completions", 404); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error message only mentions chat/completions format but the code also supports embeddings. Consider updating to: '404 Not Found. Use format: //v1/{chat/completions|embeddings}'
throw new HttpError("404 Not Found. Use format: /<model-name>/v1/chat/completions", 404); | |
throw new HttpError("404 Not Found. Use format: /<model-name>/v1/{chat/completions|embeddings}", 404); |
Copilot uses AI. Check for mistakes.
|
||
const modelFromUrl = match[1]; | ||
const endpoint = match[2]; | ||
let body; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing method validation. The original code verified POST method for both endpoints. Consider adding 'assert(request.method === "POST");' before parsing JSON.
let body; | |
let body; | |
assert(request.method === "POST"); |
Copilot uses AI. Check for mistakes.
try { | ||
body = await request.json(); | ||
} catch (e) { | ||
body = {}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Silent fallback to empty object when JSON parsing fails could mask client errors. Consider throwing a more specific error for malformed JSON requests.
Copilot uses AI. Check for mistakes.
if (endpoint === "chat/completions") { | ||
return handleCompletions(body, apiKey).catch(errHandler); | ||
} else if (endpoint === "embeddings") { | ||
return handleEmbeddings(body, apiKey).catch(errHandler); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing return statement for the embeddings case and no fallback for unexpected endpoint values. This could cause the function to return undefined.
return handleEmbeddings(body, apiKey).catch(errHandler); | |
return handleEmbeddings(body, apiKey).catch(errHandler); | |
} else { | |
throw new HttpError("Invalid endpoint. Supported endpoints are 'chat/completions' and 'embeddings'.", 400); |
Copilot uses AI. Check for mistakes.
大哥合并一下啊 |
@qqlcx5 |
简单来说用过url中的模型名来替换 请求体的模型,好处可以强制模型名
你的代码片段意图是:
用正则 /^/([\w.-]+)/v1/(chat/completions|embeddings)$/ 匹配 /模型名/v1/chat/completions 或 /模型名/v1/embeddings 这样的路径, 并从中提取模型名(如 gemini-1.5-pro),
如果不匹配则抛出 404。
这段代码的作用和你刚才的需求完全一致,并且更通用(支持 chat/completions 和 embeddings 两种 endpoint)。