1
- import { AzureKeyCredential , OpenAIClient } from '@azure/openai ' ;
2
- import { OpenAIStream , StreamingTextResponse } from 'ai' ;
1
+ import { createAzure } from '@ai-sdk/azure ' ;
2
+ import { streamText } from 'ai' ;
3
3
4
4
// destructure env vars we need
5
5
const {
6
- AZURE_OPENAI_BASE_PATH ,
6
+ AZURE_OPENAI_DEPLOYMENT_NAME ,
7
7
AZURE_OPENAI_API_KEY ,
8
8
AZURE_OPENAI_MODEL_DEPLOYMENT ,
9
9
AZURE_OPENAI_GPT4_DEPLOYMENT ,
@@ -16,7 +16,7 @@ const {
16
16
// make sure env vars are set
17
17
if (
18
18
! AZURE_OPENAI_API_KEY ||
19
- ! AZURE_OPENAI_BASE_PATH ||
19
+ ! AZURE_OPENAI_DEPLOYMENT_NAME ||
20
20
! AZURE_OPENAI_MODEL_DEPLOYMENT ||
21
21
! AZURE_OPENAI_API_VERSION
22
22
) {
@@ -87,15 +87,14 @@ export async function POST(req: Request) {
87
87
chatMessages = [ systemPrompt , ...messages ] ;
88
88
}
89
89
90
- const openai = new OpenAIClient (
91
- AZURE_OPENAI_BASE_PATH ,
92
- new AzureKeyCredential ( AZURE_OPENAI_API_KEY ) ,
93
- {
94
- apiVersion : AZURE_OPENAI_API_VERSION ,
95
- }
96
- ) ;
90
+ // create azure client
91
+ const azure = createAzure ( {
92
+ resourceName : AZURE_OPENAI_DEPLOYMENT_NAME ,
93
+ apiKey : AZURE_OPENAI_API_KEY ,
94
+ } ) ;
97
95
98
- const response = await openai . streamChatCompletions (
96
+ // instantiate azure openai model
97
+ const openai = azure (
99
98
model === 'gpt-35-turbo' && AZURE_OPENAI_GPT35_DEPLOYMENT
100
99
? AZURE_OPENAI_GPT35_DEPLOYMENT
101
100
: model === 'gpt-4' && AZURE_OPENAI_GPT4_DEPLOYMENT
@@ -107,20 +106,22 @@ export async function POST(req: Request) {
107
106
: model === 'gpt-4o-mini' && AZURE_OPENAI_GPT4O_MINI_DEPLOYMENT
108
107
? AZURE_OPENAI_GPT4O_MINI_DEPLOYMENT
109
108
: AZURE_OPENAI_GPT4O_DEPLOYMENT ,
110
- chatMessages ,
111
109
{
112
- frequencyPenalty : frequency_penalty ,
113
- maxTokens : max_tokens ,
114
- presencePenalty : presence_penalty ,
115
- temperature,
116
- topP : top_p ,
117
110
user,
118
111
}
119
112
) ;
120
113
121
- // convert the response into a friendly text-stream
122
- const stream = OpenAIStream ( response ) ;
114
+ // send the request and store the response
115
+ const response = streamText ( {
116
+ model : openai ,
117
+ messages : chatMessages ,
118
+ temperature,
119
+ topP : top_p ,
120
+ frequencyPenalty : frequency_penalty ,
121
+ presencePenalty : presence_penalty ,
122
+ maxTokens : max_tokens ,
123
+ } ) ;
123
124
124
- // send the stream back to the client
125
- return new StreamingTextResponse ( stream ) ;
125
+ // convert the response into a friendly text- stream and return to client
126
+ return response . toDataStreamResponse ( ) ;
126
127
}
0 commit comments