Skip to content

Conversation

qqlcx5
Copy link

@qqlcx5 qqlcx5 commented Jul 16, 2025

简单来说用过url中的模型名来替换 请求体的模型,好处可以强制模型名
你的代码片段意图是:
用正则 /^/([\w.-]+)/v1/(chat/completions|embeddings)$/ 匹配 /模型名/v1/chat/completions 或 /模型名/v1/embeddings 这样的路径, 并从中提取模型名(如 gemini-1.5-pro),
如果不匹配则抛出 404。
这段代码的作用和你刚才的需求完全一致,并且更通用(支持 chat/completions 和 embeddings 两种 endpoint)。

简单来说用过url中的模型名来替换 请求体的模型,好处可以强制模型名
你的代码片段意图是:
用正则 /^\/([\w.-]+)\/v1\/(chat\/completions|embeddings)$/ 匹配 /模型名/v1/chat/completions 或 /模型名/v1/embeddings 这样的路径,
并从中提取模型名(如 gemini-1.5-pro),
如果不匹配则抛出 404。
这段代码的作用和你刚才的需求完全一致,并且更通用(支持 chat/completions 和 embeddings 两种 endpoint)。
Copy link

netlify bot commented Jul 16, 2025

Deploy Preview for gemini-pro ready!

Name Link
🔨 Latest commit 36b06f5
🔍 Latest deploy log https://app.netlify.com/projects/gemini-pro/deploys/68770af006f07400081937a9
😎 Deploy Preview https://deploy-preview-65--gemini-pro.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@qqlcx5
Copy link
Author

qqlcx5 commented Jul 16, 2025

你可以看下,代码整体熟悉程度不如大佬,你看下这种方式可不可行。

@johnd0e johnd0e requested a review from Copilot July 16, 2025 21:49
Copy link

@Copilot Copilot AI left a 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);
Copy link
Preview

Copilot AI Jul 16, 2025

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}'

Suggested change
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;
Copy link
Preview

Copilot AI Jul 16, 2025

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.

Suggested change
let body;
let body;
assert(request.method === "POST");

Copilot uses AI. Check for mistakes.

try {
body = await request.json();
} catch (e) {
body = {};
Copy link
Preview

Copilot AI Jul 16, 2025

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);
Copy link
Preview

Copilot AI Jul 16, 2025

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.

Suggested change
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
Copy link
Author

qqlcx5 commented Jul 18, 2025

大哥合并一下啊

@johnd0e
Copy link
Contributor

johnd0e commented Aug 19, 2025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants