≈ the.bay.news

Streaming chat completions in plain Swift with URLSession.bytes (no SDK)

Swift Forums
Streaming chat completions in plain Swift with URLSession.bytes (no SDK)
Most LLM APIs now speak the same "OpenAI-style" HTTP protocol. You don't need a package for it: URLSession.bytes(for:) plus AsyncLineSequence handles the server-sent-event stream in about 40 lines. Here is the version I keep reusing (macOS 12 / iOS 15+). import Foundation struct ChatRequest: Encodable { struct Message: Encodable { let role: String; let content: String } let model: String let stream: Bool let messages: [Message] } struct ChatChunk: Decodable { struct Choice...

0 comments

Sign in to join the discussion — your thebay.events account works here.

No comments yet.