Skip to content

Commit

Permalink
Define core util and usage
Browse files Browse the repository at this point in the history
  • Loading branch information
megh-khaire committed Mar 30, 2024
1 parent 0e84f3d commit 9cb57e6
Showing 1 changed file with 60 additions and 5 deletions.
65 changes: 60 additions & 5 deletions src/semantic_router_clj/core.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,62 @@
(ns semantic-router-clj.core
(:gen-class))
(:gen-class)
(:require [cheshire.core :as cc]
[semantic-router-clj.layer :as srcl]
[semantic-router-clj.openai :as srco]
[semantic-router-clj.route :as srcr]))

(defn -main
"I don't do a whole lot ... yet."
[& args]
(println "Hello, World!"))

(defn router
[layer-ns query]
{:pre [(keyword? layer-ns)]}
(let [layer (srcl/get-layer layer-ns)
embedding (-> {:input query}
(assoc :model (:model layer))
(cc/generate-string true)
srco/generate-embeddings
first)]
(srcr/get-top-route layer embedding)))


;; Usage example
(comment
;; Set API key
(srco/set-api-key "<api-key>")

;; Create a new layer
(srcl/layer :test
:threshold 0.5
:model "text-embedding-3-small"
:aggregation-method :mean)

(srcl/get-layer :test)

;; Define routes for the layer
(srcr/add-route :test
{:name :chitchat
:utterances ["how's the weather today?",
"how are things going?",
"lovely weather today",
"the weather is horrendous",
"let's go to the chippy"]})

(srcr/get-route :test :chitchat)

(srcr/add-route :test
{:name :politics
:utterances ["isn't politics the best thing ever",
"why don't you tell me about your political opinions",
"don't you just love the president",
"they're going to destroy this country!",
"they will save the country!"]})

;; Query the layer
(router :test "How are you?")

(router :test "I'm interested in learning about llama 2")

(srcl/update-layer :test
:threshold 0.5
:aggregation-method :mean)

(router :test "don't you love politics?"))

0 comments on commit 9cb57e6

Please sign in to comment.