Skip to content

Commit

Permalink
ai/core: add toAIStreamResponse() helper to streamText. (#1429)
Browse files Browse the repository at this point in the history
  • Loading branch information
lgrammel committed Apr 24, 2024
1 parent 17dc992 commit 74c63b1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .changeset/neat-badgers-scream.md
@@ -0,0 +1,5 @@
---
'ai': patch
---

ai/core: add toAIStreamResponse() helper to streamText.
25 changes: 10 additions & 15 deletions examples/next-groq/app/api/chat/route.ts
@@ -1,5 +1,5 @@
import { createOpenAI } from '@ai-sdk/openai';
import { StreamingTextResponse, experimental_streamText } from 'ai';
import { experimental_streamText } from 'ai';

export const dynamic = 'force-dynamic';

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

export async function POST(req: Request) {
try {
// Extract the `messages` from the body of the request
const { messages } = await req.json();
// Extract the `messages` from the body of the request
const { messages } = await req.json();

// Call the language model
const result = await experimental_streamText({
model: groq.chat('llama2-70b-4096'),
messages,
});
// Call the language model
const result = await experimental_streamText({
model: groq.chat('llama3-70b-8192'),
messages,
});

// Respond with the stream
return new StreamingTextResponse(result.toAIStream());
} catch (error) {
console.log(error);
throw error;
}
// Respond with the stream
return result.toAIStreamResponse();
}
16 changes: 16 additions & 0 deletions packages/core/core/generate-text/stream-text.ts
Expand Up @@ -6,6 +6,8 @@ import {
} from '@ai-sdk/provider';
import {
AIStreamCallbacksAndOptions,
StreamData,
StreamingTextResponse,
createCallbacksTransformer,
createStreamDataTransformer,
} from '../../streams';
Expand Down Expand Up @@ -274,10 +276,24 @@ writes each text delta as a separate chunk.
read();
}

/**
Converts the result to a streamed response object with a stream data part stream.
It can be used with the `useChat` and `useCompletion` hooks.
@param init Optional headers.
@return A response object.
*/
toAIStreamResponse(init?: ResponseInit): Response {
return new StreamingTextResponse(this.toAIStream(), init);
}

/**
Creates a simple text stream response.
Each text delta is encoded as UTF-8 and sent as a separate chunk.
Non-text-delta events are ignored.
@param init Optional headers and status code.
*/
toTextStreamResponse(init?: ResponseInit): Response {
const encoder = new TextEncoder();
Expand Down

0 comments on commit 74c63b1

Please sign in to comment.