diff --git a/package.json b/package.json index 4abe102f..2ecd6fee 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,8 @@ "@dnd-kit/core": "^6.0.8", "@dnd-kit/sortable": "^7.0.2", "@langchain/community": "^0.0.27", + "@langchain/core": "^0.1.57", + "@langchain/openai": "^0.0.28", "@radix-ui/react-dialog": "^1.0.3", "@radix-ui/react-dropdown-menu": "^2.0.4", "@radix-ui/react-select": "^2.0.0", @@ -34,7 +36,7 @@ "dnd-kit-sortable-tree": "^0.1.58", "endent": "^2.1.0", "jotai": "^2.4.3", - "langchain": "^0.1.17", + "langchain": "^0.1.33", "object-hash": "^3.0.0", "react": "^18.2.0", "react-dom": "^18.2.0", diff --git a/src/components/Sidebar/auth/index.tsx b/src/components/Sidebar/auth/index.tsx index b03595cd..c9ac860e 100644 --- a/src/components/Sidebar/auth/index.tsx +++ b/src/components/Sidebar/auth/index.tsx @@ -17,10 +17,9 @@ const Auth = () => { const handleOpenAiKeySubmit = async (e: React.FormEvent) => { e.preventDefault() const data = new FormData(e.currentTarget) - const key = data.get('openAiKey') - const isVaildApiKey: boolean = await validateApiKey(key as string) + const key = data.get('openAiKey') as string | null - if (key && isVaildApiKey) { + if (key && (await validateApiKey(key))) { setSettings((prev) => ({ ...prev, chat: { diff --git a/src/lib/validApiKey.ts b/src/lib/validApiKey.ts index 4e88b5b2..2412c7b4 100644 --- a/src/lib/validApiKey.ts +++ b/src/lib/validApiKey.ts @@ -1,17 +1,16 @@ +import { HumanMessage } from '@langchain/core/messages' import axios from 'axios' +import { ChatOpenAI } from 'langchain/chat_models/openai' -const VERIFY_API_KEY_URL = 'https://api.openai.com/v1/models' - -export const validateApiKey = async (apiKey: string): Promise => { +export const validateApiKey = async ( + openAIApiKey: string, +): Promise => { + const model = new ChatOpenAI({ openAIApiKey }) try { - await axios.get(VERIFY_API_KEY_URL, { - headers: { - 'Content-Type': 'application/json', - Authorization: `Bearer ${apiKey}`, - }, - }) + await model.invoke([new HumanMessage("Say Ok")]) return true - } catch { + } catch (e) { + console.error(e) return false } } diff --git a/yarn.lock b/yarn.lock index 3ce373a5..0045d0c3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -821,7 +821,7 @@ "@jridgewell/resolve-uri" "3.1.0" "@jridgewell/sourcemap-codec" "1.4.14" -"@langchain/community@^0.0.27", "@langchain/community@~0.0.20": +"@langchain/community@^0.0.27": version "0.0.27" resolved "https://registry.yarnpkg.com/@langchain/community/-/community-0.0.27.tgz#910c33da06a21a635093c6f85d34ee1c31bc1bc0" integrity sha512-b/eV2MepDrGrImFbKdfiuhBYIQratKD45nILfmHpNJ4DFfs0RbCeZgyGBoIaVeIppnApkZmMGMisspjkDmJ+zA== @@ -833,7 +833,38 @@ uuid "^9.0.0" zod "^3.22.3" -"@langchain/core@~0.1.13", "@langchain/core@~0.1.16", "@langchain/core@~0.1.25": +"@langchain/community@~0.0.47": + version "0.0.48" + resolved "https://registry.yarnpkg.com/@langchain/community/-/community-0.0.48.tgz#a0a715f77d2a6914036c7b6fc32a760e1bf6bc9b" + integrity sha512-jG7dpJkgKtwbrSLsxW4mXmECopTj5tRt4+B+QcKCqZm7yEghFR44Xj5OkDWtzzsjpL47RJck5AJ8MsQnmi7WDQ== + dependencies: + "@langchain/core" "~0.1.56" + "@langchain/openai" "~0.0.28" + expr-eval "^2.0.2" + flat "^5.0.2" + langsmith "~0.1.1" + uuid "^9.0.0" + zod "^3.22.3" + zod-to-json-schema "^3.22.5" + +"@langchain/core@^0.1.57", "@langchain/core@~0.1.56": + version "0.1.57" + resolved "https://registry.yarnpkg.com/@langchain/core/-/core-0.1.57.tgz#c592ad6715e373e4203f590f20b8b999e64cfb16" + integrity sha512-6wOwidPkkRcANrOKl88+YYpm3jHfpg6W8EqZHQCImSAlxdEhyDSq2eeQKHOPCFCrfNWkClaNn+Wlzzz4Qwf9Tg== + dependencies: + ansi-styles "^5.0.0" + camelcase "6" + decamelize "1.2.0" + js-tiktoken "^1.0.8" + langsmith "~0.1.7" + ml-distance "^4.0.0" + p-queue "^6.6.2" + p-retry "4" + uuid "^9.0.0" + zod "^3.22.4" + zod-to-json-schema "^3.22.3" + +"@langchain/core@~0.1.13", "@langchain/core@~0.1.16": version "0.1.27" resolved "https://registry.yarnpkg.com/@langchain/core/-/core-0.1.27.tgz#a96e57180bf9ce8a118fa89573f3fb385e19f774" integrity sha512-I43G9r2NIs1qTjfU07D6PUGhCfc3anwziqDeoZ4mk2wphVv+8HhAbQNbOSvsuIk2p3qVkjXgxnI9cBFoEdwd0Q== @@ -850,7 +881,18 @@ zod "^3.22.4" zod-to-json-schema "^3.22.3" -"@langchain/openai@~0.0.10", "@langchain/openai@~0.0.12": +"@langchain/openai@^0.0.28", "@langchain/openai@~0.0.28": + version "0.0.28" + resolved "https://registry.yarnpkg.com/@langchain/openai/-/openai-0.0.28.tgz#afaeec61b44816935db9ae937496c964c81ab571" + integrity sha512-2s1RA3/eAnz4ahdzsMPBna9hfAqpFNlWdHiPxVGZ5yrhXsbLWWoPcF+22LCk9t0HJKtazi2GCIWc0HVXH9Abig== + dependencies: + "@langchain/core" "~0.1.56" + js-tiktoken "^1.0.7" + openai "^4.32.1" + zod "^3.22.4" + zod-to-json-schema "^3.22.3" + +"@langchain/openai@~0.0.10": version "0.0.14" resolved "https://registry.yarnpkg.com/@langchain/openai/-/openai-0.0.14.tgz#27a6ba83f6b754391868b22f3b90cd440038acf0" integrity sha512-co6nRylPrLGY/C3JYxhHt6cxLq07P086O7K3QaZH7SFFErIN9wSzJonpvhZR07DEUq6eK6wKgh2ORxA/NcjSRQ== @@ -3933,22 +3975,21 @@ kleur@^4.0.3, kleur@^4.1.5: resolved "https://registry.yarnpkg.com/kleur/-/kleur-4.1.5.tgz#95106101795f7050c6c650f350c683febddb1780" integrity sha512-o+NO+8WrRiQEE4/7nwRJhN1HWpVmJm511pBHUxPLtp0BUISzlBplORYSmTclCnJvQq2tKu/sgl3xVpkc7ZWuQQ== -langchain@^0.1.17: - version "0.1.17" - resolved "https://registry.yarnpkg.com/langchain/-/langchain-0.1.17.tgz#050732e56e1457f3c69bcf9de88981860a2237cd" - integrity sha512-w+fSAxsPdeMpJMhbS1Ur/dRhgRvw4j/vqD5kgfgmZNAh5X3nz5BXktcsyH8hwXLjuoFJMUyp3mIFy60APrY0Vw== +langchain@^0.1.33: + version "0.1.33" + resolved "https://registry.yarnpkg.com/langchain/-/langchain-0.1.33.tgz#959f0f023975092569b49f1f07fe60c11c643530" + integrity sha512-IrRd839x8eAmDutHNDMHGzIjufyWt/ckJrnRB2WifIJmtLWNRNQo5jZd7toeBU0UOVOQxoPtQGYf8lR0ar7vIQ== dependencies: "@anthropic-ai/sdk" "^0.9.1" - "@langchain/community" "~0.0.20" - "@langchain/core" "~0.1.25" - "@langchain/openai" "~0.0.12" + "@langchain/community" "~0.0.47" + "@langchain/core" "~0.1.56" + "@langchain/openai" "~0.0.28" binary-extensions "^2.2.0" - expr-eval "^2.0.2" js-tiktoken "^1.0.7" js-yaml "^4.1.0" jsonpointer "^5.0.1" - langchainhub "~0.0.6" - langsmith "~0.0.59" + langchainhub "~0.0.8" + langsmith "~0.1.7" ml-distance "^4.0.0" openapi-types "^12.1.3" p-retry "4" @@ -3957,10 +3998,10 @@ langchain@^0.1.17: zod "^3.22.4" zod-to-json-schema "^3.22.3" -langchainhub@~0.0.6: - version "0.0.6" - resolved "https://registry.yarnpkg.com/langchainhub/-/langchainhub-0.0.6.tgz#9d2d06e4ce0807b4e8a31e19611f57aef990b54d" - integrity sha512-SW6105T+YP1cTe0yMf//7kyshCgvCTyFBMTgH2H3s9rTAR4e+78DA/BBrUL/Mt4Q5eMWui7iGuAYb3pgGsdQ9w== +langchainhub@~0.0.8: + version "0.0.8" + resolved "https://registry.yarnpkg.com/langchainhub/-/langchainhub-0.0.8.tgz#fd4b96dc795e22e36c1a20bad31b61b0c33d3110" + integrity sha512-Woyb8YDHgqqTOZvWIbm2CaFDGfZ4NTSyXV687AG4vXEfoNo7cGQp7nhl7wL3ehenKWmNEmcxCLgOZzW8jE6lOQ== langsmith@~0.0.48: version "0.0.48" @@ -3973,10 +4014,10 @@ langsmith@~0.0.48: p-retry "4" uuid "^9.0.0" -langsmith@~0.0.59: - version "0.0.68" - resolved "https://registry.yarnpkg.com/langsmith/-/langsmith-0.0.68.tgz#8748d3203d348cc19e5ee4ddeef908964a62e21a" - integrity sha512-bxaJndEhUFDfv5soWKxONrLMZaVZfS+G4smJl3WYQlsEph8ierG3QbJfx1PEwl40TD0aFBjzq62usUX1UOCjuA== +langsmith@~0.1.1, langsmith@~0.1.7: + version "0.1.14" + resolved "https://registry.yarnpkg.com/langsmith/-/langsmith-0.1.14.tgz#2b889dbcfb49547614df276a4a5a063092a1585d" + integrity sha512-iEzQLLB7/0nRpAwNBAR7B7N64fyByg5UsNjSvLaCCkQ9AS68PSafjB8xQkyI8QXXrGjU1dEqDRoa8m4SUuRdUw== dependencies: "@types/uuid" "^9.0.1" commander "^10.0.1" @@ -4871,6 +4912,20 @@ openai@^4.26.0: node-fetch "^2.6.7" web-streams-polyfill "^3.2.1" +openai@^4.32.1: + version "4.33.1" + resolved "https://registry.yarnpkg.com/openai/-/openai-4.33.1.tgz#7f14c28ea0994ff6eacc2dbca2131c61e7692cd4" + integrity sha512-0DH572aSxGTT1JPOXgJQ9mjiuSPg/7scPot8hLc5I1mfQxPxLXTZWJpWerKaIWOuPkR2nrB0SamGDEehH8RuWA== + dependencies: + "@types/node" "^18.11.18" + "@types/node-fetch" "^2.6.4" + abort-controller "^3.0.0" + agentkeepalive "^4.2.1" + form-data-encoder "1.7.2" + formdata-node "^4.3.2" + node-fetch "^2.6.7" + web-streams-polyfill "^3.2.1" + openapi-types@^12.1.3: version "12.1.3" resolved "https://registry.yarnpkg.com/openapi-types/-/openapi-types-12.1.3.tgz#471995eb26c4b97b7bd356aacf7b91b73e777dd3" @@ -6618,6 +6673,11 @@ zod-to-json-schema@^3.22.3: resolved "https://registry.yarnpkg.com/zod-to-json-schema/-/zod-to-json-schema-3.22.4.tgz#f8cc691f6043e9084375e85fb1f76ebafe253d70" integrity sha512-2Ed5dJ+n/O3cU383xSY28cuVi0BCQhF8nYqWU5paEpl7fVdqdAmiLdqLyfblbNdfOFwFfi/mqU4O1pwc60iBhQ== +zod-to-json-schema@^3.22.5: + version "3.22.5" + resolved "https://registry.yarnpkg.com/zod-to-json-schema/-/zod-to-json-schema-3.22.5.tgz#3646e81cfc318dbad2a22519e5ce661615418673" + integrity sha512-+akaPo6a0zpVCCseDed504KBJUQpEW5QZw7RMneNmKw+fGaML1Z9tUNLnHHAC8x6dzVRO1eB2oEMyZRnuBZg7Q== + zod@^3.22.3, zod@^3.22.4: version "3.22.4" resolved "https://registry.yarnpkg.com/zod/-/zod-3.22.4.tgz#f31c3a9386f61b1f228af56faa9255e845cf3fff"