-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstructured_output.clj
More file actions
46 lines (42 loc) · 1.37 KB
/
Copy pathstructured_output.clj
File metadata and controls
46 lines (42 loc) · 1.37 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
(ns structured-output
"Demonstrates validated structured output with a Clojure parser."
(:require [github.copilot-sdk :as copilot]))
;; See examples/README.md for usage
(def response-schema
{"type" "object"
"properties"
{"answer" {"type" "string"}
"confidence" {"type" "number"}}
"required" ["answer" "confidence"]
"additionalProperties" false})
(defn- parse-response
[value]
(let [answer (get value "answer")
confidence (get value "confidence")]
(when-not (and (map? value)
(string? answer)
(number? confidence))
(throw
(ex-info
"Structured response did not match the expected result"
{:response value})))
{:answer answer
:confidence confidence}))
(defn run
[{:keys [model prompt]
:or {prompt
"What is the capital of France? Return a concise answer and confidence from 0 through 1."
model "gpt-5.4"}}]
(copilot/with-client-session
[session {:on-permission-request copilot/approve-all
:model model
:available-tools []}]
(let [result
(copilot/send-and-wait!
session
{:prompt prompt}
{:to-json-schema (constantly response-schema)
:parse parse-response}
120000)]
(println "Answer:" (:answer result))
(println "Confidence:" (:confidence result)))))