-
Notifications
You must be signed in to change notification settings - Fork 1.3k
fix(core): add modelscope provider to handle stream_options #848
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
Open
DS-Controller2
wants to merge
1
commit into
QwenLM:main
Choose a base branch
from
DS-Controller2:fix/modelscope-400-error
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
packages/core/src/core/openaiContentGenerator/provider/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
packages/core/src/core/openaiContentGenerator/provider/modelscope.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2025 Qwen | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import { describe, it, expect, vi, beforeEach } from 'vitest'; | ||
| import type OpenAI from 'openai'; | ||
| import { ModelScopeOpenAICompatibleProvider } from './modelscope.js'; | ||
| import type { Config } from '../../../config/config.js'; | ||
| import type { ContentGeneratorConfig } from '../../contentGenerator.js'; | ||
|
|
||
| vi.mock('openai'); | ||
|
|
||
| describe('ModelScopeOpenAICompatibleProvider', () => { | ||
| let provider: ModelScopeOpenAICompatibleProvider; | ||
| let mockContentGeneratorConfig: ContentGeneratorConfig; | ||
| let mockCliConfig: Config; | ||
|
|
||
| beforeEach(() => { | ||
| mockContentGeneratorConfig = { | ||
| apiKey: 'test-api-key', | ||
| baseUrl: 'https://api.modelscope.cn/v1', | ||
| model: 'qwen-max', | ||
| } as ContentGeneratorConfig; | ||
|
|
||
| mockCliConfig = { | ||
| getCliVersion: vi.fn().mockReturnValue('1.0.0'), | ||
| } as unknown as Config; | ||
|
|
||
| provider = new ModelScopeOpenAICompatibleProvider( | ||
| mockContentGeneratorConfig, | ||
| mockCliConfig, | ||
| ); | ||
| }); | ||
|
|
||
| describe('isModelScopeProvider', () => { | ||
| it('should return true if baseUrl includes "modelscope"', () => { | ||
| const config = { baseUrl: 'https://api.modelscope.cn/v1' }; | ||
| expect( | ||
| ModelScopeOpenAICompatibleProvider.isModelScopeProvider( | ||
| config as ContentGeneratorConfig, | ||
| ), | ||
| ).toBe(true); | ||
| }); | ||
|
|
||
| it('should return false if baseUrl does not include "modelscope"', () => { | ||
| const config = { baseUrl: 'https://api.openai.com/v1' }; | ||
| expect( | ||
| ModelScopeOpenAICompatibleProvider.isModelScopeProvider( | ||
| config as ContentGeneratorConfig, | ||
| ), | ||
| ).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe('buildRequest', () => { | ||
| it('should remove stream_options when stream is false', () => { | ||
| const originalRequest: OpenAI.Chat.ChatCompletionCreateParams = { | ||
| model: 'qwen-max', | ||
| messages: [{ role: 'user', content: 'Hello!' }], | ||
| stream: false, | ||
| stream_options: { include_usage: true }, | ||
| }; | ||
|
|
||
| const result = provider.buildRequest(originalRequest, 'prompt-id'); | ||
|
|
||
| expect(result).not.toHaveProperty('stream_options'); | ||
| }); | ||
|
|
||
| it('should keep stream_options when stream is true', () => { | ||
| const originalRequest: OpenAI.Chat.ChatCompletionCreateParams = { | ||
| model: 'qwen-max', | ||
| messages: [{ role: 'user', content: 'Hello!' }], | ||
| stream: true, | ||
| stream_options: { include_usage: true }, | ||
| }; | ||
|
|
||
| const result = provider.buildRequest(originalRequest, 'prompt-id'); | ||
|
|
||
| expect(result).toHaveProperty('stream_options'); | ||
| }); | ||
|
|
||
| it('should handle requests without stream_options', () => { | ||
| const originalRequest: OpenAI.Chat.ChatCompletionCreateParams = { | ||
| model: 'qwen-max', | ||
| messages: [{ role: 'user', content: 'Hello!' }], | ||
| stream: false, | ||
| }; | ||
|
|
||
| const result = provider.buildRequest(originalRequest, 'prompt-id'); | ||
|
|
||
| expect(result).not.toHaveProperty('stream_options'); | ||
| }); | ||
| }); | ||
| }); |
32 changes: 32 additions & 0 deletions
32
packages/core/src/core/openaiContentGenerator/provider/modelscope.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import type OpenAI from 'openai'; | ||
| import { DefaultOpenAICompatibleProvider } from './default.js'; | ||
| import type { ContentGeneratorConfig } from '../../contentGenerator.js'; | ||
|
|
||
| /** | ||
| * Provider for ModelScope API | ||
| */ | ||
| export class ModelScopeOpenAICompatibleProvider extends DefaultOpenAICompatibleProvider { | ||
| /** | ||
| * Checks if the configuration is for ModelScope. | ||
| */ | ||
| static isModelScopeProvider(config: ContentGeneratorConfig): boolean { | ||
| return !!config.baseUrl?.includes('modelscope'); | ||
| } | ||
|
|
||
| /** | ||
| * ModelScope does not support `stream_options` when `stream` is false. | ||
| * This method removes `stream_options` if `stream` is not true. | ||
| */ | ||
| override buildRequest( | ||
| request: OpenAI.Chat.ChatCompletionCreateParams, | ||
| userPromptId: string, | ||
| ): OpenAI.Chat.ChatCompletionCreateParams { | ||
| const newRequest = super.buildRequest(request, userPromptId); | ||
| if (!newRequest.stream) { | ||
| delete (newRequest as OpenAI.Chat.ChatCompletionCreateParamsNonStreaming) | ||
| .stream_options; | ||
| } | ||
|
|
||
| return newRequest; | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@DS-Controller2 Thanks for the contribution! Please go ahead and remove the
settings.jsonfile, and we'll get it merged.