Skip to content

Commit

Permalink
feat: adding the initial changes to integrate with llm to mark items
Browse files Browse the repository at this point in the history
  • Loading branch information
gumberss committed Aug 10, 2024
1 parent ce45c08 commit 82be1f6
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 24 deletions.
11 changes: 11 additions & 0 deletions src/purchase_listinator/modules/shopping/adapters/in/llm.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
(ns purchase-listinator.modules.shopping.adapters.in.llm
(:require
[clojure.data.json :as json]
[schema.core :as s]
[purchase-listinator.modules.shopping.schemas.wires.in.llm :as shopping.schemas.wires.in.llm]))

(s/defn wire->model :- [s/Uuid]
[wire :- shopping.schemas.wires.in.llm/InteractionResponse]
(when wire
(-> (:response wire)
(json/read-str))))
19 changes: 19 additions & 0 deletions src/purchase_listinator/modules/shopping/adapters/out/llm.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
(ns purchase-listinator.modules.shopping.adapters.out.llm
(:require
[purchase-listinator.modules.shopping.schemas.models.shopping-list :as models.internal.shopping-list]
[purchase-listinator.modules.shopping.schemas.wires.out.llm :as shopping.schemas.wires.out.llm]
[schema.core :as s]))

(s/defn ^:private get-items-names+id :- [shopping.schemas.wires.out.llm/ProductIdentification]
[{:keys [categories]} :- models.internal.shopping-list/ShoppingList]
(->> (mapcat :items categories)
(map #(select-keys % [:id :name]))))

(s/defn ->wire-mark-shopping :- shopping.schemas.wires.out.llm/InteractionRequest
[request-id :- s/Uuid
shopping :- models.internal.shopping-list/ShoppingList
image :- s/Str]
{:request-id request-id
:prompt-name "mark_shopping_items"
:variables {:products (get-items-names+id shopping)}
:images [image]})
21 changes: 11 additions & 10 deletions src/purchase_listinator/modules/shopping/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,25 @@
:shopping/mongo (component/using (components.mongo/new-mongo :shopping/mongo mongo-indexes) [:config])})

(def system-components-test
{:shopping/http (component/using (components.http/new-http-mock ) [:config])
:shopping/main-db (component/using (components.datahike/new-datahike :shopping/main-db schemas) [:config])
:shopping/rabbitmq (component/using (components.rabbitmq/new-rabbit-mq-fake) rabbitmq-dependencies)
{:shopping/http (component/using (components.http/new-http-mock) [:config])
:shopping/main-db (component/using (components.datahike/new-datahike :shopping/main-db schemas) [:config])
:shopping/rabbitmq (component/using (components.rabbitmq/new-rabbit-mq-fake) rabbitmq-dependencies)
:shopping/rabbitmq-channel (component/using (components.rabbitmq-channel/new-rabbit-mq-channel-fake :shopping/rabbitmq) [:config])
;todo: fake mongo
:shopping/mongo (component/using (components.mongo/new-mongo-fake) [:config])})
:shopping/mongo (component/using (components.mongo/new-mongo-fake) [:config])})

(def request-routes
{:price-suggestion/items (or (System/getenv "PRICE_SUGGESTION_ITEMS_URL") "http://localhost:3000/api/price-suggestion/by/items")
:purchase-list/allowed-lists (or (System/getenv "PURCHASE_LIST_URL") "http://localhost:3000/api/lists/allowed")
:shopping-cart/receive-events (or (System/getenv "SHOPPING_CART_RECEIVE_EVENTS") "http://localhost:3000/api/shopping-cart/events")
:shopping-cart/init-cart (or (System/getenv "SHOPPING_CART_INITIATE_CART") "http://localhost:3000/api/shopping-cart/initiate")
:shopping-cart/cart (or (System/getenv "SHOPPING_CART_GET_CART") "http://localhost:3000/api/shopping-cart/by/:list-id/:shopping-id")
:shopping-cart/exclusive-cart (or (System/getenv "SHOPPING_CART_GET_EXCLUSIVE_CART") "http://localhost:3000/api/shopping-cart/exclusive-by/:list-id/:shopping-id")})
:shopping-cart/exclusive-cart (or (System/getenv "SHOPPING_CART_GET_EXCLUSIVE_CART") "http://localhost:3000/api/shopping-cart/exclusive-by/:list-id/:shopping-id")
:llm-client/interactions (or (System/getenv "LLM_CLIENT_INTERACTION") "http://localhost:3100/api/llm/interactions")})


(def system-config
{:shopping/mongo {:port 27017
{:shopping/mongo {:port 27017
:host (or (System/getenv "MONGODB_HOST") "localhost")
:db-name "purchase-listinator"
:uri (or (System/getenv "MONGODB_URI") nil)}
Expand All @@ -71,10 +72,10 @@
{:shopping/main-db {:store {:backend :mem
:id (str (random-uuid))}}
:shopping/rabbitmq {}
:shopping/mongo {:port 27017
:host (or (System/getenv "MONGODB_HOST") "localhost")
:db-name "purchase-listinator-test"
:uri (or (System/getenv "MONGODB_URI") nil)}})
:shopping/mongo {:port 27017
:host (or (System/getenv "MONGODB_HOST") "localhost")
:db-name "purchase-listinator-test"
:uri (or (System/getenv "MONGODB_URI") nil)}})
(def config
{:rabbitmq-dependencies rabbitmq-dependencies
:webapp-dependencies webapp-dependencies
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
[id :- s/Uuid
allowed-lists-ids :- [s/Uuid]
{:keys [connection]}]
(->> (d/q '[:find (pull ?s [* {:shopping/list [:purchase-list/id]}])
(->> (d/q '[:find (pull ?s [*])
:in $ ?id [?a-l-id ...]
:where
[?s :shopping/id ?id]
Expand Down
24 changes: 22 additions & 2 deletions src/purchase_listinator/modules/shopping/diplomat/http/client.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
[purchase-listinator.components.http :as components.http]
[purchase-listinator.modules.shopping.schemas.models.cart :as models.internal.cart]
[purchase-listinator.modules.shopping.schemas.models.price-suggestion :as models.internal.price-suggestion]
[purchase-listinator.modules.shopping.schemas.models.shopping-list :as models.internal.shopping-list]
[purchase-listinator.modules.shopping.schemas.wires.in.llm :as shopping.schemas.wires.in.llm]
[purchase-listinator.modules.shopping.schemas.wires.in.price-suggestion :as wires.in.price-suggestion]
[purchase-listinator.modules.shopping.adapters.in.price-suggestion :as adapters.in.price-suggestion]
[purchase-listinator.modules.shopping.schemas.wires.in.cart :as wires.in.cart]
[purchase-listinator.modules.shopping.adapters.in.cart :as adapters.in.cart]
[purchase-listinator.modules.shopping.adapters.out.llm :as shopping.adapters.out.llm]
[purchase-listinator.modules.shopping.adapters.in.llm :as shopping.adapters.in.llm]
[schema.core :as s]))

(s/defn get-price-suggestion :- models.internal.price-suggestion/ShoppingItemSuggestedPrices
Expand Down Expand Up @@ -47,7 +51,7 @@
(-> (components.http/request http {:method :get
:url :shopping-cart/cart
:user-id user-id
:query-params {:list-id purchase-list-id
:query-params {:list-id purchase-list-id
:shopping-id shopping-id}
:result-schema wires.in.cart/Cart})
(adapters.in.cart/wire->internal)))
Expand All @@ -60,7 +64,23 @@
(-> (components.http/request http {:method :get
:url :shopping-cart/exclusive-cart
:user-id user-id
:query-params {:list-id purchase-list-id
:query-params {:list-id purchase-list-id
:shopping-id shopping-id}
:result-schema wires.in.cart/Cart})
(adapters.in.cart/wire->internal)))

(s/defn post-interaction :- models.internal.cart/Cart
[request-id :- s/Uuid
shopping :- models.internal.shopping-list/ShoppingList
image :- s/Str
user-id :- s/Uuid
http :- components.http/IHttp]
(try
(-> (components.http/request http {:method :post
:url :llm-client/interactions
:user-id user-id
:body (shopping.adapters.out.llm/->wire-mark-shopping request-id shopping image)
:result-schema shopping.schemas.wires.in.llm/InteractionResponse})
(shopping.adapters.in.llm/wire->model))
(catch Exception e
(println e))))
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,22 @@
(-> (adapters.misc/string->uuid shopping-id)
(flows.shopping/finish (adapters.misc/string->uuid user-id) component)))))

(s/defn mark-shopping-items
[{component :component
{:keys [shopping-id]} :path-params
{:keys [request-id] :as wire} :json-params
user-id :user-id}]
(misc.http/default-branch
(misc.either/try-right
(flows.shopping/mark-items (adapters.misc/string->uuid request-id)
(adapters.misc/string->uuid shopping-id)
(adapters.misc/string->uuid user-id)
(:image wire) component))))

(def routes
#{["/api/shopping/init" :post [init-shopping] :route-name :post-init-shopping]
["/api/shopping/init" :get [get-init-shopping-data] :route-name :get-init-shopping-data]
["/api/shopping/active/:list-id" :get [active-shopping] :route-name :get-active-shopping]
["/api/shopping/list/:shopping-id" :get [get-shopping-list] :route-name :get-in-progress]
["/api/shopping/finish/:shopping-id" :post [finish-shopping] :route-name :finish-shopping]})
["/api/shopping/finish/:shopping-id" :post [finish-shopping] :route-name :finish-shopping]
["/api/shopping/mark-items/:shopping-id" :post [mark-shopping-items] :route-name :mark-shopping-items]})
14 changes: 13 additions & 1 deletion src/purchase_listinator/modules/shopping/flows/shopping.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(ns purchase-listinator.modules.shopping.flows.shopping
(:require
[purchase-listinator.modules.shopping.schemas.models.shopping-list :as models.internal.shopping-list]
[schema.core :as s]
[cats.monad.either :refer [left]]
[clojure.core.async :refer [go <!! <!] :as async]
Expand Down Expand Up @@ -49,7 +50,7 @@
first-near-shopping
(left {:status 404 :data "not-found"}))))

(s/defn cart-module-management
(s/defn cart-module-management :- models.internal.shopping-list/ShoppingList
[list-id :- s/Uuid
user-id :- s/Uuid
shopping-id :- s/Uuid
Expand Down Expand Up @@ -100,3 +101,14 @@
(datomic.shopping/upsert shopping main-db)
(publishers.shopping/shopping-finished shopping rabbitmq-channel)))

(s/defn mark-items
[request-id :- s/Uuid
shopping-id :- s/Uuid
user-id :- s/Uuid
image :- s/Str
{:shopping/keys [http main-db]}]
(let [allowed-lists-ids (http.client.shopping/get-allowed-lists user-id http)
{:keys [list-id]} (datomic.shopping/get-by-id shopping-id allowed-lists-ids main-db)
shopping (cart-module-management list-id user-id shopping-id http)
response (http.client.shopping/post-interaction request-id shopping image user-id http)]
(clojure.pprint/pprint response)))
10 changes: 1 addition & 9 deletions src/purchase_listinator/modules/shopping/logic/shopping.clj
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,4 @@

(s/defn finish :- models.internal.shopping/Shopping
[shopping :- models.internal.shopping/Shopping]
(assoc shopping :status :done))

(s/defn has-price?
[{:keys [price]} :- models.internal.shopping-list/ShoppingItem]
(and price (> price 0)))
(s/defn items-without-prices :- models.internal.shopping-list/ShoppingItem
[{:keys [categories]} :- models.internal.shopping-list/ShoppingList]
(->> (mapcat :items categories)
(filter (complement has-price?))))
(assoc shopping :status :done))
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(ns purchase-listinator.modules.shopping.schemas.wires.in.llm
(:require [schema.core :as s]))

(s/defschema InteractionResponse
{:request-id s/Uuid
:response (s/maybe s/Str)
:status s/Str
:details s/Str})
12 changes: 12 additions & 0 deletions src/purchase_listinator/modules/shopping/schemas/wires/out/llm.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
(ns purchase-listinator.modules.shopping.schemas.wires.out.llm
(:require [schema.core :as s]))

(s/defschema ProductIdentification
{:id s/Uuid
:name s/Str})

(s/defschema InteractionRequest
{:request-id s/Uuid
:prompt-name s/Str
:variables {:products [ProductIdentification]}
:images [s/Str]})

0 comments on commit 82be1f6

Please sign in to comment.