Skip to content

making OKJsonValue Expressible by literals #37

@NoahKamara

Description

@NoahKamara

Im using an extension in my projects to make working with OKJSONValue easier.
Would you be open to a PR adding this to OllamaKit?

extension OKJSONValue: @retroactive ExpressibleByExtendedGraphemeClusterLiteral {}
extension OKJSONValue: @retroactive ExpressibleByUnicodeScalarLiteral {}
extension OKJSONValue: @retroactive ExpressibleByStringLiteral, @retroactive ExpressibleByIntegerLiteral, @retroactive ExpressibleByFloatLiteral, @retroactive ExpressibleByArrayLiteral, @retroactive ExpressibleByDictionaryLiteral {
    public init(stringLiteral value: String) {
        self = .string(value)
    }

    public init(integerLiteral value: Int) {
        self = .number(Double(value))
    }

    public init(floatLiteral value: Double) {
        self = .number(value)
    }

    public init(arrayLiteral elements: OKJSONValue...) {
        self = .array(elements)
    }

    public init(dictionaryLiteral elements: (String, OKJSONValue)...) {
        self = .object(.init(uniqueKeysWithValues: elements))
    }
}

This makes tool definitions easier to read:

let tools: [OKJSONValue] = [
    [
        "name": "get_current_weather",
        "description": "Get the current weather for a location",
        "parameters": [
            "type": "object",
            "properties": [
                "location": [
                    "type": "string",
                    "description": "The location to get the weather for, e.g. San Francisco, CA"
                ],
                "format": [
                    "type": "string",
                    "description": "The format to return the weather in, e.g. 'celsius' or 'fahrenheit'",
                    "enum": ["celsius", "fahrenheit"]
                ]
            ],
            "required": ["location", "format"]
        ]
    ]
]

Compared to the current implementation:

let tools_old: [OKJSONValue] = [
    .object([
        "name": .string("get_current_weather"),
        "description": .string("Get the current weather for a location"),
        "parameters": .object([
            "type": .string("object"),
            "properties": [
                "location": .object([
                    "type": .string("string"),
                    "description": "The location to get the weather for, e.g. San Francisco, CA"
                ]),
                "format": .object([
                    "type": .string("string"),
                    "description": .string("The format to return the weather in, e.g. 'celsius' or 'fahrenheit'"),
                    "enum": .array([
                        .string("celsius"),
                        .string("fahrenheit")
                    ])
                ])
            ],
            "required": .array([
                .string("location"),
                .string("format")
            ])
        ])
    ])
]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions