-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce streamMode for useChat / useCompletion. (#1350)
- Loading branch information
Showing
23 changed files
with
879 additions
and
455 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'ai': patch | ||
--- | ||
|
||
Add streamMode parameter to useChat and useCompletion. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,19 @@ | ||
import { OpenAIStream, StreamingTextResponse } from 'ai'; | ||
import OpenAI from 'openai'; | ||
import { openai } from '@ai-sdk/openai'; | ||
import { StreamingTextResponse, experimental_streamText } from 'ai'; | ||
import { APIEvent } from 'solid-start/api'; | ||
|
||
// Create an OpenAI API client | ||
const openai = new OpenAI({ | ||
apiKey: process.env['OPENAI_API_KEY'] || '', | ||
}); | ||
|
||
export const POST = async (event: APIEvent) => { | ||
// Extract the `prompt` from the body of the request | ||
const { messages } = await event.request.json(); | ||
try { | ||
const { messages } = await event.request.json(); | ||
|
||
// Ask OpenAI for a streaming chat completion given the prompt | ||
const response = await openai.chat.completions.create({ | ||
model: 'gpt-3.5-turbo', | ||
stream: true, | ||
messages, | ||
}); | ||
const result = await experimental_streamText({ | ||
model: openai.chat('gpt-4-turbo-preview'), | ||
messages, | ||
}); | ||
|
||
// Convert the response into a friendly text-stream | ||
const stream = OpenAIStream(response); | ||
// Respond with the stream | ||
return new StreamingTextResponse(stream); | ||
return new StreamingTextResponse(result.toAIStream()); | ||
} catch (error) { | ||
console.error(error); | ||
throw error; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.