@@ -25,7 +25,9 @@ async function readChunk(reader: ReadableStreamDefaultReader<Uint8Array>, handle
2525 }
2626 return true ;
2727}
28- export interface ICustomModelProviderOptions {
28+
29+ /** 与 {@link chat} 入参对齐的选项;由调用方在每次请求前给出当前值(与 props / 状态同步) */
30+ export interface ICustomModelChatOptions {
2931 url : string ;
3032 model : string ;
3133 temperature : number ;
@@ -36,58 +38,47 @@ export interface ICustomModelProviderOptions {
3638 customActions : ICustomActionItem [ ] ;
3739 customFetch ?: CustomFetch ;
3840}
41+
42+ export interface ICustomModelProviderOptions {
43+ getChatOptions : ( ) => ICustomModelChatOptions ;
44+ }
45+
3946export class CustomModelProvider extends BaseModelProvider {
40- private url : string ;
41- private model : string ;
42- private temperature : number ;
43- private customComponents : ICustomComponentItem [ ] ;
44- private customSnippets : IGenPromptSnippet [ ] ;
45- private customExamples : IGenPromptExample [ ] ;
46- private customActions : ICustomActionItem [ ] ;
47- private chatConfig : IChatConfig ;
48- private customFetch ?: CustomFetch ;
47+ private getChatOptions : ( ) => ICustomModelChatOptions ;
4948 protected responseHandlers : IResponseHandler < IStreamData > [ ] = [ ] ;
50- constructor ( { url , model , temperature , chatConfig , customComponents , customSnippets , customExamples , customActions , customFetch } : ICustomModelProviderOptions ) {
49+ constructor ( { getChatOptions } : ICustomModelProviderOptions ) {
5150 super ( { provider : 'custom' } ) ;
52- this . url = url ;
53- this . model = model ;
54- this . temperature = temperature ;
55- this . customComponents = customComponents ;
56- this . customSnippets = customSnippets ;
57- this . customExamples = customExamples ;
58- this . customActions = customActions ;
59- this . chatConfig = chatConfig ;
60- this . customFetch = customFetch ;
51+ this . getChatOptions = getChatOptions ;
6152 }
6253 validateRequest ( _ : ChatCompletionRequest ) { }
6354
64- changeLlmConfig ( model : string , temperature : number ) {
65- this . model = model ;
66- this . temperature = temperature ;
67- }
68-
69- setCustomExamples ( customExamples : IGenPromptExample [ ] ) {
70- this . customExamples = customExamples ;
71- }
7255 setResponseHandlers ( handlers : IResponseHandler < IStreamData > [ ] ) {
7356 this . responseHandlers = handlers ;
7457 }
7558
7659 async getData ( request : ChatCompletionRequest ) {
77- return await chat (
78- {
79- url : this . url ,
80- messages : request . messages ,
81- model : this . model ,
82- temperature : this . temperature ,
83- signal : request . options ?. signal ,
84- customComponents : this . customComponents ,
85- customSnippets : this . customSnippets ,
86- customExamples : this . customExamples ,
87- customActions : this . customActions ,
88- customFetch : this . customFetch ,
89- }
90- ) ;
60+ const {
61+ url,
62+ model,
63+ temperature,
64+ customComponents,
65+ customSnippets,
66+ customExamples,
67+ customActions,
68+ customFetch,
69+ } = this . getChatOptions ( ) ;
70+ return await chat ( {
71+ url,
72+ messages : request . messages ,
73+ model,
74+ temperature,
75+ signal : request . options ?. signal ,
76+ customComponents,
77+ customSnippets,
78+ customExamples,
79+ customActions,
80+ customFetch,
81+ } ) ;
9182 }
9283
9384 async chat ( _ : ChatCompletionRequest ) {
@@ -108,7 +99,8 @@ export class CustomModelProvider extends BaseModelProvider {
10899 const reader = bodyStream . getReader ( ) ;
109100
110101 const context : any = { } ;
111- context . chatConfig = this . chatConfig ;
102+ const { chatConfig } = this . getChatOptions ( ) ;
103+ context . chatConfig = chatConfig ;
112104
113105 const signal = request . options ?. signal ;
114106 signal ?. addEventListener ( 'abort' ,
0 commit comments