wkok / openai-clojure

Clojure functions to drive the OpenAI API
https://cljdoc.org/d/net.clojars.wkok/openai-clojure
MIT License
208 stars 28 forks source link

support caching via redis ? #5

Closed behrica closed 1 year ago

behrica commented 1 year ago

As the API calls cost money, I found it during "development" of any code calling openai, very, very useful to have a persistent caching of the requests and answers

So I can at least re-run my code often without paying every time. This can be done very cleanly using Redis and a Clojure implementation of "memoize" which persists into Redis.

This is (likely / maybe) not needed for "production" as "in production scenarios" I would not see repeated calls to openai with precisely the same parameters (prompts), at least not for "completion".

I would find it "usefull" to have it in this API, so I don't need to add it on top of this API, every time I use. (and I would add it every time ...)

behrica commented 1 year ago

I "think" we could make this "fully optional", so without even adding a dependency to "https://github.com/ptaoussanis/carmine" (the Redis client) by using (requiring-resolve) and request the user of this library to add dependency to Carmine to classpath. (just to be documented) And using an environement variable to "switch the cache on" (default is off)

This would not need any change in api ns.

I have some code for this ready and i am curious to try it, if you are interested.

behrica commented 1 year ago

I changed mind, as the redis caching is completely independent from this library, so it should be in a generic "redis-memoize" libray , maybe even existing.