@@ -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,54 +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- }
6855 setResponseHandlers ( handlers : IResponseHandler < IStreamData > [ ] ) {
6956 this . responseHandlers = handlers ;
7057 }
7158
7259 async getData ( request : ChatCompletionRequest ) {
73- return await chat (
74- {
75- url : this . url ,
76- messages : request . messages ,
77- model : this . model ,
78- temperature : this . temperature ,
79- signal : request . options ?. signal ,
80- customComponents : this . customComponents ,
81- customSnippets : this . customSnippets ,
82- customExamples : this . customExamples ,
83- customActions : this . customActions ,
84- customFetch : this . customFetch ,
85- }
86- ) ;
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+ } ) ;
8782 }
8883
8984 async chat ( _ : ChatCompletionRequest ) {
@@ -102,9 +97,10 @@ export class CustomModelProvider extends BaseModelProvider {
10297 const bodyStream = response . body ! ;
10398 // const chunkStream = createAsyncIterableStream(getChunkStringStream(bodyStream));
10499 const reader = bodyStream . getReader ( ) ;
105-
100+
106101 const context : any = { } ;
107- context . chatConfig = this . chatConfig ;
102+ const { chatConfig } = this . getChatOptions ( ) ;
103+ context . chatConfig = chatConfig ;
108104
109105 const signal = request . options ?. signal ;
110106 signal ?. addEventListener ( 'abort' ,
@@ -121,7 +117,7 @@ export class CustomModelProvider extends BaseModelProvider {
121117 this . handlerChunk ( data , context ) ;
122118 } ) ;
123119 this . handlerEnd ( context ) ;
124-
120+
125121 }
126122
127123 handlerChunk ( rawData : string , context : any ) {
0 commit comments