YouTube videos | 🦜🔗 Langchain

uSs4) by [NextGen Machines](https://www.youtube.com/@MayankGupta-kb5yc) - ⛓ [Use ANY language in LangSmith with REST](https://youtu.be/7BL0GEdMmgY?si=iXfOEdBLqXF6hqRM) by [Nerding I/O](https://www.youtube.com/@nerding_io) - ⛓ [How to Leverage the Full Potential of LLMs for Your Business with Langchain - Leon Ruddat](https://youtu.be/vZmoEa7oWMg?si=ZhMmydq7RtkZd56Q) by [PyData](https://www.youtube.com/@PyDataTV) - ⛓ [ChatCSV App: Chat with CSV files using LangChain and Llama 2](https://youtu.be/PvsMg6jFs8E?si=Qzg5u5gijxj933Ya) by [Muhammad Moin](https://www.youtube.com/@muhammadmoinfaisal) ###[Prompt Engineering and LangChain](https://www.youtube.com/watch?v=muXbPpG_ys4&list=PLEJK-H61Xlwzm5FYLDdKt_6yibO33zoMW) by [Venelin Valkov](https://www.youtube.com/@venelin_valkov)[](#prompt-engineering-and-langchain-by-venelin-valkov) - [Getting Started with LangChain: Load Custom Data, Run OpenAI Models, Embeddings and ChatGPT](https://www.youtube.com/watch?v=muXbPpG_ys4) - [Loaders, Indexes & Vectorstores in LangChain: Question Answering on PDF files with ChatGPT](https://www.youtube.com/watch?v=FQnvfR8Dmr0) - [LangChain Models: ChatGPT, Flan Alpaca, OpenAI Embeddings, Prompt Templates & Streaming](https://www.youtube.com/watch?v=zy6LiK5F5-s) - [LangChain Chains: Use ChatGPT to Build Conversational Agents, Summaries and Q&A on Text With LLMs](https://www.youtube.com/watch?v=h1tJZQPcimM) - [Analyze Custom CSV Data with GPT-4 using Langchain](https://www.youtube.com/watch?v=Ew3sGdX8at4) - [Build ChatGPT Chatbots with LangChain Memory: Understanding and Implementing Memory in Conversations](https://youtu.be/CyuUlf54wTs) ⛓ icon marks a new addition [last update 2023-09-21]


Community navigator | 🦜🔗 Langchain

Main:  #Community navigator Hi! Thanks for being here. We’re lucky to have a community of so many passionate developers building with LangChain–we have so much to teach and learn from each other. Community members contribute code, host meetups, write blog posts, amplify each other’s work, become each other's customers and collaborators, and so much more.  Whether you’re new to LangChain, looking to go deeper, or just want to get more exposure to the world of building with LLMs, this page can point you in the right direction.  - 🦜 Contribute to LangChain  - 🌍 Meetups, Events, and Hackathons  - 📣 Help Us Amplify Your Work  - 💬 Stay in the loop  #🦜 Contribute to LangChain LangChain is the product of over 5,000+ contributions by 1,500+ contributors, and there is **still** so much to do together. Here are some ways to get involved:  - [Open a pull request](https://github.com/langchain-ai/langchain/issues): We’d appreciate all forms of contributions–new features, infrastructure improvements, better documentation, bug fixes, etc. If you have an improvement or an idea, we’d love to work on it with you. - [Read our contributor guidelines:](https://github.com/langchain-ai/langchain/blob/bbd22b9b761389a5e40fc45b0570e1830aabb707/.github/CONTRIBUTING.md) We ask contributors to follow a ["fork and pull request"](https://docs.github.com/en/get-started/quickstart/contributing-to-projects) workflow, run a few local checks for formatting, linting, and testing before submitting, and follow certain documentation and testing conventions. - First time contributor? [Try one of these PRs with the “good first issue” tag](https://github.com/langchain-ai/langchain/contribute). - Become an expert: Our experts help the community by answering product questions in Discord. If that’s a role you’d like to play, we’d be so grateful! (And we have some special experts-only goodies/perks we can tell you more about). Send us an email to introduce yourself at [hello@langchain.dev](mailto:hello@langchain.dev) and we’ll take it from there! - Integrate with LangChain: If your product integrates with LangChain–or aspires to–we want to help make sure the experience is as smooth as possible for you and end users. Send us an email at [hello@langchain.dev](mailto:hello@langchain.dev) and tell us what you’re working on. - Become an Integration Maintainer: Partner with our team to ensure your integration stays up-to-date and talk directly with users (and answer their inquiries) in our Discord. Introduce yourself at [hello@langchain.dev](mailto:hello@langchain.dev) if you’d like to explore this role. #🌍 Meetups, Events, and Hackathons One of our favorite things about working in AI is how much enthusiasm there is for building together. We want to help make that as easy and impactful for you as possible!  - Find a meetup, hackathon, or webinar: You can find the one for you on our [global events calendar](https://mirror-feeling-d80.notion.site/0bc81da76a184297b86ca8fc782ee9a3?v=0d80342540df465396546976a50cfb3f). - Submit an event to our calendar: Email us at [events@langchain.dev](mailto:events@langchain.dev) with a link to your event page! We can also help you spread the word with our local communities. - Host a meetup: If you want to bring a group of builders together, we want to help! We can publicize your event on our event calendar/Twitter, share it with our local communities in Discord, send swag, or potentially hook you up with a sponsor. Email us at [events@langchain.dev](mailto:events@langchain.dev) to tell us about your event! - Become a meetup sponsor: We often hear from groups of builders that want to get together, but are blocked or limited on some dimension (space to host, budget for snacks, prizes to distribute, etc.). If you’d like to help, send us an email to [events@langchain.dev](mailto:events@langchain.dev) we can share more about how it works! - Speak at an event: Meetup hosts are always looking for great speakers, presenters, and panelists. If you’d like to do that at an event, send us an email to [hello@langchain.dev](mailto:hello@langchain.dev) with more information about yourself, what you want to talk about, and what city you’re based in and we’ll try to match you with an upcoming event! - Tell us about your LLM community: If you host or participate in a community that would welcome support from LangChain and/or our team, send us an email at [hello@langchain.dev](mailto:hello@langchain.dev) and let us know how we can help. #📣 Help Us Amplify Your Work If you’re working on something you’re proud of, and think the LangChain community would benefit from knowing about it, we want to help you show it off.  - Post about your work and mention us: We love hanging out on Twitter to see what people in the space are talking about and working on. If you tag [@langchainai](https://twitter.com/LangChainAI), we’ll almost certainly see it and can show you some love. - Publish something on our blog: If you’re writing about your experience building with LangChain, we’d love to post (or crosspost) it on our blog! E-mail [hello@langchain.dev](mailto:hello@langchain.dev) with a draft of your post! Or even an idea for something you want to write about. - Get your product onto our [integrations hub](https://integrations.langchain.com/): Many developers take advantage of our seamless integrations with other products, and come to our integrations hub to find out who those are. If you want to get your product up there, tell us about it (and how it works with LangChain) at [hello@langchain.dev](mailto:hello@langchain.dev). #☀ Stay in the loop Here’s where our team hangs out, talks shop, spotlights cool work, and shares what we’re up to. We’d love to see you there too.  - [Twitter](https://twitter.com/LangChainAI): We post about what we’re working on and what cool things we’re seeing in the space. If you tag @langchainai in your post, we’ll almost certainly see it, and can show you some love! - [Discord](https://discord.gg/6adMQxSpJS): conn


Community navigator | 🦜🔗 Langchain

ect with over 30,000 developers who are building with LangChain. - [GitHub](https://github.com/langchain-ai/langchain): Open pull requests, contribute to a discussion, and/or contribute - [Subscribe to our bi-weekly Release Notes](https://6w1pwbss0py.typeform.com/to/KjZB1auB): a twice/month email roundup of the coolest things going on in our orbit


Contributing to LangChain | 🦜🔗 Langchain

Main:  On this page #Contributing to LangChain Hi there! Thank you for even being interested in contributing to LangChain. As an open-source project in a rapidly developing field, we are extremely open to contributions, whether they involve new features, improved infrastructure, better documentation, or bug fixes.  ##🗺 Guidelines[](#-guidelines) ###👩💻 Contributing Code[](#-contributing-code) To contribute to this project, please follow the ["fork and pull request"](https://docs.github.com/en/get-started/quickstart/contributing-to-projects) workflow. Please do not try to push directly to this repo unless you are a maintainer.  Please follow the checked-in pull request template when opening pull requests. Note related issues and tag relevant maintainers.  Pull requests cannot land without passing the formatting, linting, and testing checks first. See [Testing](#testing) and [Formatting and Linting](#formatting-and-linting) for how to run these checks locally.  It's essential that we maintain great documentation and testing. If you:  - Fix a bug - Add a relevant unit or integration test when possible. These live in tests/unit_tests and tests/integration_tests. - Make an improvement - Update any affected example notebooks and documentation. These live in docs. - Update unit and integration tests when relevant. - Add a feature - Add a demo notebook in docs/docs/. - Add unit and integration tests. We are a small, progress-oriented team. If there's something you'd like to add or change, opening a pull request is the best way to get our attention.  ###🚩GitHub Issues[](#github-issues) Our [issues](https://github.com/langchain-ai/langchain/issues) page is kept up to date with bugs, improvements, and feature requests.  There is a taxonomy of labels to help with sorting and discovery of issues of interest. Please use these to help organize issues.  If you start working on an issue, please assign it to yourself.  If you are adding an issue, please try to keep it focused on a single, modular bug/improvement/feature. If two issues are related, or blocking, please link them rather than combining them.  We will try to keep these issues as up-to-date as possible, though with the rapid rate of development in this field some may get out of date. If you notice this happening, please let us know.  ###🙋Getting Help[](#getting-help) Our goal is to have the simplest developer setup possible. Should you experience any difficulty getting setup, please contact a maintainer! Not only do we want to help get you unblocked, but we also want to make sure that the process is smooth for future contributors.  In a similar vein, we do enforce certain linting, formatting, and documentation standards in the codebase. If you are finding these difficult (or even just annoying) to work with, feel free to contact a maintainer for help - we do not want these to get in the way of getting good code into the codebase.  ##🚀 Quick Start[](#-quick-start) This quick start guide explains how to run the repository locally. For a [development container](https://containers.dev/), see the [.devcontainer folder](https://github.com/langchain-ai/langchain/tree/master/.devcontainer).  ###Dependency Management: Poetry and other env/dependency managers[](#dependency-management-poetry-and-other-envdependency-managers) This project utilizes [Poetry](https://python-poetry.org/) v1.6.1+ as a dependency manager.  ❗Note: Before installing Poetry, if you use Conda, create and activate a new Conda env (e.g. conda create -n langchain python=3.9)  Install Poetry: [documentation on how to install it](https://python-poetry.org/docs/#installation).  ❗Note: If you use Conda or Pyenv as your environment/package manager, after installing Poetry, tell Poetry to use the virtualenv python environment (poetry config virtualenvs.prefer-active-python true)  ###Core vs. Experimental[](#core-vs-experimental) This repository contains two separate projects:  - langchain: core langchain code, abstractions, and use cases. - langchain.experimental: see the [Experimental README](https://github.com/langchain-ai/langchain/tree/master/libs/experimental/README.md) for more information. Each of these has its own development environment. Docs are run from the top-level makefile, but development is split across separate test & release flows.  For this quickstart, start with langchain core:  cd libs/langchain  ###Local Development Dependencies[](#local-development-dependencies) Install langchain development requirements (for running langchain, running examples, linting, formatting, tests, and coverage):  poetry install --with test  Then verify dependency installation:  make test  If the tests don't pass, you may need to pip install additional dependencies, such as numexpr and openapi_schema_pydantic.  If during installation you receive a WheelFileValidationError for debugpy, please make sure you are running Poetry v1.6.1+. This bug was present in older versions of Poetry (e.g. 1.4.1) and has been resolved in newer releases. If you are still seeing this bug on v1.6.1, you may also try disabling "modern installation" (poetry config installer.modern-installation false) and re-installing requirements. See [this debugpy issue](https://github.com/microsoft/debugpy/issues/1246) for more details.  ###Testing[](#testing) some test dependencies are optional; see section about optional dependencies.  Unit tests cover modular logic that does not require calls to outside APIs. If you add new logic, please add a unit test.  To run unit tests:  make test  To run unit tests in Docker:  make docker_tests  There are also [integration tests and code-coverage](https://github.com/langchain-ai/langchain/tree/master/libs/langchain/tests/README.md) available.  ###Formatting and Linting[](#formatting-and-linting) Run these locally before submitting a PR; the CI system will check also.  ####Code Formatting[](#code-formatting) Formatting for this project is done via [ruff](https://docs.astral.sh/ru


ff/rules/).  To run formatting for docs, cookbook and templates:  make format  To run formatting for a library, run the same command from the relevant library directory:  cd libs/{LIBRARY} make format  Additionally, you can run the formatter only on the files that have been modified in your current branch as compared to the master branch using the format_diff command:  make format_diff  This is especially useful when you have made changes to a subset of the project and want to ensure your changes are properly formatted without affecting the rest of the codebase.  ####Linting[](#linting) Linting for this project is done via a combination of [ruff](https://docs.astral.sh/ruff/rules/) and [mypy](http://mypy-lang.org/).  To run linting for docs, cookbook and templates:  make lint  To run linting for a library, run the same command from the relevant library directory:  cd libs/{LIBRARY} make lint  In addition, you can run the linter only on the files that have been modified in your current branch as compared to the master branch using the lint_diff command:  make lint_diff  This can be very helpful when you've made changes to only certain parts of the project and want to ensure your changes meet the linting standards without having to check the entire codebase.  We recognize linting can be annoying - if you do not want to do it, please contact a project maintainer, and they can help you with it. We do not want this to be a blocker for good code getting contributed.  ####Spellcheck[](#spellcheck) Spellchecking for this project is done via [codespell](https://github.com/codespell-project/codespell). Note that codespell finds common typos, so it could have false-positive (correctly spelled but rarely used) and false-negatives (not finding misspelled) words.  To check spelling for this project:  make spell_check  To fix spelling in place:  make spell_fix  If codespell is incorrectly flagging a word, you can skip spellcheck for that word by adding it to the codespell config in the pyproject.toml file.  [tool.codespell] ... # Add here: ignore-words-list = 'momento,collison,ned,foor,reworkd,parth,whats,aapply,mysogyny,unsecure'  ##Working with Optional Dependencies[](#working-with-optional-dependencies) Langchain relies heavily on optional dependencies to keep the Langchain package lightweight.  You only need to add a new dependency if a unit test relies on the package. If your package is only required for integration tests, then you can skip these steps and leave all pyproject.toml and poetry.lock files alone.  If you're adding a new dependency to Langchain, assume that it will be an optional dependency, and that most users won't have it installed.  Users who do not have the dependency installed should be able to import your code without any side effects (no warnings, no errors, no exceptions).  To introduce the dependency to the pyproject.toml file correctly, please do the following:  - Add the dependency to the main group as an optional dependency poetry add --optional [package_name]  - Open pyproject.toml and add the dependency to the extended_testing extra - Relock the poetry file to update the extra. poetry lock --no-update  - Add a unit test that the very least attempts to import the new code. Ideally, the unit test makes use of lightweight fixtures to test the logic of the code. - Please use the @pytest.mark.requires(package_name) decorator for any tests that require the dependency. ##Adding a Jupyter Notebook[](#adding-a-jupyter-notebook) If you are adding a Jupyter Notebook example, you'll want to install the optional dev dependencies.  To install dev dependencies:  poetry install --with dev  Launch a notebook:  poetry run jupyter notebook  When you run poetry install, the langchain package is installed as editable in the virtualenv, so your new logic can be imported into the notebook.  ##Documentation[](#documentation) While the code is split between langchain and langchain.experimental, the documentation is one holistic thing. This covers how to get started contributing to documentation.  From the top-level of this repo, install documentation dependencies:  poetry install  ###Contribute Documentation[](#contribute-documentation) The docs directory contains Documentation and API Reference.  Documentation is built using [Docusaurus 2](https://docusaurus.io/).  API Reference are largely autogenerated by [sphinx](https://www.sphinx-doc.org/en/master/) from the code. For that reason, we ask that you add good documentation to all classes and methods.  Similar to linting, we recognize documentation can be annoying. If you do not want to do it, please contact a project maintainer, and they can help you with it. We do not want this to be a blocker for good code getting contributed.  ###Build Documentation Locally[](#build-documentation-locally) In the following commands, the prefix api_ indicates that those are operations for the API Reference.  Before building the documentation, it is always a good idea to clean the build directory:  make docs_clean make api_docs_clean  Next, you can build the documentation as outlined below:  make docs_build make api_docs_build  Finally, run the link checker to ensure all links are valid:  make docs_linkcheck make api_docs_linkcheck  ###Verify Documentation changes[](#verify-documentation-changes) After pushing documentation changes to the repository, you can preview and verify that the changes are what you wanted by clicking the View deployment or Visit Preview buttons on the pull request Conversation page. This will take you to a preview of the documentation changes. This preview is created by [Vercel](https://vercel.com/docs/getting-started-with-vercel).  ##🏭 Release Process[](#-release-process) As of now, LangChain has an ad hoc release process: releases are cut with high frequency by a developer and published to [PyPI](https://pypi.org/project/langchain/).  LangChain follows the [semver](https://semver.org/) versioning standard. However, as pre-1.


0 software, even patch releases may contain [non-backwards-compatible changes](https://semver.org/#spec-item-4).  ###🌟 Recognition[](#-recognition) If your contribution has made its way into a release, we will want to give you credit on Twitter (only if you want though)! If you have a Twitter account you would like us to mention, please let us know in the PR or through another means.


LangChain Expression Language (LCEL) | 🦜🔗 Langchain

Main:  #LangChain Expression Language (LCEL) LangChain Expression Language, or LCEL, is a declarative way to easily compose chains together. LCEL was designed from day 1 to support putting prototypes in production, with no code changes, from the simplest “prompt + LLM” chain to the most complex chains (we’ve seen folks successfully run LCEL chains with 100s of steps in production). To highlight a few of the reasons you might want to use LCEL:  Streaming support When you build your chains with LCEL you get the best possible time-to-first-token (time elapsed until the first chunk of output comes out). For some chains this means eg. we stream tokens straight from an LLM to a streaming output parser, and you get back parsed, incremental chunks of output at the same rate as the LLM provider outputs the raw tokens.  Async support Any chain built with LCEL can be called both with the synchronous API (eg. in your Jupyter notebook while prototyping) as well as with the asynchronous API (eg. in a [LangServe](/docs/langsmith) server). This enables using the same code for prototypes and in production, with great performance, and the ability to handle many concurrent requests in the same server.  Optimized parallel execution Whenever your LCEL chains have steps that can be executed in parallel (eg if you fetch documents from multiple retrievers) we automatically do it, both in the sync and the async interfaces, for the smallest possible latency.  Retries and fallbacks Configure retries and fallbacks for any part of your LCEL chain. This is a great way to make your chains more reliable at scale. We’re currently working on adding streaming support for retries/fallbacks, so you can get the added reliability without any latency cost.  Access intermediate results For more complex chains it’s often very useful to access the results of intermediate steps even before the final output is produced. This can be used to let end-users know something is happening, or even just to debug your chain. You can stream intermediate results, and it’s available on every [LangServe](/docs/langserve) server.  Input and output schemas Input and output schemas give every LCEL chain Pydantic and JSONSchema schemas inferred from the structure of your chain. This can be used for validation of inputs and outputs, and is an integral part of LangServe.  Seamless LangSmith tracing integration As your chains get more and more complex, it becomes increasingly important to understand what exactly is happening at every step. With LCEL, all steps are automatically logged to [LangSmith](/docs/langsmith/) for maximum observability and debuggability.  Seamless LangServe deployment integration Any chain created with LCEL can be easily deployed using [LangServe](/docs/langserve).


Cookbook | 🦜🔗 Langchain

Main:  #Cookbook Example code for accomplishing common tasks with the LangChain Expression Language (LCEL). These examples show how to compose different Runnable (the core LCEL interface) components to achieve various tasks. If you're just getting acquainted with LCEL, the [Prompt + LLM](/docs/expression_language/cookbook/prompt_llm_parser) page is a good place to start.  [ ##📄 Prompt + LLM The most common and valuable composition is taking:  ](/docs/expression_language/cookbook/prompt_llm_parser) [ ##📄 RAG Let's look at adding in a retrieval step to a prompt and LLM, which adds up to a "retrieval-augmented generation" chain  ](/docs/expression_language/cookbook/retrieval) [ ##📄 Multiple chains Runnables can easily be used to string together multiple Chains  ](/docs/expression_language/cookbook/multiple_chains) [ ##📄 Querying a SQL DB We can replicate our SQLDatabaseChain with Runnables.  ](/docs/expression_language/cookbook/sql_db) [ ##📄 Agents You can pass a Runnable into an agent.  ](/docs/expression_language/cookbook/agent) [ ##📄 Code writing Example of how to use LCEL to write Python code.  ](/docs/expression_language/cookbook/code_writing) [ ##📄 Routing by semantic similarity With LCEL you can easily add custom routing logic to your chain to dynamically determine the chain logic based on user input. All you need to do is define a function that given an input returns a Runnable.  ](/docs/expression_language/cookbook/embedding_router) [ ##📄 Adding memory This shows how to add memory to an arbitrary chain. Right now, you can use the memory classes but need to hook it up manually  ](/docs/expression_language/cookbook/memory) [ ##📄 Adding moderation This shows how to add in moderation (or other safeguards) around your LLM application.  ](/docs/expression_language/cookbook/moderation) [ ##📄 Managing prompt size Agents dynamically call tools. The results of those tool calls are added back to the prompt, so that the agent can plan the next action. Depending on what tools are being used and how they're being called, the agent prompt can easily grow larger than the model context window.  ](/docs/expression_language/cookbook/prompt_size) [ ##📄 Using tools You can use any Tools with Runnables easily.  ](/docs/expression_language/cookbook/tools)


Agents | 🦜🔗 Langchain

Main:  #Agents You can pass a Runnable into an agent.  from langchain.agents import AgentExecutor, XMLAgent, tool from langchain.chat_models import ChatAnthropic  model = ChatAnthropic(model="claude-2")  @tool def search(query: str) -> str:     """Search things about current events."""     return "32 degrees"  tool_list = [search]  # Get prompt to use prompt = XMLAgent.get_default_prompt()  # Logic for going from intermediate steps to a string to pass into model # This is pretty tied to the prompt def convert_intermediate_steps(intermediate_steps):     log = ""     for action, observation in intermediate_steps:         log += (             f"<tool>{action.tool}</tool><tool_input>{action.tool_input}"             f"</tool_input><observation>{observation}</observation>"         )     return log   # Logic for converting tools to string to go in prompt def convert_tools(tools):     return "\n".join([f"{tool.name}: {tool.description}" for tool in tools])  Building an agent from a runnable usually involves a few things:  - Data processing for the intermediate steps. These need to represented in a way that the language model can recognize them. This should be pretty tightly coupled to the instructions in the prompt  - The prompt itself  - The model, complete with stop tokens if needed  - The output parser - should be in sync with how the prompt specifies things to be formatted.  agent = (     {         "question": lambda x: x["question"],         "intermediate_steps": lambda x: convert_intermediate_steps(             x["intermediate_steps"]         ),     }     | prompt.partial(tools=convert_tools(tool_list))     | model.bind(stop=["</tool_input>", "</final_answer>"])     | XMLAgent.get_default_output_parser() )  agent_executor = AgentExecutor(agent=agent, tools=tool_list, verbose=True)  agent_executor.invoke({"question": "whats the weather in New york?"})                > Entering new AgentExecutor chain...      <tool>search</tool>     <tool_input>weather in new york32 degrees          <final_answer>The weather in New York is 32 degrees          > Finished chain.          {'question': 'whats the weather in New york?',      'output': 'The weather in New York is 32 degrees'}


Code writing | 🦜🔗 Langchain

Main:  #Code writing Example of how to use LCEL to write Python code.  from langchain.chat_models import ChatOpenAI from langchain.prompts import (     ChatPromptTemplate, ) from langchain.schema.output_parser import StrOutputParser from langchain_experimental.utilities import PythonREPL  template = """Write some python code to solve the user's problem.   Return only python code in Markdown format, e.g.:  ```python .... ```""" prompt = ChatPromptTemplate.from_messages([("system", template), ("human", "{input}")])  model = ChatOpenAI()  def _sanitize_output(text: str):     _, after = text.split("```python")     return after.split("```")[0]  chain = prompt | model | StrOutputParser() | _sanitize_output | PythonREPL().run  chain.invoke({"input": "whats 2 plus 2"})      Python REPL can execute arbitrary code. Use with caution.          '4\n'


Routing by semantic similarity | 🦜🔗 Langchain

Main:  #Routing by semantic similarity With LCEL you can easily add [custom routing logic](/docs/expression_language/how_to/routing#using-a-custom-function) to your chain to dynamically determine the chain logic based on user input. All you need to do is define a function that given an input returns a Runnable.  One especially useful technique is to use embeddings to route a query to the most relevant prompt. Here's a very simple example.  from langchain.chat_models import ChatOpenAI from langchain.embeddings import OpenAIEmbeddings from langchain.prompts import PromptTemplate from langchain.schema.output_parser import StrOutputParser from langchain.schema.runnable import RunnableLambda, RunnablePassthrough from langchain.utils.math import cosine_similarity  physics_template = """You are a very smart physics professor. \ You are great at answering questions about physics in a concise and easy to understand manner. \ When you don't know the answer to a question you admit that you don't know.  Here is a question: {query}"""  math_template = """You are a very good mathematician. You are great at answering math questions. \ You are so good because you are able to break down hard problems into their component parts, \ answer the component parts, and then put them together to answer the broader question.  Here is a question: {query}"""  embeddings = OpenAIEmbeddings() prompt_templates = [physics_template, math_template] prompt_embeddings = embeddings.embed_documents(prompt_templates)   def prompt_router(input):     query_embedding = embeddings.embed_query(input["query"])     similarity = cosine_similarity([query_embedding], prompt_embeddings)[0]     most_similar = prompt_templates[similarity.argmax()]     print("Using MATH" if most_similar == math_template else "Using PHYSICS")     return PromptTemplate.from_template(most_similar)   chain = (     {"query": RunnablePassthrough()}     | RunnableLambda(prompt_router)     | ChatOpenAI()     | StrOutputParser() )  print(chain.invoke("What's a black hole"))      Using PHYSICS     A black hole is a region in space where gravity is extremely strong, so strong that nothing, not even light, can escape its gravitational pull. It is formed when a massive star collapses under its own gravity during a supernova explosion. The collapse causes an incredibly dense mass to be concentrated in a small volume, creating a gravitational field that is so intense that it warps space and time. Black holes have a boundary called the event horizon, which marks the point of no return for anything that gets too close. Beyond the event horizon, the gravitational pull is so strong that even light cannot escape, hence the name "black hole." While we have a good understanding of black holes, there is still much to learn, especially about what happens inside them.  print(chain.invoke("What's a path integral"))      Using MATH     Thank you for your kind words! I will do my best to break down the concept of a path integral for you.          In mathematics and physics, a path integral is a mathematical tool used to calculate the probability amplitude or wave function of a particle or system of particles. It was introduced by Richard Feynman and is an integral over all possible paths that a particle can take to go from an initial state to a final state.          To understand the concept better, let's consider an example. Suppose we have a particle moving from point A to point B in space. Classically, we would describe this particle's motion using a definite trajectory, but in quantum mechanics, particles can simultaneously take multiple paths from A to B.          The path integral formalism considers all possible paths that the particle could take and assigns a probability amplitude to each path. These probability amplitudes are then added up, taking into account the interference effects between different paths.          To calculate a path integral, we need to define an action, which is a mathematical function that describes the behavior of the system. The action is usually expressed in terms of the particle's position, velocity, and time.          Once we have the action, we can write down the path integral as an integral over all possible paths. Each path is weighted by a factor determined by the action and the principle of least action, which states that a particle takes a path that minimizes the action.          Mathematically, the path integral is expressed as:          ∫ e^(iS/ħ) D[x(t)]          Here, S is the action, ħ is the reduced Planck's constant, and D[x(t)] represents the integration over all possible paths x(t) of the particle.          By evaluating this integral, we can obtain the probability amplitude for the particle to go from the initial state to the final state. The absolute square of this amplitude gives us the probability of finding the particle in a particular state.          Path integrals have proven to be a powerful tool in various areas of physics, including quantum mechanics, quantum field theory, and statistical mechanics. They allow us to study complex systems and calculate probabilities that are difficult to obtain using other methods.          I hope this explanation helps you understand the concept of a path integral. If you have any further questions, feel free to ask!


Adding memory | 🦜🔗 Langchain

Main:  #Adding memory This shows how to add memory to an arbitrary chain. Right now, you can use the memory classes but need to hook it up manually  from operator import itemgetter  from langchain.chat_models import ChatOpenAI from langchain.memory import ConversationBufferMemory from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder from langchain.schema.runnable import RunnableLambda, RunnablePassthrough  model = ChatOpenAI() prompt = ChatPromptTemplate.from_messages(     [         ("system", "You are a helpful chatbot"),         MessagesPlaceholder(variable_name="history"),         ("human", "{input}"),     ] )  memory = ConversationBufferMemory(return_messages=True)  memory.load_memory_variables({})      {'history': []}  chain = (     RunnablePassthrough.assign(         history=RunnableLambda(memory.load_memory_variables) | itemgetter("history")     )     | prompt     | model )  inputs = {"input": "hi im bob"} response = chain.invoke(inputs) response      AIMessage(content='Hello Bob! How can I assist you today?', additional_kwargs={}, example=False)  memory.save_context(inputs, {"output": response.content})  memory.load_memory_variables({})      {'history': [HumanMessage(content='hi im bob', additional_kwargs={}, example=False),       AIMessage(content='Hello Bob! How can I assist you today?', additional_kwargs={}, example=False)]}  inputs = {"input": "whats my name"} response = chain.invoke(inputs) response      AIMessage(content='Your name is Bob.', additional_kwargs={}, example=False)


Adding moderation | 🦜🔗 Langchain

Main:  #Adding moderation This shows how to add in moderation (or other safeguards) around your LLM application.  from langchain.chains import OpenAIModerationChain from langchain.llms import OpenAI from langchain.prompts import ChatPromptTemplate  moderate = OpenAIModerationChain()  model = OpenAI() prompt = ChatPromptTemplate.from_messages([("system", "repeat after me: {input}")])  chain = prompt | model  chain.invoke({"input": "you are stupid"})      '\n\nYou are stupid.'  moderated_chain = chain | moderate  moderated_chain.invoke({"input": "you are stupid"})      {'input': '\n\nYou are stupid',      'output': "Text was found that violates OpenAI's content policy."}


Multiple chains | 🦜🔗 Langchain

Main:  Skip to main content  🦜🔗 LangChain  Search CTRLK LangChain Expression LanguageCookbookMultiple chains On this page Multiple chains Runnables can easily be used to string together multiple Chains  from operator import itemgetter  from langchain.chat_models import ChatOpenAI from langchain.prompts import ChatPromptTemplate from langchain.schema import StrOutputParser  prompt1 = ChatPromptTemplate.from_template("what is the city {person} is from?") prompt2 = ChatPromptTemplate.from_template(     "what country is the city {city} in? respond in {language}" )  model = ChatOpenAI()  chain1 = prompt1 | model | StrOutputParser()  chain2 = (     {"city": chain1, "language": itemgetter("language")}     | prompt2     | model     | StrOutputParser() )  chain2.invoke({"person": "obama", "language": "spanish"})      'El país donde se encuentra la ciudad de Honolulu, donde nació Barack Obama, el 44º Presidente de los Estados Unidos, es Estados Unidos. Honolulu se encuentra en la isla de Oahu, en el estado de Hawái.'  from langchain.schema.runnable import RunnablePassthrough  prompt1 = ChatPromptTemplate.from_template(     "generate a {attribute} color. Return the name of the color and nothing else:" ) prompt2 = ChatPromptTemplate.from_template(     "what is a fruit of color: {color}. Return the name of the fruit and nothing else:" ) prompt3 = ChatPromptTemplate.from_template(     "what is a country with a flag that has the color: {color}. Return the name of the country and nothing else:" ) prompt4 = ChatPromptTemplate.from_template(     "What is the color of {fruit} and the flag of {country}?" )  model_parser = model | StrOutputParser()  color_generator = (     {"attribute": RunnablePassthrough()} | prompt1 | {"color": model_parser} ) color_to_fruit = prompt2 | model_parser color_to_country = prompt3 | model_parser question_generator = (     color_generator | {"fruit": color_to_fruit, "country": color_to_country} | prompt4 )  question_generator.invoke("warm")      ChatPromptValue(messages=[HumanMessage(content='What is the color of strawberry and the flag of China?', additional_kwargs={}, example=False)])  prompt = question_generator.invoke("warm") model.invoke(prompt)      AIMessage(content='The color of an apple is typically red or green. The flag of China is predominantly red with a large yellow star in the upper left corner and four smaller yellow stars surrounding it.', additional_kwargs={}, example=False)  Branching and Merging You may want the output of one component to be processed by 2 or more other components. RunnableParallels let you split or fork the chain so multiple components can process the input in parallel. Later, other components can join or merge the results to synthesize a final response. This type of chain creates a computation graph that looks like the following:       Input       / \      /   \  Branch1 Branch2      \   /       \ /       Combine  planner = (     ChatPromptTemplate.from_template("Generate an argument about: {input}")     | ChatOpenAI()     | StrOutputParser()     | {"base_response": RunnablePassthrough()} )  arguments_for = (     ChatPromptTemplate.from_template(         "List the pros or positive aspects of {base_response}"     )     | ChatOpenAI()     | StrOutputParser() ) arguments_against = (     ChatPromptTemplate.from_template(         "List the cons or negative aspects of {base_response}"     )     | ChatOpenAI()     | StrOutputParser() )  final_responder = (     ChatPromptTemplate.from_messages(         [             ("ai", "{original_response}"),             ("human", "Pros:\n{results_1}\n\nCons:\n{results_2}"),             ("system", "Generate a final response given the critique"),         ]     )     | ChatOpenAI()     | StrOutputParser() )  chain = (     planner     | {         "results_1": arguments_for,         "results_2": arguments_against,         "original_response": itemgetter("base_response"),     }     | final_responder )  chain.invoke({"input": "scrum"})      'While Scrum has its potential cons and challenges, many organizations have successfully embraced and implemented this project management framework to great effect. The cons mentioned above can be mitigated or overcome with proper training, support, and a commitment to continuous improvement. It is also important to note that not all cons may be applicable to every organization or project.\n\nFor example, while Scrum may be complex initially, with proper training and guidance, teams can quickly grasp the concepts and practices. The lack of predictability can be mitigated by implementing techniques such as velocity tracking and release planning. The limited documentation can be addressed by maintaining a balance between lightweight documentation and clear communication among team members. The dependency on team collaboration can be improved through effective communication channels and regular team-building activities.\n\nScrum can be scaled and adapted to larger projects by using frameworks like Scrum of Scrums or LeSS (Large Scale Scrum). Concerns about speed versus quality can be addressed by incorporating quality assurance practices, such as continuous integration and automated testing, into the Scrum process. Scope creep can be managed by having a well-defined and prioritized product backlog, and a strong product owner can be developed through training and mentorship.\n\nResistance to change can be overcome by providing proper education and communication to stakeholders and involving them in the decision-making process. Ultimately, the cons of Scrum can be seen as opportunities for growth and improvement, and with the right mindset and support, they can be effectively managed.\n\nIn conclusion, while Scrum may have its challenges and potential cons, the benefits and advantages it offers in terms of collaboration, flexibility, adaptability, transparency, and customer satisfaction make it a widely adopted and successful project management framework. With proper implementation


Multiple chains | 🦜🔗 Langchain

and continuous improvement, organizations can leverage Scrum to drive innovation, efficiency, and project success.'  Previous RAG Next Querying a SQL DB Community Discord Twitter GitHub Python JS/TS More Homepage Blog Copyright © 2023 LangChain, Inc.


Prompt + LLM | 🦜🔗 Langchain

Main:  On this page The most common and valuable composition is taking:  PromptTemplate / ChatPromptTemplate -> LLM / ChatModel -> OutputParser  Almost any other chains you build will use this building block.  ##PromptTemplate + LLM[](#prompttemplate--llm) The simplest composition is just combining a prompt and model to create a chain that takes user input, adds it to a prompt, passes it to a model, and returns the raw model output.  Note, you can mix and match PromptTemplate/ChatPromptTemplates and LLMs/ChatModels as you like here.  from langchain.chat_models import ChatOpenAI from langchain.prompts import ChatPromptTemplate  prompt = ChatPromptTemplate.from_template("tell me a joke about {foo}") model = ChatOpenAI() chain = prompt | model  chain.invoke({"foo": "bears"})      AIMessage(content="Why don't bears wear shoes?\n\nBecause they have bear feet!", additional_kwargs={}, example=False)  Often times we want to attach kwargs that'll be passed to each model call. Here are a few examples of that:  ###Attaching Stop Sequences[](#attaching-stop-sequences) chain = prompt | model.bind(stop=["\n"])  chain.invoke({"foo": "bears"})      AIMessage(content='Why did the bear never wear shoes?', additional_kwargs={}, example=False)  ###Attaching Function Call information[](#attaching-function-call-information) functions = [     {         "name": "joke",         "description": "A joke",         "parameters": {             "type": "object",             "properties": {                 "setup": {"type": "string", "description": "The setup for the joke"},                 "punchline": {                     "type": "string",                     "description": "The punchline for the joke",                 },             },             "required": ["setup", "punchline"],         },     } ] chain = prompt | model.bind(function_call={"name": "joke"}, functions=functions)  chain.invoke({"foo": "bears"}, config={})      AIMessage(content='', additional_kwargs={'function_call': {'name': 'joke', 'arguments': '{\n  "setup": "Why don\'t bears wear shoes?",\n  "punchline": "Because they have bear feet!"\n}'}}, example=False)  ##PromptTemplate + LLM + OutputParser[](#prompttemplate--llm--outputparser) We can also add in an output parser to easily transform the raw LLM/ChatModel output into a more workable format  from langchain.schema.output_parser import StrOutputParser  chain = prompt | model | StrOutputParser()  Notice that this now returns a string - a much more workable format for downstream tasks  chain.invoke({"foo": "bears"})      "Why don't bears wear shoes?\n\nBecause they have bear feet!"  ###Functions Output Parser[](#functions-output-parser) When you specify the function to return, you may just want to parse that directly  from langchain.output_parsers.openai_functions import JsonOutputFunctionsParser  chain = (     prompt     | model.bind(function_call={"name": "joke"}, functions=functions)     | JsonOutputFunctionsParser() )  chain.invoke({"foo": "bears"})      {'setup': "Why don't bears like fast food?",      'punchline': "Because they can't catch it!"}  from langchain.output_parsers.openai_functions import JsonKeyOutputFunctionsParser  chain = (     prompt     | model.bind(function_call={"name": "joke"}, functions=functions)     | JsonKeyOutputFunctionsParser(key_name="setup") )  chain.invoke({"foo": "bears"})      "Why don't bears wear shoes?"  ##Simplifying input[](#simplifying-input) To make invocation even simpler, we can add a RunnableParallel to take care of creating the prompt input dict for us:  from langchain.schema.runnable import RunnableParallel, RunnablePassthrough  map_ = RunnableParallel(foo=RunnablePassthrough()) chain = (     map_     | prompt     | model.bind(function_call={"name": "joke"}, functions=functions)     | JsonKeyOutputFunctionsParser(key_name="setup") )  chain.invoke("bears")      "Why don't bears wear shoes?"  Since we're composing our map with another Runnable, we can even use some syntactic sugar and just use a dict:  chain = (     {"foo": RunnablePassthrough()}     | prompt     | model.bind(function_call={"name": "joke"}, functions=functions)     | JsonKeyOutputFunctionsParser(key_name="setup") )  chain.invoke("bears")      "Why don't bears like fast food?"


Managing prompt size | 🦜🔗 Langchain

Main:  #Managing prompt size Agents dynamically call tools. The results of those tool calls are added back to the prompt, so that the agent can plan the next action. Depending on what tools are being used and how they're being called, the agent prompt can easily grow larger than the model context window.  With LCEL, it's easy to add custom functionality for managing the size of prompts within your chain or agent. Let's look at simple agent example that can search Wikipedia for information.  # !pip install langchain wikipedia  from operator import itemgetter  from langchain.agents import AgentExecutor, load_tools from langchain.agents.format_scratchpad import format_to_openai_function_messages from langchain.agents.output_parsers import OpenAIFunctionsAgentOutputParser from langchain.chat_models import ChatOpenAI from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder from langchain.prompts.chat import ChatPromptValue from langchain.tools import WikipediaQueryRun from langchain.tools.render import format_tool_to_openai_function from langchain.utilities import WikipediaAPIWrapper  wiki = WikipediaQueryRun(     api_wrapper=WikipediaAPIWrapper(top_k_results=5, doc_content_chars_max=10_000) ) tools = [wiki]  prompt = ChatPromptTemplate.from_messages(     [         ("system", "You are a helpful assistant"),         ("user", "{input}"),         MessagesPlaceholder(variable_name="agent_scratchpad"),     ] ) llm = ChatOpenAI(model="gpt-3.5-turbo")  Let's try a many-step question without any prompt size handling:  agent = (     {         "input": itemgetter("input"),         "agent_scratchpad": lambda x: format_to_openai_function_messages(             x["intermediate_steps"]         ),     }     | prompt     | llm.bind(functions=[format_tool_to_openai_function(t) for t in tools])     | OpenAIFunctionsAgentOutputParser() )  agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True) agent_executor.invoke(     {         "input": "Who is the current US president? What's their home state? What's their home state's bird? What's that bird's scientific name?"     } )                > Entering new AgentExecutor chain...          Invoking: `Wikipedia` with `List of presidents of the United States`               Page: List of presidents of the United States     Summary: The president of the United States is the head of state and head of government of the United States, indirectly elected to a four-year term via the Electoral College. The officeholder leads the executive branch of the federal government and is the commander-in-chief of the United States Armed Forces. Since the office was established in 1789, 45 men have served in 46 presidencies. The first president, George Washington, won a unanimous vote of the Electoral College. Grover Cleveland served two non-consecutive terms and is therefore counted as the 22nd and 24th president of the United States, giving rise to the discrepancy between the number of presidencies and the number of persons who have served as president. The incumbent president is Joe Biden.The presidency of William Henry Harrison, who died 31 days after taking office in 1841, was the shortest in American history. Franklin D. Roosevelt served the longest, over twelve years, before dying early in his fourth term in 1945. He is the only U.S. president to have served more than two terms. Since the ratification of the Twenty-second Amendment to the United States Constitution in 1951, no person may be elected president more than twice, and no one who has served more than two years of a term to which someone else was elected may be elected more than once.Four presidents died in office of natural causes (William Henry Harrison, Zachary Taylor, Warren G. Harding, and Franklin D. Roosevelt), four were assassinated (Abraham Lincoln, James A. Garfield, William McKinley, and John F. Kennedy), and one resigned (Richard Nixon, facing impeachment and removal from office). John Tyler was the first vice president to assume the presidency during a presidential term, and set the precedent that a vice president who does so becomes the fully functioning president with his presidency.Throughout most of its history, American politics has been dominated by political parties. The Constitution is silent on the issue of political parties, and at the time it came into force in 1789, no organized parties existed. Soon after the 1st Congress convened, political factions began rallying around dominant Washington administration officials, such as Alexander Hamilton and Thomas Jefferson. Concerned about the capacity of political parties to destroy the fragile unity holding the nation together, Washington remained unaffiliated with any political faction or party throughout his eight-year presidency. He was, and remains, the only U.S. president never affiliated with a political party.          Page: List of presidents of the United States by age     Summary: In this list of presidents of the United States by age, the first table charts the age of each president of the United States at the time of presidential inauguration (first inauguration if elected to multiple and consecutive terms), upon leaving office, and at the time of death. Where the president is still living, their lifespan and post-presidency timespan are calculated up to November 14, 2023.          Page: List of vice presidents of the United States     Summary: There have been 49 vice presidents of the United States since the office was created in 1789. Originally, the vice president was the person who received the second-most votes for president in the Electoral College. But after the election of 1800 produced a tie between Thomas Jefferson and Aaron Burr, requiring the House of Representatives to choose between them, lawmakers acted to prevent such a situation from recurring. The Twelfth Amendment was added to the Constitution in 1804, creating the current system where electors cast a separate ballot for the vice presidency.The vi


Managing prompt size | 🦜🔗 Langchain

ce president is the first person in the presidential line of succession—that is, they assume the presidency if the president dies, resigns, or is impeached and removed from office. Nine vice presidents have ascended to the presidency in this way: eight (John Tyler, Millard Fillmore, Andrew Johnson, Chester A. Arthur, Theodore Roosevelt, Calvin Coolidge, Harry S. Truman, and Lyndon B. Johnson) through the president's death and one (Gerald Ford) through the president's resignation. The vice president also serves as the president of the Senate and may choose to cast a tie-breaking vote on decisions made by the Senate. Vice presidents have exercised this latter power to varying extents over the years.Before adoption of the Twenty-fifth Amendment in 1967, an intra-term vacancy in the office of the vice president could not be filled until the next post-election inauguration. Several such vacancies occurred: seven vice presidents died, one resigned and eight succeeded to the presidency. This amendment allowed for a vacancy to be filled through appointment by the president and confirmation by both chambers of the Congress. Since its ratification, the vice presidency has been vacant twice (both in the context of scandals surrounding the Nixon administration) and was filled both times through this process, namely in 1973 following Spiro Agnew's resignation, and again in 1974 after Gerald Ford succeeded to the presidency. The amendment also established a procedure whereby a vice president may, if the president is unable to discharge the powers and duties of the office, temporarily assume the powers and duties of the office as acting president. Three vice presidents have briefly acted as president under the 25th Amendment: George H. W. Bush on July 13, 1985; Dick Cheney on June 29, 2002, and on July 21, 2007; and Kamala Harris on November 19, 2021.     The persons who have served as vice president were born in or primarily affiliated with 27 states plus the District of Columbia. New York has produced the most of any state as eight have been born there and three others considered it their home state. Most vice presidents have been in their 50s or 60s and had political experience before assuming the office. Two vice presidents—George Clinton and John C. Calhoun—served under more than one president. Ill with tuberculosis and recovering in Cuba on Inauguration Day in 1853, William R. King, by an Act of Congress, was allowed to take the oath outside the United States. He is the only vice president to take his oath of office in a foreign country.          Page: List of presidents of the United States by net worth     Summary: The list of presidents of the United States by net worth at peak varies greatly. Debt and depreciation often means that presidents' net worth is less than $0 at the time of death. Most presidents before 1845 were extremely wealthy, especially Andrew Jackson and George Washington.         Presidents since 1929, when Herbert Hoover took office, have generally been wealthier than presidents of the late nineteenth and early twentieth centuries; with the exception of Harry S. Truman, all presidents since this time have been millionaires. These presidents have often received income from autobiographies and other writing. Except for Franklin D. Roosevelt and John F. Kennedy (both of whom died while in office), all presidents beginning with Calvin Coolidge have written autobiographies. In addition, many presidents—including Bill Clinton—have earned considerable income from public speaking after leaving office.The richest president in history may be Donald Trump. However, his net worth is not precisely known because the Trump Organization is privately held.Truman was among the poorest U.S. presidents, with a net worth considerably less than $1 million. His financial situation contributed to the doubling of the presidential salary to $100,000 in 1949. In addition, the presidential pension was created in 1958 when Truman was again experiencing financial difficulties. Harry and Bess Truman received the first Medicare cards in 1966 via the Social Security Act of 1965.          Page: List of presidents of the United States by home state     Summary: These lists give the states of primary affiliation and of birth for each president of the United States.     Invoking: `Wikipedia` with `Joe Biden`               Page: Joe Biden     Summary: Joseph Robinette Biden Jr. (  BY-dən; born November 20, 1942) is an American politician who is the 46th and current president of the United States. Ideologically a moderate member of the Democratic Party, he previously served as the 47th vice president from 2009 to 2017 under President Barack Obama and represented Delaware in the United States Senate from 1973 to 2009.     Born in Scranton, Pennsylvania, Biden moved with his family to Delaware in 1953. He studied at the University of Delaware before earning his law degree from Syracuse University. He was elected to the New Castle County Council in 1970 and to the U.S. Senate in 1972. As a senator, Biden drafted and led the effort to pass the Violent Crime Control and Law Enforcement Act and the Violence Against Women Act. He also oversaw six U.S. Supreme Court confirmation hearings, including the contentious hearings for Robert Bork and Clarence Thomas. Biden ran unsuccessfully for the Democratic presidential nomination in 1988 and 2008. In 2008, Obama chose Biden as his running mate, and Biden was a close counselor to Obama during his two terms as vice president. In the 2020 presidential election, Biden and his running mate, Kamala Harris, defeated incumbents Donald Trump and Mike Pence. Biden is the second Catholic president in U.S. history (after John F. Kennedy), and his politics have been widely described as profoundly influenced by Catholic social teaching.     Taking office at age 78, Biden is the oldest president in U.S. history, the first to have a female vice president, and the first from Delaware. In 202


Managing prompt size | 🦜🔗 Langchain

1, he signed a bipartisan infrastructure bill, as well as a $1.9 trillion economic stimulus package in response to the COVID-19 pandemic and its related recession. Biden proposed the Build Back Better Act, which failed in Congress, but aspects of which were incorporated into the Inflation Reduction Act that was signed into law in 2022. Biden also signed the bipartisan CHIPS and Science Act, which focused on manufacturing, appointed Ketanji Brown Jackson to the Supreme Court and worked with congressional Republicans to prevent a first-ever national default by negotiating a deal to raise the debt ceiling. In foreign policy, Biden restored America's membership in the Paris Agreement. He oversaw the complete withdrawal of U.S. troops from Afghanistan that ended the war in Afghanistan, during which the Afghan government collapsed and the Taliban seized control. Biden has responded to the Russian invasion of Ukraine by imposing sanctions on Russia and authorizing civilian and military aid to Ukraine. During the 2023 Israel–Hamas war, Biden announced American military support for Israel, and condemned the actions of Hamas and other Palestinian militants as terrorism. In April 2023, he announced his candidacy for the Democratic Party nomination in the 2024 presidential election.          Page: Presidency of Joe Biden     Summary: Joe Biden's tenure as the 46th president of the United States began with his inauguration on January 20, 2021. Biden, a Democrat from Delaware who previously served as vice president under Barack Obama, took office following his victory in the 2020 presidential election over Republican incumbent president Donald Trump. Upon his inauguration, he became the oldest president in American history, breaking the record set by his predecessor Trump. Biden entered office amid the COVID-19 pandemic, an economic crisis, and increased political polarization.On the first day of his presidency, Biden made an effort to revert President Trump's energy policy by restoring U.S. participation in the Paris Agreement and revoking the permit for the Keystone XL pipeline. He also halted funding for Trump's border wall, an expansion of the Mexican border wall. On his second day, he issued a series of executive orders to reduce the impact of COVID-19, including invoking the Defense Production Act of 1950, and set an early goal of achieving one hundred million COVID-19 vaccinations in the United States in his first 100 days.Biden signed into law the American Rescue Plan Act of 2021; a $1.9 trillion stimulus bill that temporarily established expanded unemployment insurance and sent $1,400 stimulus checks to most Americans in response to continued economic pressure from COVID-19. He signed the bipartisan Infrastructure Investment and Jobs Act; a ten-year plan brokered by Biden alongside Democrats and Republicans in Congress, to invest in American roads, bridges, public transit, ports and broadband access. Biden signed the Juneteenth National Independence Day Act, making Juneteenth a federal holiday in the United States. He appointed Ketanji Brown Jackson to the U.S. Supreme Court—the first Black woman to serve on the court. After The Supreme Court overturned Roe v. Wade, Biden took executive actions, such as the signing of Executive Order 14076, to preserve and protect women's health rights nationwide, against abortion bans in Republican led states. Biden proposed a significant expansion of the U.S. social safety net through the Build Back Better Act, but those efforts, along with voting rights legislation, failed in Congress. However, in August 2022, Biden signed the Inflation Reduction Act of 2022, a domestic appropriations bill that included some of the provisions of the Build Back Better Act after the entire bill failed to pass. It included significant federal investment in climate and domestic clean energy production, tax credits for solar panels, electric cars and other home energy programs as well as a three-year extension of Affordable Care Act subsidies. Biden signed the CHIPS and Science Act, bolstering the semiconductor and manufacturing industry, the Honoring our PACT Act, expanding healthcare for US veterans, and the Electoral Count Reform and Presidential Transition Improvement Act. In late 2022, Biden signed the Respect for Marriage Act, which repealed the Defense of Marriage Act and codified same-sex and interracial marriage in the United States. In response to the debt-ceiling crisis of 2023, Biden negotiated and signed the Fiscal Responsibility Act of 2023, which restrains federal spending for fiscal years 2024 and 2025, implements minor changes to SNAP and TANF, includes energy permitting reform, claws back some IRS funding and unspent money for COVID-19, and suspends the debt ceiling to January 1, 2025. Biden established the American Climate Corps and created the first ever White House Office of Gun Violence Prevention. On September 26, 2023, Joe Biden visited a United Auto Workers picket line during the 2023 United Auto Workers strike, making him the first US president to visit one.     The foreign policy goal of the Biden administration is to restore the US to a "position of trusted leadership" among global democracies in order to address the challenges posed by Russia and China. In foreign policy, Biden completed the withdrawal of U.S. military forces from Afghanistan, declaring an end to nation-building efforts and shifting U.S. foreign policy toward strategic competition with China and, to a lesser extent, Russia. However, during the withdrawal, the Afghan government collapsed and the Taliban seized control, leading to Biden receiving bipartisan criticism. He responded to the Russian invasion of Ukraine by imposing sanctions on Russia as well as providing Ukraine with over $100 billion in combined military, economic, and humanitarian aid. Biden also approved a raid which led to the death of Abu Ibrahim al-Hashimi al-Qurashi, the leader of the Islamic State, and approved a


Managing prompt size | 🦜🔗 Langchain

drone strike which killed Ayman Al Zawahiri, leader of Al-Qaeda. Biden signed AUKUS, an international security alliance, together with Australia and the United Kingdom. Biden called for the expansion of NATO with the addition of Finland and Sweden, and rallied NATO allies in support of Ukraine. During the 2023 Israel–Hamas war, Biden condemned Hamas and other Palestinian militants as terrorism and announced American military support for Israel; Biden also showed his support and sympathy towards Palestinians affected by the war and has sent humanitarian aid.     Biden began his term with over 50% approval ratings; however, these fell significantly after the withdrawal from Afghanistan and remained low as the country experienced high inflation and rising gas prices. His age and mental fitness have also been a subject of discussion.          Page: Family of Joe Biden     Summary: Joe Biden, the 46th and current president of the United States, has family members who are prominent in law, education, activism and politics. Biden's immediate family became the first family of the United States on his inauguration on January 20, 2021. His immediate family circle was also the second family of the United States from 2009 to 2017, when Biden was vice president. Biden's family is mostly descended from the British Isles, with most of their ancestors coming from Ireland and England, and a smaller number descending from the French.Of Joe Biden's sixteen great-great-grandparents, ten were born in Ireland. He is descended from the Blewitts of County Mayo and the Finnegans of County Louth. One of Biden's great-great-great-grandfathers was born in Sussex, England, and emigrated to Maryland in the United States by 1820.          Page: Cabinet of Joe Biden     Summary: Joe Biden assumed office as President of the United States on January 20, 2021. The president has the authority to nominate members of his Cabinet to the United States Senate for confirmation under the Appointments Clause of the United States Constitution.     Before confirmation and during congressional hearings, a high-level career member of an executive department heads this pre-confirmed cabinet on an acting basis. The Cabinet's creation was part of the transition of power following the 2020 United States presidential election.     In addition to the 15 heads of executive departments, there are 10 Cabinet-level officials. Biden altered his cabinet struct     Invoking: `Wikipedia` with `Delaware`               Page: Delaware     Summary: Delaware (  DEL-ə-wair) is a state in the Mid-Atlantic region of the United States. It borders Maryland to its south and west, Pennsylvania to its north, New Jersey to its northeast, and the Atlantic Ocean to its east. The state's name derives from the adjacent Delaware Bay, which in turn was named after Thomas West, 3rd Baron De La Warr, an English nobleman and the Colony of Virginia's first colonial-era governor.Delaware occupies the northeastern portion of the Delmarva Peninsula, and some islands and territory within the Delaware River. It is the 2nd smallest and 6th least populous state, but also the 6th most densely populated. Delaware's most populous city is Wilmington, and the state's capital is Dover, the 2nd most populous city in Delaware. The state is divided into three counties, the fewest number of counties of any of the 50 U.S. states; from north to south, the three counties are: New Castle County, Kent County, and Sussex County.      The southern two counties, Kent and Sussex counties, historically have been predominantly agrarian economies/ New Castle is more urbanized and is considered part of the Delaware Valley metropolitan statistical area that surrounds and includes Philadelphia, the nation's 6th most populous city. Delaware is considered part of the Southern United States by the U.S. Census Bureau, but the state's geography, culture, and history are a hybrid of the Mid-Atlantic and Northeastern regions of the country.Before Delaware coastline was explored and developed by Europeans in the 16th century, the state was inhabited by several Native Americans tribes, including the Lenape in the north and Nanticoke in the south. The state was first colonized by Dutch traders at Zwaanendael, near present-day Lewes, Delaware, in 1631.      Delaware was one of the Thirteen Colonies that participated in the American Revolution and American Revolutionary War, in which the American Continental Army, led by George Washington, defeated the British, ended British colonization and establishing the United States as a sovereign and independent nation.      On December 7, 1787, Delaware was the first state to ratify the Constitution of the United States, earning the state the nickname "The First State".Since the turn of the 20th century, Delaware has become an onshore corporate haven whose corporate laws are deemed appealed to corporations; over half of all New York Stock Exchange-listed corporations and over three-fifths of the Fortune 500 is legally incorporated in the state.          Page: Delaware City, Delaware     Summary: Delaware City is a city in New Castle County, Delaware, United States. The population was 1,885 as of 2020. It is a small port town on the eastern terminus of the Chesapeake and Delaware Canal and is the location of the Forts Ferry Crossing to Fort Delaware on Pea Patch Island.          Page: Delaware River     Summary: The Delaware River is a major river in the Mid-Atlantic region of the United States and is the longest free-flowing (undammed) river in the Eastern United States. From the meeting of its branches in Hancock, New York, the river flows for 282 miles (454 km) along the borders of New York, Pennsylvania, New Jersey, and Delaware, before emptying into Delaware Bay.     The river has been recognized by the National Wildlife Federation as one of the country's Great Waters and has been called the "Lifeblood of the Northeast" by American Rivers. Its watershed drains an area o


Managing prompt size | 🦜🔗 Langchain

f 13,539 square miles (35,070 km2) and provides drinking water for 17 million people, including half of New York City via the Delaware Aqueduct.     The Delaware River has two branches that rise in the Catskill Mountains of New York: the West Branch at Mount Jefferson in Jefferson, Schoharie County, and the East Branch at Grand Gorge, Delaware County. The branches merge to form the main Delaware River at Hancock, New York. Flowing south, the river remains relatively undeveloped, with 152 miles (245 km) protected as the Upper, Middle, and Lower Delaware National Scenic Rivers. At Trenton, New Jersey, the Delaware becomes tidal, navigable, and significantly more industrial. This section forms the backbone of the Delaware Valley metropolitan area, serving the port cities of Philadelphia, Camden, New Jersey, and Wilmington, Delaware. The river flows into Delaware Bay at Liston Point, 48 miles (77 km) upstream of the bay's outlet to the Atlantic Ocean between Cape May and Cape Henlopen.     Before the arrival of European settlers, the river was the homeland of the Lenape native people. They called the river Lenapewihittuk, or Lenape River, and Kithanne, meaning the largest river in this part of the country.In 1609, the river was visited by a Dutch East India Company expedition led by Henry Hudson. Hudson, an English navigator, was hired to find a western route to Cathay (China), but his encounters set the stage for Dutch colonization of North America in the 17th century. Early Dutch and Swedish settlements were established along the lower section of the river and Delaware Bay. Both colonial powers called the river the South River (Zuidrivier), compared to the Hudson River, which was known as the North River. After the English expelled the Dutch and took control of the New Netherland colony in 1664, the river was renamed Delaware after Sir Thomas West, 3rd Baron De La Warr, an English nobleman and the Virginia colony's first royal governor who defended the colony during the First Anglo-Powhatan War.          Page: Lenape     Summary: The Lenape (English: , , ; Lenape languages: [lenaːpe]), also called the Lenni Lenape and Delaware people, are an indigenous people of the Northeastern Woodlands, who live in the United States and Canada.The Lenape's historical territory included present-day northeastern Delaware, all of New Jersey, the eastern Pennsylvania regions of the Lehigh Valley and Northeastern Pennsylvania, and New York Bay, western Long Island, and the lower Hudson Valley in New York state. Today they are based in Oklahoma, Wisconsin, and Ontario.     During the last decades of the 18th century, European settlers and the effects of the American Revolutionary War displaced most Lenape from their homelands and pushed them north and west. In the 1860s, under the Indian removal policy, the U.S. federal government relocated most Lenape remaining in the Eastern United States to the Indian Territory and surrounding regions. Lenape people currently belong to the Delaware Nation and Delaware Tribe of Indians in Oklahoma, the Stockbridge–Munsee Community in Wisconsin, and the Munsee-Delaware Nation, Moravian of the Thames First Nation, and Delaware of Six Nations in Ontario.          Page: University of Delaware     Summary: The University of Delaware (colloquially known as UD or Delaware) is a privately governed, state-assisted land-grant research university located in Newark, Delaware. UD is the largest university in Delaware. It offers three associate's programs, 148 bachelor's programs, 121 master's programs (with 13 joint degrees), and 55 doctoral programs across its eight colleges. The main campus is in Newark, with satellite campuses in Dover, Wilmington, Lewes, and Georgetown. It is considered a large institution with approximately 18,200 undergraduate and 4,200 graduate students. It is a privately governed university which receives public funding for being a land-grant, sea-grant, and space-grant state-supported research institution.UD is classified among "R1: Doctoral Universities – Very high research activity". According to the National Science Foundation, UD spent $186 million on research and development in 2018, ranking it 119th in the nation.  It is recognized with the Community Engagement Classification by the Carnegie Foundation for the Advancement of Teaching.UD students, alumni, and sports teams are known as the "Fightin' Blue Hens", more commonly shortened to "Blue Hens", and the school colors are Delaware blue and gold. UD sponsors 21 men's and women's NCAA Division-I sports teams and have competed in the Colonial Athletic Association (CAA) since 2001.       ---------------------------------------------------------------------------      BadRequestError                           Traceback (most recent call last)      Cell In[5], line 14           1 agent = (           2     {           3         "input": itemgetter("input"),        (...)          10     | OpenAIFunctionsAgentOutputParser()          11 )          13 agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)     ---> 14 agent_executor.invoke(          15     {"input": "Who is the current US president? What's their home state? What's their home state's bird? What's that bird's scientific name?"}          16 )       File ~/langchain/libs/langchain/langchain/chains/base.py:87, in Chain.invoke(self, input, config, **kwargs)          80 def invoke(          81     self,          82     input: Dict[str, Any],          83     config: Optional[RunnableConfig] = None,          84     **kwargs: Any,          85 ) -> Dict[str, Any]:          86     config = config or {}     ---> 87     return self(          88         input,          89         callbacks=config.get("callbacks"),          90         tags=config.get("tags"),          91         metadata=config.get("metadata"),          92         run_name=config.get("run_name"),          93         **kwargs,          94     )       File ~/langchain/libs/langchain/


Managing prompt size | 🦜🔗 Langchain

langchain/chains/base.py:310, in Chain.__call__(self, inputs, return_only_outputs, callbacks, tags, metadata, run_name, include_run_info)         308 except BaseException as e:         309     run_manager.on_chain_error(e)     --> 310     raise e         311 run_manager.on_chain_end(outputs)         312 final_outputs: Dict[str, Any] = self.prep_outputs(         313     inputs, outputs, return_only_outputs         314 )       File ~/langchain/libs/langchain/langchain/chains/base.py:304, in Chain.__call__(self, inputs, return_only_outputs, callbacks, tags, metadata, run_name, include_run_info)         297 run_manager = callback_manager.on_chain_start(         298     dumpd(self),         299     inputs,         300     name=run_name,         301 )         302 try:         303     outputs = (     --> 304         self._call(inputs, run_manager=run_manager)         305         if new_arg_supported         306         else self._call(inputs)         307     )         308 except BaseException as e:         309     run_manager.on_chain_error(e)       File ~/langchain/libs/langchain/langchain/agents/agent.py:1167, in AgentExecutor._call(self, inputs, run_manager)        1165 # We now enter the agent loop (until it returns something).        1166 while self._should_continue(iterations, time_elapsed):     -> 1167     next_step_output = self._take_next_step(        1168         name_to_tool_map,        1169         color_mapping,        1170         inputs,        1171         intermediate_steps,        1172         run_manager=run_manager,        1173     )        1174     if isinstance(next_step_output, AgentFinish):        1175         return self._return(        1176             next_step_output, intermediate_steps, run_manager=run_manager        1177         )       File ~/langchain/libs/langchain/langchain/agents/agent.py:954, in AgentExecutor._take_next_step(self, name_to_tool_map, color_mapping, inputs, intermediate_steps, run_manager)         951     intermediate_steps = self._prepare_intermediate_steps(intermediate_steps)         953     # Call the LLM to see what to do.     --> 954     output = self.agent.plan(         955         intermediate_steps,         956         callbacks=run_manager.get_child() if run_manager else None,         957         **inputs,         958     )         959 except OutputParserException as e:         960     if isinstance(self.handle_parsing_errors, bool):       File ~/langchain/libs/langchain/langchain/agents/agent.py:389, in RunnableAgent.plan(self, intermediate_steps, callbacks, **kwargs)         377 """Given input, decided what to do.         378          379 Args:        (...)         386     Action specifying what tool to use.         387 """         388 inputs = {**kwargs, **{"intermediate_steps": intermediate_steps}}     --> 389 output = self.runnable.invoke(inputs, config={"callbacks": callbacks})         390 if isinstance(output, AgentAction):         391     output = [output]       File ~/langchain/libs/langchain/langchain/schema/runnable/base.py:1427, in RunnableSequence.invoke(self, input, config)        1425 try:        1426     for i, step in enumerate(self.steps):     -> 1427         input = step.invoke(        1428             input,        1429             # mark each step as a child run        1430             patch_config(        1431                 config, callbacks=run_manager.get_child(f"seq:step:{i+1}")        1432             ),        1433         )        1434 # finish the root run        1435 except BaseException as e:       File ~/langchain/libs/langchain/langchain/schema/runnable/base.py:2765, in RunnableBindingBase.invoke(self, input, config, **kwargs)        2759 def invoke(        2760     self,        2761     input: Input,        2762     config: Optional[RunnableConfig] = None,        2763     **kwargs: Optional[Any],        2764 ) -> Output:     -> 2765     return self.bound.invoke(        2766         input,        2767         self._merge_configs(config),        2768         **{**self.kwargs, **kwargs},        2769     )       File ~/langchain/libs/langchain/langchain/chat_models/base.py:142, in BaseChatModel.invoke(self, input, config, stop, **kwargs)         131 def invoke(         132     self,         133     input: LanguageModelInput,        (...)         137     **kwargs: Any,         138 ) -> BaseMessage:         139     config = config or {}         140     return cast(         141         ChatGeneration,     --> 142         self.generate_prompt(         143             [self._convert_input(input)],         144             stop=stop,         145             callbacks=config.get("callbacks"),         146             tags=config.get("tags"),         147             metadata=config.get("metadata"),         148             run_name=config.get("run_name"),         149             **kwargs,         150         ).generations[0][0],         151     ).message       File ~/langchain/libs/langchain/langchain/chat_models/base.py:459, in BaseChatModel.generate_prompt(self, prompts, stop, callbacks, **kwargs)         451 def generate_prompt(         452     self,         453     prompts: List[PromptValue],        (...)         456     **kwargs: Any,         457 ) -> LLMResult:         458     prompt_messages = [p.to_messages() for p in prompts]     --> 459     return self.generate(prompt_messages, stop=stop, callbacks=callbacks, **kwargs)       File ~/langchain/libs/langchain/langchain/chat_models/base.py:349, in BaseChatModel.generate(self, messages, stop, callbacks, tags, metadata, run_name, **kwargs)         347         if run_managers:         348             run_managers[i].on_llm_error(e)     --> 349         raise e         350 flattened_outputs = [         351     LLMResult(generations=[res.generations], llm_output=res.llm_output)         352     for res in results         353 ]         354 llm_output = self._combine_llm_outputs([res.llm_output for res in results])       File ~/langchain/libs/langchain/langchain/chat_m


Managing prompt size | 🦜🔗 Langchain

odels/base.py:339, in BaseChatModel.generate(self, messages, stop, callbacks, tags, metadata, run_name, **kwargs)         336 for i, m in enumerate(messages):         337     try:         338         results.append(     --> 339             self._generate_with_cache(         340                 m,         341                 stop=stop,         342                 run_manager=run_managers[i] if run_managers else None,         343                 **kwargs,         344             )         345         )         346     except BaseException as e:         347         if run_managers:       File ~/langchain/libs/langchain/langchain/chat_models/base.py:492, in BaseChatModel._generate_with_cache(self, messages, stop, run_manager, **kwargs)         488     raise ValueError(         489         "Asked to cache, but no cache found at `langchain.cache`."         490     )         491 if new_arg_supported:     --> 492     return self._generate(         493         messages, stop=stop, run_manager=run_manager, **kwargs         494     )         495 else:         496     return self._generate(messages, stop=stop, **kwargs)       File ~/langchain/libs/langchain/langchain/chat_models/openai.py:417, in ChatOpenAI._generate(self, messages, stop, run_manager, stream, **kwargs)         415 message_dicts, params = self._create_message_dicts(messages, stop)         416 params = {**params, **kwargs}     --> 417 response = self.completion_with_retry(         418     messages=message_dicts, run_manager=run_manager, **params         419 )         420 return self._create_chat_result(response)       File ~/langchain/libs/langchain/langchain/chat_models/openai.py:339, in ChatOpenAI.completion_with_retry(self, run_manager, **kwargs)         337 """Use tenacity to retry the completion call."""         338 if is_openai_v1():     --> 339     return self.client.create(**kwargs)         341 retry_decorator = _create_retry_decorator(self, run_manager=run_manager)         343 @retry_decorator         344 def _completion_with_retry(**kwargs: Any) -> Any:       File ~/langchain/.venv/lib/python3.9/site-packages/openai/_utils/_utils.py:299, in required_args.<locals>.inner.<locals>.wrapper(*args, **kwargs)         297             msg = f"Missing required argument: {quote(missing[0])}"         298     raise TypeError(msg)     --> 299 return func(*args, **kwargs)       File ~/langchain/.venv/lib/python3.9/site-packages/openai/resources/chat/completions.py:594, in Completions.create(self, messages, model, frequency_penalty, function_call, functions, logit_bias, max_tokens, n, presence_penalty, response_format, seed, stop, stream, temperature, tool_choice, tools, top_p, user, extra_headers, extra_query, extra_body, timeout)         548 @required_args(["messages", "model"], ["messages", "model", "stream"])         549 def create(         550     self,        (...)         592     timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,         593 ) -> ChatCompletion | Stream[ChatCompletionChunk]:     --> 594     return self._post(         595         "/chat/completions",         596         body=maybe_transform(         597             {         598                 "messages": messages,         599                 "model": model,         600                 "frequency_penalty": frequency_penalty,         601                 "function_call": function_call,         602                 "functions": functions,         603                 "logit_bias": logit_bias,         604                 "max_tokens": max_tokens,         605                 "n": n,         606                 "presence_penalty": presence_penalty,         607                 "response_format": response_format,         608                 "seed": seed,         609                 "stop": stop,         610                 "stream": stream,         611                 "temperature": temperature,         612                 "tool_choice": tool_choice,         613                 "tools": tools,         614                 "top_p": top_p,         615                 "user": user,         616             },         617             completion_create_params.CompletionCreateParams,         618         ),         619         options=make_request_options(         620             extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout         621         ),         622         cast_to=ChatCompletion,         623         stream=stream or False,         624         stream_cls=Stream[ChatCompletionChunk],         625     )       File ~/langchain/.venv/lib/python3.9/site-packages/openai/_base_client.py:1055, in SyncAPIClient.post(self, path, cast_to, body, options, files, stream, stream_cls)        1041 def post(        1042     self,        1043     path: str,        (...)        1050     stream_cls: type[_StreamT] | None = None,        1051 ) -> ResponseT | _StreamT:        1052     opts = FinalRequestOptions.construct(        1053         method="post", url=path, json_data=body, files=to_httpx_files(files), **options        1054     )     -> 1055     return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls))       File ~/langchain/.venv/lib/python3.9/site-packages/openai/_base_client.py:834, in SyncAPIClient.request(self, cast_to, options, remaining_retries, stream, stream_cls)         825 def request(         826     self,         827     cast_to: Type[ResponseT],        (...)         832     stream_cls: type[_StreamT] | None = None,         833 ) -> ResponseT | _StreamT:     --> 834     return self._request(         835         cast_to=cast_to,         836         options=options,         837         stream=stream,         838         stream_cls=stream_cls,         839         remaining_retries=remaining_retries,         840     )       File ~/langchain/.venv/lib/python3.9/site-packages/openai/_base_client.py:877, in SyncAPIClient._request(self, cast_to, options, remaining_retries, stream, stream_cls)


Managing prompt size | 🦜🔗 Langchain

874     # If the response is streamed then we need to explicitly read the response         875     # to completion before attempting to access the response text.         876     err.response.read()     --> 877     raise self._make_status_error_from_response(err.response) from None         878 except httpx.TimeoutException as err:         879     if retries > 0:       BadRequestError: Error code: 400 - {'error': {'message': "This model's maximum context length is 4097 tokens. However, your messages resulted in 5478 tokens (5410 in the messages, 68 in the functions). Please reduce the length of the messages or functions.", 'type': 'invalid_request_error', 'param': 'messages', 'code': 'context_length_exceeded'}}  [LANGSMITH TRACE](HTTPS://SMITH.LANGCHAIN.COM/PUBLIC/60909EAE-F4F1-43EB-9F96-354F5176F66F/R) Unfortunately we run out of space in our model's context window before we the agent can get to the final answer. Now let's add some prompt handling logic. To keep things simple, if our messages have too many tokens we'll start dropping the earliest AI, Function message pairs (this is the model tool invocation message and the subsequent tool output message) in the chat history.  def condense_prompt(prompt: ChatPromptValue) -> ChatPromptValue:     messages = prompt.to_messages()     num_tokens = llm.get_num_tokens_from_messages(messages)     ai_function_messages = messages[2:]     while num_tokens > 4_000:         ai_function_messages = ai_function_messages[2:]         num_tokens = llm.get_num_tokens_from_messages(             messages[:2] + ai_function_messages         )     messages = messages[:2] + ai_function_messages     return ChatPromptValue(messages=messages)   agent = (     {         "input": itemgetter("input"),         "agent_scratchpad": lambda x: format_to_openai_function_messages(             x["intermediate_steps"]         ),     }     | prompt     | condense_prompt     | llm.bind(functions=[format_tool_to_openai_function(t) for t in tools])     | OpenAIFunctionsAgentOutputParser() )  agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True) agent_executor.invoke(     {         "input": "Who is the current US president? What's their home state? What's their home state's bird? What's that bird's scientific name?"     } )                > Entering new AgentExecutor chain...          Invoking: `Wikipedia` with `List of presidents of the United States`               Page: List of presidents of the United States     Summary: The president of the United States is the head of state and head of government of the United States, indirectly elected to a four-year term via the Electoral College. The officeholder leads the executive branch of the federal government and is the commander-in-chief of the United States Armed Forces. Since the office was established in 1789, 45 men have served in 46 presidencies. The first president, George Washington, won a unanimous vote of the Electoral College. Grover Cleveland served two non-consecutive terms and is therefore counted as the 22nd and 24th president of the United States, giving rise to the discrepancy between the number of presidencies and the number of persons who have served as president. The incumbent president is Joe Biden.The presidency of William Henry Harrison, who died 31 days after taking office in 1841, was the shortest in American history. Franklin D. Roosevelt served the longest, over twelve years, before dying early in his fourth term in 1945. He is the only U.S. president to have served more than two terms. Since the ratification of the Twenty-second Amendment to the United States Constitution in 1951, no person may be elected president more than twice, and no one who has served more than two years of a term to which someone else was elected may be elected more than once.Four presidents died in office of natural causes (William Henry Harrison, Zachary Taylor, Warren G. Harding, and Franklin D. Roosevelt), four were assassinated (Abraham Lincoln, James A. Garfield, William McKinley, and John F. Kennedy), and one resigned (Richard Nixon, facing impeachment and removal from office). John Tyler was the first vice president to assume the presidency during a presidential term, and set the precedent that a vice president who does so becomes the fully functioning president with his presidency.Throughout most of its history, American politics has been dominated by political parties. The Constitution is silent on the issue of political parties, and at the time it came into force in 1789, no organized parties existed. Soon after the 1st Congress convened, political factions began rallying around dominant Washington administration officials, such as Alexander Hamilton and Thomas Jefferson. Concerned about the capacity of political parties to destroy the fragile unity holding the nation together, Washington remained unaffiliated with any political faction or party throughout his eight-year presidency. He was, and remains, the only U.S. president never affiliated with a political party.          Page: List of presidents of the United States by age     Summary: In this list of presidents of the United States by age, the first table charts the age of each president of the United States at the time of presidential inauguration (first inauguration if elected to multiple and consecutive terms), upon leaving office, and at the time of death. Where the president is still living, their lifespan and post-presidency timespan are calculated up to November 14, 2023.          Page: List of vice presidents of the United States     Summary: There have been 49 vice presidents of the United States since the office was created in 1789. Originally, the vice president was the person who received the second-most votes for president in the Electoral College. But after the election of 1800 produced a tie between Thomas Jefferson and Aaron Burr, requiring the House of Representatives to choose between them, lawmakers acted to prevent such a situation