Skip to content

Commit 913dd8f

Browse files
committed
add modules
1 parent 29e124c commit 913dd8f

8 files changed

Lines changed: 212 additions & 31 deletions

File tree

Package.resolved

Lines changed: 96 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ let package = Package(
1616
targets: ["FunctionCalling-AWSBedrock"]),
1717
],
1818
dependencies: [
19-
.package(url: "https://github.com/fumito-ito/FunctionCalling", from: "0.3.0"),
19+
.package(url: "https://github.com/FunctionCalling/FunctionCalling", from: "0.3.0"),
2020
.package(url: "https://github.com/awslabs/aws-sdk-swift", from: "0.77.1"),
2121
],
2222
targets: [
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//
2+
// Smithy_Document+Extension.swift
3+
//
4+
//
5+
// Created by 伊藤史 on 2024/09/18.
6+
//
7+
8+
import FunctionCalling
9+
import Foundation
10+
import struct Smithy.Document
11+
12+
extension Smithy.Document {
13+
init(from schema: FunctionCalling.InputSchema) throws {
14+
let data = try JSONEncoder().encode(schema)
15+
self = try Self.make(from: data)
16+
}
17+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//
2+
// ToolInputSchema+Extension.swift
3+
//
4+
//
5+
// Created by 伊藤史 on 2024/09/18.
6+
//
7+
8+
import FunctionCalling
9+
import AWSBedrockRuntime
10+
11+
extension BedrockRuntimeClientTypes.ToolInputSchema {
12+
init(from schema: FunctionCalling.InputSchema) throws {
13+
self = .json(try .init(from: schema))
14+
}
15+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//
2+
// ToolSpecification+Extension.swift
3+
//
4+
//
5+
// Created by 伊藤史 on 2024/09/18.
6+
//
7+
8+
import FunctionCalling
9+
import AWSBedrockRuntime
10+
11+
extension BedrockRuntimeClientTypes.ToolSpecification {
12+
init(from tool: FunctionCalling.Tool) throws {
13+
self.init(
14+
description: tool.description,
15+
inputSchema: try .init(from: tool.inputSchema),
16+
name: tool.name
17+
)
18+
}
19+
}

Sources/FunctionCalling-AWSBedrock/FunctionCalling-AWSBedrock.swift

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,21 @@
44
import FunctionCalling
55
import AWSBedrockRuntime
66
import Foundation
7-
import struct Smithy.Document
7+
8+
let decoder: JSONDecoder = {
9+
let decoder = JSONDecoder()
10+
decoder.keyDecodingStrategy = .convertFromSnakeCase
11+
12+
return decoder
13+
}()
814

915
extension ToolContainer {
1016
typealias FunctionCallingTool = FunctionCalling.Tool
11-
17+
1218
public var bedrockAllTools: [BedrockRuntimeClientTypes.Tool] {
1319
get throws {
14-
let data = allTools.data(using: .utf8)!
15-
let functionCallingTools = try JSONDecoder().decode([FunctionCallingTool].self, from: data)
20+
let data = allTools.replacingOccurrences(of: "\n", with: "").data(using: .utf8)!
21+
let functionCallingTools = try decoder.decode([FunctionCallingTool].self, from: data)
1622
return try functionCallingTools.map { try $0.toBedrockTool }
1723
}
1824
}
@@ -25,26 +31,3 @@ extension FunctionCalling.Tool {
2531
}
2632
}
2733
}
28-
29-
extension BedrockRuntimeClientTypes.ToolSpecification {
30-
init(from tool: FunctionCalling.Tool) throws {
31-
self.init(
32-
description: tool.description,
33-
inputSchema: try .init(from: tool.inputSchema),
34-
name: tool.name
35-
)
36-
}
37-
}
38-
39-
extension BedrockRuntimeClientTypes.ToolInputSchema {
40-
init(from schema: FunctionCalling.InputSchema) throws {
41-
self = .json(try .init(from: schema))
42-
}
43-
}
44-
45-
extension Smithy.Document {
46-
init(from schema: FunctionCalling.InputSchema) throws {
47-
let data = try JSONEncoder().encode(schema)
48-
self = try Self.make(from: data)
49-
}
50-
}
Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,38 @@
11
import Testing
2-
@testable import MyLibrary
2+
@testable import FunctionCalling_AWSBedrock
3+
import FunctionCalling
34

4-
@Test func example() async throws {
5-
// Write your test here and use APIs like `#expect(...)` to check expected conditions.
5+
actor BedrockTests {
6+
@FunctionCalling(service: .claude)
7+
struct FunctionContainer {
8+
/// Return current weather of location that passed by the argument
9+
/// - Parameter location: location that
10+
/// - Returns: string of weather
11+
@CallableFunction
12+
func getWeather(location: String) -> String {
13+
return "Sunny"
14+
}
15+
16+
@CallableFunction
17+
func getStock(args: String) -> Int {
18+
return 0
19+
}
20+
}
21+
22+
@Test func checkConvertedResults() async throws {
23+
let functions = try FunctionContainer().bedrockAllTools
24+
25+
#expect(functions.count == 2)
26+
27+
let getWeather = try #require(functions.first?.toolSpecification)
28+
#expect(getWeather.name == "getWeather")
29+
#expect(getWeather.description == """
30+
Return current weather of location that passed by the argument- Parameter location: location that- Returns: string of weather
31+
""")
32+
33+
let getStock = try #require(functions[1].toolSpecification)
34+
#expect(getStock.name == "getStock")
35+
#expect(getStock.description == "")
36+
}
637
}
38+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//
2+
// BedrockRuntimeClientTypes+Extension.swift
3+
// FunctionCalling-AWSBedrock
4+
//
5+
// Created by 伊藤史 on 2024/09/19.
6+
//
7+
8+
import AWSBedrockRuntime
9+
10+
extension BedrockRuntimeClientTypes.Tool {
11+
var toolSpecification: BedrockRuntimeClientTypes.ToolSpecification? {
12+
switch self {
13+
case .toolspec(let specification):
14+
return specification
15+
case .sdkUnknown:
16+
return nil
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)