From 5c5774577805acae61f375e8263c3f978b68a5f6 Mon Sep 17 00:00:00 2001 From: Nicolas Molina Date: Sun, 27 Oct 2024 03:19:11 -0400 Subject: [PATCH] save --- apps/api/package.json | 1 + apps/api/src/chatbot/graph.ts | 6 +- apps/api/src/chatbot/index.ts | 2 +- apps/api/yarn.lock | 157 +++++++++++++++++++++++++++++++++- 4 files changed, 161 insertions(+), 5 deletions(-) diff --git a/apps/api/package.json b/apps/api/package.json index fec8132..9cbf4be 100644 --- a/apps/api/package.json +++ b/apps/api/package.json @@ -14,6 +14,7 @@ "@langchain/langgraph-checkpoint-sqlite": "^0.1.2", "@langchain/mistralai": "^0.1.1", "@langchain/openai": "^0.3.11", + "@libsql/client": "^0.14.0", "hono": "^4.6.7", "zod": "^3.23.8" }, diff --git a/apps/api/src/chatbot/graph.ts b/apps/api/src/chatbot/graph.ts index 2a0614f..c224cdd 100644 --- a/apps/api/src/chatbot/graph.ts +++ b/apps/api/src/chatbot/graph.ts @@ -1,5 +1,7 @@ import { END, START, StateGraph } from "@langchain/langgraph"; import { PostgresSaver } from "@langchain/langgraph-checkpoint-postgres"; +import { SqliteSaver } from "@langchain/langgraph-checkpoint-sqlite"; +import { Client as LibsqlClient, createClient } from "@libsql/client/web"; import { GraphState } from "./graph.state"; @@ -21,9 +23,9 @@ interface Props { databaseUrl: string; } -export const createGraph = (data: Props) => { - console.log("Creating graph with data", data.databaseUrl); +export const createGraph = async (data: Props) => { const checkpointer = PostgresSaver.fromConnString(data.databaseUrl); + await checkpointer.setup(); const llmGpt4 = models.gpt4(data.openAIKey); const llmMistral = models.mistral(data.mistralKey); diff --git a/apps/api/src/chatbot/index.ts b/apps/api/src/chatbot/index.ts index adb99ae..faaec3a 100644 --- a/apps/api/src/chatbot/index.ts +++ b/apps/api/src/chatbot/index.ts @@ -39,7 +39,7 @@ app.post("/webhook", async (c) => { headers.append("Authorization", `Bearer ${fbToken}`); headers.append("Content-Type", "application/json"); - const agent = createGraph({ openAIKey, mistralKey, databaseUrl }); + const agent = await createGraph({ openAIKey, mistralKey, databaseUrl }); const userMessage = message.text.body; const threadId = `thread_${message.from}`; const config = { configurable: { thread_id: threadId } }; diff --git a/apps/api/yarn.lock b/apps/api/yarn.lock index f97c0c5..4f6e3e0 100644 --- a/apps/api/yarn.lock +++ b/apps/api/yarn.lock @@ -322,6 +322,82 @@ zod "^3.22.4" zod-to-json-schema "^3.22.3" +"@libsql/client@^0.14.0": + version "0.14.0" + resolved "https://registry.npmjs.org/@libsql/client/-/client-0.14.0.tgz#df30cfb21e84797398afdd268e153cf1594c68a1" + integrity sha512-/9HEKfn6fwXB5aTEEoMeFh4CtG0ZzbncBb1e++OCdVpgKZ/xyMsIVYXm0w7Pv4RUel803vE6LwniB3PqD72R0Q== + dependencies: + "@libsql/core" "^0.14.0" + "@libsql/hrana-client" "^0.7.0" + js-base64 "^3.7.5" + libsql "^0.4.4" + promise-limit "^2.7.0" + +"@libsql/core@^0.14.0": + version "0.14.0" + resolved "https://registry.npmjs.org/@libsql/core/-/core-0.14.0.tgz#bbdbdf89227f1325b79e49d00babace81a47bf23" + integrity sha512-nhbuXf7GP3PSZgdCY2Ecj8vz187ptHlZQ0VRc751oB2C1W8jQUXKKklvt7t1LJiUTQBVJuadF628eUk+3cRi4Q== + dependencies: + js-base64 "^3.7.5" + +"@libsql/darwin-arm64@0.4.6": + version "0.4.6" + resolved "https://registry.npmjs.org/@libsql/darwin-arm64/-/darwin-arm64-0.4.6.tgz#7d62de8683c51716180c43149a7cd73e2e359017" + integrity sha512-45i604CJ2Lubbg7NqtDodjarF6VgST8rS5R8xB++MoRqixtDns9PZ6tocT9pRJDWuTWEiy2sjthPOFWMKwYAsg== + +"@libsql/darwin-x64@0.4.6": + version "0.4.6" + resolved "https://registry.npmjs.org/@libsql/darwin-x64/-/darwin-x64-0.4.6.tgz#e6798a53ddff027debe0ef18d85c112669cb5b5c" + integrity sha512-dRKliflhfr5zOPSNgNJ6C2nZDd4YA8bTXF3MUNqNkcxQ8BffaH9uUwL9kMq89LkFIZQHcyP75bBgZctxfJ/H5Q== + +"@libsql/hrana-client@^0.7.0": + version "0.7.0" + resolved "https://registry.npmjs.org/@libsql/hrana-client/-/hrana-client-0.7.0.tgz#c059d8106b9d40dd931217333710aff2ceb5216e" + integrity sha512-OF8fFQSkbL7vJY9rfuegK1R7sPgQ6kFMkDamiEccNUvieQ+3urzfDFI616oPl8V7T9zRmnTkSjMOImYCAVRVuw== + dependencies: + "@libsql/isomorphic-fetch" "^0.3.1" + "@libsql/isomorphic-ws" "^0.1.5" + js-base64 "^3.7.5" + node-fetch "^3.3.2" + +"@libsql/isomorphic-fetch@^0.3.1": + version "0.3.1" + resolved "https://registry.npmjs.org/@libsql/isomorphic-fetch/-/isomorphic-fetch-0.3.1.tgz#42023816d5645a5a3f3a78bb3899bdc5814c7b88" + integrity sha512-6kK3SUK5Uu56zPq/Las620n5aS9xJq+jMBcNSOmjhNf/MUvdyji4vrMTqD7ptY7/4/CAVEAYDeotUz60LNQHtw== + +"@libsql/isomorphic-ws@^0.1.5": + version "0.1.5" + resolved "https://registry.npmjs.org/@libsql/isomorphic-ws/-/isomorphic-ws-0.1.5.tgz#e2d1faf965ba0f3be9301fbf5640164d03c4e606" + integrity sha512-DtLWIH29onUYR00i0GlQ3UdcTRC6EP4u9w/h9LxpUZJWRMARk6dQwZ6Jkd+QdwVpuAOrdxt18v0K2uIYR3fwFg== + dependencies: + "@types/ws" "^8.5.4" + ws "^8.13.0" + +"@libsql/linux-arm64-gnu@0.4.6": + version "0.4.6" + resolved "https://registry.npmjs.org/@libsql/linux-arm64-gnu/-/linux-arm64-gnu-0.4.6.tgz#98ab9919de63cbe0373ff7fa472bdb2cdd86d03e" + integrity sha512-DMPavVyY6vYPAYcQR1iOotHszg+5xSjHSg6F9kNecPX0KKdGq84zuPJmORfKOPtaWvzPewNFdML/e+s1fu09XQ== + +"@libsql/linux-arm64-musl@0.4.6": + version "0.4.6" + resolved "https://registry.npmjs.org/@libsql/linux-arm64-musl/-/linux-arm64-musl-0.4.6.tgz#6964b81174da83d303df84893e964125d9df155d" + integrity sha512-whuHSYAZyclGjM3L0mKGXyWqdAy7qYvPPn+J1ve7FtGkFlM0DiIPjA5K30aWSGJSRh72sD9DBZfnu8CpfSjT6w== + +"@libsql/linux-x64-gnu@0.4.6": + version "0.4.6" + resolved "https://registry.npmjs.org/@libsql/linux-x64-gnu/-/linux-x64-gnu-0.4.6.tgz#ecd3bec4460ed8a7d031d749da9096dac9b936e5" + integrity sha512-0ggx+5RwEbYabIlDBBAvavdfIJCZ757u6nDZtBeQIhzW99EKbWG3lvkXHM3qudFb/pDWSUY4RFBm6vVtF1cJGA== + +"@libsql/linux-x64-musl@0.4.6": + version "0.4.6" + resolved "https://registry.npmjs.org/@libsql/linux-x64-musl/-/linux-x64-musl-0.4.6.tgz#607ab4c9dba5ac264f70b307fc4b0c9287bc2164" + integrity sha512-SWNrv7Hz72QWlbM/ZsbL35MPopZavqCUmQz2HNDZ55t0F+kt8pXuP+bbI2KvmaQ7wdsoqAA4qBmjol0+bh4ndw== + +"@libsql/win32-x64-msvc@0.4.6": + version "0.4.6" + resolved "https://registry.npmjs.org/@libsql/win32-x64-msvc/-/win32-x64-msvc-0.4.6.tgz#ba21a5ea72208d95d9da0d649331a9761cb70594" + integrity sha512-Q0axn110zDNELfkEog3Nl8p9BU4eI/UvgaHevGyOiSDN7s0KPfj0j6jwVHk4oz3o/d/Gg3DRIxomZ4ftfTOy/g== + "@mistralai/mistralai@^0.4.0": version "0.4.0" resolved "https://registry.npmjs.org/@mistralai/mistralai/-/mistralai-0.4.0.tgz#77ded49e7869a4119988f47ce347c347ab41571e" @@ -329,6 +405,11 @@ dependencies: node-fetch "^2.6.7" +"@neon-rs/load@^0.0.4": + version "0.0.4" + resolved "https://registry.npmjs.org/@neon-rs/load/-/load-0.0.4.tgz#2a2a3292c6f1fef043f49886712d3c96a547532e" + integrity sha512-kTPhdZyTQxB+2wpiRcFWrDcejc4JI6tkPuS7UZCG4l6Zvc5kU/gGQ/ozvHTh1XR5tS+UlfAfGuPajjzQjCiHCw== + "@types/node-fetch@^2.6.4": version "2.6.11" resolved "https://registry.npmjs.org/@types/node-fetch/-/node-fetch-2.6.11.tgz#9b39b78665dae0e82a08f02f4967d62c66f95d24" @@ -368,6 +449,13 @@ resolved "https://registry.npmjs.org/@types/uuid/-/uuid-10.0.0.tgz#e9c07fe50da0f53dc24970cca94d619ff03f6f6d" integrity sha512-7gqG38EyHgyP1S+7+xomFtL+ZNHcKv6DwNaCZmJmo1vgMugyF3TCnXVg4t1uk89mLNwnLtnY3TpOpCOyp1/xHQ== +"@types/ws@^8.5.4": + version "8.5.12" + resolved "https://registry.npmjs.org/@types/ws/-/ws-8.5.12.tgz#619475fe98f35ccca2a2f6c137702d85ec247b7e" + integrity sha512-3tPRkv1EtkDpzlgyKyI8pGsGZAGPEaXeu0DOj5DI25Ja91bdAYddYHbADRYVrZMRbfW+1l5YwXVDKohDJNQxkQ== + dependencies: + "@types/node" "*" + abort-controller@^3.0.0: version "3.0.0" resolved "https://registry.npmjs.org/abort-controller/-/abort-controller-3.0.0.tgz#eaf54d53b62bae4138e809ca225c8439a6efb392" @@ -528,6 +616,11 @@ data-uri-to-buffer@^2.0.0: resolved "https://registry.yarnpkg.com/data-uri-to-buffer/-/data-uri-to-buffer-2.0.2.tgz#d296973d5a4897a5dbe31716d118211921f04770" integrity sha512-ND9qDTLc6diwj+Xe5cdAgVTbLVdXbtxTJRXRhli8Mowuaan+0EJOtdqJ0QCHNSSPyoXGx9HX2/VMnKeC34AChA== +data-uri-to-buffer@^4.0.0: + version "4.0.1" + resolved "https://registry.npmjs.org/data-uri-to-buffer/-/data-uri-to-buffer-4.0.1.tgz#d8feb2b2881e6a4f58c2e08acfd0e2834e26222e" + integrity sha512-0R9ikRb668HB7QDxT1vkpuUBtqc53YyAwMwGeUFKRojY/NWKvdZ+9UYtRfGmhqNbRkTSVpMbmyhXipFFv2cb/A== + date-fns@^4.1.0: version "4.1.0" resolved "https://registry.yarnpkg.com/date-fns/-/date-fns-4.1.0.tgz#64b3d83fff5aa80438f5b1a633c2e83b8a1c2d14" @@ -567,6 +660,11 @@ delayed-stream@~1.0.0: resolved "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz#df3ae199acadfb7d440aaae0b29e2272b24ec619" integrity sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ== +detect-libc@2.0.2: + version "2.0.2" + resolved "https://registry.npmjs.org/detect-libc/-/detect-libc-2.0.2.tgz#8ccf2ba9315350e1241b88d0ac3b0e1fbd99605d" + integrity sha512-UX6sGumvvqSaXgdKGUsgZWqcUyIXZ/vZTrlRT/iobiKhGL0zL4d3osHj3uqllWJK+i+sixDS/3COVEOFbupFyw== + detect-libc@^2.0.0: version "2.0.3" resolved "https://registry.npmjs.org/detect-libc/-/detect-libc-2.0.3.tgz#f0cd503b40f9939b894697d19ad50895e30cf700" @@ -642,6 +740,14 @@ expand-template@^2.0.3: resolved "https://registry.npmjs.org/expand-template/-/expand-template-2.0.3.tgz#6e14b3fcee0f3a6340ecb57d2e8918692052a47c" integrity sha512-XYfuKMvj4O35f/pOXLObndIRvyQ+/+6AhODh+OKWj9S9498pHHn/IMszH+gt0fBCRWMNfk1ZSp5x3AifmnI2vg== +fetch-blob@^3.1.2, fetch-blob@^3.1.4: + version "3.2.0" + resolved "https://registry.npmjs.org/fetch-blob/-/fetch-blob-3.2.0.tgz#f09b8d4bbd45adc6f0c20b7e787e793e309dcce9" + integrity sha512-7yAQpD2UMJzLi1Dqv7qFYnPbaPx7ZfFK6PiIxQ4PfkGPyNyl2Ugx+a/umUonmKqjhM4DnfbMvdX6otXq83soQQ== + dependencies: + node-domexception "^1.0.0" + web-streams-polyfill "^3.0.3" + file-uri-to-path@1.0.0: version "1.0.0" resolved "https://registry.npmjs.org/file-uri-to-path/-/file-uri-to-path-1.0.0.tgz#553a7b8446ff6f684359c445f1e37a05dacc33dd" @@ -676,6 +782,13 @@ formdata-node@^4.3.2: node-domexception "1.0.0" web-streams-polyfill "4.0.0-beta.3" +formdata-polyfill@^4.0.10: + version "4.0.10" + resolved "https://registry.npmjs.org/formdata-polyfill/-/formdata-polyfill-4.0.10.tgz#24807c31c9d402e002ab3d8c720144ceb8848423" + integrity sha512-buewHzMvYL29jdeQTVILecSaZKnt/RJWjoZCF5OW60Z67/GmSLBkOFM7qh1PI3zFNtJbaZL5eQu1vLfazOwj4g== + dependencies: + fetch-blob "^3.1.2" + fs-constants@^1.0.0: version "1.0.0" resolved "https://registry.npmjs.org/fs-constants/-/fs-constants-1.0.0.tgz#6be0de9be998ce16af8afc24497b9ee9b7ccd9ad" @@ -786,6 +899,11 @@ itty-time@^1.0.6: resolved "https://registry.yarnpkg.com/itty-time/-/itty-time-1.0.6.tgz#a6eeda619f19d2f4c480ceddd013b93acb05714d" integrity sha512-+P8IZaLLBtFv8hCkIjcymZOp4UJ+xW6bSlQsXGqrkmJh7vSiMFSlNne0mCYagEE0N7HDNR5jJBRxwN0oYv61Rw== +js-base64@^3.7.5: + version "3.7.7" + resolved "https://registry.npmjs.org/js-base64/-/js-base64-3.7.7.tgz#e51b84bf78fbf5702b9541e2cb7bfcb893b43e79" + integrity sha512-7rCnleh0z2CkXhH67J8K1Ytz0b2Y+yxTPL+/KOJoa20hfnVQ/3/T6W/KflYI4bRHRagNeXeU2bkNGI3v1oS/lw== + js-tiktoken@^1.0.12: version "1.0.15" resolved "https://registry.npmjs.org/js-tiktoken/-/js-tiktoken-1.0.15.tgz#92a7d829f6950c2cfb35cc52555502e3d6e2ebac" @@ -805,6 +923,22 @@ langsmith@^0.2.0: semver "^7.6.3" uuid "^10.0.0" +libsql@^0.4.4: + version "0.4.6" + resolved "https://registry.npmjs.org/libsql/-/libsql-0.4.6.tgz#367390ad562d1157aa32f5dedededcf9858ac730" + integrity sha512-F5M+ltteK6dCcpjMahrkgT96uFJvVI8aQ4r9f2AzHQjC7BkAYtvfMSTWGvRBezRgMUIU2h1Sy0pF9nOGOD5iyA== + dependencies: + "@neon-rs/load" "^0.0.4" + detect-libc "2.0.2" + optionalDependencies: + "@libsql/darwin-arm64" "0.4.6" + "@libsql/darwin-x64" "0.4.6" + "@libsql/linux-arm64-gnu" "0.4.6" + "@libsql/linux-arm64-musl" "0.4.6" + "@libsql/linux-x64-gnu" "0.4.6" + "@libsql/linux-x64-musl" "0.4.6" + "@libsql/win32-x64-msvc" "0.4.6" + magic-string@^0.25.3: version "0.25.9" resolved "https://registry.yarnpkg.com/magic-string/-/magic-string-0.25.9.tgz#de7f9faf91ef8a1c91d02c2e5314c8277dbcdd1c" @@ -889,7 +1023,7 @@ node-abi@^3.3.0: dependencies: semver "^7.3.5" -node-domexception@1.0.0: +node-domexception@1.0.0, node-domexception@^1.0.0: version "1.0.0" resolved "https://registry.npmjs.org/node-domexception/-/node-domexception-1.0.0.tgz#6888db46a1f71c0b76b3f7555016b63fe64766e5" integrity sha512-/jKZoMpw0F8GRwl4/eLROPA3cfcXtLApP0QzLmUT/HuPCZWyB7IY9ZrMeKw2O/nFIqPQB3PVM9aYm0F312AXDQ== @@ -901,6 +1035,15 @@ node-fetch@^2.6.7: dependencies: whatwg-url "^5.0.0" +node-fetch@^3.3.2: + version "3.3.2" + resolved "https://registry.npmjs.org/node-fetch/-/node-fetch-3.3.2.tgz#d1e889bacdf733b4ff3b2b243eb7a12866a0b78b" + integrity sha512-dRB78srN/l6gqWulah9SrxeYnxeddIG30+GOqK/9OlLVyLg3HPnr6SqOWTWOXKRwC2eGYCkZ59NNuSgvSrpgOA== + dependencies: + data-uri-to-buffer "^4.0.0" + fetch-blob "^3.1.4" + formdata-polyfill "^4.0.10" + node-forge@^1: version "1.3.1" resolved "https://registry.yarnpkg.com/node-forge/-/node-forge-1.3.1.tgz#be8da2af243b2417d5f646a770663a92b7e9ded3" @@ -1085,6 +1228,11 @@ printable-characters@^1.0.42: resolved "https://registry.yarnpkg.com/printable-characters/-/printable-characters-1.0.42.tgz#3f18e977a9bd8eb37fcc4ff5659d7be90868b3d8" integrity sha512-dKp+C4iXWK4vVYZmYSd0KBH5F/h1HoZRsbJ82AVKRO3PEo8L4lBS/vLwhVtpwwuYcoIsVY+1JYKR268yn480uQ== +promise-limit@^2.7.0: + version "2.7.0" + resolved "https://registry.npmjs.org/promise-limit/-/promise-limit-2.7.0.tgz#eb5737c33342a030eaeaecea9b3d3a93cb592b26" + integrity sha512-7nJ6v5lnJsXwGprnGXga4wx6d1POjvi5Qmf1ivTRxTjH4Z/9Czja/UCMLVmB9N93GeWOU93XaFaEt6jbuoagNw== + pump@^3.0.0: version "3.0.2" resolved "https://registry.npmjs.org/pump/-/pump-3.0.2.tgz#836f3edd6bc2ee599256c924ffe0d88573ddcbf8" @@ -1330,6 +1478,11 @@ web-streams-polyfill@4.0.0-beta.3: resolved "https://registry.npmjs.org/web-streams-polyfill/-/web-streams-polyfill-4.0.0-beta.3.tgz#2898486b74f5156095e473efe989dcf185047a38" integrity sha512-QW95TCTaHmsYfHDybGMwO5IJIM93I/6vTRk+daHTWFPhwh+C8Cg7j7XyKrwrj8Ib6vYXe0ocYNrmzY4xAAN6ug== +web-streams-polyfill@^3.0.3: + version "3.3.3" + resolved "https://registry.npmjs.org/web-streams-polyfill/-/web-streams-polyfill-3.3.3.tgz#2073b91a2fdb1fbfbd401e7de0ac9f8214cecb4b" + integrity sha512-d2JWLCivmZYTSIoge9MsgFCZrt571BikcWGYkjC1khllbTeDlGqZ2D8vD8E/lJa8WGWbb7Plm8/XJYV7IJHZZw== + webidl-conversions@^3.0.0: version "3.0.1" resolved "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz#24534275e2a7bc6be7bc86611cc16ae0a5654871" @@ -1386,7 +1539,7 @@ wrappy@1: resolved "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz#b5243d8f3ec1aa35f1364605bc0d1036e30ab69f" integrity sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ== -ws@^8.17.1: +ws@^8.13.0, ws@^8.17.1: version "8.18.0" resolved "https://registry.yarnpkg.com/ws/-/ws-8.18.0.tgz#0d7505a6eafe2b0e712d232b42279f53bc289bbc" integrity sha512-8VbfWfHLbbwu3+N6OKsOMpBdT4kXPDDB9cJk2bJ6mh9ucxdlnNvH1e+roYkKmN9Nxw2yjz7VzeO9oOz2zJ04Pw==