kevinhermawan / OllamaKit

Ollama client for Swift
https://kevinhermawan.github.io/OllamaKit/documentation/ollamakit
MIT License
205 stars 15 forks source link

making OKJsonValue Expressible by literals #37

Open NoahKamara opened 1 week ago

NoahKamara commented 1 week ago

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")
            ])
        ])
    ])
]
kevinhermawan commented 5 days ago

Hi @NoahKamara, I've developed a Swift library called JSONSchema that aims to be more idiomatic to Swift. I've already integrated it into my LLMChatOpenAI library and plan to incorporate it into OllamaKit as well.

If you have a moment, I'd appreciate it if you could try out JSONSchema and share your thoughts. I'm always open to pull requests if you'd like to contribute!