Skip to content

Commit 922608a

Browse files
committed
add modules
1 parent e0678fe commit 922608a

File tree

5 files changed

+490
-0
lines changed

5 files changed

+490
-0
lines changed

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
.DS_Store
2+
/.build
3+
/Packages
4+
xcuserdata/
5+
DerivedData/
6+
.swiftpm/configuration/registries.json
7+
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
8+
.netrc
9+
110
# Xcode
211
#
312
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore

Package.resolved

Lines changed: 69 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: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// swift-tools-version: 5.10
2+
// The swift-tools-version declares the minimum version of Swift required to build this package.
3+
4+
import PackageDescription
5+
6+
let package = Package(
7+
name: "FunctionCalling-AIProxySwift",
8+
platforms: [
9+
.iOS(.v17),
10+
.macOS(.v13)
11+
],
12+
products: [
13+
// Products define the executables and libraries a package produces, making them visible to other packages.
14+
.library(
15+
name: "FunctionCalling-AIProxySwift",
16+
targets: ["FunctionCalling-AIProxySwift"]),
17+
],
18+
dependencies: [
19+
.package(url: "https://github.com/fumito-ito/FunctionCalling", from: "0.4.0"),
20+
.package(url: "https://github.com/lzell/AIProxySwift.git", from: "0.34.0")
21+
],
22+
targets: [
23+
// Targets are the basic building blocks of a package, defining a module or a test suite.
24+
// Targets can depend on other targets in this package and products from dependencies.
25+
.target(
26+
name: "FunctionCalling-AIProxySwift",
27+
dependencies: [
28+
.product(name: "FunctionCalling", package: "FunctionCalling"),
29+
.product(name: "AIProxy", package: "AIProxySwift")
30+
]),
31+
.testTarget(
32+
name: "FunctionCalling-AIProxySwiftTests",
33+
dependencies: ["FunctionCalling-AIProxySwift"]
34+
),
35+
]
36+
)
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// The Swift Programming Language
2+
// https://docs.swift.org/swift-book
3+
4+
import FunctionCalling
5+
import AIProxy
6+
7+
extension ToolContainer {
8+
// https://github.com/lzell/AIProxySwift?tab=readme-ov-file#how-to-use-openai-structured-outputs-json-schemas-in-a-tool-call
9+
func toOpenAITools(strict: Bool = false) -> [OpenAIChatCompletionTool] {
10+
guard let allTools else { return [] }
11+
12+
return allTools.map { tool in
13+
OpenAIChatCompletionTool.function(
14+
name: tool.name,
15+
description: tool.description,
16+
parameters: tool.inputSchema.toJSONSchema(),
17+
strict: strict
18+
)
19+
}
20+
}
21+
22+
// https://github.com/lzell/AIProxySwift?tab=readme-ov-file#how-to-use-streaming-tool-calls-with-anthropic
23+
func toAnthropicTools() -> [AnthropicTool] {
24+
guard let allTools else { return [] }
25+
26+
return allTools.map { tool in
27+
AnthropicTool(
28+
description: tool.description,
29+
inputSchema: tool.inputSchema.toJSONSchema(),
30+
name: tool.name
31+
)
32+
}
33+
}
34+
35+
// https://github.com/lzell/AIProxySwift?tab=readme-ov-file#how-to-make-a-tool-call-request-with-llama-and-togetherai
36+
func toTogetherAITools() -> [TogetherAITool] {
37+
guard let allTools else { return [] }
38+
39+
return allTools.map { tool in
40+
TogetherAITool(
41+
function: .init(
42+
description: tool.description,
43+
name: tool.name,
44+
parameters: tool.inputSchema.toJSONSchema()
45+
)
46+
)
47+
}
48+
}
49+
}
50+
51+
private extension FunctionCalling.InputSchema {
52+
func toJSONSchema() -> [String: AIProxyJSONValue] {
53+
var jsonSchema: [String: AIProxyJSONValue] = [
54+
"type": .string(self.type.rawValue)
55+
]
56+
57+
if let format {
58+
jsonSchema["format"] = .string(format)
59+
}
60+
61+
if let description {
62+
jsonSchema["description"] = .string(description)
63+
}
64+
65+
if let nullable {
66+
jsonSchema["nullable"] = .bool(nullable)
67+
}
68+
69+
if let enumValues {
70+
jsonSchema["enum"] = .array(enumValues.map { .string($0) })
71+
}
72+
73+
if let items {
74+
jsonSchema["items"] = .object(items.toJSONSchema())
75+
}
76+
77+
if let properties {
78+
jsonSchema["properties"] = .object(properties.mapValues { AIProxyJSONValue.object($0.toJSONSchema()) })
79+
}
80+
81+
if let requiredProperties {
82+
jsonSchema["required"] = .array(requiredProperties.map { .string($0) })
83+
}
84+
85+
return jsonSchema
86+
}
87+
}

0 commit comments

Comments
 (0)