Skip to content

Commit

Permalink
Merge pull request #54 from fumito-ito/feature/computer-use
Browse files Browse the repository at this point in the history
Computer Use support
  • Loading branch information
fumito-ito authored Oct 31, 2024
2 parents 1e00305 + 68a7d67 commit b82728e
Show file tree
Hide file tree
Showing 29 changed files with 607 additions and 447 deletions.
20 changes: 0 additions & 20 deletions Example.swiftpm/FunctionTools.swift

This file was deleted.

58 changes: 2 additions & 56 deletions Example.swiftpm/Package.resolved
Original file line number Diff line number Diff line change
@@ -1,66 +1,12 @@
{
"pins" : [
{
"identity" : "documentationcomment",
"kind" : "remoteSourceControl",
"location" : "https://github.com/fumito-ito/DocumentationComment.git",
"state" : {
"revision" : "90d3c5b9cccddb202c8adb99138733c868d7b082",
"version" : "0.0.6"
}
},
{
"identity" : "functioncalling",
"kind" : "remoteSourceControl",
"location" : "https://github.com/fumito-ito/FunctionCalling",
"state" : {
"revision" : "24c07c25d57a66b0e34af8d1ffb1192c2b1313a3",
"version" : "0.4.0"
}
},
{
"identity" : "grmustache.swift",
"kind" : "remoteSourceControl",
"location" : "https://github.com/groue/GRMustache.swift",
"state" : {
"revision" : "c4ee40ef49ae523b06fa470b8d27185ec027330d",
"version" : "5.0.1"
}
},
{
"identity" : "swift-cmark",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-cmark.git",
"state" : {
"revision" : "3bc2f3e25df0cecc5dc269f7ccae65d0f386f06a",
"version" : "0.4.0"
}
},
{
"identity" : "swift-markdown",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-markdown.git",
"state" : {
"revision" : "4aae40bf6fff5286e0e1672329d17824ce16e081",
"version" : "0.4.0"
}
},
{
"identity" : "swift-syntax",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-syntax.git",
"state" : {
"revision" : "0687f71944021d616d34d922343dcef086855920",
"version" : "600.0.1"
}
},
{
"identity" : "swiftyjsonlines",
"kind" : "remoteSourceControl",
"location" : "https://github.com/fumito-ito/SwiftyJSONLines.git",
"state" : {
"revision" : "ae30455420897b2c45621a88b3ae3f052a8b5f3d",
"version" : "0.0.2"
"revision" : "fb957dada1e920b6916c7083e99895f8f7fc885a",
"version" : "0.0.3"
}
}
],
Expand Down
2 changes: 1 addition & 1 deletion Example.swiftpm/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ let package = Package(
name: "Example",
targets: ["AppModule"],
bundleIdentifier: "com.github.fumito-ito.AnthropicSwiftSDK.Example",
teamIdentifier: "K489QY5CFD",
teamIdentifier: "L8LPZ499U7",
displayVersion: "1.0",
bundleVersion: "1",
appIcon: .placeholder(icon: .clock),
Expand Down
5 changes: 2 additions & 3 deletions Example.swiftpm/Protocol/MessagesSubject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import Foundation
import SwiftUI
import AnthropicSwiftSDK
import FunctionCalling

protocol MessagesSubject {
var messages: [ChatMessage] { get }
Expand Down Expand Up @@ -50,7 +49,7 @@ protocol MessageSendable {
temperature: Double?,
topP: Double?,
topK: Int?,
toolContainer: ToolContainer?,
tools: [Tool]?,
toolChoice: ToolChoice
) async throws -> MessagesResponse
}
Expand All @@ -72,7 +71,7 @@ protocol MessageStreamable {
temperature: Double?,
topP: Double?,
topK: Int?,
toolContainer: ToolContainer?,
tools: [Tool]?,
toolChoice: ToolChoice
) async throws -> AsyncThrowingStream<StreamingResponse, Error>
}
5 changes: 2 additions & 3 deletions Example.swiftpm/ViewModel/MockViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import Foundation
import AnthropicSwiftSDK
import FunctionCalling

@Observable class MockViewModel: StreamMessagesSubject, SendMessagesSubject {
required init(messageHandler: any MessageStreamable, title: String, model: AnthropicSwiftSDK.Model) {
Expand Down Expand Up @@ -72,13 +71,13 @@ import FunctionCalling
}

struct MockMessageStreamable: MessageStreamable {
func streamMessage(_ messages: [AnthropicSwiftSDK.Message], model: AnthropicSwiftSDK.Model, system: [AnthropicSwiftSDK.SystemPrompt], maxTokens: Int, metaData: AnthropicSwiftSDK.MetaData?, stopSequence: [String]?, temperature: Double?, topP: Double?, topK: Int?, toolContainer: (any FunctionCalling.ToolContainer)?, toolChoice: AnthropicSwiftSDK.ToolChoice) async throws -> AsyncThrowingStream<any AnthropicSwiftSDK.StreamingResponse, any Error> {
func streamMessage(_ messages: [AnthropicSwiftSDK.Message], model: AnthropicSwiftSDK.Model, system: [AnthropicSwiftSDK.SystemPrompt], maxTokens: Int, metaData: AnthropicSwiftSDK.MetaData?, stopSequence: [String]?, temperature: Double?, topP: Double?, topK: Int?, tools: [Tool]?, toolChoice: AnthropicSwiftSDK.ToolChoice) async throws -> AsyncThrowingStream<any AnthropicSwiftSDK.StreamingResponse, any Error> {
fatalError()
}
}

struct MockMessagesSendable: MessageSendable {
func createMessage(_ messages: [Message], model: Model, system: [SystemPrompt], maxTokens: Int, metaData: MetaData?, stopSequence: [String]?, temperature: Double?, topP: Double?, topK: Int?, toolContainer: (any ToolContainer)?, toolChoice: ToolChoice) async throws -> MessagesResponse {
func createMessage(_ messages: [Message], model: Model, system: [SystemPrompt], maxTokens: Int, metaData: MetaData?, stopSequence: [String]?, temperature: Double?, topP: Double?, topK: Int?, tools: [Tool]?, toolChoice: ToolChoice) async throws -> MessagesResponse {
fatalError()
}
}
1 change: 0 additions & 1 deletion Example.swiftpm/ViewModel/SendBatchViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import AnthropicSwiftSDK
@Observable class SendMessageBatchesViewModel: SendMessageBatchesSubject {

private let messageHandler: MessageBatchSendable
private let functionTools = FunctionTools()
let title: String
let model: Model

Expand Down
3 changes: 1 addition & 2 deletions Example.swiftpm/ViewModel/SendViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import AnthropicSwiftSDK

@Observable class SendViewModel: SendMessagesSubject {
private let messageHandler: MessageSendable
private let functionTools = FunctionTools()
let title: String
let model: Model

Expand Down Expand Up @@ -51,7 +50,7 @@ import AnthropicSwiftSDK
temperature: nil,
topP: nil,
topK: nil,
toolContainer: functionTools,
tools: nil,
toolChoice: .auto
)

Expand Down
3 changes: 1 addition & 2 deletions Example.swiftpm/ViewModel/StreamViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import AnthropicSwiftSDK

@Observable class StreamViewModel: StreamMessagesSubject {
private let messageHandler: MessageStreamable
private let functionTools = FunctionTools()
let title: String
let model: Model

Expand Down Expand Up @@ -51,7 +50,7 @@ import AnthropicSwiftSDK
temperature: nil,
topP: nil,
topK: nil,
toolContainer: functionTools,
tools: nil,
toolChoice: .auto
)
for try await chunk in stream {
Expand Down
54 changes: 0 additions & 54 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -1,59 +1,5 @@
{
"pins" : [
{
"identity" : "documentationcomment",
"kind" : "remoteSourceControl",
"location" : "https://github.com/fumito-ito/DocumentationComment.git",
"state" : {
"revision" : "90d3c5b9cccddb202c8adb99138733c868d7b082",
"version" : "0.0.6"
}
},
{
"identity" : "functioncalling",
"kind" : "remoteSourceControl",
"location" : "https://github.com/fumito-ito/FunctionCalling",
"state" : {
"revision" : "24c07c25d57a66b0e34af8d1ffb1192c2b1313a3",
"version" : "0.4.0"
}
},
{
"identity" : "grmustache.swift",
"kind" : "remoteSourceControl",
"location" : "https://github.com/groue/GRMustache.swift",
"state" : {
"revision" : "c4ee40ef49ae523b06fa470b8d27185ec027330d",
"version" : "5.0.1"
}
},
{
"identity" : "swift-cmark",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-cmark.git",
"state" : {
"revision" : "3ccff77b2dc5b96b77db3da0d68d28068593fa53",
"version" : "0.5.0"
}
},
{
"identity" : "swift-markdown",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-markdown.git",
"state" : {
"revision" : "8f79cb175981458a0a27e76cb42fee8e17b1a993",
"version" : "0.5.0"
}
},
{
"identity" : "swift-syntax",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-syntax.git",
"state" : {
"revision" : "0687f71944021d616d34d922343dcef086855920",
"version" : "600.0.1"
}
},
{
"identity" : "swiftyjsonlines",
"kind" : "remoteSourceControl",
Expand Down
2 changes: 0 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ let package = Package(
targets: ["AnthropicSwiftSDK"]),
],
dependencies: [
.package(url: "https://github.com/fumito-ito/FunctionCalling", from: "0.4.0"),
.package(url: "https://github.com/fumito-ito/SwiftyJSONLines.git", from: "0.0.3")
],
targets: [
Expand All @@ -25,7 +24,6 @@ let package = Package(
.target(
name: "AnthropicSwiftSDK",
dependencies: [
.product(name: "FunctionCalling", package: "FunctionCalling"),
.product(name: "SwiftyJSONLines", package: "SwiftyJSONLines")
]
),
Expand Down
24 changes: 22 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ for try await chunk in stream {

Claude is capable of interacting with external client-side tools and functions, allowing you to equip Claude with your own custom tools to perform a wider variety of tasks.

AnthropicSwiftSDK supports `Tool Use` in conjunction with the [`@FunctionCalling`](https://github.com/fumito-ito/FunctionCalling) macro. You can easily handle `Tool Use` with the following code.
AnthropicSwiftSDK supports `Tool Use` in conjunction with the [`@FunctionCalling`](https://github.com/fumito-ito/FunctionCalling) macro and [FunctionCalling-AnthropicSwiftSDK](https://github.com/FunctionCalling/FunctionCalling-AnthropicSwiftSDK.git) extension. You can easily handle `Tool Use` with the following code.

```swift
@FunctionCalling(service: .claude)
Expand All @@ -102,7 +102,7 @@ let result = try await Anthropic(apiKey: "your_claude_api_key")
.createMessage(
[message],
maxTokens: 1024,
toolContainer: MyFunctionTools() // <= pass tool container here
tools: MyFunctionTools().anthropicSwiftTools // <= pass tool container here
)
```

Expand Down Expand Up @@ -158,6 +158,26 @@ let batch = MessageBatch(
let response = try await anthropic.messageBatches.createBatches(batches: [batch])
```

### [Computer Use (beta)](https://docs.anthropic.com/en/docs/build-with-claude/computer-use#computer-tool)

The upgraded Claude 3.5 Sonnet model is capable of interacting with tools that can manipulate a computer desktop environment.

By implementing the following code, you can instruct Claude to return commands for executing tasks on a computer

```swift
let anthropic = Anthropic(apiKey: "YOUR_OWN_API_KEY")

let message = Message(role: .user, content: [.text("Find flights from San Francisco to a place with warmer weather.")])
let response = try await anthropic.messages.createMessage(
[message],
maxTokens: 1024,
tools: [
.computer(.init(name: "my_computer", displayWidthPx: 1024, displayHeightPx: 768, displayNumber: 1),
.bash(.init(name: "bash"))
]
)
```

## Extensions

By introducing an extension Swift package, it is possible to access the Anthropic Claude API through AWS Bedrock and Vertex AI. The supported services are as follows:
Expand Down
8 changes: 3 additions & 5 deletions Sources/AnthropicSwiftSDK/Entity/Batch/BatchParameter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
// Created by 伊藤史 on 2024/10/18.
//

import FunctionCalling

public struct BatchParameter {
/// Input messages.
public let messages: [Message]
Expand Down Expand Up @@ -38,7 +36,7 @@ public struct BatchParameter {
/// Only sample from the top K options for each subsequent token.
public let topK: Int?
/// Definitions of tools that the model may use.
public let toolContainer: ToolContainer?
public let tools: [Tool]?
/// How the model should use the provided tools. The model can use a specific tool, any available tool, or decide by itself.
public let toolChoice: ToolChoice

Expand All @@ -52,7 +50,7 @@ public struct BatchParameter {
temperature: Double? = nil,
topP: Double? = nil,
topK: Int? = nil,
toolContainer: ToolContainer? = nil,
tools: [Tool]? = nil,
toolChoice: ToolChoice = .auto
) {
self.messages = messages
Expand All @@ -64,7 +62,7 @@ public struct BatchParameter {
self.temperature = temperature
self.topP = topP
self.topK = topK
self.toolContainer = toolContainer
self.tools = tools
self.toolChoice = toolChoice
}
}
4 changes: 2 additions & 2 deletions Sources/AnthropicSwiftSDK/Entity/Content.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import Foundation

/// Type of content block.
enum ContentType: String {
public enum ContentType: String {
/// single string
case text
/// image content
Expand All @@ -33,7 +33,7 @@ public enum Content {
case toolResult(ToolResultContent)

/// The type of content block.
var contentType: ContentType {
public var contentType: ContentType {
switch self {
case .text:
return ContentType.text
Expand Down
Loading

0 comments on commit b82728e

Please sign in to comment.