mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-17 08:50:15 +01:00
fix(OpenAIClient): use official SDK to identify client and avoid false Rate Limit Error (#1161)
* chore: add eslint ignore unused var pattern * feat: add extractBaseURL helper for valid OpenAI reverse proxies, with tests * feat(OpenAIClient): add new chatCompletion using official OpenAI node SDK * fix(ci): revert change to FORCE_PROMPT condition
This commit is contained in:
parent
ed3d7c9f80
commit
5ab9802aa9
10 changed files with 297 additions and 12 deletions
56
api/utils/extractBaseURL.spec.js
Normal file
56
api/utils/extractBaseURL.spec.js
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
const extractBaseURL = require('./extractBaseURL');
|
||||
|
||||
describe('extractBaseURL', () => {
|
||||
test('should extract base URL up to /v1 for standard endpoints', () => {
|
||||
const url = 'https://localhost:8080/v1/chat/completions';
|
||||
expect(extractBaseURL(url)).toBe('https://localhost:8080/v1');
|
||||
});
|
||||
|
||||
test('should include /openai in the extracted URL when present', () => {
|
||||
const url = 'https://localhost:8080/v1/openai';
|
||||
expect(extractBaseURL(url)).toBe('https://localhost:8080/v1/openai');
|
||||
});
|
||||
|
||||
test('should stop at /openai and not include any additional paths', () => {
|
||||
const url = 'https://fake.open.ai/v1/openai/you-are-cool';
|
||||
expect(extractBaseURL(url)).toBe('https://fake.open.ai/v1/openai');
|
||||
});
|
||||
|
||||
test('should return the correct base URL for official openai endpoints', () => {
|
||||
const url = 'https://api.openai.com/v1/chat/completions';
|
||||
expect(extractBaseURL(url)).toBe('https://api.openai.com/v1');
|
||||
});
|
||||
|
||||
test('should handle URLs with reverse proxy pattern correctly', () => {
|
||||
const url = 'https://gateway.ai.cloudflare.com/v1/ACCOUNT_TAG/GATEWAY/openai/completions';
|
||||
expect(extractBaseURL(url)).toBe(
|
||||
'https://gateway.ai.cloudflare.com/v1/ACCOUNT_TAG/GATEWAY/openai',
|
||||
);
|
||||
});
|
||||
|
||||
test('should return null if the URL does not match the expected pattern', () => {
|
||||
const url = 'https://someotherdomain.com/notv1';
|
||||
expect(extractBaseURL(url)).toBeNull();
|
||||
});
|
||||
|
||||
// Test our JSDoc examples.
|
||||
test('should extract base URL up to /v1 for open.ai standard endpoint', () => {
|
||||
const url = 'https://open.ai/v1/chat';
|
||||
expect(extractBaseURL(url)).toBe('https://open.ai/v1');
|
||||
});
|
||||
|
||||
test('should extract base URL up to /v1 for open.ai standard endpoint with additional path', () => {
|
||||
const url = 'https://open.ai/v1/chat/completions';
|
||||
expect(extractBaseURL(url)).toBe('https://open.ai/v1');
|
||||
});
|
||||
|
||||
test('should handle URLs with ACCOUNT/GATEWAY pattern followed by /openai', () => {
|
||||
const url = 'https://open.ai/v1/ACCOUNT/GATEWAY/openai/completions';
|
||||
expect(extractBaseURL(url)).toBe('https://open.ai/v1/ACCOUNT/GATEWAY/openai');
|
||||
});
|
||||
|
||||
test('should include /openai in the extracted URL with additional segments', () => {
|
||||
const url = 'https://open.ai/v1/hi/openai';
|
||||
expect(extractBaseURL(url)).toBe('https://open.ai/v1/hi/openai');
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue