Skip to content

Support Tool Use #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions .swiftpm/configuration/Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

95 changes: 95 additions & 0 deletions Example.swiftpm/.swiftpm/configuration/Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@
value = ""
isEnabled = "YES">
</EnvironmentVariable>
<EnvironmentVariable
key = "IDEPreferLogStreaming"
value = "YES"
isEnabled = "YES">
</EnvironmentVariable>
</EnvironmentVariables>
</LaunchAction>
<ProfileAction
Expand Down
20 changes: 20 additions & 0 deletions Example.swiftpm/FunctionTools.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// FunctionTools.swift
// Example
//
// Created by 伊藤史 on 2024/07/23.
//

import Foundation
import FunctionCalling

@FunctionCalling(service: .claude)
struct FunctionTools {
/// Returns the temperature at the location specified by the argument, together with the units.
/// - Parameter location: location to specify the temperature
/// - Returns: the temperature at the location
@CallableFunction
func getWeather(location: String) -> String {
return "32 Celsius"
}
}
54 changes: 54 additions & 0 deletions Example.swiftpm/Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions Example.swiftpm/Protocol/MessagesSubject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import Foundation
import SwiftUI
import AnthropicSwiftSDK
import FunctionCalling

protocol MessagesSubject {
var messages: [ChatMessage] { get }
Expand Down Expand Up @@ -42,7 +43,9 @@ protocol MessageSendable {
stopSequence: [String]?,
temperature: Double?,
topP: Double?,
topK: Int?
topK: Int?,
toolContainer: ToolContainer?,
toolChoice: ToolChoice
) async throws -> MessagesResponse
}

Expand All @@ -56,6 +59,8 @@ protocol MessageStreamable {
stopSequence: [String]?,
temperature: Double?,
topP: Double?,
topK: Int?
topK: Int?,
toolContainer: ToolContainer?,
toolChoice: ToolChoice
) async throws -> AsyncThrowingStream<StreamingResponse, Error>
}
5 changes: 3 additions & 2 deletions Example.swiftpm/ViewModel/MockViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

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 @@ -71,13 +72,13 @@ import AnthropicSwiftSDK
}

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

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

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

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

if case let .text(reply) = result.content.first {
Expand Down
9 changes: 6 additions & 3 deletions Example.swiftpm/ViewModel/StreamViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import AnthropicSwiftSDK

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

Expand Down Expand Up @@ -40,7 +41,7 @@ import AnthropicSwiftSDK
task = Task {
do {
isLoading = true
let stream = try await messageHandler.streamMessage(
let stream = try await self.messageHandler.streamMessage(
[message],
model: model,
system: nil,
Expand All @@ -49,13 +50,15 @@ import AnthropicSwiftSDK
stopSequence: nil,
temperature: nil,
topP: nil,
topK: nil
topK: nil,
toolContainer: functionTools,
toolChoice: .auto
)
for try await chunk in stream {
switch chunk.type {
case .contentBlockDelta:
if let response = chunk as? StreamingContentBlockDeltaResponse {
messages.append(.init(user: .assistant, text: response.delta.text))
messages.append(.init(user: .assistant, text: response.delta.text ?? ""))
}
default:
break
Expand Down
Loading
Loading