Swift functions that answer questions about text
A SwiftPM package over the C library, for Swift 6. Each call returns its answer and its run facts.
Install
| Channel | Note |
|---|---|
thinkthen-swift-VERSION-TARGET.tar.gz | The package source, from each release. |
thinkthen-c-VERSION-TARGET.tar.gz | The C library it calls, from the same release. |
Set THINKTHEN_API_KEY in the environment.
A first call
// swift-tools-version: 6.0
import PackageDescription
let package = Package(
name: "FirstCall",
dependencies: [.package(path: "thinkthen-swift")],
targets: [
.executableTarget(
name: "FirstCall",
dependencies: [
.product(
name: "ThinkThen",
package: "thinkthen-swift"
),
],
path: ".",
sources: ["main.swift"]
),
]
)import ThinkThen
let tt = try Engine()
let question = "Does the customer ask for a refund?"
var isRefund = try tt.decide(
question,
"Please refund my order. It arrived broken."
)
precondition(isRefund.value.outcome == .yes)
let refund = """
{"decide": "\(question)", "threshold": "0.2:0.8"}
"""
isRefund = try tt.decide(
refund,
"I want to send this back."
)
precondition(isRefund.value.outcome == .unsure)
tt.close()swift build \
-Xlinker -L -Xlinker "$PWD/thinkthen-c/lib" \
-Xlinker -rpath -Xlinker "$PWD/thinkthen-c/lib"
.build/debug/FirstCallNot sure comes back as .unsure. Branch on it and send those cases to a person.
Run facts
decide returns a CallResult. .value holds the answer, and .facts holds this call's run facts as JSON text.
Errors
A failed call throws a DoorFailure. Its kind names usage, backend, deadline, local, cancelled or defect.
Settings and recording
Engine(settingsJSON:) takes the settings as JSON. "record" writes a recording to a folder. "replay" answers from that recording with no connection. The Settings page lists every setting.
Jev, Liquid d1 and Ollama from Swift
Swift reads its backend from the environment. Set THINKTHEN_BACKEND to the name below. Set the backend's key variable in the shell, and the same sample asks that backend.
| Backend | Name | Key variable |
|---|---|---|
| TypeSafe Jev | typesafe | TYPESAFE_API_KEY |
| Liquid d1 | liquid | LIQUIDAI_API_KEY, then LIQUID_API_KEY |
| Ollama | ollama | OLLAMA_API_KEY. A local address needs no key. |
export THINKTHEN_BACKEND=typesafe
export TYPESAFE_API_KEY=...export THINKTHEN_BACKEND=liquid
export LIQUIDAI_API_KEY=...export THINKTHEN_BACKEND=ollama
export THINKTHEN_BASE_URL=http://localhost:11535/v1The Swift project file names its sample, so this sample has its own.
// swift-tools-version: 6.0
import PackageDescription
let package = Package(
name: "Backends",
dependencies: [.package(path: "thinkthen-swift")],
targets: [
.executableTarget(
name: "Backends",
dependencies: [
.product(
name: "ThinkThen",
package: "thinkthen-swift"
),
],
path: ".",
sources: ["backends.swift"]
),
]
)import ThinkThen
@main
struct Backends {
static func main() throws {
let tt = try Engine()
let question = "Does the customer ask for a refund?"
let brokenIsRefund = try tt.decide(
question,
"Please refund my order. It arrived broken."
)
let thanksIsRefund = try tt.decide(
question,
"Thanks for the quick help yesterday!"
)
precondition(brokenIsRefund.value.outcome == .yes)
precondition(thanksIsRefund.value.outcome == .no)
tt.close()
}
}swift build \
-Xlinker -L -Xlinker "$PWD/thinkthen-c/lib" \
-Xlinker -rpath -Xlinker "$PWD/thinkthen-c/lib"
.build/debug/BackendsThe configuration file's backend names a default backend for every run. The Configuration page shows the file.
OpenAI Decisions API from Swift
OpenAI announced its Decisions API on 2026-09-29 in its DevDay 2026 recap. OpenAI has published no address, schema or price, so ThinkThen cannot call it. The OpenAI Decisions API page says what ThinkThen knows.
What is different in Swift
- Add the package with
.package(path:). Its product isThinkThen. - The linker needs the C library's folder, through
-Xlinker -Land-Xlinker -rpath. - Call
close()once, after every call on the engine ends.
On GitHub: github.com/botassembly/thinkthen