wkok/openai-clojure

Function call support

Closed this issue · 5 comments

What would it take to support a new function call API?

Replacing https://github.com/openai/openai-openapi/blob/master/openapi.yaml would be the first thing to do right? With only that I get
Value does not match schema: (not (integer? "inf")) when running chat completion. I could have a go at PR with the help of some pointers.

wkok commented

Yes, you'll need to update the yaml file after replacing it by changing the default max_tokens from inf to a valid integer (4096 was the default in earlier versions)
https://github.com/openai/openai-openapi/blob/0c901d9a3537cc446e83b97a265010610e760159/openapi.yaml#L2458

It's mostly a matter of copy/paste one of the existing/similar functions in the api namespace, add the docstring, add a test

Happy to accept a PR if you want to give it a go

bbss commented

I'm hitting

1. Caused by clojure.lang.ExceptionInfo
   Could not coerce value to schema: {:body {:functions [{:parameters
   {:type disallowed-key, :properties disallowed-key, :required
   disallowed-key}}]}}
   {:type :schema-tools.coerce/error,
    :schema
    {:body
     {{:k :function_call} Any,
      {:k :stop} Any,
      {:k :n} (maybe (default Int 1)),
      {:k :presence_penalty} (maybe (default Num 0)),
      {:k :stream} (maybe (default Bool false)),
      {:k :temperature} (maybe (default Num 1)),
      {:k :functions}
      [{:name java.lang.String,
        {:k :description} java.lang.String,
        {:k :parameters} {}}],
      {:k :max_tokens} (default Int 8000),
      :messages
      [{:role (enum "user" "assistant" "function" "system"),
        {:k :content} java.lang.String,
        {:k :name} java.lang.String,
        {:k :function_call}
        {{:k :name} java.lang.String,
         {:k :arguments} java.lang.String}}],
      {:k :frequency_penalty} (maybe (default Num 0)),
      {:k :logit_bias} (maybe {Any Any}),
      {:k :top_p} (maybe (default Num 1)),
      {:k :user} java.lang.String,
      :model java.lang.String}},
    :value
    {:body
     {:model "gpt-4-0613",
      :messages
      [{:role "system",
        :content
        "You are a helpful programming AI, you ask clarification if it's not clear which function should be called. "}
       {:role "user",
        :content "Hey, what's the weather like in Seoul?"}],
      :functions
      [{:name "get_current_weather",
        :description "Get the current weather in a given location",
        :parameters
        {:type "object",
         :properties
         {:location
          {:type "string",
           :description "The city and state, e.g. San Francisco, CA"},
          :unit {:type "string", :enum ["celsius" "fahrenheit"]}},
         :required ["location"]}}],
      :wkok.openai-clojure.core/options nil}},
    :error
    {:body
     {:functions
      [{:parameters
        {:type disallowed-key,
         :properties disallowed-key,
         :required disallowed-key}}]}}}

This is the example from the documentation.

I prefer open maps in schema's because of these kind of errors, or at least just warn instead of erroring.. Can I disable the schema coercion somewhere?

Thanks for the really clean library by the way, have been happily using it.

wkok commented

The branch https://github.com/wkok/openai-clojure/tree/openai-v1.3.0 should now get past this validation error

wkok commented

v0.7.0 released - Added support for OpenAI API v1.3.0 which includes functions

bbss commented

Thanks, confirmed working!