@@ -2,7 +2,7 @@ import { Readable } from 'node:stream';
22import { randomUUID } from 'node:crypto' ;
33import { HttpRequest , InvocationContext , HttpResponseInit , app } from '@azure/functions' ;
44import { AIChatCompletionRequest , AIChatCompletionDelta } from '@microsoft/ai-chat-protocol' ;
5- import { AzureChatOpenAI } from '@langchain/openai' ;
5+ import { ChatOpenAI } from '@langchain/openai' ;
66import { AzureCosmsosDBNoSQLChatMessageHistory } from '@langchain/azure-cosmosdb' ;
77import { createReactAgent } from "@langchain/langgraph/prebuilt" ;
88import { AIMessage , HumanMessage } from '@langchain/core/messages' ;
@@ -40,7 +40,7 @@ Make sure the last question ends with ">>".
4040- When using images in answers, use tables if you are showing multiple images in a list, to make the layout cleaner. Otherwise, try using a single image at the bottom of your answer.
4141` ;
4242
43- const titleSystemPrompt = `Create a title for this chat session, based on the user question. The title should be less than 32 characters. Do NOT use double-quotes.` ;
43+ const titleSystemPrompt = `Create a title for this chat session, based on the user question. The title should be less than 32 characters. Do NOT use double-quotes. The title should be concise, descriptive, and catchy. Respond with only the title, no other text. ` ;
4444
4545export async function postChats ( request : HttpRequest , context : InvocationContext ) : Promise < HttpResponseInit > {
4646 const azureOpenAiEndpoint = process . env . AZURE_OPENAI_API_ENDPOINT ;
@@ -83,14 +83,25 @@ export async function postChats(request: HttpRequest, context: InvocationContext
8383 } ;
8484 }
8585
86- const credentials = getCredentials ( ) ;
87- const azureADTokenProvider = getAzureOpenAiTokenProvider ( ) ;
88-
89- const model = new AzureChatOpenAI ( { azureADTokenProvider, streaming : true } ) ;
86+ const model = new ChatOpenAI ( {
87+ configuration : {
88+ baseURL : azureOpenAiEndpoint ,
89+ async fetch ( url , init = { } ) {
90+ const token = await getAzureOpenAiTokenProvider ( ) ( ) ;
91+ const headers = new Headers ( init . headers ) ;
92+ headers . set ( 'Authorization' , `Bearer ${ token } ` ) ;
93+ return fetch ( url , { ...init , headers } ) ;
94+ } ,
95+ } ,
96+ modelName : process . env . AZURE_OPENAI_MODEL ?? 'gpt-5-mini' ,
97+ streaming : true ,
98+ useResponsesApi : true ,
99+ apiKey : 'not_used'
100+ } ) ;
90101 const chatHistory = new AzureCosmsosDBNoSQLChatMessageHistory ( {
91102 sessionId,
92103 userId,
93- credentials,
104+ credentials : getCredentials ( ) ,
94105 containerName : 'history' ,
95106 databaseName : 'historyDB' ,
96107 } ) ;
@@ -156,8 +167,8 @@ export async function postChats(request: HttpRequest, context: InvocationContext
156167 [ 'system' , titleSystemPrompt ] ,
157168 [ 'human' , question ] ,
158169 ] ) ;
159- context . log ( `Title for session: ${ response . content as string } ` ) ;
160- chatHistory . setContext ( { title : response . content } ) ;
170+ context . log ( `Title for session: ${ response . text } ` ) ;
171+ chatHistory . setContext ( { title : response . text } ) ;
161172 }
162173
163174 return {
@@ -186,16 +197,16 @@ async function* createJsonStream(chunks: AsyncIterable<StreamEvent>, sessionId:
186197 const data = chunk . data ;
187198 let responseChunk : AIChatCompletionDelta | undefined ;
188199
189- if ( chunk . event === 'on_chain_end' && chunk . name === 'RunnableSequence' ) {
200+ if ( chunk . event === 'on_chain_end' && chunk . name === 'RunnableSequence' && data . output ?. content . length > 0 ) {
190201 // End of our agentic chain
191- const content = data ?. output ? .content ?? '' ;
202+ const content = data ?. output . content [ 0 ] . text ?? '' ;
192203 await onComplete ( content ) ;
193204
194205 } else if ( chunk . event === 'on_chat_model_stream' && data . chunk . content . length > 0 ) {
195206 // Streaming response from the LLM
196207 responseChunk = {
197208 delta : {
198- content : data . chunk . content ,
209+ content : data . chunk . content [ 0 ] . text ,
199210 role : 'assistant' ,
200211 } ,
201212 context : {
0 commit comments