|
| 1 | +// |
| 2 | +// ChatQuery+Extension.swift |
| 3 | +// FunctionCalling-MacPaw-OpenAI |
| 4 | +// |
| 5 | +// Created by 伊藤史 on 2024/09/27. |
| 6 | +// |
| 7 | + |
| 8 | +import FunctionCalling |
| 9 | +import OpenAI |
| 10 | + |
| 11 | +extension ChatQuery.ChatCompletionToolParam { |
| 12 | + init(tool: Tool) { |
| 13 | + self.init(function: .init(tool: tool)) |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +extension ChatQuery.ChatCompletionToolParam.FunctionDefinition { |
| 18 | + init(tool: Tool) { |
| 19 | + self.init( |
| 20 | + name: tool.name, |
| 21 | + description: tool.description, |
| 22 | + parameters: FunctionParameters(inputSchema: tool.inputSchema) |
| 23 | + ) |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +extension ChatQuery.ChatCompletionToolParam.FunctionDefinition.FunctionParameters { |
| 28 | + init(inputSchema: InputSchema) { |
| 29 | + self.init( |
| 30 | + type: JSONType(dataType: inputSchema.type), |
| 31 | + properties: inputSchema.properties?.mapValues { Property(inputSchema: $0) }, |
| 32 | + required: inputSchema.requiredProperties, |
| 33 | + enum: inputSchema.enumValues |
| 34 | + ) |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +extension ChatQuery.ChatCompletionToolParam.FunctionDefinition.FunctionParameters.Property.Items { |
| 39 | + init?(inputSchema: InputSchema?) { |
| 40 | + guard let inputSchema else { |
| 41 | + return nil |
| 42 | + } |
| 43 | + |
| 44 | + self.init( |
| 45 | + type: JSONType(dataType: inputSchema.type), |
| 46 | + properties: inputSchema.properties?.mapValues { |
| 47 | + ChatQuery.ChatCompletionToolParam.FunctionDefinition.FunctionParameters.Property(inputSchema: $0) |
| 48 | + }, |
| 49 | + enum: inputSchema.enumValues |
| 50 | + ) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +extension ChatQuery.ChatCompletionToolParam.FunctionDefinition.FunctionParameters.Property { |
| 55 | + init(inputSchema: InputSchema) { |
| 56 | + self.init( |
| 57 | + type: JSONType(dataType: inputSchema.type), |
| 58 | + description: inputSchema.description, |
| 59 | + format: inputSchema.format, |
| 60 | + items: Items(inputSchema: inputSchema.items), |
| 61 | + required: inputSchema.requiredProperties, |
| 62 | + enum: inputSchema.enumValues |
| 63 | + ) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +extension ChatQuery.ChatCompletionToolParam.FunctionDefinition.FunctionParameters.JSONType { |
| 68 | + init(dataType: InputSchema.DataType) { |
| 69 | + switch dataType { |
| 70 | + case .string: |
| 71 | + self = .string |
| 72 | + case .number: |
| 73 | + self = .number |
| 74 | + case .integer: |
| 75 | + self = .integer |
| 76 | + case .boolean: |
| 77 | + self = .boolean |
| 78 | + case .array: |
| 79 | + self = .array |
| 80 | + case .object: |
| 81 | + self = .object |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments