diff --git a/app/api/langchain/tool/agent/agentapi.ts b/app/api/langchain/tool/agent/agentapi.ts index 11713343b..239341c3c 100644 --- a/app/api/langchain/tool/agent/agentapi.ts +++ b/app/api/langchain/tool/agent/agentapi.ts @@ -4,7 +4,7 @@ import { getServerSideConfig } from "@/app/config/server"; import { BaseCallbackHandler } from "@langchain/core/callbacks/base"; import { BufferMemory, ChatMessageHistory } from "langchain/memory"; -import { AgentExecutor } from "langchain/agents"; +import { AgentExecutor, AgentStep } from "langchain/agents"; import { ACCESS_CODE_PREFIX, ServiceProvider } from "@/app/constant"; // import * as langchainTools from "langchain/tools"; @@ -30,6 +30,9 @@ import { } from "@langchain/core/prompts"; import { ChatOpenAI } from "@langchain/openai"; import { + BaseMessage, + FunctionMessage, + ToolMessage, SystemMessage, HumanMessage, AIMessage, @@ -359,9 +362,10 @@ export class AgentApi { returnMessages: true, chatHistory: new ChatMessageHistory(pastMessages), }); + const MEMORY_KEY = "chat_history"; const prompt = ChatPromptTemplate.fromMessages([ - new MessagesPlaceholder("chat_history"), - ["human", "{input}"], + new MessagesPlaceholder(MEMORY_KEY), + ["user", "{input}"], new MessagesPlaceholder("agent_scratchpad"), ]); const modelWithTools = llm.bind({ @@ -369,10 +373,13 @@ export class AgentApi { }); const runnableAgent = RunnableSequence.from([ { - input: (i: { input: string; steps: ToolsAgentStep[] }) => i.input, - agent_scratchpad: (i: { input: string; steps: ToolsAgentStep[] }) => - formatToOpenAIToolMessages(i.steps), - chat_history: async (_: { + input: (i: { input: string; steps: ToolsAgentStep[] }) => { + return i.input; + }, + agent_scratchpad: (i: { input: string; steps: ToolsAgentStep[] }) => { + return formatToOpenAIToolMessages(i.steps); + }, + chat_history: async (i: { input: string; steps: ToolsAgentStep[]; }) => { @@ -391,12 +398,14 @@ export class AgentApi { }); executor - .call( + .invoke( { input: reqBody.messages.slice(-1)[0].content, signal: this.controller.signal, }, - [handler], + { + callbacks: [handler], + }, ) .catch((error) => { if (this.controller.signal.aborted) { diff --git a/package.json b/package.json index 32a18e93f..cc1f960ea 100644 --- a/package.json +++ b/package.json @@ -20,14 +20,14 @@ "@aws-sdk/s3-request-presigner": "^3.414.0", "@fortaine/fetch-event-source": "^3.0.6", "@hello-pangea/dnd": "^16.5.0", - "@langchain/community": "^0.0.20", - "@langchain/core": "^0.1.17", - "@langchain/openai": "^0.0.12", + "langchain": "0.1.20", + "@langchain/community": "0.0.30", + "@langchain/openai": "0.0.14", "@next/third-parties": "^14.1.0", "@svgr/webpack": "^6.5.1", "@vercel/analytics": "^0.1.11", "@vercel/speed-insights": "^1.0.2", - "axios": "^1.4.0", + "axios": "^0.26.0", "cheerio": "^1.0.0-rc.12", "duck-duck-scrape": "^2.2.4", "emoji-picker-react": "^4.5.15", @@ -37,7 +37,6 @@ "html-to-image": "^1.11.11", "html-to-text": "^9.0.5", "https-proxy-agent": "^7.0.2", - "langchain": "^0.1.6", "mermaid": "^10.6.1", "nanoid": "^5.0.3", "next": "^13.4.9", @@ -78,6 +77,7 @@ "webpack": "^5.88.1" }, "resolutions": { - "lint-staged/yaml": "^2.2.2" + "lint-staged/yaml": "^2.2.2", + "@langchain/core": "0.1.30" } } \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index e3ce96de2..48f3144b5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1813,28 +1813,28 @@ "@jridgewell/resolve-uri" "3.1.0" "@jridgewell/sourcemap-codec" "1.4.14" -"@langchain/community@^0.0.20", "@langchain/community@~0.0.17": - version "0.0.20" - resolved "https://registry.yarnpkg.com/@langchain/community/-/community-0.0.20.tgz#a64307e959545fa0b4ed6b67d5aba3437cd76879" - integrity sha512-maPMjvF50Z+4eMs7HKmY3wfT+k6IjULqLUVPtVdN1zSGobRvnUIbQMKUY2IXVTZmaMXKBAIob+49X8vjO2snDQ== +"@langchain/community@0.0.30", "@langchain/community@~0.0.29": + version "0.0.30" + resolved "https://registry.yarnpkg.com/@langchain/community/-/community-0.0.30.tgz#968f08bd8581fc6c9725f49e5d27f4ebb1bae89e" + integrity sha512-duYJtC8wN8FeIBkijGmDcOPpcK6dPU3Uwamh9K3ASuM97AUI2gEzZJeVtUPQiWFRPyDOAlPfNtrqxVUy2kmp9g== dependencies: - "@langchain/core" "~0.1.16" - "@langchain/openai" "~0.0.10" + "@langchain/core" "~0.1.29" + "@langchain/openai" "~0.0.14" flat "^5.0.2" - langsmith "~0.0.48" + langsmith "~0.1.1" uuid "^9.0.0" zod "^3.22.3" -"@langchain/core@^0.1.17", "@langchain/core@~0.1.13", "@langchain/core@~0.1.16": - version "0.1.17" - resolved "https://registry.yarnpkg.com/@langchain/core/-/core-0.1.17.tgz#2718cefe4db67e97fda676a7f654ab79ece60e8b" - integrity sha512-PNmQgyAsDFm3DsZD+Djmm+sxH8xTGMlAryhYNgTg1Wkvhh+ztCqcVVYAv+aWch8CM56FBYMD8Guq0TJuRJJxEA== +"@langchain/core@0.1.30", "@langchain/core@~0.1.13", "@langchain/core@~0.1.29": + version "0.1.30" + resolved "https://registry.yarnpkg.com/@langchain/core/-/core-0.1.30.tgz#6f9b5d3cfe9bb447f94bc8c71be1527b9decd302" + integrity sha512-3oqEKgwe7U/efieZrCC6BLwOHm4YPj27mRNBZLaB5BwPh3a7gXIevxEnbjN3o5j9kJqc5acG7nn35h7Wkrf2Ag== dependencies: ansi-styles "^5.0.0" camelcase "6" decamelize "1.2.0" js-tiktoken "^1.0.8" - langsmith "~0.0.48" + langsmith "~0.1.1" ml-distance "^4.0.0" p-queue "^6.6.2" p-retry "4" @@ -1842,16 +1842,16 @@ zod "^3.22.4" zod-to-json-schema "^3.22.3" -"@langchain/openai@^0.0.12", "@langchain/openai@~0.0.10", "@langchain/openai@~0.0.12": - version "0.0.12" - resolved "https://registry.yarnpkg.com/@langchain/openai/-/openai-0.0.12.tgz#4c6a4dda3ca96f103482f389299e018340aa2b75" - integrity sha512-MR9x1xRXwJpdYlVx9Tga89q/MvxPrSTYyA5vy9tQ8dfQHNWnlgmI4gB/hDIsWUu1ooScagD4wW+aTnohTX+g+g== +"@langchain/openai@0.0.14", "@langchain/openai@~0.0.14": + version "0.0.14" + resolved "https://registry.yarnpkg.com/@langchain/openai/-/openai-0.0.14.tgz#27a6ba83f6b754391868b22f3b90cd440038acf0" + integrity sha512-co6nRylPrLGY/C3JYxhHt6cxLq07P086O7K3QaZH7SFFErIN9wSzJonpvhZR07DEUq6eK6wKgh2ORxA/NcjSRQ== dependencies: "@langchain/core" "~0.1.13" js-tiktoken "^1.0.7" - openai "^4.24.2" - zod "^3.22.3" - zod-to-json-schema "3.20.3" + openai "^4.26.0" + zod "^3.22.4" + zod-to-json-schema "^3.22.3" "@next/env@13.4.9": version "13.4.9" @@ -3168,14 +3168,12 @@ axe-core@^4.6.2: resolved "https://registry.yarnpkg.com/axe-core/-/axe-core-4.6.3.tgz#fc0db6fdb65cc7a80ccf85286d91d64ababa3ece" integrity sha512-/BQzOX780JhsxDnPpH4ZiyrJAzcd8AfzFPkv+89veFSr1rcMjuq2JDCwypKaPeB6ljHp9KjXhPpjgCvQlWYuqg== -axios@^1.4.0: - version "1.4.0" - resolved "https://registry.yarnpkg.com/axios/-/axios-1.4.0.tgz#38a7bf1224cd308de271146038b551d725f0be1f" - integrity sha512-S4XCWMEmzvo64T9GfvQDOXgYRDJ/wsSZc7Jvdgx5u1sd0JwsuPLqb3SYmusag+edF6ziyMensPVqLTSc1PiSEA== +axios@^0.26.0: + version "0.26.1" + resolved "https://registry.yarnpkg.com/axios/-/axios-0.26.1.tgz#1ede41c51fcf51bbbd6fd43669caaa4f0495aaa9" + integrity sha512-fPwcX4EvnSHuInCMItEhAGnaSEXRBjtzh9fOtsE6E1G6p7vl7edEeZe11QHf18+6+9gR5PbKV/sGKNaD8YaMeA== dependencies: - follow-redirects "^1.15.0" - form-data "^4.0.0" - proxy-from-env "^1.1.0" + follow-redirects "^1.14.8" axobject-query@^3.1.1: version "3.1.1" @@ -4675,10 +4673,10 @@ flatted@^3.1.0: resolved "https://registry.yarnpkg.com/flatted/-/flatted-3.2.7.tgz#609f39207cb614b89d0765b477cb2d437fbf9787" integrity sha512-5nqDSxl8nn5BSNxyR3n4I6eDmbolI6WT+QqR547RwxQapgjQBmtktdP+HTBb/a/zLsbzERTONyUB5pefh5TtjQ== -follow-redirects@^1.15.0: - version "1.15.2" - resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.2.tgz#b460864144ba63f2681096f274c4e57026da2c13" - integrity sha512-VQLG33o04KaQ8uYi2tVNbdrWp1QWxNNea+nmIB4EVM28v0hmP17z7aG1+wAkNzVq4KeXTq3221ye5qTJP91JwA== +follow-redirects@^1.14.8: + version "1.15.5" + resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.5.tgz#54d4d6d062c0fa7d9d17feb008461550e3ba8020" + integrity sha512-vSFWUON1B+yAw1VN4xMfxgn5fTUiaOzAJCKBwIIgT/+7CuGy9+r+5gITvP62j3RmaD5Ph65UaERdOSRGUzZtgw== for-each@^0.3.3: version "0.3.3" @@ -5500,22 +5498,22 @@ kleur@^4.0.3: resolved "https://registry.yarnpkg.com/kleur/-/kleur-4.1.5.tgz#95106101795f7050c6c650f350c683febddb1780" integrity sha512-o+NO+8WrRiQEE4/7nwRJhN1HWpVmJm511pBHUxPLtp0BUISzlBplORYSmTclCnJvQq2tKu/sgl3xVpkc7ZWuQQ== -langchain@^0.1.6: - version "0.1.6" - resolved "https://registry.yarnpkg.com/langchain/-/langchain-0.1.6.tgz#7bd5586aa8c3db31d766ea815f2bd8f82544fcac" - integrity sha512-ju4LLw6vTax3bfSkphcK8CE6yIKe1NXLT95Ys/gvoONRfZkV4FpNMjDc9Y+keUvzmTv2buhiFU/gslbA4eJtrw== +langchain@0.1.20: + version "0.1.20" + resolved "https://registry.yarnpkg.com/langchain/-/langchain-0.1.20.tgz#d1e5e3ae97ac007fb318517995385a30aed6487e" + integrity sha512-NaO4Ovbwb0EHqyo83Wg7TKQE7tV1WGvnGdncpMiazBL9Qgv+vYpDpOV66C64M2C/4fYrNJV/BKDljjFhSE8PSw== dependencies: "@anthropic-ai/sdk" "^0.9.1" - "@langchain/community" "~0.0.17" - "@langchain/core" "~0.1.16" - "@langchain/openai" "~0.0.12" + "@langchain/community" "~0.0.29" + "@langchain/core" "~0.1.29" + "@langchain/openai" "~0.0.14" 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.1" ml-distance "^4.0.0" openapi-types "^12.1.3" p-retry "4" @@ -5524,26 +5522,15 @@ langchain@^0.1.6: 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.53" - resolved "https://registry.yarnpkg.com/langsmith/-/langsmith-0.0.53.tgz#70e8bbbb6fdde8a5217321a0d47199cc15699d9e" - integrity sha512-w2Rgx4ixE+wuItFVKGnc+Wmzj91RevMd7sev9BHWC8VFztDPuyrNZQD55QpxphM6JLb9hF9osxvAiYDGtDZbuQ== - dependencies: - "@types/uuid" "^9.0.1" - commander "^10.0.1" - p-queue "^6.6.2" - p-retry "4" - uuid "^9.0.0" - -langsmith@~0.0.59: - version "0.0.62" - resolved "https://registry.yarnpkg.com/langsmith/-/langsmith-0.0.62.tgz#79e306b82436d8f165b976b05842ebd0f7058a1d" - integrity sha512-OjjlNbxbfEUSgbBLA7JS7Lwg0M+oMZp4ZSwujR9TZBcSKvpv1f3lE2X9e9vTWe9huoUMlUAvwoaSWdDG6w6QLQ== +langsmith@~0.1.1: + version "0.1.3" + resolved "https://registry.yarnpkg.com/langsmith/-/langsmith-0.1.3.tgz#b086c5dd0709c41da417bc8b672e8f4a03e80809" + integrity sha512-kQMS3QySeU0Qt9A71d9trUXbeKn33HfxpRc7hRjSB967zcdTAngh66NcqYqBflD3nOL4FK6LKmvfb3vbNDEoPg== dependencies: "@types/uuid" "^9.0.1" commander "^10.0.1" @@ -6510,10 +6497,10 @@ open@^8.4.0: is-docker "^2.1.1" is-wsl "^2.2.0" -openai@^4.24.2: - version "4.25.0" - resolved "https://registry.yarnpkg.com/openai/-/openai-4.25.0.tgz#b40099d625cccb19cbf1cab88915ac1699ece0ed" - integrity sha512-qLMFOizjxKuDfQkBrczZPYo6XVL4bdcuz9MR11Q+M91kGcs8dQw+O90nRcC+qWuhaGphQkfXQJMn4cd7Yew3Kg== +openai@^4.26.0: + version "4.28.0" + resolved "https://registry.yarnpkg.com/openai/-/openai-4.28.0.tgz#ded00e3d98c25758b5406c9675ec27a957e00930" + integrity sha512-JM8fhcpmpGN0vrUwGquYIzdcEQHtFuom6sRCbbCM6CfzZXNuRk33G7KfeRAIfnaCxSpzrP5iHtwJzIm6biUZ2Q== dependencies: "@types/node" "^18.11.18" "@types/node-fetch" "^2.6.4" @@ -6743,11 +6730,6 @@ property-information@^6.0.0: resolved "https://registry.yarnpkg.com/property-information/-/property-information-6.2.0.tgz#b74f522c31c097b5149e3c3cb8d7f3defd986a1d" integrity sha512-kma4U7AFCTwpqq5twzC1YVIDXSqg6qQK6JN0smOw8fgRy1OkMi0CYSzFmsy6dnqSenamAtj0CyXMUJ1Mf6oROg== -proxy-from-env@^1.1.0: - version "1.1.0" - resolved "https://registry.yarnpkg.com/proxy-from-env/-/proxy-from-env-1.1.0.tgz#e102f16ca355424865755d2c9e8ea4f24d58c3e2" - integrity sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg== - punycode@^2.1.0: version "2.3.0" resolved "https://registry.yarnpkg.com/punycode/-/punycode-2.3.0.tgz#f67fa67c94da8f4d0cfff981aee4118064199b8f" @@ -7925,11 +7907,6 @@ yocto-queue@^0.1.0: resolved "https://registry.yarnpkg.com/yocto-queue/-/yocto-queue-0.1.0.tgz#0294eb3dee05028d31ee1a5fa2c556a6aaf10a1b" integrity sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q== -zod-to-json-schema@3.20.3: - version "3.20.3" - resolved "https://registry.yarnpkg.com/zod-to-json-schema/-/zod-to-json-schema-3.20.3.tgz#8c95d8c20f20455ffa0b4b526c29703f35f6d787" - integrity sha512-/Q3wnyxAfCt94ZcrGiXXoiAfRqasxl9CX64LZ9fj+4dKH68zulUtU0uk1WMxQPfAxQ0ZI70dKzcoW7hHj+DwSQ== - zod-to-json-schema@^3.22.3: version "3.22.3" resolved "https://registry.yarnpkg.com/zod-to-json-schema/-/zod-to-json-schema-3.22.3.tgz#1c71f9fa23f80b2f3b5eed537afa8a13a66a5200"