generated from softwaremill/sbt-template
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Direct-style streaming using ox (#193)
- Loading branch information
Showing
11 changed files
with
341 additions
and
23 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
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
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
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
48 changes: 48 additions & 0 deletions
48
streaming/ox/src/main/scala/sttp/openai/streaming/ox/oxStreaming.scala
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,48 @@ | ||
package sttp.openai.streaming.ox | ||
|
||
import ox.channels.Source | ||
import ox.{IO, Ox} | ||
import sttp.client4.Request | ||
import sttp.client4.impl.ox.sse.OxServerSentEvents | ||
import sttp.model.sse.ServerSentEvent | ||
import sttp.openai.OpenAI | ||
import sttp.openai.OpenAIExceptions.OpenAIException | ||
import sttp.openai.OpenAIExceptions.OpenAIException.DeserializationOpenAIException | ||
import sttp.openai.json.SttpUpickleApiExtension.deserializeJsonSnake | ||
import sttp.openai.requests.completions.chat.ChatChunkRequestResponseData.ChatChunkResponse | ||
import sttp.openai.requests.completions.chat.ChatChunkRequestResponseData.ChatChunkResponse.DoneEvent | ||
import sttp.openai.requests.completions.chat.ChatRequestBody.ChatBody | ||
|
||
import java.io.InputStream | ||
|
||
extension (client: OpenAI) | ||
/** Creates and streams a model response as chunk objects for the given chat conversation defined in chatBody. | ||
* | ||
* The chunk [[Source]] can be obtained from the response within a concurrency scope (e.g. [[ox.supervised]]), and the [[IO]] capability | ||
* must be provided. The request will complete and the connection close only once the source is fully consumed. | ||
* | ||
* [[https://platform.openai.com/docs/api-reference/chat/create]] | ||
* | ||
* @param chatBody | ||
* Chat request body. | ||
*/ | ||
def createStreamedChatCompletion( | ||
chatBody: ChatBody | ||
): Request[Either[OpenAIException, Ox ?=> IO ?=> Source[Either[DeserializationOpenAIException, ChatChunkResponse]]]] = | ||
client | ||
.createChatCompletionAsInputStream(chatBody) | ||
.mapResponse(mapEventToResponse) | ||
|
||
private def mapEventToResponse( | ||
response: Either[OpenAIException, InputStream] | ||
): Either[OpenAIException, Ox ?=> IO ?=> Source[Either[DeserializationOpenAIException, ChatChunkResponse]]] = | ||
response.map(s => | ||
OxServerSentEvents | ||
.parse(s) | ||
.transform { | ||
_.takeWhile(_ != DoneEvent) | ||
.collect { case ServerSentEvent(Some(data), _, _, _) => | ||
deserializeJsonSnake[ChatChunkResponse].apply(data) | ||
} | ||
} | ||
) |
Oops, something went wrong.