A core.async backed interface for leveraging OpenAI's realtime API. Calling connect! will return a core.async channel suitable for the environment. Clojure on the JVM will use a websocket backed transport, while ClojureScript will use a WebRTC backed channel.
A core.async backed interface for leveraging OpenAI's realtime API. Calling connect! will return a core.async channel suitable for the environment. Clojure on the JVM will use a websocket backed transport, while ClojureScript will use a WebRTC backed channel.
(capture-audio! session-ch)(capture-audio! session-ch options)Start capturing audio. Returns a stop function. Options given will be forwarded to a platform specific API. Common options:
Start capturing audio. Returns a stop function. Options given will be forwarded to a platform specific API. Common options: - :timeout-ms - How often to append chunks to the OpenAI input buffer. defaults to 500 ms
(connect!)(connect! {:keys [call-id model xf-out ex-handler xf-in log-fn]
:or {model "gpt-realtime"}
:as params})Open a websocket connection with OpenAI. This channel type is intended for use in server-to-server applications. The stream! function provides a convenient means for taking events while controlling audio playback and the capture-audio! function can be used to add mic support to web socket backed connections. This type is also useful for creating a "sideband" channel that follows a client side call. This is the purpose of the call-id parameter.
Params: :api-key - (optional) A valid api key for OpenAI. Defaults to reading the OPENAI_API_KEY environment variable :model - (optional) The realtime model to use for this websocket session. Deafults to gpt-realtime :call-id - (optional) Overrides the :model parameter. Used for creating a sideband channel :xf-out - (optional) A transducer that will be applied to all outputs. Note: this xf will be applied AFTER filtering and json serialization :ex-handler - (optional) An ex-handler for the output channel. Generally pairs with :xf - follows the same rules as clojure.core.async/chan :xf-in - (optional) A transducer that will be applied to every input value BEFORE json serialization. Should return an event map or throw. No ex-handler is supported for inputs. :log-fn - (optional) A function of form (fn [& xs)). Can be useful for debugging
Open a websocket connection with OpenAI. This channel type is intended for use in server-to-server
applications. The stream! function provides a convenient means for taking events while controlling audio playback
and the capture-audio! function can be used to add mic support to web socket backed connections. This type is also useful
for creating a "sideband" channel that follows a client side call. This is the purpose of the call-id parameter.
Params:
:api-key - (optional) A valid api key for OpenAI. Defaults to reading the OPENAI_API_KEY environment variable
:model - (optional) The realtime model to use for this websocket session. Deafults to gpt-realtime
:call-id - (optional) Overrides the :model parameter. Used for creating a [sideband](https://platform.openai.com/docs/guides/realtime-server-controls) channel
:xf-out - (optional) A transducer that will be applied to all outputs. Note: this xf will be applied AFTER filtering and json serialization
:ex-handler - (optional) An ex-handler for the output channel. Generally pairs with :xf - follows the same rules as clojure.core.async/chan
:xf-in - (optional) A transducer that will be applied to every input value BEFORE json serialization. Should return an event map or throw.
No ex-handler is supported for inputs.
:log-fn - (optional) A function of form (fn [& xs)). Can be useful for debugging(connect! client-secret {:keys [xf-in xf-out] :as params})Open an RTCPeerConnection with OpenAI. This channel type is intended for use in web applications. puts are sent over the peer connection's data channel, and takes are derived from data channel message events. Since an ephemeral token is required for web rtc, this channel type requires a secret generated by making a POST request to https://api.openai.com/v1/realtime/client_secrets - (result is expected to have keywordized keys)
Note: The stream! and capture-audio! functions are not useful in web rtc since audio and mic support are much easier to access in the browser. Simply closing the channel generated by connect! will stop all audio playback and capture.
Note: The :content-types param is unique to the ClojureScript version. It is used to control whhich user media to ask permission for.
Params: :buffer - (optional) buffer size for the channel - defaults to 10 :content-types - (optional) [:vector [:enum "input_text" "input_audio"]] - one or more content types provided by the user - defaults to #{"input_audio" "input_text"} :media-stream-track - (optional) An instance of MediaStreamTrack - this supersedes :content-types. Useful for custom permission scenarios (i.e calling navigator.mediaDevices.getUserMedia() in advance) :xf-out - (optional) A transducer that will be applied to all outputs. Note: this xf will be applied AFTER filtering and json serialization :ex-handler - (optional) An ex-handler for the output channel. Generally pairs with :xf - follows the same rules as clojure.core.async/chan :xf-in - (optional) A transducer that will be applied to every input value BEFORE json serialization. Should return an event map or throw. No ex-handler is supported for inputs.
Open an RTCPeerConnection with OpenAI. This channel type is intended for use in web applications. puts are sent
over the peer connection's data channel, and takes are derived from data channel message events. Since an ephemeral token
is required for web rtc, this channel type requires a secret generated by making a POST request to https://api.openai.com/v1/realtime/client_secrets - (result is expected to have keywordized keys)
Note: The stream! and capture-audio! functions are not useful in web rtc since audio and mic support are much easier to access in the browser. Simply
closing the channel generated by connect! will stop all audio playback and capture.
Note: The :content-types param is unique to the ClojureScript version. It is used to control whhich user media to ask permission for.
Params:
:buffer - (optional) buffer size for the channel - defaults to 10
:content-types - (optional) [:vector [:enum "input_text" "input_audio"]] - one or more content types provided by the user - defaults to #{"input_audio" "input_text"}
:media-stream-track - (optional) An instance of MediaStreamTrack - this supersedes :content-types. Useful for custom permission scenarios (i.e calling navigator.mediaDevices.getUserMedia() in advance)
:xf-out - (optional) A transducer that will be applied to all outputs. Note: this xf will be applied AFTER filtering and json serialization
:ex-handler - (optional) An ex-handler for the output channel. Generally pairs with :xf - follows the same rules as clojure.core.async/chan
:xf-in - (optional) A transducer that will be applied to every input value BEFORE json serialization. Should return an event map or throw.
No ex-handler is supported for inputs.(create-client-secret)(create-client-secret params)Creates a client-secret with its initial session. Returns response body as json map with keywordized keys.
Params: :api-key - (optional) A valid api key for OpenAI. Defaults to reading the OPENAI_API_KEY environment variable :session - (optional) A map matching the reelthyme.schema/Session schema :expires_after - (optional) A map matching the reelthyme.schema/ExpiresAfter schema
Creates a client-secret with its initial session. Returns response body as json map with keywordized keys. Params: :api-key - (optional) A valid api key for OpenAI. Defaults to reading the OPENAI_API_KEY environment variable :session - (optional) A map matching the reelthyme.schema/Session schema :expires_after - (optional) A map matching the reelthyme.schema/ExpiresAfter schema
(event? m)Check if the given map is an event from openai
Check if the given map is an event from openai
(out->json {:keys [text]})Likewise, lets get the server sent events as a Clojure map
Likewise, lets get the server sent events as a Clojure map
(out->json ev)(pong? m)Check if the given event map represents a pong event
Check if the given event map represents a pong event
(stream! session & [buf-or-n xf ex-handler])Returns a [channel stop-audio] tuple. The channel receives all non response.audio.delta events. stop-audio is a function that can be invoked with 0 or 1 arguments and is used to control audio playback. stop-audio accepts an optional map of options that will be passed to the platform specific stop-audio function.
Options for java:
Note: This function aims to be a sensible default for a multimodal session. If greater control is desired, just take from a session channel directly
Returns a [channel stop-audio] tuple. The channel receives all non response.audio.delta events. stop-audio is a function that can be invoked with 0 or 1 arguments and is used to control audio playback. stop-audio accepts an optional map of options that will be passed to the platform specific stop-audio function. Options for java: - :drain? - (bool) If true, will finish all playback before shutting down. Otherwise shuts down immediately. Default is false Note: This function aims to be a sensible default for a multimodal session. If greater control is desired, just take from a session channel directly
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |