Skip to content
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

fix: 修复环境变量BASE_URL设置为Cloudflare AI Gateway时, #4926

Closed

Conversation

evansuner
Copy link

@evansuner evansuner commented Jul 5, 2024

请求路径错误的问题
报错信息:
Unknown request URL:
POST /v1/v1/chat/completions?path=v1&path=chat&path=completions.
Please check the URL for typos,
or see the docs at https://platform.openai.com/docs/api-reference/.

Summary by CodeRabbit

  • Bug Fixes
    • Improved handling of URL patterns to ensure compatibility with different base URLs, including Cloudflare, for more reliable API requests.

请求路径错误的问题
报错信息:
Unknown request URL:
POST /v1/v1/chat/completions?path=v1&path=chat&path=completions.
Please check the URL for typos,
or see the docs at https://platform.openai.com/docs/api-reference/.
Copy link

vercel bot commented Jul 5, 2024

Someone is attempting to deploy a commit to the NextChat Team on Vercel.

A member of the Team first needs to authorize it.

Copy link
Contributor

coderabbitai bot commented Jul 5, 2024

Walkthrough

The requestOpenai function has been enhanced to dynamically adjust its URL path based on the baseUrl. This adaptive logic ensures compatibility with different server patterns, specifically customizing how paths are handled for Cloudflare versus other cases.

Changes

File Summary
app/api/common.ts Updated requestOpenai function to conditionally modify the path variable based on the baseUrl.

Poem

In code’s vast and sprawling maze,
A path we shape in many ways. 🛤️
Through Cloudflare’s guard and open gates,
We tailor routes, no path debates. 🛡️
Adapt and thrive, our app now sings,
With open skies its service brings. ☁️🐇


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between c4a6c93 and b8f2aa2.

Files selected for processing (1)
  • app/api/common.ts (2 hunks)
Additional comments not posted (1)
app/api/common.ts (1)

43-51: Verify the conditional path modification logic.

The logic to conditionally modify the path based on baseUrl looks correct. Ensure that it handles all URL patterns appropriately and doesn't introduce new issues.

Verification successful

Verify the conditional path modification logic.

The logic to conditionally modify the path based on baseUrl looks correct. The req.nextUrl.pathname and req.nextUrl.search are used to construct the path, and the replacement logic is applied conditionally based on the baseUrl. This should handle the intended URL patterns appropriately.

  • app/api/common.ts: The requestOpenai function modifies the path based on whether the baseUrl corresponds to Cloudflare or other URLs.

Given the context and the usage of requestOpenai, the logic appears to be sound and should handle the URL patterns correctly.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that the conditional path modification logic handles all URL patterns correctly.

# Test: Search for all occurrences of the `requestOpenai` function and check the paths being passed to it.
rg --type python $'requestOpenai'

Length of output: 63


Script:

#!/bin/bash
# Description: Verify that the conditional path modification logic handles all URL patterns correctly.

# Test: Search for all occurrences of the `requestOpenai` function and check the paths being passed to it.
rg --type js requestOpenai
rg --type ts requestOpenai

Length of output: 292


Script:

#!/bin/bash
# Extract the relevant code from `app/api/openai/[...path]/route.ts` to analyze the paths being passed to `requestOpenai`.
rg --type ts --context 10 'requestOpenai' app/api/openai/[...path]/route.ts

Length of output: 1287


Script:

#!/bin/bash
# Search for the structure of `req.nextUrl` and its usage in the codebase.
rg --type ts 'req.nextUrl'

Length of output: 408


Script:

#!/bin/bash
# Search for the `NextRequest` type definition in the codebase to understand the structure of `req.nextUrl`.
rg --type ts 'NextRequest'

Length of output: 1201

Copy link
Contributor

github-actions bot commented Jul 5, 2024

Your build has completed!

Preview deployment

@lloydzhou
Copy link
Contributor

lloydzhou commented Jul 5, 2024

#4405 #3357 #3347 #3079

@Issues-translate-bot

This comment was marked as spam.

@lloydzhou
Copy link
Contributor

https://developers.cloudflare.com/ai-gateway/providers/openai/

看了下 cloudflare ai gateway的文档,这个修复应该能解决问题。
但是,这种在代码中直接判断单个域名,再做特殊处理的处理方式值得商榷。

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically.


https://developers.cloudflare.com/ai-gateway/providers/openai/

After reading the documentation of cloudflare ai gateway, this fix should solve the problem.
However, this method of directly determining a single domain name in the code and then doing special processing is questionable.

@evansuner
Copy link
Author

https://developers.cloudflare.com/ai-gateway/providers/openai/

看了下 cloudflare ai gateway的文档,这个修复应该能解决问题。 但是,这种在代码中直接判断单个域名,再做特殊处理的处理方式值得商榷。

谢谢大佬的回复,我知道我方式在某些情况下可能会导致代码的可维护性以及可扩展性问题,有没有什么办法能够解决这个问题呢

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically.


https://developers.cloudflare.com/ai-gateway/providers/openai/

After reading the documentation of cloudflare ai gateway, this fix should solve the problem. However, this method of directly determining a single domain name in the code and then doing special processing is questionable.

Thank you for your reply. I know that my method may cause code maintainability and scalability problems in some cases. Is there any way to solve this problem?

@lloydzhou
Copy link
Contributor

lloydzhou commented Jul 12, 2024

@evansuner
最终还是妥协了,还是需要用域名判断是不是ai gateway的域名。
只是,统一放到了一个地方,针对openai, azure-openai, anthropic在cf上的转发链接进行了处理。

最后,非常感谢您积极提交代码维护项目!

@lloydzhou lloydzhou closed this Jul 12, 2024
@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically.


@evansuner
In the end, we compromised, and we still need to use the domain name to determine whether it is the domain name of ai gateway.
However, they are put in one place and the forwarding links of openai, azure-openai, and anthropic on cf are processed.

@lloydzhou lloydzhou added the enhancement New feature or request label Jul 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants