Yet another Anthropic API client for Swift.
Swift Package Manager
Just add to your Package.swift under dependencies
let package = Package(
name: "MyPackage",
products: [...],
targets: [
.target(
"YouAppModule",
dependencies: [
.product(name: "AnthropicSwiftSDK", package: "AnthropicSwiftSDK")
]
)
],
dependencies: [
.package(url: "https://github.com/fumito-ito/AnthropicSwiftSDK.git", .upToNextMajor(from: "0.2.1"))
]
)
let anthropic = Anthropic(apiKey: "YOUR_OWN_API_KEY")
let message = Message(role: .user, content: [.text("This is test text")])
let response = try await anthropic.messages.createMessage([message], maxTokens: 1024)
This library currently supports Messages API. If you need legacy Text Completion API, please make PRs or Issues.
Send a structured list of input messages with text and/or image content, and the model will generate the next message in the conversation.
The Messages API can be used for for either single queries or stateless multi-turn conversations.
let anthropic = Anthropic(apiKey: "YOUR_OWN_API_KEY")
let message = Message(role: .user, content: [.text("This is test text")])
let response = try await anthropic.messages.createMessage([message], maxTokens: 1024)
for content in response.content {
switch content {
case .text(let text):
print(text)
case .image(let imageContent):
// handle base64 encoded image content
}
}
Send a structured list of input messages with text and/or image content, and the model will generate incrementally stream the response using server-sent events (SSE).
let anthropic = Anthropic(apiKey: "YOUR_OWN_API_KEY")
let message = Message(role: .user, content: [.text("This is test text")])
let stream = try await anthropic.messages.streamMessage([message], maxTokens: 1024)
for try await chunk in stream {
switch chunk.type {
case .messageStart:
// handle message start object with casting chunk into `StreamingMessageStartResponse`
}
}
This library provides support for the Anthropic Bedrock API through a separate package.
let package = Package(
name: "MyPackage",
products: [...],
targets: [
.target(
"YouAppModule",
dependencies: [
.product(name: "AnthropicSwiftSDK_Bedrock", package: "AnthropicSwiftSDK")
]
)
],
dependencies: [
.package(url: "https://github.com/fumito-ito/AnthropicSwiftSDK.git", .upToNextMajor(from: "0.2.1"))
]
)
To create an AnthropicBedrockClient
from a BedrockRuntimeClient
with a Model
to access Claude on Bedrock.
The API usage is the same as the normal AnthropicClient.
let client = try BedrockRuntimeClient(region: "USEast1")
let anthropic = BedrockRuntimeClient.useAnthropic(client, model: .claude_3_Haiku)
let response = try await anthropic.messages.createMessage(Message(role: .user, content: [.text("This is test text")]), maxTokens: 1024)
for content in response.content {
switch content {
case .text(let text):
print(text)
case .image(let imageContent):
// handle base64 encoded image content
}
}
Of course, Streaming Message API
works in the same way.
let client = try BedrockRuntimeClient(region: "USEast1")
let anthropic = BedrockRuntimeClient.useAnthropic(client, model: .claude_3_Haiku)
let stream = try await anthropic.messages.streamMessage([Message(role: .user, content: [.text("This is test text")])], maxTokens: 1024)
for try await chunk in stream {
switch chunk.type {
case .messageStart:
// handle message start object with casting chunk into `StreamingMessageStartResponse`
}
}
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.