| 1 | import Foundation |
| 2 | |
| 3 | import LlamaStackClient |
| 4 | |
| 5 | func convertToNativeSwiftType(_ value: Any) -> Any { |
| 6 | switch value { |
| 7 | case let number as NSNumber: |
| 8 | if CFGetTypeID(number) == CFBooleanGetTypeID() { |
| 9 | return number.boolValue |
| 10 | } |
| 11 | if floor(number.doubleValue) == number.doubleValue { |
| 12 | return number.intValue |
| 13 | } |
| 14 | return number.doubleValue |
| 15 | case let string as String: |
| 16 | return string |
| 17 | case let array as [Any]: |
| 18 | return array.map(convertToNativeSwiftType) |
| 19 | case let dict as [String: Any]: |
| 20 | return dict.mapValues(convertToNativeSwiftType) |
| 21 | case is NSNull: |
| 22 | return NSNull() |
| 23 | default: |
| 24 | return value |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | public class SystemDefaultGenerator { |
| 29 | public init() {} |
| 30 | |
| 31 | public func gen() -> PromptTemplate { |
| 32 | let templateStr = """ |
| 33 | Cutting Knowledge Date: December 2023 |
| 34 | Today Date: {{ today }} |
| 35 | """ |
| 36 | |
| 37 | let dateFormatter = DateFormatter() |
| 38 | dateFormatter.dateFormat = "dd MMMM yyyy" |
| 39 | |
| 40 | return PromptTemplate( |
| 41 | template: templateStr, |
| 42 | data: ["today": dateFormatter.string(from: Date())] |
| 43 | ) |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | |
| 48 | public class FunctionTagCustomToolGenerator { |
| 49 | public init() {} |
| 50 | |
| 51 | public func gen(customTools: [Components.Schemas.ToolDefinition]) throws -> PromptTemplate { |
| 52 | // TODO: required params |
| 53 | // TODO: {{#unless @last}},{{/unless}} |
| 54 | |
| 55 | let templateStr = """ |
| 56 | You are an expert in composing functions. You are given a question and a set of possible functions. |
| 57 | Based on the question, you will need to make one or more function/tool calls to achieve the purpose. |
| 58 | If none of the function can be used, point it out. If the given question lacks the parameters required by the function, |
| 59 | also point it out. You should only return the function call in tools call sections. |
| 60 | |
| 61 | If you decide to invoke any of the function(s), you MUST put it in the format of [func_name1(params_name1=params_value1, params_name2=params_value2...), func_name2(params)] |
| 62 | You SHOULD NOT include any other text in the response. |
| 63 | |
| 64 | Here is a list of functions in JSON format that you can invoke. |
| 65 | |
| 66 | [ |
| 67 | {% for t in custom_tools %} |
| 68 | { |
| 69 | "name": "{{t.tool_name}}", |
| 70 | "description": "{{t.description}}", |
| 71 | "parameters": { |
| 72 | "type": "dict", |
| 73 | "properties": { {{t.parameters}} } |
| 74 | } |
| 75 | |
| 76 | {{/let}} |
| 77 | {% endfor -%} |
| 78 | ] |
| 79 | """ |
| 80 | |
| 81 | let encoder = JSONEncoder() |
| 82 | return PromptTemplate( |
| 83 | template: templateStr, |
| 84 | data: ["custom_tools": try customTools.map { |
| 85 | let data = try encoder.encode($0) |
| 86 | let obj = try JSONSerialization.jsonObject(with: data) |
| 87 | return convertToNativeSwiftType(obj) |
| 88 | }] |
| 89 | ) |
| 90 | } |
| 91 | } |