From ed3fa01fcb31891b0201dbdf050046e28be2b5ee Mon Sep 17 00:00:00 2001 From: Nicole White Date: Thu, 26 Oct 2023 20:28:14 -0400 Subject: [PATCH] Use agents in langchain example (#21) --- JavaScript/langchain/README.md | 12 ++++- JavaScript/langchain/package-lock.json | 50 ++++++++++++++------ JavaScript/langchain/package.json | 2 +- JavaScript/langchain/src/index.js | 59 +++++++++--------------- Python/langchain/README.md | 12 ++++- Python/langchain/main.py | 63 ++++++++++++++------------ Python/langchain/poetry.lock | 49 ++++++++++++++++++-- Python/langchain/pyproject.toml | 3 +- 8 files changed, 159 insertions(+), 91 deletions(-) diff --git a/JavaScript/langchain/README.md b/JavaScript/langchain/README.md index 7ca8f6e2..e0e86759 100644 --- a/JavaScript/langchain/README.md +++ b/JavaScript/langchain/README.md @@ -46,6 +46,14 @@ npm install npm run start ``` -## View logs in Autoblocks +## View the trace in Autoblocks -After you run the script, you can find the trace on the [explore page](https://app.autoblocks.ai/explore). +Go to the [explore page](https://app.autoblocks.ai/explore). When you find the trace, switch to the Trace Tree view to +see a tree of all the spans in the trace. + +The first span that is selected will show you the overall question and answer of the LangChain pipeline, but you can also +drill into individual spans by clicking on them to understand how LangChain is working under the hood. + +![explore-trace-top-level-span](https://github.com/autoblocksai/autoblocks-examples/assets/7498009/590e232a-eeaf-46a1-b9e3-3c0a8648234b) + +![explore-trace-nested-span](https://github.com/autoblocksai/autoblocks-examples/assets/7498009/b32d7776-8378-49cc-a866-7ba6bd08f5e5) diff --git a/JavaScript/langchain/package-lock.json b/JavaScript/langchain/package-lock.json index 8690eea3..c95d539b 100644 --- a/JavaScript/langchain/package-lock.json +++ b/JavaScript/langchain/package-lock.json @@ -11,7 +11,7 @@ "dependencies": { "@autoblocks/client": "^0.0.15", "dotenv-cli": "^7.3.0", - "langchain": "^0.0.166" + "langchain": "^0.0.173" }, "engines": { "node": ">=18" @@ -398,9 +398,9 @@ } }, "node_modules/langchain": { - "version": "0.0.166", - "resolved": "https://registry.npmjs.org/langchain/-/langchain-0.0.166.tgz", - "integrity": "sha512-ZO3hwZmvMaeR2bcGJGjyt8llCkzeGVso0c1KAj+StHZhf/piDgRMlxEL+4Hb8iFVCZboT228MY7xdgjaSGDMsg==", + "version": "0.0.173", + "resolved": "https://registry.npmjs.org/langchain/-/langchain-0.0.173.tgz", + "integrity": "sha512-J10B8bLuqbkYhQ0ECBm4ZRLZCFN5gHGSVcgZtRSwYikDJ046N2ILiGUI75kofDUZxzJqA8udYuSBiyWQfKVy4w==", "dependencies": { "@anthropic-ai/sdk": "^0.6.2", "ansi-styles": "^5.0.0", @@ -443,10 +443,10 @@ "@cloudflare/ai": "^1.0.12", "@elastic/elasticsearch": "^8.4.0", "@getmetal/metal-sdk": "*", - "@getzep/zep-js": "^0.7.0", - "@gomomento/sdk": "^1.41.2", - "@gomomento/sdk-core": "^1.41.2", - "@gomomento/sdk-web": "^1.41.2", + "@getzep/zep-js": "^0.8.0", + "@gomomento/sdk": "^1.44.1", + "@gomomento/sdk-core": "^1.44.1", + "@gomomento/sdk-web": "^1.44.1", "@google-ai/generativelanguage": "^0.2.1", "@google-cloud/storage": "^6.10.1", "@huggingface/inference": "^1.5.1", @@ -467,20 +467,25 @@ "@tensorflow/tfjs-converter": "*", "@tensorflow/tfjs-core": "*", "@upstash/redis": "^1.20.6", + "@vercel/kv": "^0.2.3", "@vercel/postgres": "^0.5.0", "@writerai/writer-sdk": "^0.40.2", "@xata.io/client": "^0.25.1", "@xenova/transformers": "^2.5.4", "@zilliz/milvus2-sdk-node": ">=2.2.7", "apify-client": "^2.7.1", + "assemblyai": "^2.0.2", "axios": "*", - "cassandra-driver": "^4.6.4", + "cassandra-driver": "^4.7.2", "cheerio": "^1.0.0-rc.12", "chromadb": "*", + "closevector-common": "0.1.0-alpha.1", + "closevector-node": "0.1.0-alpha.10", + "closevector-web": "0.1.0-alpha.16", "cohere-ai": ">=6.0.0", "d3-dsv": "^2.0.0", "epub2": "^3.0.1", - "faiss-node": "^0.3.0", + "faiss-node": "^0.5.1", "fast-xml-parser": "^4.2.7", "firebase-admin": "^11.9.0", "google-auth-library": "^8.9.0", @@ -490,7 +495,7 @@ "ignore": "^5.2.0", "ioredis": "^5.3.2", "jsdom": "*", - "llmonitor": "*", + "llmonitor": "^0.5.8", "lodash": "^4.17.21", "mammoth": "*", "mongodb": "^5.2.0", @@ -502,7 +507,7 @@ "peggy": "^3.0.2", "pg": "^8.11.0", "pg-copy-streams": "^6.0.5", - "pickleparser": "^0.1.0", + "pickleparser": "^0.2.1", "playwright": "^1.32.1", "portkey-ai": "^0.1.11", "puppeteer": "^19.7.2", @@ -635,6 +640,9 @@ "@upstash/redis": { "optional": true }, + "@vercel/kv": { + "optional": true + }, "@vercel/postgres": { "optional": true }, @@ -653,6 +661,9 @@ "apify-client": { "optional": true }, + "assemblyai": { + "optional": true + }, "axios": { "optional": true }, @@ -665,6 +676,15 @@ "chromadb": { "optional": true }, + "closevector-common": { + "optional": true + }, + "closevector-node": { + "optional": true + }, + "closevector-web": { + "optional": true + }, "cohere-ai": { "optional": true }, @@ -1420,9 +1440,9 @@ "integrity": "sha512-p/nXbhSEcu3pZRdkW1OfJhpsVtW1gd4Wa1fnQc9YLiTfAjn0312eMKimbdIQzuZl9aa9xUGaRlP9T/CJE/ditQ==" }, "langchain": { - "version": "0.0.166", - "resolved": "https://registry.npmjs.org/langchain/-/langchain-0.0.166.tgz", - "integrity": "sha512-ZO3hwZmvMaeR2bcGJGjyt8llCkzeGVso0c1KAj+StHZhf/piDgRMlxEL+4Hb8iFVCZboT228MY7xdgjaSGDMsg==", + "version": "0.0.173", + "resolved": "https://registry.npmjs.org/langchain/-/langchain-0.0.173.tgz", + "integrity": "sha512-J10B8bLuqbkYhQ0ECBm4ZRLZCFN5gHGSVcgZtRSwYikDJ046N2ILiGUI75kofDUZxzJqA8udYuSBiyWQfKVy4w==", "requires": { "@anthropic-ai/sdk": "^0.6.2", "ansi-styles": "^5.0.0", diff --git a/JavaScript/langchain/package.json b/JavaScript/langchain/package.json index 1604e6cf..b95efa00 100644 --- a/JavaScript/langchain/package.json +++ b/JavaScript/langchain/package.json @@ -15,6 +15,6 @@ "dependencies": { "@autoblocks/client": "^0.0.15", "dotenv-cli": "^7.3.0", - "langchain": "^0.0.166" + "langchain": "^0.0.173" } } diff --git a/JavaScript/langchain/src/index.js b/JavaScript/langchain/src/index.js index 1f385b27..393a47d2 100644 --- a/JavaScript/langchain/src/index.js +++ b/JavaScript/langchain/src/index.js @@ -1,49 +1,34 @@ import { AutoblocksCallbackHandler } from '@autoblocks/client/langchain'; - -import { SimpleSequentialChain, LLMChain } from "langchain/chains"; import { OpenAI } from "langchain/llms/openai"; -import { PromptTemplate } from "langchain/prompts"; +import { DynamicTool } from "langchain/tools"; +import { Calculator } from "langchain/tools/calculator"; +import { initializeAgentExecutorWithOptions } from "langchain/agents"; const main = async () => { - // This is an LLMChain to write a synopsis given a title of a play. - const synopsisLLM = new OpenAI({ temperature: 0.7 }); - const titleTemplate = `You are a playwright. Given the title of a play, it is your job to write a synopsis for that title. - -Title: {title} -Playwright: This is a synopsis for the above play:`; - const synopsisTemplate = new PromptTemplate({ - template: titleTemplate, - inputVariables: ["title"], - }); - const synopsisChain = new LLMChain({ llm: synopsisLLM, prompt: synopsisTemplate }); - - // This is an LLMChain to write a review of a play given a synopsis. - const reviewLLM = new OpenAI({ temperature: 0.7 }); - const reviewTemplate = `You are a play critic from the New York Times. Given the synopsis of a play, it is your job to write a review for that play. - -Play Synopsis: -{synopsis} -Review from a New York Times play critic of the above play:`; - const reviewPromptTemplate = new PromptTemplate({ - template: reviewTemplate, - inputVariables: ["synopsis"], - }); - const reviewChain = new LLMChain({ - llm: reviewLLM, - prompt: reviewPromptTemplate, - }); + const model = new OpenAI({ temperature: 0 }); + const tools = [ + new Calculator(), + new DynamicTool({ + name: "Today's Date", + description: + "call this to get today's date. input should be an empty string.", + func: () => new Date().getDate(), + }), + ]; - // This is the overall chain where we run these two chains in sequence. - const overallChain = new SimpleSequentialChain({ - chains: [synopsisChain, reviewChain], + const executor = await initializeAgentExecutorWithOptions(tools, model, { + agentType: 'structured-chat-zero-shot-react-description', }); - // Run the chain const handler = new AutoblocksCallbackHandler(); - const review = await overallChain.run("Tragedy at sunset on the beach", { callbacks: [handler] }); + const output = await executor.run( + "What is today's date? What is that date divided by 2?", + { callbacks: [handler] + }); - console.log('Review:'); - console.log(review); + console.log(`Output: ${output}`); + console.log('\n'); + console.log('View your trace: https://app.autoblocks.ai/explore') } main(); diff --git a/Python/langchain/README.md b/Python/langchain/README.md index a0a710fa..b4741f70 100644 --- a/Python/langchain/README.md +++ b/Python/langchain/README.md @@ -46,6 +46,14 @@ poetry install poetry run python main.py ``` -## View logs in Autoblocks +## View the trace in Autoblocks -Go to the [explore page](https://app.autoblocks.ai/explore) to see the trace. +Go to the [explore page](https://app.autoblocks.ai/explore). When you find the trace, switch to the Trace Tree view to +see a tree of all the spans in the trace. + +The first span that is selected will show you the overall question and answer of the LangChain pipeline, but you can also +drill into individual spans by clicking on them to understand how LangChain is working under the hood. + +![explore-trace-top-level-span](https://github.com/autoblocksai/autoblocks-examples/assets/7498009/941c09b7-86e9-4e0b-9df4-2a9be0b32771) + +![explore-teace-nested-span](https://github.com/autoblocksai/autoblocks-examples/assets/7498009/99f02ba9-c3ea-4645-aa9d-17f6d83be790) diff --git a/Python/langchain/main.py b/Python/langchain/main.py index 81dd55c0..7a4cefc8 100644 --- a/Python/langchain/main.py +++ b/Python/langchain/main.py @@ -1,39 +1,44 @@ import dotenv from autoblocks.vendor.langchain import AutoblocksCallbackHandler -from langchain.chains import LLMChain -from langchain.chains import SimpleSequentialChain +from langchain.agents import AgentType +from langchain.agents import initialize_agent +from langchain.chains import LLMMathChain from langchain.llms import OpenAI -from langchain.prompts import PromptTemplate +from langchain.tools import Tool +from langchain.tools import tool +from datetime import datetime dotenv.load_dotenv(".env") -if __name__ == "__main__": - # This is an LLMChain to write a synopsis given a title of a play. - synopsis_llm = OpenAI(temperature=0.7) - synopsis_template = """You are a playwright. Given the title of a play, it is your job to write a synopsis for that title. - -Title: {title} -Playwright: This is a synopsis for the above play:""" - synopsis_prompt_template = PromptTemplate(input_variables=["title"], template=synopsis_template) - synopsis_chain = LLMChain(llm=synopsis_llm, prompt=synopsis_prompt_template) - - # This is an LLMChain to write a review of a play given a synopsis. - review_llm = OpenAI(temperature=0.7) - review_template = """You are a play critic from the New York Times. Given the synopsis of a play, it is your job to write a review for that play. +@tool +def todays_date(*args, **kwargs) -> int: + """Returns today's date""" + return datetime.now().day -Play Synopsis: -{synopsis} -Review from a New York Times play critic of the above play:""" - review_prompt_template = PromptTemplate(input_variables=["synopsis"], template=review_template) - review_chain = LLMChain(llm=review_llm, prompt=review_prompt_template) - # This is the overall chain where we run these two chains in sequence. - overall_chain = SimpleSequentialChain(chains=[synopsis_chain, review_chain]) - - # Run the chain +if __name__ == "__main__": + llm = OpenAI(temperature=0) + llm_math_chain = LLMMathChain.from_llm(llm) + tools = [ + Tool.from_function( + func=llm_math_chain.run, + name="Calculator", + description="useful for when you need to answer questions about math", + ), + todays_date, + ] + agent = initialize_agent( + tools, + llm, + agent=AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION, + ) handler = AutoblocksCallbackHandler() - review = overall_chain.run("Tragedy at sunset on the beach", callbacks=[handler]) - - print("Review:") - print(review) + output = agent.run( + "What is today's date? What is that date divided by 2?", + callbacks=[handler], + ) + + print(f"Output: {output}") + print() + print("View your trace: https://app.autoblocks.ai/explore") diff --git a/Python/langchain/poetry.lock b/Python/langchain/poetry.lock index f9fd13ac..1f17d28d 100644 --- a/Python/langchain/poetry.lock +++ b/Python/langchain/poetry.lock @@ -571,13 +571,13 @@ files = [ [[package]] name = "langchain" -version = "0.0.319" +version = "0.0.323" description = "Building applications with LLMs through composability" optional = false python-versions = ">=3.8.1,<4.0" files = [ - {file = "langchain-0.0.319-py3-none-any.whl", hash = "sha256:a61448fd418ff9478f2be3477c9c92acbf6b6acd8163c58c994a6158d4d116f8"}, - {file = "langchain-0.0.319.tar.gz", hash = "sha256:4fe5025e5fd48dcf8e02107fefe173ba997af3c8960871cc4a4467e24bb89375"}, + {file = "langchain-0.0.323-py3-none-any.whl", hash = "sha256:8c305c8d162262439b0cb73a6621c6ae8b3abde56d45c561a6b88709567cc765"}, + {file = "langchain-0.0.323.tar.gz", hash = "sha256:320116337933fdda48911e84f46c2d71e74eba647c05922a117c71669ccee9e2"}, ] [package.dependencies] @@ -738,6 +738,47 @@ files = [ {file = "mypy_extensions-1.0.0.tar.gz", hash = "sha256:75dbf8955dc00442a438fc4d0666508a9a97b6bd41aa2f0ffe9d2f2725af0782"}, ] +[[package]] +name = "numexpr" +version = "2.8.7" +description = "Fast numerical expression evaluator for NumPy" +optional = false +python-versions = ">=3.9" +files = [ + {file = "numexpr-2.8.7-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:d88531ffea3ea9287e8a1665c6a2d0206d3f4660d5244423e2a134a7f0ce5fba"}, + {file = "numexpr-2.8.7-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:db1065ba663a854115cf1f493afd7206e2efcef6643129e8061e97a51ad66ebb"}, + {file = "numexpr-2.8.7-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:a4546416004ff2e7eb9cf52c2d7ab82732b1b505593193ee9f93fa770edc5230"}, + {file = "numexpr-2.8.7-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:cb2f473fdfd09d17db3038e34818d05b6bc561a36785aa927d6c0e06bccc9911"}, + {file = "numexpr-2.8.7-cp310-cp310-musllinux_1_1_x86_64.whl", hash = "sha256:5496fc9e3ae214637cbca1ab556b0e602bd3afe9ff4c943a29c482430972cda8"}, + {file = "numexpr-2.8.7-cp310-cp310-win32.whl", hash = "sha256:d43f1f0253a6f2db2f76214e6f7ae9611b422cba3f7d4c86415d7a78bbbd606f"}, + {file = "numexpr-2.8.7-cp310-cp310-win_amd64.whl", hash = "sha256:cf5f112bce5c5966c47cc33700bc14ce745c8351d437ed57a9574fff581f341a"}, + {file = "numexpr-2.8.7-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:32934d51b5bc8a6636436326da79ed380e2f151989968789cf65b1210572cb46"}, + {file = "numexpr-2.8.7-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:f021ac93cb3dd5d8ba2882627b615b1f58cb089dcc85764c6fbe7a549ed21b0c"}, + {file = "numexpr-2.8.7-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:dccf572763517db6562fb7b17db46aacbbf62a9ca0a66672872f4f71aee7b186"}, + {file = "numexpr-2.8.7-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:11121b14ee3179bade92e823f25f1b94e18716d33845db5081973331188c3338"}, + {file = "numexpr-2.8.7-cp311-cp311-musllinux_1_1_x86_64.whl", hash = "sha256:81451962d4145a46dba189df65df101d4d1caddb6efe6ebfe05982cd9f62b2cf"}, + {file = "numexpr-2.8.7-cp311-cp311-win32.whl", hash = "sha256:da55ba845b847cc33c4bf81cee4b1bddfb0831118cabff8db62888ab8697ec34"}, + {file = "numexpr-2.8.7-cp311-cp311-win_amd64.whl", hash = "sha256:fd93b88d5332069916fa00829ea1b972b7e73abcb1081eee5c905a514b8b59e3"}, + {file = "numexpr-2.8.7-cp312-cp312-macosx_10_9_x86_64.whl", hash = "sha256:5340d2c86d83f52e1a3e7fd97c37d358ae99af9de316bdeeab2565b9b1e622ca"}, + {file = "numexpr-2.8.7-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:f3bdf8cbc00c77a46230c765d242f92d35905c239b20c256c48dbac91e49f253"}, + {file = "numexpr-2.8.7-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:d46c47e361fa60966a3339cb4f463ae6151ce7d78ed38075f06e8585d2c8929f"}, + {file = "numexpr-2.8.7-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:a371cfc1670a18eea2d5c70abaa95a0e8824b70d28da884bad11931266e3a0ca"}, + {file = "numexpr-2.8.7-cp312-cp312-musllinux_1_1_x86_64.whl", hash = "sha256:47a249cecd1382d482a5bf1fac0d11392fb2ed0f7d415ebc4cd901959deb1ec9"}, + {file = "numexpr-2.8.7-cp312-cp312-win32.whl", hash = "sha256:b8a5b2c21c26b62875bf819d375d798b96a32644e3c28bd4ce7789ed1fb489da"}, + {file = "numexpr-2.8.7-cp312-cp312-win_amd64.whl", hash = "sha256:f29f4d08d9b0ed6fa5d32082971294b2f9131b8577c2b7c36432ed670924313f"}, + {file = "numexpr-2.8.7-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:4ecaa5be24cf8fa0f00108e9dfa1021b7510e9dd9d159b8d8bc7c7ddbb995b31"}, + {file = "numexpr-2.8.7-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:3a84284e0a407ca52980fd20962e89aff671c84cd6e73458f2e29ea2aa206356"}, + {file = "numexpr-2.8.7-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:e838289e3b7bbe100b99e35496e6cc4cc0541c2207078941ee5a1d46e6b925ae"}, + {file = "numexpr-2.8.7-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:0983052f308ea75dd232eb7f4729eed839db8fe8d82289940342b32cc55b15d0"}, + {file = "numexpr-2.8.7-cp39-cp39-musllinux_1_1_x86_64.whl", hash = "sha256:8bf005acd7f1985c71b1b247aaac8950d6ea05a0fe0bbbbf3f96cd398b136daa"}, + {file = "numexpr-2.8.7-cp39-cp39-win32.whl", hash = "sha256:56ec95f8d1db0819e64987dcf1789acd500fa4ea396eeabe4af6efdcb8902d07"}, + {file = "numexpr-2.8.7-cp39-cp39-win_amd64.whl", hash = "sha256:c7bf60fc1a9c90a9cb21c4c235723e579bff70c8d5362228cb2cf34426104ba2"}, + {file = "numexpr-2.8.7.tar.gz", hash = "sha256:596eeb3bbfebc912f4b6eaaf842b61ba722cebdb8bc42dfefa657d3a74953849"}, +] + +[package.dependencies] +numpy = ">=1.13.3" + [[package]] name = "numpy" version = "1.25.2" @@ -1374,4 +1415,4 @@ multidict = ">=4.0" [metadata] lock-version = "2.0" python-versions = "^3.9" -content-hash = "b38e399c7d1a8bfde595776adfb6efce567ae2e1d896a6a9627a83ece05bc451" +content-hash = "4f73ba3982761f7137fc2881aa9be50f72fdf1d8afe70624cfee1d5f74d30723" diff --git a/Python/langchain/pyproject.toml b/Python/langchain/pyproject.toml index c8550fdb..35797ed0 100644 --- a/Python/langchain/pyproject.toml +++ b/Python/langchain/pyproject.toml @@ -10,5 +10,6 @@ readme = "README.md" python = "^3.9" autoblocksai = "^0.0.11" python-dotenv = "^1.0.0" -langchain = "^0.0.319" openai = "^0.28.1" +numexpr = "^2.8.7" +langchain = "^0.0.323"