Skip to content

morozRed/InworldAiKit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

InworldAiKit

Swift client library for Inworld APIs.

Features

  • LLM chat completion (/llm/v1alpha/completions:completeChat)
  • Voice read endpoints (/voices/v1/voices, /voices/v1/voices/{id})
  • TTS REST synthesis (/tts/v1/voice)
  • TTS bidirectional WebSocket session support (/tts/v1/voice:streamBidirectional)

Requirements

  • Swift 6.2+
  • Platforms:
    • iOS 15+
    • macOS 12+
    • tvOS 15+
    • watchOS 8+

Installation

Add this package in Xcode with your repository URL, or use Package.swift:

.package(url: "https://github.com/morozRed/InworldAiKit.git", from: "0.1.0")

Then add "InworldAiKit" to your target dependencies.

Authentication

Inworld supports Basic and JWT Bearer authentication.

  • Server-side: Basic auth is supported.
  • Client-side apps: use backend-issued JWT bearer tokens; do not ship long-lived Basic credentials.

Quickstart

import InworldAiKit

let client = InworldClient(
    configuration: InworldConfiguration(
        authProvider: BearerAuthProvider(token: "<JWT>")
    )
)

LLM chat completion

let response = try await client.llm.completeChat(
    ChatCompletionRequest(
        model: "inworld-model",
        messages: [
            ChatMessage(role: .system, content: "You are concise."),
            ChatMessage(role: .user, content: "Give me one sentence about space.")
        ],
        temperature: 0.2
    )
)

print(response.text ?? "")

Voice API

let voices = try await client.voice.listVoices(pageSize: 20)
print(voices.voices.map(\.name))

if let first = voices.voices.first {
    let voice = try await client.voice.getVoice(id: first.id)
    print(voice)
}

TTS REST synthesis

let tts = try await client.tts.synthesizeSpeech(
    TTSSynthesizeSpeechRequest(
        text: "Hello from InworldAiKit",
        voiceID: "voice_123",
        model: "inworld-tts",
        audioConfig: TTSAudioConfig(audioEncoding: "MP3", sampleRateHertz: 44100)
    )
)

let audioData = tts.decodedAudioData

TTS WebSocket streaming

let session = try await client.tts.openWebSocketSession()
try await session.createContext(.init(model: "inworld-tts", voice: "voice_123"), contextId: "ctx1")
try await session.sendText("Streaming synthesis text", contextId: "ctx1", flush: true)

let stream = await session.events()
for try await event in stream {
    switch event {
    case .audioChunk(let chunk):
        _ = chunk.decodedAudioData
    case .status(let status):
        print("status:", status)
    default:
        break
    }
}

Error handling

API and transport errors are normalized into InworldError.

do {
    _ = try await client.voice.listVoices()
} catch let error as InworldError {
    print(error.kind, error.httpStatus ?? -1, error.message)
}

Testing

swift test

Current scope

Included in current implementation:

  • LLM completion
  • Voice list/get
  • TTS REST and streaming session primitives

Not included:

  • STT endpoints

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages