Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

making OKJsonValue Expressible by literals #37

Open
NoahKamara opened this issue Sep 24, 2024 · 1 comment
Open

making OKJsonValue Expressible by literals #37

NoahKamara opened this issue Sep 24, 2024 · 1 comment

Comments

@NoahKamara
Copy link

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
Copy link
Owner

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants