atacan/swift-openai-api
OpenAI API client generated using Swift OpenAPI Generator
Swift OpenAI API
This is a Swift package for the OpenAI public API. It is generated from the official OpenAI OpenAPI specification using Swift OpenAPI Generator.
Why not generate it yourself?
OpenAI's OpenAPI specification has some issues. Some of them are fixed in the download script with string replacements.
For example, duplicate models are removed, type mismatches are fixed.
Additions
- The server-sent-events response type for chat completions is now supported
- The original API document has only 200 status documented as response. We add all the possible error responses with decodable error message payload, so that you can know what the error is.
Usage
AuthenticationMiddlewareis provided to add API key authentication.- Check out Tests
Installation
Add the following to your Package.swift file:
dependencies: [
.package(url: "https://github.com/atacan/swift-openai-api", from: "0.1.0"),
],
targets: [
.target(name: "YourTarget", dependencies: [
.product(name: "OpenAIUrlSessionClient", package: "swift-openai-api"),
// .product(name: "OpenAIAsyncHTTPClient", package: "swift-openai-api"),
]),
]Notes
WebSocket
First incoming message is a transcription_session.created event.
{
"type": "transcription_session.created",
"event_id": "event_BkolR18obdJWg4a3bKuhW",
"session": {
"id": "sess_BkolRXnnaIXV7cV3lu7Bv",
"object": "realtime.transcription_session",
"expires_at": 1750499725,
"input_audio_noise_reduction": null,
"turn_detection": {
"type": "server_vad",
"threshold": 0.5,
"prefix_padding_ms": 300,
"silence_duration_ms": 200
},
"input_audio_format": "pcm16",
"input_audio_transcription": null,
"client_secret": null,
"include": null
}
}RealtimeServerEvent.discriminator
Run the following to find the type value for each RealtimeServerEvent and use them to create the mapping in the overlay.json
yq '.components.schemas | .[] | select(key | test("RealtimeServerEvent.*")) | .properties.type.enum[]' openapi.yamlPackage Metadata
Repository: atacan/swift-openai-api
Stars: 4
Forks: 0
Open issues: 0
Default branch: main
Primary language: swift
License: MIT
README: README.md