Last active
December 17, 2024 06:42
-
-
Save ikappaki/631d24aab511dc8947084653a64885f3 to your computer and use it in GitHub Desktop.
pipecat-ai 01-say-one-thing in Basilisp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (import asyncio aiohttp os sys) | |
| (defmacro import-from | |
| "Helper function to import each name in NAMES from MODULE as a local | |
| variable, allowing them to be referenced without the module name. | |
| https://github.com/basilisp-lang/basilisp/issues/1154." | |
| [module & names] | |
| (let [defs (for [n (vec (map str names))] | |
| `(def ~(symbol n) (importing-resolve (symbol ~(str module "/" n)))))] | |
| `(do ~@defs))) | |
| (import-from pipecat.frames.frames EndFrame TTSSpeakFrame) | |
| (import-from pipecat.pipeline.pipeline Pipeline) | |
| (import-from pipecat.pipeline.task PipelineTask) | |
| (import-from pipecat.pipeline.runner PipelineRunner) | |
| (import-from pipecat.services.cartesia CartesiaTTSService) | |
| (import-from pipecat.transports.services.daily DailyParams, DailyTransport) | |
| (import-from runner configure) | |
| (import-from loguru logger) | |
| (import-from dotenv load-dotenv) | |
| (load-dotenv "./.env" ** :override true) | |
| (.remove logger 0) | |
| (.add logger sys/stderr ** :level "DEBUG") | |
| (defasync main | |
| [] | |
| ;; Basilisp doesn't support `async for`, so raw `aenter/aexit` calls | |
| ;; are used instead. I'll open a new feature request for this. | |
| (let [session (aiohttp/ClientSession)] | |
| (await (.__aenter__ session)) | |
| (try | |
| (let [[room-url] (await (configure session)) | |
| transport (DailyTransport room-url nil "Say One Thing" (DailyParams ** :audio-out-enabled true)) | |
| tts (CartesiaTTSService | |
| ** | |
| :api-key (os/getenv "CARTESIA_API_KEY") | |
| :voice-id "79a125e8-cd45-4c13-8a67-188112f4dd22" ;; British Lady | |
| ) | |
| runner (PipelineRunner) | |
| task (PipelineTask (Pipeline #py [tts, (.output transport)])) | |
| ;; Register an event handler so we can play the audio when the | |
| ;; participant joins. | |
| on-first-participant-joined | |
| ((.event-handler transport "on_first_participant_joined") | |
| (fn ^:async on-first-participant-joined | |
| [transport participant] | |
| (let [participant-name (-> (.get participant "info" #py {}) | |
| (.get "userName" ""))] | |
| (await (.queue-frames task | |
| #py [(TTSSpeakFrame (str "Hello there " participant-name "!")) (EndFrame)])))))] | |
| (await (.run runner task))) | |
| (finally | |
| (await (.__aexit__ session nil nil nil)))))) | |
| (asyncio/run (main)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Adding to this example the
runner.pyconversion used in this file:Mentions:
(defn ^:async ..)instead ofdefasyncbecause I can't get clj-kondo to recognize this macro (help is appreciated)Original