feat: Add COHERE_API_URL environment variable support for custom Cohere endpoints#9544
feat: Add COHERE_API_URL environment variable support for custom Cohere endpoints#9544busla wants to merge 8 commits intodanny-avila:mainfrom
Conversation
|
@danny-avila are you available for a review? |
|
I was just about to try out this great looking project, when I noticed the Cohere API URL cannot be changed. This feature would really be useful. |
|
@danny-avila This seems to be ready for review (as per our Discord convo). |
|
It would be fantastic if it was possible to get this in 🙏 |
|
Hi, Thanks for looking into this and proposing an easy change to use custom Cohere endpoints for the reranker. As we'd also like to use this, I've tested the solution, but until now, haven't been able to get it working. What I did is the following:
when starting Librechat, I see no errors, however, when I use web search, I see the following message in my logs: error: Error using Cohere reranker: Request failed with status code 401 which seems to suggest that it's using the wrong endpoint. Am I overloooking something here ? |
Can you verify with a simple script that your api is correct when you call your custom endpoint? |
|
The custom endpoint works as expected. With the help of mitmdump, I discovered it's not using the correct endpoint, but sending a request to api.cohere.com/v2/rank . That on it's turn is causing the 401 error. The only reference to this endpoint that I could find is part of the agents package, which Librechat depends on (https://github.com/danny-avila/agents/tree/main/src/tools/search/rerankers.ts) |
Just so I understand you correctly, did you set the |
|
No, that wasn't indeed very clear. What I meant to say is, that when I test the custom endpoint in a simple script as you suggested, I see endpoint calls respond as expected. In Librechat though, even when I specify the custom endpoint using the COHERE_API_URL and COHERE_API_KEY (after making the changes to packages/data-provider/src/config.ts), Librechat is still calling the api.cohere.com/v2/rank endpoint when I use the web search feature. I assumed the agents package that still uses this endpoint was the reason for that. Could you perhaps share which version of the agents package you're using ? |
|
Thanks for the clarification. Let's me take a look at this again. |
…ank endpoints - Add cohereBaseUrl to webSearchSchema configuration - Update loadWebSearchConfig to load cohereBaseUrl from environment - Add cohereBaseUrl to TWebSearchKeys type definition - Configure cohereBaseUrl as optional parameter for cohere reranker - Add COHERE_BASE_URL documentation to .env.example This enables configuration of custom Cohere endpoints (such as LiteLLM proxy) for web search reranking. The configuration infrastructure is ready for when the @librechat/agents package supports custom base URLs. Usage: COHERE_BASE_URL=http://litellm:8000/v1 COHERE_API_KEY=your_api_key
Replace enum with dynamic getter to enable runtime environment variable lookup. CohereConstants.API_URL now automatically uses COHERE_BASE_URL if set, otherwise defaults to https://api.cohere.ai/v1. This enables using custom Cohere endpoints (like LiteLLM proxy) for web search reranking by simply setting the COHERE_BASE_URL environment variable. Usage: export COHERE_BASE_URL=http://litellm:8000/v1 export COHERE_API_KEY=your_api_key The change is backwards compatible and requires no configuration changes.
4a71871 to
868df81
Compare
This reverts commit 868df81.
Update web.spec.ts to include cohereApiUrl in: - Expected default config object - URL default placeholders test - Custom URLs preservation test Co-Authored-By: Claude <noreply@anthropic.com>
|
Added a PR to the agents repo for this to take effect |
|
Any news on when this could be merged @danny-avila ? |
|
Would also love to see this merged |
|
just want to say i've been running a custom image with this PR for the past month or two, works perfectly. so adding to the official build would be very welcome! |
Summary
This PR adds support for overriding the Cohere API endpoint using the
COHERE_API_URLenvironment variable, enabling integration with custom Cohere endpoints such as LiteLLM proxy deployments.Key Changes
COHERE_API_URLenvironment variable for custom Cohere endpoint configurationTechnical Details
packages/data-provider/src/config.ts,.env.examplehttps://api.cohere.ai/v1) if not configuredas constassertionUsage Examples
Benefits
Testing
Documentation
.env.examplewith clear usage instructionsThis implementation follows LibreChat's established patterns for API provider configuration and maintains the same high standards for code quality, security, and maintainability.
Type of Change
Testing
🤖 Generated with Claude Code