Closed paulhdk closed 3 months ago
Hi @paulhdk,
yes, it seems the OpenAPI doc hints at a streaming variant, but doesn't actually define it.
You can edit the OpenAPI document (and possibly report it upstream to the maintainers to add it there too) and change the response content type from just:
content:
application/json:
schema:
$ref: "#/components/schemas/CreateChatCompletionResponse"
to something like:
content:
application/json:
schema:
$ref: "#/components/schemas/CreateChatCompletionResponse"
text/event-stream: {}
This will ask Swift OpenAPI Generator to generate a second case for the streaming variant.
You can then use the built-in support for Server-sent Events in Swift OpenAPI Runtime to parse the events generated as the CreateChatCompletionStreamResponse
struct.
An example of how to use streaming is here: https://github.com/apple/swift-openapi-generator/blob/main/Examples/event-streams-client-example/Sources/EventStreamsClient/EventStreamsClient.swift#L43-L52
Thanks for you quick reply, @czechboy0!
Hi @paulhdk,
yes, it seems the OpenAPI doc hints at a streaming variant, but doesn't actually define it.
You can edit the OpenAPI document (and possibly report it upstream to the maintainers to add it there too) and change the response content type from just:
content: application/json: schema: $ref: "#/components/schemas/CreateChatCompletionResponse"
to something like:
content: application/json: schema: $ref: "#/components/schemas/CreateChatCompletionResponse" text/event-stream: {}
The CreateChatCompletionResponse
struct is defined in the spec, so could I add it in here somehow? Maybe instead of text/event-stream: {}
do:
text/event-stream:
schema:
$ref: "#/components/schemas/CreateChatCompletionStreamResponse"
Not sure if this is this the correct way to do this, and not sure if it’s necessary at all.
And yes, once I have something working, I’ll open a PR in OpenAI’s OpenAPI repo.
This will ask Swift OpenAPI Generator to generate a second case for the streaming variant.
You can then use the built-in support for Server-sent Events in Swift OpenAPI Runtime to parse the events generated as the
CreateChatCompletionStreamResponse
struct.
Nice!
An example of how to use streaming is here: https://github.com/apple/swift-openapi-generator/blob/main/Examples/event-streams-client-example/Sources/EventStreamsClient/EventStreamsClient.swift#L43-L52
Yes, I’m aware of that example. Thanks for providing this one as well as the other examples - very helpful for devs getting started with swift-openapi-generator (like myself)!
Yes you can provide the schema for the event type, but today Swift OpenAPI Generator doesn't do anything with it. But it can be helpful for adopters reading the doc, to know which type to decode the stream of JSON payloads in the SSE as.
Yes you can provide the schema for the event type, but today Swift OpenAPI Generator doesn't do anything with it. But it can be helpful for adopters reading the doc, to know which type to decode the stream of JSON payloads in the SSE as.
Brilliant - thanks for your help!
Once I’ve succesfully tested this, I’ll close this issue and open up a PR in OpenAI’s OpenAPI repo.
Question
I’ve been using the swift-openapi-generator package to generate code for invoking the OpenAI OpenAPI, which is defined here on GitHub.
When invoking the
/chat/completions
endpoint viacreateChatCompletion()
, you will receive aCreateChatCompletionResponse
unless you specifiedstream: true
in your request, in that case, you’ll receive severalCreateChatCompletionStreamResponse
objects as the response is being streamed.This made explicit in the
/chat/completion
definition in OpenAI’sopenapi.yml
:AFAICT, the
CreateChatCompletionStreamResponse
is never linked to thecreateChatCompletion()
function in the spec, which is why, I believe, swift-openapi-generator doesn’t have a chance of generating acreateChatCompletion()
implementation that returns streamed responses, and which is why, I’m only seeing acreateChatCompletion()
in my generatedclient.swift
file that returns aChatCompletionResponse
, indepenet of whetherstream
was set or not.Is this reasoning correct? Do you have any pointers, or am I missing something?