File tree 3 files changed +31
-15
lines changed
examples/next-groq/app/api/chat
packages/core/core/generate-text
3 files changed +31
-15
lines changed Original file line number Diff line number Diff line change
1
+ ---
2
+ ' ai ' : patch
3
+ ---
4
+
5
+ ai/core: add toAIStreamResponse() helper to streamText.
Original file line number Diff line number Diff line change 1
1
import { createOpenAI } from '@ai-sdk/openai' ;
2
- import { StreamingTextResponse , experimental_streamText } from 'ai' ;
2
+ import { experimental_streamText } from 'ai' ;
3
3
4
4
export const dynamic = 'force-dynamic' ;
5
5
@@ -9,20 +9,15 @@ const groq = createOpenAI({
9
9
} ) ;
10
10
11
11
export async function POST ( req : Request ) {
12
- try {
13
- // Extract the `messages` from the body of the request
14
- const { messages } = await req . json ( ) ;
12
+ // Extract the `messages` from the body of the request
13
+ const { messages } = await req . json ( ) ;
15
14
16
- // Call the language model
17
- const result = await experimental_streamText ( {
18
- model : groq . chat ( 'llama2 -70b-4096 ' ) ,
19
- messages,
20
- } ) ;
15
+ // Call the language model
16
+ const result = await experimental_streamText ( {
17
+ model : groq . chat ( 'llama3 -70b-8192 ' ) ,
18
+ messages,
19
+ } ) ;
21
20
22
- // Respond with the stream
23
- return new StreamingTextResponse ( result . toAIStream ( ) ) ;
24
- } catch ( error ) {
25
- console . log ( error ) ;
26
- throw error ;
27
- }
21
+ // Respond with the stream
22
+ return result . toAIStreamResponse ( ) ;
28
23
}
Original file line number Diff line number Diff line change 6
6
} from '@ai-sdk/provider' ;
7
7
import {
8
8
AIStreamCallbacksAndOptions ,
9
+ StreamData ,
10
+ StreamingTextResponse ,
9
11
createCallbacksTransformer ,
10
12
createStreamDataTransformer ,
11
13
} from '../../streams' ;
@@ -274,10 +276,24 @@ writes each text delta as a separate chunk.
274
276
read ( ) ;
275
277
}
276
278
279
+ /**
280
+ Converts the result to a streamed response object with a stream data part stream.
281
+ It can be used with the `useChat` and `useCompletion` hooks.
282
+
283
+ @param init Optional headers.
284
+
285
+ @return A response object.
286
+ */
287
+ toAIStreamResponse ( init ?: ResponseInit ) : Response {
288
+ return new StreamingTextResponse ( this . toAIStream ( ) , init ) ;
289
+ }
290
+
277
291
/**
278
292
Creates a simple text stream response.
279
293
Each text delta is encoded as UTF-8 and sent as a separate chunk.
280
294
Non-text-delta events are ignored.
295
+
296
+ @param init Optional headers and status code.
281
297
*/
282
298
toTextStreamResponse ( init ?: ResponseInit ) : Response {
283
299
const encoder = new TextEncoder ( ) ;
You can’t perform that action at this time.
0 commit comments