@@ -4,8 +4,8 @@ import type { AIMessageChunk, BaseMessageLike } from '@langchain/core/messages'
4
4
import type { Runnable } from '@langchain/core/runnables'
5
5
import type { StructuredTool } from '@langchain/core/tools'
6
6
import { concat } from '@langchain/core/utils/stream'
7
- import { AzureChatOpenAI } from '@langchain/openai'
8
- import { formatInstructions , ValidModelName , validModels } from '../../../config'
7
+ import { AzureChatOpenAI , ChatOpenAICallOptions } from '@langchain/openai'
8
+ import { ValidModelName , validModels } from '../../../config'
9
9
import type { ChatEvent } from '../../../shared/chat'
10
10
import type { ChatMessage } from '../../../shared/chat'
11
11
import type { ChatToolDef , ChatToolOutput } from '../../../shared/tools'
@@ -16,20 +16,15 @@ import { MockModel } from './MockModel'
16
16
17
17
type ChatModel = Runnable < BaseLanguageModelInput , AIMessageChunk , BaseChatModelCallOptions >
18
18
19
- const getChatModel = ( model : ValidModelName , tools : StructuredTool [ ] , temperature : number ) : ChatModel => {
20
- const modelConfig = validModels . find ( ( m ) => m . name === model )
21
- if ( ! modelConfig ) {
22
- throw new Error ( `Invalid model: ${ model } ` )
23
- }
24
-
19
+ const getChatModel = ( modelConfig : ( typeof validModels ) [ number ] , tools : StructuredTool [ ] , temperature : number ) : ChatModel => {
25
20
const chatModel =
26
21
modelConfig . name === 'mock'
27
22
? new MockModel ( { tools, temperature } )
28
- : new AzureChatOpenAI ( {
29
- model,
23
+ : new AzureChatOpenAI < ChatOpenAICallOptions > ( {
24
+ model : modelConfig . name ,
30
25
azureOpenAIApiKey : AZURE_API_KEY ,
31
26
azureOpenAIApiVersion : '2023-05-15' ,
32
- azureOpenAIApiDeploymentName : model , // In Azure, always use the acual model name as the deployment name
27
+ azureOpenAIApiDeploymentName : modelConfig . name , // In Azure, always use the acual model name as the deployment name
33
28
azureOpenAIApiInstanceName : AZURE_RESOURCE ,
34
29
temperature : 'temperature' in modelConfig ? modelConfig . temperature : temperature , // If model config specifies a temperature, use it; otherwise, use the user supplied temperature.
35
30
reasoning : {
@@ -65,20 +60,24 @@ export const streamChat = async ({
65
60
} ) => {
66
61
const toolsByName = Object . fromEntries ( tools . map ( ( tool ) => [ tool . name , tool ] ) )
67
62
68
- const chatModel = getChatModel ( model , tools , temperature )
63
+ const modelConfig = validModels . find ( ( m ) => m . name === model )
64
+ if ( ! modelConfig ) {
65
+ throw new Error ( `Invalid model: ${ model } ` )
66
+ }
67
+
68
+ const chatModel = getChatModel ( modelConfig , tools , temperature )
69
69
70
70
const messages : BaseMessageLike [ ] = [
71
- {
72
- role : 'system' ,
73
- content : formatInstructions ,
74
- } ,
71
+ ...( 'instructions' in modelConfig ? [ { role : 'system' , content : modelConfig . instructions } ] : [ ] ) ,
75
72
{
76
73
role : 'system' ,
77
74
content : systemMessage ,
78
75
} ,
79
76
...chatMessages ,
80
77
]
81
78
79
+ console . log ( '📌 messages' , messages )
80
+
82
81
const result = await chatTurn ( chatModel , messages , toolsByName , writeEvent , user )
83
82
84
83
if ( result . toolCalls . length > 0 ) {
0 commit comments