Skip to content

Commit

Permalink
Changes to API to generate resolver and take seed on args
Browse files Browse the repository at this point in the history
Signed-off-by: Pratik Karki <[email protected]>
  • Loading branch information
pratik-fractl committed Oct 30, 2023
1 parent d5f7b6b commit f64c8c1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 21 deletions.
44 changes: 23 additions & 21 deletions src/fractl/gpt/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@
[fractl.lang.internal :as li]
[fractl.global-state :as gs]
[fractl.datafmt.json :as json]
[fractl.gpt.seed :as seed]))
[fractl.gpt.seed :as seed]
[fractl.gpt.resolver-seed :as resolver-seed]))

(def ^:private default-conversation seed/full-conversation)
(def ^:private resolver-conversation resolver-seed/conversation)

(defn add-to-conversation
([history role s]
(concat history [{:role role :content s}]))
([msgs]
(concat default-conversation msgs)))
([seed msgs]
(concat seed msgs)))

(defn post [gpt result-handler request]
(http/do-post
Expand Down Expand Up @@ -43,22 +45,22 @@
{:gpt-model gpt-model-name
:api-key api-key})

(defn- interactive-generate-helper [gpt response-handler request]
(let [request (add-to-conversation request)]
(defn- interactive-generate-helper [gpt seed response-handler request]
(let [request (add-to-conversation seed request)]
(post gpt (fn [r]
(when-let [[choice next-request] (response-handler
(choices (:chat-response r)))]
(interactive-generate-helper
gpt response-handler (add-to-conversation
(add-to-conversation request "assistant" choice)
"user" next-request))))
gpt seed response-handler (add-to-conversation
(add-to-conversation request "assistant" choice)
"user" next-request))))
request)))

(defn interactive-generate
([gpt-model-name api-key response-handler request]
(interactive-generate-helper (init-gpt gpt-model-name api-key) response-handler request))
([response-handler request]
(interactive-generate default-model (u/getenv "OPENAI_API_KEY") response-handler request)))
([gpt-model-name api-key seed response-handler request]
(interactive-generate-helper (init-gpt gpt-model-name api-key) seed response-handler request))
([seed response-handler request]
(interactive-generate default-model (u/getenv "OPENAI_API_KEY") seed response-handler request)))

(declare maybe-intern-component)

Expand All @@ -76,9 +78,9 @@
(catch #?(:clj Exception :cljs :default) ex
[nil #?(:clj (.getMessage ex) :cljs ex)])))

(defn non-interactive-generate-helper [gpt response-handler request]
(defn non-interactive-generate-helper [gpt seed response-handler request]
(let [orig-request request
request (add-to-conversation request)]
request (add-to-conversation seed request)]
(post gpt (fn [r]
(let [choices (choices (:chat-response r))
[choice err-msg] (find-choice choices)]
Expand All @@ -89,14 +91,14 @@
request)))

(defn non-interactive-generate
([gpt-model-name api-key response-handler request]
(non-interactive-generate-helper (init-gpt gpt-model-name api-key) response-handler request))
([api-key response-handler request]
([gpt-model-name api-key seed response-handler request]
(non-interactive-generate-helper (init-gpt gpt-model-name api-key) seed response-handler request))
([api-key seed response-handler request]
(if (nil? api-key)
(non-interactive-generate default-model (u/getenv "OPENAI_API_KEY") response-handler request)
(non-interactive-generate default-model api-key response-handler request)))
([response-handler request]
(non-interactive-generate default-model (u/getenv "OPENAI_API_KEY") response-handler request)))
(non-interactive-generate default-model (u/getenv "OPENAI_API_KEY") seed response-handler request)
(non-interactive-generate default-model api-key seed response-handler request)))
([seed response-handler request]
(non-interactive-generate default-model (u/getenv "OPENAI_API_KEY") seed response-handler request)))

(defn- prnf [s]
#?(:clj
Expand Down
2 changes: 2 additions & 0 deletions src/fractl/http.clj
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,7 @@
(POST uh/query-prefix [] (:query handlers))
(POST uh/dynamic-eval-prefix [] (:eval handlers))
(POST uh/gpt-prefix [] (:gpt handlers))
(POST uh/ai-prefix [] (:ai handlers))
(POST uh/auth-callback-prefix [] (:auth-callback handlers))
(GET "/meta/:component" [] (:meta handlers))
(GET "/" [] process-root-get)
Expand Down Expand Up @@ -982,6 +983,7 @@
:query (partial process-query evaluator auth-info)
:eval (partial process-dynamic-eval evaluator auth-info nil)
:gpt (partial process-gpt-chat auth-info)
:ai (partial process-gpt-chat auth-info)
:auth-callback (partial process-auth-callback evaluator config auth-info)
:meta (partial process-meta-request auth-info)})
(if (:thread config)
Expand Down
1 change: 1 addition & 0 deletions src/fractl/util/http.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
(def dynamic-eval-prefix "/_dynamic/")
(def auth-callback-prefix "/_authcallback")
(def gpt-prefix "/_gpt/")
(def ai-prefix "/_ai/")

(defn- remote-resolver-error [response]
(u/throw-ex (str "remote resolver error - " (or (:error response) response))))
Expand Down

0 comments on commit f64c8c1

Please sign in to comment.