Skip to content
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
6 changes: 3 additions & 3 deletions 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 @@ -8,6 +8,7 @@ extension ToolContainer {
public typealias AIProxyOpenAITool = OpenAIChatCompletionRequestBody.Tool
public typealias AIProxyAnthropicTool = AnthropicTool
public typealias AIProxyTogetherAITool = TogetherAITool
public typealias AIProxyDeepSeekTool = DeepSeekChatCompletionRequestBody.Tool

// swiftlint:disable:next line_length
// https://github.com/lzell/AIProxySwift?tab=readme-ov-file#how-to-use-openai-structured-outputs-json-schemas-in-a-tool-call
Expand Down Expand Up @@ -52,6 +53,20 @@ extension ToolContainer {
)
}
}

// swiftlint:disable:next line_length
// https://github.com/lzell/AIProxySwift/blob/f118afad4941db0ca9c7b6c7f993c483ffd287c6/Sources/AIProxy/DeepSeek/DeepSeekChatCompletionRequestBody.swift#L73-L79
public func toDeepSeekTools() -> [AIProxyDeepSeekTool] {
guard let allTools else { return [] }

return allTools.map { tool in
AIProxyDeepSeekTool.function(
name: tool.name,
description: tool.description,
parameters: tool.inputSchema.toJSONSchema()
)
}
}
}

private extension FunctionCalling.InputSchema {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,97 @@ final class FunctionCallingAIProxySwiftTests: XCTestCase {
}
XCTAssertEqual(enumValues, ["option1", "option2"])
}

func testToDeepSeekTools() throws {
let deepSeekTools = toolContainer.toDeepSeekTools()

XCTAssertEqual(deepSeekTools.count, 1)

let deepSeekTool = try XCTUnwrap(deepSeekTools.first)

guard case .function(let name, let description, let parameters) = deepSeekTool else {
XCTFail("Cannot unwrap function")
return
}

// name
XCTAssertEqual(name, "testTool")
// description
XCTAssertEqual(description, "A test tool")

// inputSchema
guard let parameters else {
XCTFail("Parameters should be a dictionary")
return
}

// inputSchema.type
guard case .string(let type) = parameters["type"] else {
XCTFail("Parameters should be a dictionary")
return
}
XCTAssertEqual(type, "object")

// inputSchema.requiredProperties
guard case .array(let required) = parameters["required"] else {
XCTFail("Parameters should be a dictionary")
return
}

let requiredProperties = required.compactMap { requiredProperty in
switch requiredProperty {
case .string(let propertyName):
return propertyName
default:
return nil
}
}
XCTAssertEqual(requiredProperties, ["testParam"])

// inputSchema.properties
guard case .object(let properties) = parameters["properties"] else {
XCTFail("Parameters should be a dictionary")
return
}
XCTAssertEqual(properties.count, 1)

// inputSchema.properties.testParam
let testParam = try XCTUnwrap(properties["testParam"])

guard case .object(let prop) = testParam else {
XCTFail("Parameters should be a dictionary")
return
}

guard case .string(let type) = prop["type"] else {
XCTFail("Parameters should be a dictionary")
return
}
XCTAssertEqual(type, "string")

guard case .string(let description) = prop["description"] else {
XCTFail("Parameters should be a dictionary")
return
}
XCTAssertEqual(description, "A test parameter")

guard case .array(let enumValueArray) = prop["enum"] else {
XCTFail("Parameters should be a dictionary")
return
}

XCTAssertEqual(enumValueArray.count, 2)

let enumValues = enumValueArray.compactMap { enumValue in
switch enumValue {
case .string(let value):
return value
default:
return nil
}
}
XCTAssertEqual(enumValues, ["option1", "option2"])
}
// swiftlint:enable cyclomatic_complexity
// swiftlint:enable function_body_length
}