forked from copilot-community-sdk/copilot-sdk-clojure
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers_query.clj
More file actions
47 lines (40 loc) · 1.57 KB
/
helpers_query.clj
File metadata and controls
47 lines (40 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
(ns helpers-query
(:require [clojure.core.async :refer [<!! go-loop <!]]
[github.copilot-sdk :as copilot :refer [evt]]
[github.copilot-sdk.helpers :as h]))
;; See examples/README.md for usage
(def defaults
{:prompt "What is the capital of Japan? Answer in one sentence."})
(defn run
[{:keys [prompt] :or {prompt (:prompt defaults)}}]
(println "Query:" prompt)
(println "🤖:" (h/query prompt)))
(defn run-multi
[{:keys [questions] :or {questions ["What is 2+2? Just the number."
"What is the capital of France? Just the city."
"Who wrote Hamlet? Just the name."]}}]
(doseq [q questions]
(println "Q:" q)
(println "A:" (h/query q))
(println)))
;; Define a multimethod for handling events by type
(defmulti handle-event :type)
(defmethod handle-event :default [_] nil)
(defmethod handle-event (evt :assistant.message_delta) [{{:keys [delta-content]} :data}]
(print delta-content)
(flush))
(defmethod handle-event (evt :assistant.message) [_] (println))
(defn run-streaming
[{:keys [prompt] :or {prompt "Explain the concept of immutability in 2-3 sentences."}}]
(println "Query:" prompt)
(println)
(run! handle-event (h/query-seq! prompt :session {:streaming? true})))
(defn run-async
[{:keys [prompt] :or {prompt "Tell me a short joke."}}]
(println "Query:" prompt)
(println)
(let [ch (h/query-chan prompt :session {:streaming? true})]
(<!! (go-loop []
(when-let [event (<! ch)]
(handle-event event)
(recur))))))