Skip to content

Commit 74c63b1

Browse files
authoredApr 24, 2024··
ai/core: add toAIStreamResponse() helper to streamText. (#1429)
1 parent 17dc992 commit 74c63b1

File tree

3 files changed

+31
-15
lines changed

3 files changed

+31
-15
lines changed
 

‎.changeset/neat-badgers-scream.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'ai': patch
3+
---
4+
5+
ai/core: add toAIStreamResponse() helper to streamText.
+10-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createOpenAI } from '@ai-sdk/openai';
2-
import { StreamingTextResponse, experimental_streamText } from 'ai';
2+
import { experimental_streamText } from 'ai';
33

44
export const dynamic = 'force-dynamic';
55

@@ -9,20 +9,15 @@ const groq = createOpenAI({
99
});
1010

1111
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();
1514

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+
});
2120

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();
2823
}

‎packages/core/core/generate-text/stream-text.ts

+16
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import {
66
} from '@ai-sdk/provider';
77
import {
88
AIStreamCallbacksAndOptions,
9+
StreamData,
10+
StreamingTextResponse,
911
createCallbacksTransformer,
1012
createStreamDataTransformer,
1113
} from '../../streams';
@@ -274,10 +276,24 @@ writes each text delta as a separate chunk.
274276
read();
275277
}
276278

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+
277291
/**
278292
Creates a simple text stream response.
279293
Each text delta is encoded as UTF-8 and sent as a separate chunk.
280294
Non-text-delta events are ignored.
295+
296+
@param init Optional headers and status code.
281297
*/
282298
toTextStreamResponse(init?: ResponseInit): Response {
283299
const encoder = new TextEncoder();

0 commit comments

Comments
 (0)
Please sign in to comment.