All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.jlangch.venice.openai-demo.venice Maven / Gradle / Ivy

The newest version!
;;;;   __    __         _
;;;;   \ \  / /__ _ __ (_) ___ ___
;;;;    \ \/ / _ \ '_ \| |/ __/ _ \
;;;;     \  /  __/ | | | | (_|  __/
;;;;      \/ \___|_| |_|_|\___\___|
;;;;
;;;;
;;;; Copyright 2017-2024 Venice
;;;;
;;;; Licensed under the Apache License, Version 2.0 (the "License");
;;;; you may not use this file except in compliance with the License.
;;;; You may obtain a copy of the License at
;;;;
;;;;     http://www.apache.org/licenses/LICENSE-2.0
;;;;
;;;; Unless required by applicable law or agreed to in writing, software
;;;; distributed under the License is distributed on an "AS IS" BASIS,
;;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;;;; See the License for the specific language governing permissions and
;;;; limitations under the License.

;;;; OpenAI demo data


(ns openai-demo)


(defn demo-weather-function-defs [] 
   ;; Returns a Venice data map with the OpenAI demo function definitions, that corresponds 
   ;; to the OpenAI chat completion request 'tools' JSON data.
   ;; For better readability the map keys are Venice keyword. Strings would equally work.
  [ {
      :type "function"
      :function {
        :name "get_current_weather"
        :description "Get the current weather"
        :parameters {
          :type "object"
          :properties {
            "location" {
              :type "string"
              :description "The city and state, e.g. San Francisco, CA"
            }
            "format" {
              :type "string"
              :enum ["celsius", "fahrenheit"]
              :description "The temperature unit to use. Infer this from the users location."
            }
          }
          :required ["location", "format"]
        }
      }
    },
    {
      :type "function"
      :function {
        :name "get_n_day_weather_forecast"
        :description "Get an N-day weather forecast"
        :parameters {
          :type "object"
          :properties {
            "location" {
              :type "string"
              :description "The city and state, e.g. San Francisco, CA"
            }
            "format" {
              :type "string"
              :enum ["celsius", "fahrenheit"]
              :description "The temperature unit to use. Infer this from the users location.",
            }
            "num_days" {
              :type "integer"
              :description "The number of days to forecast"
            }
          }
          :required ["location", "format", "num_days"]
        }
      }
  } ] )


(defn demo-weather-function-map []
  { "get_current_weather"        get-current-weather
    "get_n_day_weather_forecast" get-n-day-weather-forecast } )


(defn get-current-weather 
  ([named-args] 
    (assert map? named-args)
    ;; argument dispatching
    (get-current-weather (get named-args "location") 
                         (get named-args "format")))

  ([location format]
    (println """
             Calling fn "get-current-weather" with \
             location="~{location}", format="~{format}"
             """)
    (case location
      "Glasgow"             (json/write-str
                              { :location    location
                                :format      format
                                :temperature (temperature 16 format)
                                :general     "sunny" })

      "San Francisco, CA"   (json/write-str
                              { :location    location
                                :format      format
                                :temperature (temperature 12 format)
                                :general     "rainy" })
      (json/write-str { :location location
                        :error    "No weather data available for ~{location}!" }))))


(defn get-n-day-weather-forecast 
  ([named-args]
    (assert map? named-args)
    ;; argument dispatching
    (get-n-day-weather-forecast (get named-args "location")
                                (get named-args "format")
                                (get named-args "n_days")))

  ([location format n-days]
    (println """
             Calling fn "get-current-weather" with \
             location="~{location}", format="~{format}", n-days="~{n-days}"
             """)
    (case location
      "Glasgow"             (json/write-str
                              { :location    location
                                :format      format
                                :n_days      n-days
                                :temperature (temperature 16 format)
                                :general     "mostly sunny" })

      "San Francisco, CA"   (json/write-str
                              { :location    location
                                :format      format
                                :n_days      n-days
                                :temperature (temperature 12 format)
                                :general     "mostly rainy" })
      (json/write-str { :location location
                        :error    "No weather data available for ~{location}!" }))))


(defn- temperature [t format]
  (str/format "%#.1f" (if (fahrenheit? format) (celsius-to-fahrenheit t) (double t))))

(defn- fahrenheit? [format]
  (str/equals-ignore-case? format "fahrenheit"))

(defn- celsius-to-fahrenheit [c]
  (-> (double c) (* 9) (/ 5) (+ 32)))




© 2015 - 2024 Weber Informatics LLC | Privacy Policy