,link,text 0,https://python.langchain.com/docs/get_started,Get startedGet startedGet started with LangChain📄️ IntroductionLangChain is a framework for developing applications powered by language models. It enables applications that:📄️ Installation📄️ QuickstartInstallationNextIntroduction 1,https://python.langchain.com/docs/get_started/introduction,"Get startedIntroductionOn this pageIntroductionLangChain is a framework for developing applications powered by language models. It enables applications that:Are context-aware: connect a language model to sources of context (prompt instructions, few shot examples, content to ground its response in, etc.)Reason: rely on a language model to reason (about how to answer based on provided context, what actions to take, etc.)The main value props of LangChain are:Components: abstractions for working with language models, along with a collection of implementations for each abstraction. Components are modular and easy-to-use, whether you are using the rest of the LangChain framework or notOff-the-shelf chains: a structured assembly of components for accomplishing specific higher-level tasksOff-the-shelf chains make it easy to get started. For complex applications, components make it easy to customize existing chains and build new ones.Get started​Here’s how to install LangChain, set up your environment, and start building.We recommend following our Quickstart guide to familiarize yourself with the framework by building your first LangChain application.Note: These docs are for the LangChain Python package. For documentation on LangChain.js, the JS/TS version, head here.Modules​LangChain provides standard, extendable interfaces and external integrations for the following modules, listed from least to most complex:Model I/O​Interface with language modelsRetrieval​Interface with application-specific dataChains​Construct sequences of callsAgents​Let chains choose which tools to use given high-level directivesMemory​Persist application state between runs of a chainCallbacks​Log and stream intermediate steps of any chainExamples, ecosystem, and resources​Use cases​Walkthroughs and best-practices for common end-to-end use cases, like:Document question answeringChatbotsAnalyzing structured dataand much more...Guides​Learn best practices for developing with LangChain.Ecosystem​LangChain is part of a rich ecosystem of tools that integrate with our framework and build on top of it. Check out our growing list of integrations and dependent repos.Additional resources​Our community is full of prolific developers, creative builders, and fantastic teachers. Check out YouTube tutorials for great tutorials from folks in the community, and Gallery for a list of awesome LangChain projects, compiled by the folks at KyroLabs.Community​Head to the Community navigator to find places to ask questions, share feedback, meet other developers, and dream about the future of LLM’s.API reference​Head to the reference section for full documentation of all classes and methods in the LangChain Python package.PreviousGet startedNextInstallationGet startedModulesExamples, ecosystem, and resourcesUse casesGuidesEcosystemAdditional resourcesCommunityAPI reference" 2,https://python.langchain.com/docs/get_started/installation,"Get startedInstallationInstallationOfficial release​To install LangChain run:PipCondapip install langchainconda install langchain -c conda-forgeThis will install the bare minimum requirements of LangChain. A lot of the value of LangChain comes when integrating it with various model providers, datastores, etc. By default, the dependencies needed to do that are NOT installed. However, there are two other ways to install LangChain that do bring in those dependencies.To install modules needed for the common LLM providers, run:pip install langchain[llms]To install all modules needed for all integrations, run:pip install langchain[all]Note that if you are using zsh, you'll need to quote square brackets when passing them as an argument to a command, for example:pip install 'langchain[all]'From source​If you want to install from source, you can do so by cloning the repo and be sure that the directory is PATH/TO/REPO/langchain/libs/langchain running:pip install -e .PreviousIntroductionNextQuickstart" 3,https://python.langchain.com/docs/get_started/quickstart,"Get startedQuickstartOn this pageQuickstartInstallation​To install LangChain run:PipCondapip install langchainconda install langchain -c conda-forgeFor more details, see our Installation guide.Environment setup​Using LangChain will usually require integrations with one or more model providers, data stores, APIs, etc. For this example, we'll use OpenAI's model APIs.First we'll need to install their Python package:pip install openaiAccessing the API requires an API key, which you can get by creating an account and heading here. Once we have a key we'll want to set it as an environment variable by running:export OPENAI_API_KEY=""...""If you'd prefer not to set an environment variable you can pass the key in directly via the openai_api_key named parameter when initiating the OpenAI LLM class:from langchain.llms import OpenAIllm = OpenAI(openai_api_key=""..."")Building an application​Now we can start building our language model application. LangChain provides many modules that can be used to build language model applications. Modules can be used as stand-alones in simple applications and they can be combined for more complex use cases.The most common and most important chain that LangChain helps create contains three things:LLM: The language model is the core reasoning engine here. In order to work with LangChain, you need to understand the different types of language models and how to work with them.Prompt Templates: This provides instructions to the language model. This controls what the language model outputs, so understanding how to construct prompts and different prompting strategies is crucial.Output Parsers: These translate the raw response from the LLM to a more workable format, making it easy to use the output downstream.In this getting started guide we will cover those three components by themselves, and then go over how to combine all of them. Understanding these concepts will set you up well for being able to use and customize LangChain applications. Most LangChain applications allow you to configure the LLM and/or the prompt used, so knowing how to take advantage of this will be a big enabler.LLMs​There are two types of language models, which in LangChain are called:LLMs: this is a language model which takes a string as input and returns a stringChatModels: this is a language model which takes a list of messages as input and returns a messageThe input/output for LLMs is simple and easy to understand - a string. But what about ChatModels? The input there is a list of ChatMessages, and the output is a single ChatMessage. A ChatMessage has two required components:content: This is the content of the message.role: This is the role of the entity from which the ChatMessage is coming from.LangChain provides several objects to easily distinguish between different roles:HumanMessage: A ChatMessage coming from a human/user.AIMessage: A ChatMessage coming from an AI/assistant.SystemMessage: A ChatMessage coming from the system.FunctionMessage: A ChatMessage coming from a function call.If none of those roles sound right, there is also a ChatMessage class where you can specify the role manually. For more information on how to use these different messages most effectively, see our prompting guide.LangChain provides a standard interface for both, but it's useful to understand this difference in order to construct prompts for a given language model. The standard interface that LangChain provides has two methods:predict: Takes in a string, returns a stringpredict_messages: Takes in a list of messages, returns a message.Let's see how to work with these different types of models and these different types of inputs. First, let's import an LLM and a ChatModel.from langchain.llms import OpenAIfrom langchain.chat_models import ChatOpenAIllm = OpenAI()chat_model = ChatOpenAI()llm.predict(""hi!"")>>> ""Hi""chat_model.predict(""hi!"")>>> ""Hi""The OpenAI and ChatOpenAI objects are basically just configuration objects. You can initialize them with parameters like temperature and others, and pass them around.Next, let's use the predict method to run over a string input.text = ""What would be a good company name for a company that makes colorful socks?""llm.predict(text)# >> Feetful of Funchat_model.predict(text)# >> Socks O'ColorFinally, let's use the predict_messages method to run over a list of messages.from langchain.schema import HumanMessagetext = ""What would be a good company name for a company that makes colorful socks?""messages = [HumanMessage(content=text)]llm.predict_messages(messages)# >> Feetful of Funchat_model.predict_messages(messages)# >> Socks O'ColorFor both these methods, you can also pass in parameters as keyword arguments. For example, you could pass in temperature=0 to adjust the temperature that is used from what the object was configured with. Whatever values are passed in during run time will always override what the object was configured with.Prompt templates​Most LLM applications do not pass user input directly into an LLM. Usually they will add the user input to a larger piece of text, called a prompt template, that provides additional context on the specific task at hand.In the previous example, the text we passed to the model contained instructions to generate a company name. For our application, it'd be great if the user only had to provide the description of a company/product, without having to worry about giving the model instructions.PromptTemplates help with exactly this! They bundle up all the logic for going from user input into a fully formatted prompt. This can start off very simple - for example, a prompt to produce the above string would just be:from langchain.prompts import PromptTemplateprompt = PromptTemplate.from_template(""What is a good name for a company that makes {product}?"")prompt.format(product=""colorful socks"")What is a good name for a company that makes colorful socks?However, the advantages of using these over raw string formatting are several. You can ""partial"" out variables - e.g. you can format only some of the variables at a time. You can compose them together, easily combining different templates into a single prompt. For explanations of these functionalities, see the section on prompts for more detail.PromptTemplates can also be used to produce a list of messages. In this case, the prompt not only contains information about the content, but also each message (its role, its position in the list, etc) Here, what happens most often is a ChatPromptTemplate is a list of ChatMessageTemplates. Each ChatMessageTemplate contains instructions for how to format that ChatMessage - its role, and then also its content. Let's take a look at this below:from langchain.prompts.chat import ChatPromptTemplatetemplate = ""You are a helpful assistant that translates {input_language} to {output_language}.""human_template = ""{text}""chat_prompt = ChatPromptTemplate.from_messages([ (""system"", template), (""human"", human_template),])chat_prompt.format_messages(input_language=""English"", output_language=""French"", text=""I love programming."")[ SystemMessage(content=""You are a helpful assistant that translates English to French."", additional_kwargs={}), HumanMessage(content=""I love programming."")]ChatPromptTemplates can also be constructed in other ways - see the section on prompts for more detail.Output parsers​OutputParsers convert the raw output of an LLM into a format that can be used downstream. There are few main type of OutputParsers, including:Convert text from LLM -> structured information (e.g. JSON)Convert a ChatMessage into just a stringConvert the extra information returned from a call besides the message (like OpenAI function invocation) into a string.For full information on this, see the section on output parsersIn this getting started guide, we will write our own output parser - one that converts a comma separated list into a list.from langchain.schema import BaseOutputParserclass CommaSeparatedListOutputParser(BaseOutputParser): """"""Parse the output of an LLM call to a comma-separated list."""""" def parse(self, text: str): """"""Parse the output of an LLM call."""""" return text.strip().split("", "")CommaSeparatedListOutputParser().parse(""hi, bye"")# >> ['hi', 'bye']PromptTemplate + LLM + OutputParser​We can now combine all these into one chain. This chain will take input variables, pass those to a prompt template to create a prompt, pass the prompt to a language model, and then pass the output through an (optional) output parser. This is a convenient way to bundle up a modular piece of logic. Let's see it in action!from langchain.chat_models import ChatOpenAIfrom langchain.prompts.chat import ChatPromptTemplatefrom langchain.schema import BaseOutputParserclass CommaSeparatedListOutputParser(BaseOutputParser): """"""Parse the output of an LLM call to a comma-separated list."""""" def parse(self, text: str): """"""Parse the output of an LLM call."""""" return text.strip().split("", "")template = """"""You are a helpful assistant who generates comma separated lists.A user will pass in a category, and you should generate 5 objects in that category in a comma separated list.ONLY return a comma separated list, and nothing more.""""""human_template = ""{text}""chat_prompt = ChatPromptTemplate.from_messages([ (""system"", template), (""human"", human_template),])chain = chat_prompt | ChatOpenAI() | CommaSeparatedListOutputParser()chain.invoke({""text"": ""colors""})# >> ['red', 'blue', 'green', 'yellow', 'orange']Note that we are using the | syntax to join these components together. This | syntax is called the LangChain Expression Language. To learn more about this syntax, read the documentation here.Next steps​This is it! We've now gone over how to create the core building block of LangChain applications. There is a lot more nuance in all these components (LLMs, prompts, output parsers) and a lot more different components to learn about as well. To continue on your journey:Dive deeper into LLMs, prompts, and output parsersLearn the other key componentsRead up on LangChain Expression Language to learn how to chain these components togetherCheck out our helpful guides for detailed walkthroughs on particular topicsExplore end-to-end use casesPreviousInstallationNextLangChain Expression Language (LCEL)InstallationEnvironment setupBuilding an applicationLLMsPrompt templatesOutput parsersPromptTemplate + LLM + OutputParserNext steps" 4,https://python.langchain.com/docs/expression_language/,"LangChain Expression LanguageOn this pageLangChain Expression Language (LCEL)LangChain Expression Language or LCEL is a declarative way to easily compose chains together. There are several benefits to writing chains in this manner (as opposed to writing normal code):Async, Batch, and Streaming Support Any chain constructed this way will automatically have full sync, async, batch, and streaming support. This makes it easy to prototype a chain in a Jupyter notebook using the sync interface, and then expose it as an async streaming interface.Fallbacks The non-determinism of LLMs makes it important to be able to handle errors gracefully. With LCEL you can easily attach fallbacks to any chain.Parallelism Since LLM applications involve (sometimes long) API calls, it often becomes important to run things in parallel. With LCEL syntax, any components that can be run in parallel automatically are.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 for maximal observability and debuggability.Interface​The base interface shared by all LCEL objectsHow to​How to use core features of LCELCookbook​Examples of common LCEL usage patternsPreviousQuickstartNextInterface" 5,https://python.langchain.com/docs/expression_language/interface,"LangChain Expression LanguageInterfaceOn this pageInterfaceIn an effort to make it as easy as possible to create custom chains, we've implemented a ""Runnable"" protocol that most components implement. This is a standard interface with a few different methods, which makes it easy to define custom chains as well as making it possible to invoke them in a standard way. The standard interface exposed includes:stream: stream back chunks of the responseinvoke: call the chain on an inputbatch: call the chain on a list of inputsThese also have corresponding async methods:astream: stream back chunks of the response asyncainvoke: call the chain on an input asyncabatch: call the chain on a list of inputs asyncastream_log: stream back intermediate steps as they happen, in addition to the final responseThe type of the input varies by component:ComponentInput TypePromptDictionaryRetrieverSingle stringLLM, ChatModelSingle string, list of chat messages or a PromptValueToolSingle string, or dictionary, depending on the toolOutputParserThe output of an LLM or ChatModelThe output type also varies by component:ComponentOutput TypeLLMStringChatModelChatMessagePromptPromptValueRetrieverList of documentsToolDepends on the toolOutputParserDepends on the parserAll runnables expose properties to inspect the input and output types:input_schema: an input Pydantic model auto-generated from the structure of the Runnableoutput_schema: an output Pydantic model auto-generated from the structure of the RunnableLet's take a look at these methods! To do so, we'll create a super simple PromptTemplate + ChatModel chain.from langchain.prompts import ChatPromptTemplatefrom langchain.chat_models import ChatOpenAImodel = ChatOpenAI()prompt = ChatPromptTemplate.from_template(""tell me a joke about {topic}"")chain = prompt | modelInput Schema​A description of the inputs accepted by a Runnable. This is a Pydantic model dynamically generated from the structure of any Runnable. You can call .schema() on it to obtain a JSONSchema representation.# The input schema of the chain is the input schema of its first part, the prompt.chain.input_schema.schema() {'title': 'PromptInput', 'type': 'object', 'properties': {'topic': {'title': 'Topic', 'type': 'string'}}}Output Schema​A description of the outputs produced by a Runnable. This is a Pydantic model dynamically generated from the structure of any Runnable. You can call .schema() on it to obtain a JSONSchema representation.# The output schema of the chain is the output schema of its last part, in this case a ChatModel, which outputs a ChatMessagechain.output_schema.schema() {'title': 'ChatOpenAIOutput', 'anyOf': [{'$ref': '#/definitions/HumanMessageChunk'}, {'$ref': '#/definitions/AIMessageChunk'}, {'$ref': '#/definitions/ChatMessageChunk'}, {'$ref': '#/definitions/FunctionMessageChunk'}, {'$ref': '#/definitions/SystemMessageChunk'}], 'definitions': {'HumanMessageChunk': {'title': 'HumanMessageChunk', 'description': 'A Human Message chunk.', 'type': 'object', 'properties': {'content': {'title': 'Content', 'type': 'string'}, 'additional_kwargs': {'title': 'Additional Kwargs', 'type': 'object'}, 'type': {'title': 'Type', 'default': 'human', 'enum': ['human'], 'type': 'string'}, 'example': {'title': 'Example', 'default': False, 'type': 'boolean'}, 'is_chunk': {'title': 'Is Chunk', 'default': True, 'enum': [True], 'type': 'boolean'}}, 'required': ['content']}, 'AIMessageChunk': {'title': 'AIMessageChunk', 'description': 'A Message chunk from an AI.', 'type': 'object', 'properties': {'content': {'title': 'Content', 'type': 'string'}, 'additional_kwargs': {'title': 'Additional Kwargs', 'type': 'object'}, 'type': {'title': 'Type', 'default': 'ai', 'enum': ['ai'], 'type': 'string'}, 'example': {'title': 'Example', 'default': False, 'type': 'boolean'}, 'is_chunk': {'title': 'Is Chunk', 'default': True, 'enum': [True], 'type': 'boolean'}}, 'required': ['content']}, 'ChatMessageChunk': {'title': 'ChatMessageChunk', 'description': 'A Chat Message chunk.', 'type': 'object', 'properties': {'content': {'title': 'Content', 'type': 'string'}, 'additional_kwargs': {'title': 'Additional Kwargs', 'type': 'object'}, 'type': {'title': 'Type', 'default': 'chat', 'enum': ['chat'], 'type': 'string'}, 'role': {'title': 'Role', 'type': 'string'}, 'is_chunk': {'title': 'Is Chunk', 'default': True, 'enum': [True], 'type': 'boolean'}}, 'required': ['content', 'role']}, 'FunctionMessageChunk': {'title': 'FunctionMessageChunk', 'description': 'A Function Message chunk.', 'type': 'object', 'properties': {'content': {'title': 'Content', 'type': 'string'}, 'additional_kwargs': {'title': 'Additional Kwargs', 'type': 'object'}, 'type': {'title': 'Type', 'default': 'function', 'enum': ['function'], 'type': 'string'}, 'name': {'title': 'Name', 'type': 'string'}, 'is_chunk': {'title': 'Is Chunk', 'default': True, 'enum': [True], 'type': 'boolean'}}, 'required': ['content', 'name']}, 'SystemMessageChunk': {'title': 'SystemMessageChunk', 'description': 'A System Message chunk.', 'type': 'object', 'properties': {'content': {'title': 'Content', 'type': 'string'}, 'additional_kwargs': {'title': 'Additional Kwargs', 'type': 'object'}, 'type': {'title': 'Type', 'default': 'system', 'enum': ['system'], 'type': 'string'}, 'is_chunk': {'title': 'Is Chunk', 'default': True, 'enum': [True], 'type': 'boolean'}}, 'required': ['content']}}}Stream​for s in chain.stream({""topic"": ""bears""}): print(s.content, end="""", flush=True) Why don't bears wear shoes? Because they have bear feet!Invoke​chain.invoke({""topic"": ""bears""}) AIMessage(content=""Why don't bears wear shoes?\n\nBecause they have bear feet!"")Batch​chain.batch([{""topic"": ""bears""}, {""topic"": ""cats""}]) [AIMessage(content=""Why don't bears wear shoes?\n\nBecause they have bear feet!""), AIMessage(content=""Why don't cats play poker in the wild?\n\nToo many cheetahs!"")]You can set the number of concurrent requests by using the max_concurrency parameterchain.batch([{""topic"": ""bears""}, {""topic"": ""cats""}], config={""max_concurrency"": 5}) [AIMessage(content=""Why don't bears wear shoes?\n\nBecause they have bear feet!""), AIMessage(content=""Sure, here's a cat joke for you:\n\nWhy don't cats play poker in the wild?\n\nToo many cheetahs!"")]Async Stream​async for s in chain.astream({""topic"": ""bears""}): print(s.content, end="""", flush=True) Sure, here's a bear joke for you: Why don't bears wear shoes? Because they have bear feet!Async Invoke​await chain.ainvoke({""topic"": ""bears""}) AIMessage(content=""Why don't bears wear shoes? \n\nBecause they have bear feet!"")Async Batch​await chain.abatch([{""topic"": ""bears""}]) [AIMessage(content=""Why don't bears wear shoes?\n\nBecause they have bear feet!"")]Async Stream Intermediate Steps​All runnables also have a method .astream_log() which can be used to stream (as they happen) all or part of the intermediate steps of your chain/sequence. This is useful eg. to show progress to the user, to use intermediate results, or even just to debug your chain.You can choose to stream all steps (default), or include/exclude steps by name, tags or metadata.This method yields JSONPatch ops that when applied in the same order as received build up the RunState.class LogEntry(TypedDict): id: str """"""ID of the sub-run."""""" name: str """"""Name of the object being run."""""" type: str """"""Type of the object being run, eg. prompt, chain, llm, etc."""""" tags: List[str] """"""List of tags for the run."""""" metadata: Dict[str, Any] """"""Key-value pairs of metadata for the run."""""" start_time: str """"""ISO-8601 timestamp of when the run started."""""" streamed_output_str: List[str] """"""List of LLM tokens streamed by this run, if applicable."""""" final_output: Optional[Any] """"""Final output of this run. Only available after the run has finished successfully."""""" end_time: Optional[str] """"""ISO-8601 timestamp of when the run ended. Only available after the run has finished.""""""class RunState(TypedDict): id: str """"""ID of the run."""""" streamed_output: List[Any] """"""List of output chunks streamed by Runnable.stream()"""""" final_output: Optional[Any] """"""Final output of the run, usually the result of aggregating (`+`) streamed_output. Only available after the run has finished successfully."""""" logs: Dict[str, LogEntry] """"""Map of run names to sub-runs. If filters were supplied, this list will contain only the runs that matched the filters.""""""Streaming JSONPatch chunks​This is useful eg. to stream the JSONPatch in an HTTP server, and then apply the ops on the client to rebuild the run state there. See LangServe for tooling to make it easier to build a webserver from any Runnable.from langchain.embeddings import OpenAIEmbeddingsfrom langchain.schema.output_parser import StrOutputParserfrom langchain.schema.runnable import RunnablePassthroughfrom langchain.vectorstores import FAISStemplate = """"""Answer the question based only on the following context:{context}Question: {question}""""""prompt = ChatPromptTemplate.from_template(template)vectorstore = FAISS.from_texts([""harrison worked at kensho""], embedding=OpenAIEmbeddings())retriever = vectorstore.as_retriever()retrieval_chain = ( {""context"": retriever.with_config(run_name='Docs'), ""question"": RunnablePassthrough()} | prompt | model | StrOutputParser())async for chunk in retrieval_chain.astream_log(""where did harrison work?"", include_names=['Docs']): print(chunk) RunLogPatch({'op': 'replace', 'path': '', 'value': {'final_output': None, 'id': 'fd6fcf62-c92c-4edf-8713-0fc5df000f62', 'logs': {}, 'streamed_output': []}}) RunLogPatch({'op': 'add', 'path': '/logs/Docs', 'value': {'end_time': None, 'final_output': None, 'id': '8c998257-1ec8-4546-b744-c3fdb9728c41', 'metadata': {}, 'name': 'Docs', 'start_time': '2023-10-05T12:52:35.668', 'streamed_output_str': [], 'tags': ['map:key:context', 'FAISS'], 'type': 'retriever'}}) RunLogPatch({'op': 'add', 'path': '/logs/Docs/final_output', 'value': {'documents': [Document(page_content='harrison worked at kensho')]}}, {'op': 'add', 'path': '/logs/Docs/end_time', 'value': '2023-10-05T12:52:36.033'}) RunLogPatch({'op': 'add', 'path': '/streamed_output/-', 'value': ''}) RunLogPatch({'op': 'add', 'path': '/streamed_output/-', 'value': 'H'}) RunLogPatch({'op': 'add', 'path': '/streamed_output/-', 'value': 'arrison'}) RunLogPatch({'op': 'add', 'path': '/streamed_output/-', 'value': ' worked'}) RunLogPatch({'op': 'add', 'path': '/streamed_output/-', 'value': ' at'}) RunLogPatch({'op': 'add', 'path': '/streamed_output/-', 'value': ' Kens'}) RunLogPatch({'op': 'add', 'path': '/streamed_output/-', 'value': 'ho'}) RunLogPatch({'op': 'add', 'path': '/streamed_output/-', 'value': '.'}) RunLogPatch({'op': 'add', 'path': '/streamed_output/-', 'value': ''}) RunLogPatch({'op': 'replace', 'path': '/final_output', 'value': {'output': 'Harrison worked at Kensho.'}})Streaming the incremental RunState​You can simply pass diff=False to get incremental values of RunState.async for chunk in retrieval_chain.astream_log(""where did harrison work?"", include_names=['Docs'], diff=False): print(chunk) RunLog({'final_output': None, 'id': 'f95ccb87-31f1-48ea-a51c-d2dadde44185', 'logs': {}, 'streamed_output': []}) RunLog({'final_output': None, 'id': 'f95ccb87-31f1-48ea-a51c-d2dadde44185', 'logs': {'Docs': {'end_time': None, 'final_output': None, 'id': '621597dd-d716-4532-938d-debc21a453d1', 'metadata': {}, 'name': 'Docs', 'start_time': '2023-10-05T12:52:36.935', 'streamed_output_str': [], 'tags': ['map:key:context', 'FAISS'], 'type': 'retriever'}}, 'streamed_output': []}) RunLog({'final_output': None, 'id': 'f95ccb87-31f1-48ea-a51c-d2dadde44185', 'logs': {'Docs': {'end_time': '2023-10-05T12:52:37.217', 'final_output': {'documents': [Document(page_content='harrison worked at kensho')]}, 'id': '621597dd-d716-4532-938d-debc21a453d1', 'metadata': {}, 'name': 'Docs', 'start_time': '2023-10-05T12:52:36.935', 'streamed_output_str': [], 'tags': ['map:key:context', 'FAISS'], 'type': 'retriever'}}, 'streamed_output': []}) RunLog({'final_output': None, 'id': 'f95ccb87-31f1-48ea-a51c-d2dadde44185', 'logs': {'Docs': {'end_time': '2023-10-05T12:52:37.217', 'final_output': {'documents': [Document(page_content='harrison worked at kensho')]}, 'id': '621597dd-d716-4532-938d-debc21a453d1', 'metadata': {}, 'name': 'Docs', 'start_time': '2023-10-05T12:52:36.935', 'streamed_output_str': [], 'tags': ['map:key:context', 'FAISS'], 'type': 'retriever'}}, 'streamed_output': ['']}) RunLog({'final_output': None, 'id': 'f95ccb87-31f1-48ea-a51c-d2dadde44185', 'logs': {'Docs': {'end_time': '2023-10-05T12:52:37.217', 'final_output': {'documents': [Document(page_content='harrison worked at kensho')]}, 'id': '621597dd-d716-4532-938d-debc21a453d1', 'metadata': {}, 'name': 'Docs', 'start_time': '2023-10-05T12:52:36.935', 'streamed_output_str': [], 'tags': ['map:key:context', 'FAISS'], 'type': 'retriever'}}, 'streamed_output': ['', 'H']}) RunLog({'final_output': None, 'id': 'f95ccb87-31f1-48ea-a51c-d2dadde44185', 'logs': {'Docs': {'end_time': '2023-10-05T12:52:37.217', 'final_output': {'documents': [Document(page_content='harrison worked at kensho')]}, 'id': '621597dd-d716-4532-938d-debc21a453d1', 'metadata': {}, 'name': 'Docs', 'start_time': '2023-10-05T12:52:36.935', 'streamed_output_str': [], 'tags': ['map:key:context', 'FAISS'], 'type': 'retriever'}}, 'streamed_output': ['', 'H', 'arrison']}) RunLog({'final_output': None, 'id': 'f95ccb87-31f1-48ea-a51c-d2dadde44185', 'logs': {'Docs': {'end_time': '2023-10-05T12:52:37.217', 'final_output': {'documents': [Document(page_content='harrison worked at kensho')]}, 'id': '621597dd-d716-4532-938d-debc21a453d1', 'metadata': {}, 'name': 'Docs', 'start_time': '2023-10-05T12:52:36.935', 'streamed_output_str': [], 'tags': ['map:key:context', 'FAISS'], 'type': 'retriever'}}, 'streamed_output': ['', 'H', 'arrison', ' worked']}) RunLog({'final_output': None, 'id': 'f95ccb87-31f1-48ea-a51c-d2dadde44185', 'logs': {'Docs': {'end_time': '2023-10-05T12:52:37.217', 'final_output': {'documents': [Document(page_content='harrison worked at kensho')]}, 'id': '621597dd-d716-4532-938d-debc21a453d1', 'metadata': {}, 'name': 'Docs', 'start_time': '2023-10-05T12:52:36.935', 'streamed_output_str': [], 'tags': ['map:key:context', 'FAISS'], 'type': 'retriever'}}, 'streamed_output': ['', 'H', 'arrison', ' worked', ' at']}) RunLog({'final_output': None, 'id': 'f95ccb87-31f1-48ea-a51c-d2dadde44185', 'logs': {'Docs': {'end_time': '2023-10-05T12:52:37.217', 'final_output': {'documents': [Document(page_content='harrison worked at kensho')]}, 'id': '621597dd-d716-4532-938d-debc21a453d1', 'metadata': {}, 'name': 'Docs', 'start_time': '2023-10-05T12:52:36.935', 'streamed_output_str': [], 'tags': ['map:key:context', 'FAISS'], 'type': 'retriever'}}, 'streamed_output': ['', 'H', 'arrison', ' worked', ' at', ' Kens']}) RunLog({'final_output': None, 'id': 'f95ccb87-31f1-48ea-a51c-d2dadde44185', 'logs': {'Docs': {'end_time': '2023-10-05T12:52:37.217', 'final_output': {'documents': [Document(page_content='harrison worked at kensho')]}, 'id': '621597dd-d716-4532-938d-debc21a453d1', 'metadata': {}, 'name': 'Docs', 'start_time': '2023-10-05T12:52:36.935', 'streamed_output_str': [], 'tags': ['map:key:context', 'FAISS'], 'type': 'retriever'}}, 'streamed_output': ['', 'H', 'arrison', ' worked', ' at', ' Kens', 'ho']}) RunLog({'final_output': None, 'id': 'f95ccb87-31f1-48ea-a51c-d2dadde44185', 'logs': {'Docs': {'end_time': '2023-10-05T12:52:37.217', 'final_output': {'documents': [Document(page_content='harrison worked at kensho')]}, 'id': '621597dd-d716-4532-938d-debc21a453d1', 'metadata': {}, 'name': 'Docs', 'start_time': '2023-10-05T12:52:36.935', 'streamed_output_str': [], 'tags': ['map:key:context', 'FAISS'], 'type': 'retriever'}}, 'streamed_output': ['', 'H', 'arrison', ' worked', ' at', ' Kens', 'ho', '.']}) RunLog({'final_output': None, 'id': 'f95ccb87-31f1-48ea-a51c-d2dadde44185', 'logs': {'Docs': {'end_time': '2023-10-05T12:52:37.217', 'final_output': {'documents': [Document(page_content='harrison worked at kensho')]}, 'id': '621597dd-d716-4532-938d-debc21a453d1', 'metadata': {}, 'name': 'Docs', 'start_time': '2023-10-05T12:52:36.935', 'streamed_output_str': [], 'tags': ['map:key:context', 'FAISS'], 'type': 'retriever'}}, 'streamed_output': ['', 'H', 'arrison', ' worked', ' at', ' Kens', 'ho', '.', '']}) RunLog({'final_output': {'output': 'Harrison worked at Kensho.'}, 'id': 'f95ccb87-31f1-48ea-a51c-d2dadde44185', 'logs': {'Docs': {'end_time': '2023-10-05T12:52:37.217', 'final_output': {'documents': [Document(page_content='harrison worked at kensho')]}, 'id': '621597dd-d716-4532-938d-debc21a453d1', 'metadata': {}, 'name': 'Docs', 'start_time': '2023-10-05T12:52:36.935', 'streamed_output_str': [], 'tags': ['map:key:context', 'FAISS'], 'type': 'retriever'}}, 'streamed_output': ['', 'H', 'arrison', ' worked', ' at', ' Kens', 'ho', '.', '']})Parallelism​Let's take a look at how LangChain Expression Language support parallel requests as much as possible. For example, when using a RunnableParallel (often written as a dictionary) it executes each element in parallel.from langchain.schema.runnable import RunnableParallelchain1 = ChatPromptTemplate.from_template(""tell me a joke about {topic}"") | modelchain2 = ChatPromptTemplate.from_template(""write a short (2 line) poem about {topic}"") | modelcombined = RunnableParallel(joke=chain1, poem=chain2)chain1.invoke({""topic"": ""bears""}) CPU times: user 31.7 ms, sys: 8.59 ms, total: 40.3 ms Wall time: 1.05 s AIMessage(content=""Why don't bears like fast food?\n\nBecause they can't catch it!"", additional_kwargs={}, example=False)chain2.invoke({""topic"": ""bears""}) CPU times: user 42.9 ms, sys: 10.2 ms, total: 53 ms Wall time: 1.93 s AIMessage(content=""In forest's embrace, bears roam free,\nSilent strength, nature's majesty."", additional_kwargs={}, example=False)combined.invoke({""topic"": ""bears""}) CPU times: user 96.3 ms, sys: 20.4 ms, total: 117 ms Wall time: 1.1 s {'joke': AIMessage(content=""Why don't bears wear socks?\n\nBecause they have bear feet!"", additional_kwargs={}, example=False), 'poem': AIMessage(content=""In forest's embrace,\nMajestic bears leave their trace."", additional_kwargs={}, example=False)}PreviousLangChain Expression Language (LCEL)NextHow toInput SchemaOutput SchemaStreamInvokeBatchAsync StreamAsync InvokeAsync BatchAsync Stream Intermediate StepsStreaming JSONPatch chunksStreaming the incremental RunStateParallelism" 6,https://python.langchain.com/docs/expression_language/how_to/,"LangChain Expression LanguageHow toHow to📄️ Bind runtime argsSometimes we want to invoke a Runnable within a Runnable sequence with constant arguments that are not part of the output of the preceding Runnable in the sequence, and which are not part of the user input. We can use Runnable.bind() to easily pass these arguments in.📄️ Add fallbacksThere are many possible points of failure in an LLM application, whether that be issues with LLM API's, poor model outputs, issues with other integrations, etc. Fallbacks help you gracefully handle and isolate these issues.📄️ Run arbitrary functionsYou can use arbitrary functions in the pipeline📄️ Use RunnableParallel/RunnableMapRunnableParallel (aka. RunnableMap) makes it easy to execute multiple Runnables in parallel, and to return the output of these Runnables as a map.📄️ Route between multiple RunnablesThis notebook covers how to do routing in the LangChain Expression Language.PreviousInterfaceNextBind runtime args" 7,https://python.langchain.com/docs/expression_language/how_to/binding,"LangChain Expression LanguageHow toBind runtime argsOn this pageBind runtime argsSometimes we want to invoke a Runnable within a Runnable sequence with constant arguments that are not part of the output of the preceding Runnable in the sequence, and which are not part of the user input. We can use Runnable.bind() to easily pass these arguments in.Suppose we have a simple prompt + model sequence:from langchain.chat_models import ChatOpenAIfrom langchain.prompts import ChatPromptTemplatefrom langchain.schema import StrOutputParserfrom langchain.schema.runnable import RunnablePassthroughprompt = ChatPromptTemplate.from_messages( [ (""system"", ""Write out the following equation using algebraic symbols then solve it. Use the format\n\nEQUATION:...\nSOLUTION:...\n\n""), (""human"", ""{equation_statement}"") ])model = ChatOpenAI(temperature=0)runnable = {""equation_statement"": RunnablePassthrough()} | prompt | model | StrOutputParser()print(runnable.invoke(""x raised to the third plus seven equals 12"")) EQUATION: x^3 + 7 = 12 SOLUTION: Subtracting 7 from both sides of the equation, we get: x^3 = 12 - 7 x^3 = 5 Taking the cube root of both sides, we get: x = ∛5 Therefore, the solution to the equation x^3 + 7 = 12 is x = ∛5.and want to call the model with certain stop words:runnable = ( {""equation_statement"": RunnablePassthrough()} | prompt | model.bind(stop=""SOLUTION"") | StrOutputParser())print(runnable.invoke(""x raised to the third plus seven equals 12"")) EQUATION: x^3 + 7 = 12 Attaching OpenAI functions​One particularly useful application of binding is to attach OpenAI functions to a compatible OpenAI model:functions = [ { ""name"": ""solver"", ""description"": ""Formulates and solves an equation"", ""parameters"": { ""type"": ""object"", ""properties"": { ""equation"": { ""type"": ""string"", ""description"": ""The algebraic expression of the equation"" }, ""solution"": { ""type"": ""string"", ""description"": ""The solution to the equation"" } }, ""required"": [""equation"", ""solution""] } } ]# Need gpt-4 to solve this one correctlyprompt = ChatPromptTemplate.from_messages( [ (""system"", ""Write out the following equation using algebraic symbols then solve it.""), (""human"", ""{equation_statement}"") ])model = ChatOpenAI(model=""gpt-4"", temperature=0).bind(function_call={""name"": ""solver""}, functions=functions)runnable = ( {""equation_statement"": RunnablePassthrough()} | prompt | model)runnable.invoke(""x raised to the third plus seven equals 12"") AIMessage(content='', additional_kwargs={'function_call': {'name': 'solver', 'arguments': '{\n""equation"": ""x^3 + 7 = 12"",\n""solution"": ""x = ∛5""\n}'}}, example=False)PreviousHow toNextAdd fallbacksAttaching OpenAI functions" 8,https://python.langchain.com/docs/expression_language/how_to/fallbacks,"LangChain Expression LanguageHow toAdd fallbacksOn this pageAdd fallbacksThere are many possible points of failure in an LLM application, whether that be issues with LLM API's, poor model outputs, issues with other integrations, etc. Fallbacks help you gracefully handle and isolate these issues.Crucially, fallbacks can be applied not only on the LLM level but on the whole runnable level.Handling LLM API Errors​This is maybe the most common use case for fallbacks. A request to an LLM API can fail for a variety of reasons - the API could be down, you could have hit rate limits, any number of things. Therefore, using fallbacks can help protect against these types of things.IMPORTANT: By default, a lot of the LLM wrappers catch errors and retry. You will most likely want to turn those off when working with fallbacks. Otherwise the first wrapper will keep on retrying and not failing.from langchain.chat_models import ChatOpenAI, ChatAnthropicFirst, let's mock out what happens if we hit a RateLimitError from OpenAIfrom unittest.mock import patchfrom openai.error import RateLimitError# Note that we set max_retries = 0 to avoid retrying on RateLimits, etcopenai_llm = ChatOpenAI(max_retries=0)anthropic_llm = ChatAnthropic()llm = openai_llm.with_fallbacks([anthropic_llm])# Let's use just the OpenAI LLm first, to show that we run into an errorwith patch('openai.ChatCompletion.create', side_effect=RateLimitError()): try: print(openai_llm.invoke(""Why did the chicken cross the road?"")) except: print(""Hit error"") Hit error# Now let's try with fallbacks to Anthropicwith patch('openai.ChatCompletion.create', side_effect=RateLimitError()): try: print(llm.invoke(""Why did the the chicken cross the road?"")) except: print(""Hit error"") content=' I don\'t actually know why the chicken crossed the road, but here are some possible humorous answers:\n\n- To get to the other side!\n\n- It was too chicken to just stand there. \n\n- It wanted a change of scenery.\n\n- It wanted to show the possum it could be done.\n\n- It was on its way to a poultry farmers\' convention.\n\nThe joke plays on the double meaning of ""the other side"" - literally crossing the road to the other side, or the ""other side"" meaning the afterlife. So it\'s an anti-joke, with a silly or unexpected pun as the answer.' additional_kwargs={} example=FalseWe can use our ""LLM with Fallbacks"" as we would a normal LLM.from langchain.prompts import ChatPromptTemplateprompt = ChatPromptTemplate.from_messages( [ (""system"", ""You're a nice assistant who always includes a compliment in your response""), (""human"", ""Why did the {animal} cross the road""), ])chain = prompt | llmwith patch('openai.ChatCompletion.create', side_effect=RateLimitError()): try: print(chain.invoke({""animal"": ""kangaroo""})) except: print(""Hit error"") content="" I don't actually know why the kangaroo crossed the road, but I'm happy to take a guess! Maybe the kangaroo was trying to get to the other side to find some tasty grass to eat. Or maybe it was trying to get away from a predator or other danger. Kangaroos do need to cross roads and other open areas sometimes as part of their normal activities. Whatever the reason, I'm sure the kangaroo looked both ways before hopping across!"" additional_kwargs={} example=FalseSpecifying errors to handle​We can also specify the errors to handle if we want to be more specific about when the fallback is invoked:llm = openai_llm.with_fallbacks([anthropic_llm], exceptions_to_handle=(KeyboardInterrupt,))chain = prompt | llmwith patch('openai.ChatCompletion.create', side_effect=RateLimitError()): try: print(chain.invoke({""animal"": ""kangaroo""})) except: print(""Hit error"") Hit errorFallbacks for Sequences​We can also create fallbacks for sequences, that are sequences themselves. Here we do that with two different models: ChatOpenAI and then normal OpenAI (which does not use a chat model). Because OpenAI is NOT a chat model, you likely want a different prompt.# First let's create a chain with a ChatModel# We add in a string output parser here so the outputs between the two are the same typefrom langchain.schema.output_parser import StrOutputParserchat_prompt = ChatPromptTemplate.from_messages( [ (""system"", ""You're a nice assistant who always includes a compliment in your response""), (""human"", ""Why did the {animal} cross the road""), ])# Here we're going to use a bad model name to easily create a chain that will errorchat_model = ChatOpenAI(model_name=""gpt-fake"")bad_chain = chat_prompt | chat_model | StrOutputParser()# Now lets create a chain with the normal OpenAI modelfrom langchain.llms import OpenAIfrom langchain.prompts import PromptTemplateprompt_template = """"""Instructions: You should always include a compliment in your response.Question: Why did the {animal} cross the road?""""""prompt = PromptTemplate.from_template(prompt_template)llm = OpenAI()good_chain = prompt | llm# We can now create a final chain which combines the twochain = bad_chain.with_fallbacks([good_chain])chain.invoke({""animal"": ""turtle""}) '\n\nAnswer: The turtle crossed the road to get to the other side, and I have to say he had some impressive determination.'PreviousBind runtime argsNextRun arbitrary functionsHandling LLM API ErrorsSpecifying errors to handleFallbacks for Sequences" 9,https://python.langchain.com/docs/expression_language/how_to/functions,"LangChain Expression LanguageHow toRun arbitrary functionsOn this pageRun arbitrary functionsYou can use arbitrary functions in the pipelineNote that all inputs to these functions need to be a SINGLE argument. If you have a function that accepts multiple arguments, you should write a wrapper that accepts a single input and unpacks it into multiple argument.from langchain.schema.runnable import RunnableLambdafrom langchain.prompts import ChatPromptTemplatefrom langchain.chat_models import ChatOpenAIfrom operator import itemgetterdef length_function(text): return len(text)def _multiple_length_function(text1, text2): return len(text1) * len(text2)def multiple_length_function(_dict): return _multiple_length_function(_dict[""text1""], _dict[""text2""])prompt = ChatPromptTemplate.from_template(""what is {a} + {b}"")model = ChatOpenAI()chain1 = prompt | modelchain = { ""a"": itemgetter(""foo"") | RunnableLambda(length_function), ""b"": {""text1"": itemgetter(""foo""), ""text2"": itemgetter(""bar"")} | RunnableLambda(multiple_length_function)} | prompt | modelchain.invoke({""foo"": ""bar"", ""bar"": ""gah""}) AIMessage(content='3 + 9 equals 12.', additional_kwargs={}, example=False)Accepting a Runnable Config​Runnable lambdas can optionally accept a RunnableConfig, which they can use to pass callbacks, tags, and other configuration information to nested runs.from langchain.schema.runnable import RunnableConfigfrom langchain.schema.output_parser import StrOutputParserimport jsondef parse_or_fix(text: str, config: RunnableConfig): fixing_chain = ( ChatPromptTemplate.from_template( ""Fix the following text:\n\n```text\n{input}\n```\nError: {error}"" "" Don't narrate, just respond with the fixed data."" ) | ChatOpenAI() | StrOutputParser() ) for _ in range(3): try: return json.loads(text) except Exception as e: text = fixing_chain.invoke({""input"": text, ""error"": e}, config) return ""Failed to parse""from langchain.callbacks import get_openai_callbackwith get_openai_callback() as cb: RunnableLambda(parse_or_fix).invoke(""{foo: bar}"", {""tags"": [""my-tag""], ""callbacks"": [cb]}) print(cb) Tokens Used: 65 Prompt Tokens: 56 Completion Tokens: 9 Successful Requests: 1 Total Cost (USD): $0.00010200000000000001PreviousAdd fallbacksNextUse RunnableParallel/RunnableMapAccepting a Runnable Config" 10,https://python.langchain.com/docs/expression_language/how_to/map,"LangChain Expression LanguageHow toUse RunnableParallel/RunnableMapOn this pageUse RunnableParallel/RunnableMapRunnableParallel (aka. RunnableMap) makes it easy to execute multiple Runnables in parallel, and to return the output of these Runnables as a map.from langchain.chat_models import ChatOpenAIfrom langchain.prompts import ChatPromptTemplatefrom langchain.schema.runnable import RunnableParallelmodel = ChatOpenAI()joke_chain = ChatPromptTemplate.from_template(""tell me a joke about {topic}"") | modelpoem_chain = ChatPromptTemplate.from_template(""write a 2-line poem about {topic}"") | modelmap_chain = RunnableParallel(joke=joke_chain, poem=poem_chain)map_chain.invoke({""topic"": ""bear""}) {'joke': AIMessage(content=""Why don't bears wear shoes? \n\nBecause they have bear feet!"", additional_kwargs={}, example=False), 'poem': AIMessage(content=""In woodland depths, bear prowls with might,\nSilent strength, nature's sovereign, day and night."", additional_kwargs={}, example=False)}Manipulating outputs/inputs​Maps can be useful for manipulating the output of one Runnable to match the input format of the next Runnable in a sequence.from langchain.embeddings import OpenAIEmbeddingsfrom langchain.schema.output_parser import StrOutputParserfrom langchain.schema.runnable import RunnablePassthroughfrom langchain.vectorstores import FAISSvectorstore = FAISS.from_texts([""harrison worked at kensho""], embedding=OpenAIEmbeddings())retriever = vectorstore.as_retriever()template = """"""Answer the question based only on the following context:{context}Question: {question}""""""prompt = ChatPromptTemplate.from_template(template)retrieval_chain = ( {""context"": retriever, ""question"": RunnablePassthrough()} | prompt | model | StrOutputParser())retrieval_chain.invoke(""where did harrison work?"") 'Harrison worked at Kensho.'Here the input to prompt is expected to be a map with keys ""context"" and ""question"". The user input is just the question. So we need to get the context using our retriever and passthrough the user input under the ""question"" key.Note that when composing a RunnableMap when another Runnable we don't even need to wrap our dictuionary in the RunnableMap class — the type conversion is handled for us.Parallelism​RunnableMaps are also useful for running independent processes in parallel, since each Runnable in the map is executed in parallel. For example, we can see our earlier joke_chain, poem_chain and map_chain all have about the same runtime, even though map_chain executes both of the other two.joke_chain.invoke({""topic"": ""bear""}) 958 ms ± 402 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)poem_chain.invoke({""topic"": ""bear""}) 1.22 s ± 508 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)map_chain.invoke({""topic"": ""bear""}) 1.15 s ± 119 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)PreviousRun arbitrary functionsNextRoute between multiple RunnablesManipulating outputs/inputsParallelism" 11,https://python.langchain.com/docs/expression_language/how_to/routing,"LangChain Expression LanguageHow toRoute between multiple RunnablesOn this pageRoute between multiple RunnablesThis notebook covers how to do routing in the LangChain Expression Language.Routing allows you to create non-deterministic chains where the output of a previous step defines the next step. Routing helps provide structure and consistency around interactions with LLMs.There are two ways to perform routing:Using a RunnableBranch.Writing custom factory function that takes the input of a previous step and returns a runnable. Importantly, this should return a runnable and NOT actually execute.We'll illustrate both methods using a two step sequence where the first step classifies an input question as being about LangChain, Anthropic, or Other, then routes to a corresponding prompt chain.Using a RunnableBranch​A RunnableBranch is initialized with a list of (condition, runnable) pairs and a default runnable. It selects which branch by passing each condition the input it's invoked with. It selects the first condition to evaluate to True, and runs the corresponding runnable to that condition with the input. If no provided conditions match, it runs the default runnable.Here's an example of what it looks like in action:from langchain.prompts import PromptTemplatefrom langchain.chat_models import ChatAnthropicfrom langchain.schema.output_parser import StrOutputParserFirst, let's create a chain that will identify incoming questions as being about LangChain, Anthropic, or Other:chain = PromptTemplate.from_template(""""""Given the user question below, classify it as either being about `LangChain`, `Anthropic`, or `Other`. Do not respond with more than one word.{question}Classification:"""""") | ChatAnthropic() | StrOutputParser()chain.invoke({""question"": ""how do I call Anthropic?""}) ' Anthropic'Now, let's create three sub chains:langchain_chain = PromptTemplate.from_template(""""""You are an expert in langchain. \Always answer questions starting with ""As Harrison Chase told me"". \Respond to the following question:Question: {question}Answer:"""""") | ChatAnthropic()anthropic_chain = PromptTemplate.from_template(""""""You are an expert in anthropic. \Always answer questions starting with ""As Dario Amodei told me"". \Respond to the following question:Question: {question}Answer:"""""") | ChatAnthropic()general_chain = PromptTemplate.from_template(""""""Respond to the following question:Question: {question}Answer:"""""") | ChatAnthropic()from langchain.schema.runnable import RunnableBranchbranch = RunnableBranch( (lambda x: ""anthropic"" in x[""topic""].lower(), anthropic_chain), (lambda x: ""langchain"" in x[""topic""].lower(), langchain_chain), general_chain)full_chain = { ""topic"": chain, ""question"": lambda x: x[""question""]} | branchfull_chain.invoke({""question"": ""how do I use Anthropic?""}) AIMessage(content="" As Dario Amodei told me, here are some ways to use Anthropic:\n\n- Sign up for an account on Anthropic's website to access tools like Claude, Constitutional AI, and Writer. \n\n- Use Claude for tasks like email generation, customer service chat, and QA. Claude can understand natural language prompts and provide helpful responses.\n\n- Use Constitutional AI if you need an AI assistant that is harmless, honest, and helpful. It is designed to be safe and aligned with human values.\n\n- Use Writer to generate natural language content for things like marketing copy, stories, reports, and more. Give it a topic and prompt and it will create high-quality written content.\n\n- Check out Anthropic's documentation and blog for tips, tutorials, examples, and announcements about new capabilities as they continue to develop their AI technology.\n\n- Follow Anthropic on social media or subscribe to their newsletter to stay up to date on new features and releases.\n\n- For most people, the easiest way to leverage Anthropic's technology is through their website - just create an account to get started!"", additional_kwargs={}, example=False)full_chain.invoke({""question"": ""how do I use LangChain?""}) AIMessage(content=' As Harrison Chase told me, here is how you use LangChain:\n\nLangChain is an AI assistant that can have conversations, answer questions, and generate text. To use LangChain, you simply type or speak your input and LangChain will respond. \n\nYou can ask LangChain questions, have discussions, get summaries or explanations about topics, and request it to generate text on a subject. Some examples of interactions:\n\n- Ask general knowledge questions and LangChain will try to answer factually. For example ""What is the capital of France?""\n\n- Have conversations on topics by taking turns speaking. You can prompt the start of a conversation by saying something like ""Let\'s discuss machine learning""\n\n- Ask for summaries or high-level explanations on subjects. For example ""Can you summarize the main themes in Shakespeare\'s Hamlet?"" \n\n- Give creative writing prompts or requests to have LangChain generate text in different styles. For example ""Write a short children\'s story about a mouse"" or ""Generate a poem in the style of Robert Frost about nature""\n\n- Correct LangChain if it makes an inaccurate statement and provide the right information. This helps train it.\n\nThe key is interacting naturally and giving it clear prompts and requests', additional_kwargs={}, example=False)full_chain.invoke({""question"": ""whats 2 + 2""}) AIMessage(content=' 2 + 2 = 4', additional_kwargs={}, example=False)Using a custom function​You can also use a custom function to route between different outputs. Here's an example:def route(info): if ""anthropic"" in info[""topic""].lower(): return anthropic_chain elif ""langchain"" in info[""topic""].lower(): return langchain_chain else: return general_chainfrom langchain.schema.runnable import RunnableLambdafull_chain = { ""topic"": chain, ""question"": lambda x: x[""question""]} | RunnableLambda(route)full_chain.invoke({""question"": ""how do I use Anthroipc?""}) AIMessage(content=' As Dario Amodei told me, to use Anthropic IPC you first need to import it:\n\n```python\nfrom anthroipc import ic\n```\n\nThen you can create a client and connect to the server:\n\n```python \nclient = ic.connect()\n```\n\nAfter that, you can call methods on the client and get responses:\n\n```python\nresponse = client.ask(""What is the meaning of life?"")\nprint(response)\n```\n\nYou can also register callbacks to handle events: \n\n```python\ndef on_poke(event):\n print(""Got poked!"")\n\nclient.on(\'poke\', on_poke)\n```\n\nAnd that\'s the basics of using the Anthropic IPC client library for Python! Let me know if you have any other questions!', additional_kwargs={}, example=False)full_chain.invoke({""question"": ""how do I use LangChain?""}) AIMessage(content=' As Harrison Chase told me, to use LangChain you first need to sign up for an API key at platform.langchain.com. Once you have your API key, you can install the Python library and write a simple Python script to call the LangChain API. Here is some sample code to get started:\n\n```python\nimport langchain\n\napi_key = ""YOUR_API_KEY""\n\nlangchain.set_key(api_key)\n\nresponse = langchain.ask(""What is the capital of France?"")\n\nprint(response.response)\n```\n\nThis will send the question ""What is the capital of France?"" to the LangChain API and print the response. You can customize the request by providing parameters like max_tokens, temperature, etc. The LangChain Python library documentation has more details on the available options. The key things are getting an API key and calling langchain.ask() with your question text. Let me know if you have any other questions!', additional_kwargs={}, example=False)full_chain.invoke({""question"": ""whats 2 + 2""}) AIMessage(content=' 4', additional_kwargs={}, example=False)PreviousUse RunnableParallel/RunnableMapNextCookbookUsing a RunnableBranchUsing a custom function" 12,https://python.langchain.com/docs/expression_language/cookbook/,"LangChain Expression LanguageCookbookCookbookExample 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 page is a good place to start.📄️ Prompt + LLMThe most common and valuable composition is taking:📄️ RAGLet's look at adding in a retrieval step to a prompt and LLM, which adds up to a ""retrieval-augmented generation"" chain📄️ Multiple chainsRunnables can easily be used to string together multiple Chains📄️ Querying a SQL DBWe can replicate our SQLDatabaseChain with Runnables.📄️ AgentsYou can pass a Runnable into an agent.📄️ Code writingExample of how to use LCEL to write Python code.📄️ Adding memoryThis shows how to add memory to an arbitrary chain. Right now, you can use the memory classes but need to hook it up manually📄️ Adding moderationThis shows how to add in moderation (or other safeguards) around your LLM application.📄️ Using toolsYou can use any Tools with Runnables easily.PreviousRoute between multiple RunnablesNextPrompt + LLM" 13,https://python.langchain.com/docs/expression_language/cookbook/prompt_llm_parser,"LangChain Expression LanguageCookbookPrompt + LLMOn this pagePrompt + LLMThe most common and valuable composition is taking:PromptTemplate / ChatPromptTemplate -> LLM / ChatModel -> OutputParserAlmost any other chains you build will use this building block.PromptTemplate + LLM​The simplest composition is just combing 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 input.Note, you can mix and match PromptTemplate/ChatPromptTemplates and LLMs/ChatModels as you like here.from langchain.prompts import ChatPromptTemplatefrom langchain.chat_models import ChatOpenAIprompt = ChatPromptTemplate.from_template(""tell me a joke about {foo}"")model = ChatOpenAI()chain = prompt | modelchain.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's a few examples of that: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​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​We can also add in an output parser to easily trasform the raw LLM/ChatModel output into a more workable formatfrom langchain.schema.output_parser import StrOutputParserchain = prompt | model | StrOutputParser()Notice that this now returns a string - a much more workable format for downstream taskschain.invoke({""foo"": ""bears""}) ""Why don't bears wear shoes?\n\nBecause they have bear feet!""Functions Output Parser​When you specify the function to return, you may just want to parse that directlyfrom langchain.output_parsers.openai_functions import JsonOutputFunctionsParserchain = ( 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 JsonKeyOutputFunctionsParserchain = ( 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​To make invocation even simpler, we can add a RunnableMap to take care of creating the prompt input dict for us:from langchain.schema.runnable import RunnableMap, RunnablePassthroughmap_ = RunnableMap(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?""PreviousCookbookNextRAGPromptTemplate + LLMAttaching Stop SequencesAttaching Function Call informationPromptTemplate + LLM + OutputParserFunctions Output ParserSimplifying input" 14,https://python.langchain.com/docs/expression_language/cookbook/retrieval,"LangChain Expression LanguageCookbookRAGOn this pageRAGLet's look at adding in a retrieval step to a prompt and LLM, which adds up to a ""retrieval-augmented generation"" chainpip install langchain openai faiss-cpu tiktokenfrom operator import itemgetterfrom langchain.prompts import ChatPromptTemplatefrom langchain.chat_models import ChatOpenAIfrom langchain.embeddings import OpenAIEmbeddingsfrom langchain.schema.output_parser import StrOutputParserfrom langchain.schema.runnable import RunnablePassthroughfrom langchain.vectorstores import FAISSvectorstore = FAISS.from_texts([""harrison worked at kensho""], embedding=OpenAIEmbeddings())retriever = vectorstore.as_retriever()template = """"""Answer the question based only on the following context:{context}Question: {question}""""""prompt = ChatPromptTemplate.from_template(template)model = ChatOpenAI()chain = ( {""context"": retriever, ""question"": RunnablePassthrough()} | prompt | model | StrOutputParser())chain.invoke(""where did harrison work?"") 'Harrison worked at Kensho.'template = """"""Answer the question based only on the following context:{context}Question: {question}Answer in the following language: {language}""""""prompt = ChatPromptTemplate.from_template(template)chain = { ""context"": itemgetter(""question"") | retriever, ""question"": itemgetter(""question""), ""language"": itemgetter(""language"")} | prompt | model | StrOutputParser()chain.invoke({""question"": ""where did harrison work"", ""language"": ""italian""}) 'Harrison ha lavorato a Kensho.'Conversational Retrieval Chain​We can easily add in conversation history. This primarily means adding in chat_message_historyfrom langchain.schema.runnable import RunnableMapfrom langchain.schema import format_documentfrom langchain.prompts.prompt import PromptTemplate_template = """"""Given the following conversation and a follow up question, rephrase the follow up question to be a standalone question, in its original language.Chat History:{chat_history}Follow Up Input: {question}Standalone question:""""""CONDENSE_QUESTION_PROMPT = PromptTemplate.from_template(_template)template = """"""Answer the question based only on the following context:{context}Question: {question}""""""ANSWER_PROMPT = ChatPromptTemplate.from_template(template)DEFAULT_DOCUMENT_PROMPT = PromptTemplate.from_template(template=""{page_content}"")def _combine_documents(docs, document_prompt = DEFAULT_DOCUMENT_PROMPT, document_separator=""\n\n""): doc_strings = [format_document(doc, document_prompt) for doc in docs] return document_separator.join(doc_strings)from typing import Tuple, Listdef _format_chat_history(chat_history: List[Tuple]) -> str: buffer = """" for dialogue_turn in chat_history: human = ""Human: "" + dialogue_turn[0] ai = ""Assistant: "" + dialogue_turn[1] buffer += ""\n"" + ""\n"".join([human, ai]) return buffer_inputs = RunnableMap( standalone_question=RunnablePassthrough.assign( chat_history=lambda x: _format_chat_history(x['chat_history']) ) | CONDENSE_QUESTION_PROMPT | ChatOpenAI(temperature=0) | StrOutputParser(),)_context = { ""context"": itemgetter(""standalone_question"") | retriever | _combine_documents, ""question"": lambda x: x[""standalone_question""]}conversational_qa_chain = _inputs | _context | ANSWER_PROMPT | ChatOpenAI()conversational_qa_chain.invoke({ ""question"": ""where did harrison work?"", ""chat_history"": [],}) AIMessage(content='Harrison was employed at Kensho.', additional_kwargs={}, example=False)conversational_qa_chain.invoke({ ""question"": ""where did he work?"", ""chat_history"": [(""Who wrote this notebook?"", ""Harrison"")],}) AIMessage(content='Harrison worked at Kensho.', additional_kwargs={}, example=False)With Memory and returning source documents​This shows how to use memory with the above. For memory, we need to manage that outside at the memory. For returning the retrieved documents, we just need to pass them through all the way.from operator import itemgetterfrom langchain.memory import ConversationBufferMemorymemory = ConversationBufferMemory(return_messages=True, output_key=""answer"", input_key=""question"")# First we add a step to load memory# This adds a ""memory"" key to the input objectloaded_memory = RunnablePassthrough.assign( chat_history=memory.load_memory_variables | itemgetter(""history""),)# Now we calculate the standalone questionstandalone_question = { ""standalone_question"": { ""question"": lambda x: x[""question""], ""chat_history"": lambda x: _format_chat_history(x['chat_history']) } | CONDENSE_QUESTION_PROMPT | ChatOpenAI(temperature=0) | StrOutputParser(),}# Now we retrieve the documentsretrieved_documents = { ""docs"": itemgetter(""standalone_question"") | retriever, ""question"": lambda x: x[""standalone_question""]}# Now we construct the inputs for the final promptfinal_inputs = { ""context"": lambda x: _combine_documents(x[""docs""]), ""question"": itemgetter(""question"")}# And finally, we do the part that returns the answersanswer = { ""answer"": final_inputs | ANSWER_PROMPT | ChatOpenAI(), ""docs"": itemgetter(""docs""),}# And now we put it all together!final_chain = loaded_memory | expanded_memory | standalone_question | retrieved_documents | answerinputs = {""question"": ""where did harrison work?""}result = final_chain.invoke(inputs)result {'answer': AIMessage(content='Harrison was employed at Kensho.', additional_kwargs={}, example=False), 'docs': [Document(page_content='harrison worked at kensho', metadata={})]}# Note that the memory does not save automatically# This will be improved in the future# For now you need to save it yourselfmemory.save_context(inputs, {""answer"": result[""answer""].content})memory.load_memory_variables({}) {'history': [HumanMessage(content='where did harrison work?', additional_kwargs={}, example=False), AIMessage(content='Harrison was employed at Kensho.', additional_kwargs={}, example=False)]}PreviousPrompt + LLMNextMultiple chainsConversational Retrieval ChainWith Memory and returning source documents" 15,https://python.langchain.com/docs/expression_language/cookbook/multiple_chains,"LangChain Expression LanguageCookbookMultiple chainsOn this pageMultiple chainsRunnables can easily be used to string together multiple Chainsfrom operator import itemgetterfrom langchain.chat_models import ChatOpenAIfrom langchain.prompts import ChatPromptTemplatefrom langchain.schema import StrOutputParserprompt1 = 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 RunnableMap, RunnablePassthroughprompt1 = 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_parsercolor_to_country = prompt3 | model_parserquestion_generator = color_generator | {""fruit"": color_to_fruit, ""country"": color_to_country} | prompt4question_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. RunnableMaps 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 \ / \ / Combineplanner = ( 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 and continuous improvement, organizations can leverage Scrum to drive innovation, efficiency, and project success.'PreviousRAGNextQuerying a SQL DBBranching and Merging" 16,https://python.langchain.com/docs/expression_language/cookbook/sql_db,"LangChain Expression LanguageCookbookQuerying a SQL DBQuerying a SQL DBWe can replicate our SQLDatabaseChain with Runnables.from langchain.prompts import ChatPromptTemplatetemplate = """"""Based on the table schema below, write a SQL query that would answer the user's question:{schema}Question: {question}SQL Query:""""""prompt = ChatPromptTemplate.from_template(template)from langchain.utilities import SQLDatabaseWe'll need the Chinook sample DB for this example. There's many places to download it from, e.g. https://database.guide/2-sample-databases-sqlite/db = SQLDatabase.from_uri(""sqlite:///./Chinook.db"")def get_schema(_): return db.get_table_info()def run_query(query): return db.run(query)from langchain.chat_models import ChatOpenAIfrom langchain.schema.output_parser import StrOutputParserfrom langchain.schema.runnable import RunnablePassthroughmodel = ChatOpenAI()sql_response = ( RunnablePassthrough.assign(schema=get_schema) | prompt | model.bind(stop=[""\nSQLResult:""]) | StrOutputParser() )sql_response.invoke({""question"": ""How many employees are there?""}) 'SELECT COUNT(*) FROM Employee'template = """"""Based on the table schema below, question, sql query, and sql response, write a natural language response:{schema}Question: {question}SQL Query: {query}SQL Response: {response}""""""prompt_response = ChatPromptTemplate.from_template(template)full_chain = ( RunnablePassthrough.assign(query=sql_response) | RunnablePassthrough.assign( schema=get_schema, response=lambda x: db.run(x[""query""]), ) | prompt_response | model)full_chain.invoke({""question"": ""How many employees are there?""}) AIMessage(content='There are 8 employees.', additional_kwargs={}, example=False)PreviousMultiple chainsNextAgents" 17,https://python.langchain.com/docs/expression_language/cookbook/agent,"LangChain Expression LanguageCookbookAgentsAgentsYou can pass a Runnable into an agent.from langchain.agents import XMLAgent, tool, AgentExecutorfrom langchain.chat_models import ChatAnthropicmodel = ChatAnthropic(model=""claude-2"")@tooldef search(query: str) -> str: """"""Search things about current events."""""" return ""32 degrees""tool_list = [search]# Get prompt to useprompt = XMLAgent.get_default_prompt()# Logic for going from intermediate steps to a string to pass into model# This is pretty tied to the promptdef convert_intermediate_steps(intermediate_steps): log = """" for action, observation in intermediate_steps: log += ( f""{action.tool}{action.tool_input}"" f""{observation}"" ) return log# Logic for converting tools to string to go in promptdef 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 promptThe prompt itselfThe model, complete with stop tokens if neededThe 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=["""", """"]) | 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... search weather in new york32 degrees 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'}PreviousQuerying a SQL DBNextCode writing" 18,https://python.langchain.com/docs/expression_language/cookbook/code_writing,"LangChain Expression LanguageCookbookCode writingCode writingExample of how to use LCEL to write Python code.from langchain.chat_models import ChatOpenAIfrom langchain.prompts import ChatPromptTemplate, SystemMessagePromptTemplate, HumanMessagePromptTemplatefrom langchain.schema.output_parser import StrOutputParserfrom langchain.utilities import PythonREPLtemplate = """"""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().runchain.invoke({""input"": ""whats 2 plus 2""}) Python REPL can execute arbitrary code. Use with caution. '4\n'PreviousAgentsNextAdding memory" 19,https://python.langchain.com/docs/expression_language/cookbook/memory,"LangChain Expression LanguageCookbookAdding memoryAdding memoryThis shows how to add memory to an arbitrary chain. Right now, you can use the memory classes but need to hook it up manuallyfrom operator import itemgetterfrom langchain.chat_models import ChatOpenAIfrom langchain.memory import ConversationBufferMemoryfrom langchain.schema.runnable import RunnablePassthroughfrom langchain.prompts import ChatPromptTemplate, MessagesPlaceholdermodel = 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( memory=memory.load_memory_variables | itemgetter(""history"")) | prompt | modelinputs = {""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)PreviousCode writingNextAdding moderation" 20,https://python.langchain.com/docs/expression_language/cookbook/moderation,"LangChain Expression LanguageCookbookAdding moderationAdding moderationThis shows how to add in moderation (or other safeguards) around your LLM application.from langchain.chains import OpenAIModerationChainfrom langchain.llms import OpenAIfrom langchain.prompts import ChatPromptTemplatemoderate = OpenAIModerationChain()model = OpenAI()prompt = ChatPromptTemplate.from_messages([ (""system"", ""repeat after me: {input}"")])chain = prompt | modelchain.invoke({""input"": ""you are stupid""}) '\n\nYou are stupid.'moderated_chain = chain | moderatemoderated_chain.invoke({""input"": ""you are stupid""}) {'input': '\n\nYou are stupid', 'output': ""Text was found that violates OpenAI's content policy.""}PreviousAdding memoryNextUsing tools" 21,https://python.langchain.com/docs/expression_language/cookbook/tools,"LangChain Expression LanguageCookbookUsing toolsUsing toolsYou can use any Tools with Runnables easily.pip install duckduckgo-searchfrom langchain.chat_models import ChatOpenAIfrom langchain.prompts import ChatPromptTemplatefrom langchain.schema.output_parser import StrOutputParserfrom langchain.tools import DuckDuckGoSearchRunsearch = DuckDuckGoSearchRun()template = """"""turn the following user input into a search query for a search engine:{input}""""""prompt = ChatPromptTemplate.from_template(template)model = ChatOpenAI()chain = prompt | model | StrOutputParser() | searchchain.invoke({""input"": ""I'd like to figure out what games are tonight""}) 'What sports games are on TV today & tonight? Watch and stream live sports on TV today, tonight, tomorrow. Today\'s 2023 sports TV schedule includes football, basketball, baseball, hockey, motorsports, soccer and more. Watch on TV or stream online on ESPN, FOX, FS1, CBS, NBC, ABC, Peacock, Paramount+, fuboTV, local channels and many other networks. MLB Games Tonight: How to Watch on TV, Streaming & Odds - Thursday, September 7. Seattle Mariners\' Julio Rodriguez greets teammates in the dugout after scoring against the Oakland Athletics in a ... Circle - Country Music and Lifestyle. Live coverage of all the MLB action today is available to you, with the information provided below. The Brewers will look to pick up a road win at PNC Park against the Pirates on Wednesday at 12:35 PM ET. Check out the latest odds and with BetMGM Sportsbook. Use bonus code ""GNPLAY"" for special offers! MLB Games Tonight: How to Watch on TV, Streaming & Odds - Tuesday, September 5. Houston Astros\' Kyle Tucker runs after hitting a double during the fourth inning of a baseball game against the Los Angeles Angels, Sunday, Aug. 13, 2023, in Houston. (AP Photo/Eric Christian Smith) (APMedia) The Houston Astros versus the Texas Rangers is one of ... The second half of tonight\'s college football schedule still has some good games remaining to watch on your television.. We\'ve already seen an exciting one when Colorado upset TCU. And we saw some ...'PreviousAdding moderationNextLangChain Expression Language (LCEL)" 22,https://python.langchain.com/docs/expression_language/,"LangChain Expression LanguageOn this pageLangChain Expression Language (LCEL)LangChain Expression Language or LCEL is a declarative way to easily compose chains together. There are several benefits to writing chains in this manner (as opposed to writing normal code):Async, Batch, and Streaming Support Any chain constructed this way will automatically have full sync, async, batch, and streaming support. This makes it easy to prototype a chain in a Jupyter notebook using the sync interface, and then expose it as an async streaming interface.Fallbacks The non-determinism of LLMs makes it important to be able to handle errors gracefully. With LCEL you can easily attach fallbacks to any chain.Parallelism Since LLM applications involve (sometimes long) API calls, it often becomes important to run things in parallel. With LCEL syntax, any components that can be run in parallel automatically are.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 for maximal observability and debuggability.Interface​The base interface shared by all LCEL objectsHow to​How to use core features of LCELCookbook​Examples of common LCEL usage patternsPreviousQuickstartNextInterface" 23,https://python.langchain.com/docs/modules/,"ModulesOn this pageModulesLangChain provides standard, extendable interfaces and external integrations for the following modules, listed from least to most complex:Model I/O​Interface with language modelsRetrieval​Interface with application-specific dataChains​Construct sequences of callsAgents​Let chains choose which tools to use given high-level directivesMemory​Persist application state between runs of a chainCallbacks​Log and stream intermediate steps of any chainPreviousLangChain Expression Language (LCEL)NextModel I/O" 24,https://python.langchain.com/docs/modules/model_io/,"ModulesModel I/​OModel I/OThe core element of any language model application is...the model. LangChain gives you the building blocks to interface with any language model.Prompts: Templatize, dynamically select, and manage model inputsLanguage models: Make calls to language models through common interfacesOutput parsers: Extract information from model outputsPreviousModulesNextPrompts" 25,https://python.langchain.com/docs/modules/model_io/prompts/,"ModulesModel I/​OPromptsPromptsA prompt for a language model is a set of instructions or input provided by a user to guide the model's response, helping it understand the context and generate relevant and coherent language-based output, such as answering questions, completing sentences, or engaging in a conversation.LangChain provides several classes and functions to help construct and work with prompts.Prompt templates: Parametrized model inputsExample selectors: Dynamically select examples to include in promptsPreviousModel I/ONextPrompt templates" 26,https://python.langchain.com/docs/modules/model_io/prompts/prompt_templates/,"ModulesModel I/​OPromptsPrompt templatesPrompt templatesPrompt templates are pre-defined recipes for generating prompts for language models.A template may include instructions, few-shot examples, and specific context and questions appropriate for a given task.LangChain provides tooling to create and work with prompt templates.LangChain strives to create model agnostic templates to make it easy to reuse existing templates across different language models.Typically, language models expect the prompt to either be a string or else a list of chat messages.Prompt template​Use PromptTemplate to create a template for a string prompt.By default, PromptTemplate uses Python's str.format syntax for templating; however other templating syntax is available (e.g., jinja2).from langchain.prompts import PromptTemplateprompt_template = PromptTemplate.from_template( ""Tell me a {adjective} joke about {content}."")prompt_template.format(adjective=""funny"", content=""chickens"")""Tell me a funny joke about chickens.""The template supports any number of variables, including no variables:from langchain.prompts import PromptTemplateprompt_template = PromptTemplate.from_template(""Tell me a joke"")prompt_template.format()For additional validation, specify input_variables explicitly. These variables will be compared against the variables present in the template string during instantiation, raising an exception if there is a mismatch; for example,from langchain.prompts import PromptTemplateinvalid_prompt = PromptTemplate( input_variables=[""adjective""], template=""Tell me a {adjective} joke about {content}."")You can create custom prompt templates that format the prompt in any way you want. For more information, see Custom Prompt Templates.Chat prompt template​The prompt to chat models is a list of chat messages.Each chat message is associated with content, and an additional parameter called role. For example, in the OpenAI Chat Completions API, a chat message can be associated with an AI assistant, a human or a system role.Create a chat prompt template like this:from langchain.prompts import ChatPromptTemplatetemplate = ChatPromptTemplate.from_messages([ (""system"", ""You are a helpful AI bot. Your name is {name}.""), (""human"", ""Hello, how are you doing?""), (""ai"", ""I'm doing well, thanks!""), (""human"", ""{user_input}""),])messages = template.format_messages( name=""Bob"", user_input=""What is your name?"")ChatPromptTemplate.from_messages accepts a variety of message representations.For example, in addition to using the 2-tuple representation of (type, content) used above, you could pass in an instance of MessagePromptTemplate or BaseMessage.from langchain.prompts import ChatPromptTemplatefrom langchain.prompts.chat import SystemMessage, HumanMessagePromptTemplatetemplate = ChatPromptTemplate.from_messages( [ SystemMessage( content=( ""You are a helpful assistant that re-writes the user's text to "" ""sound more upbeat."" ) ), HumanMessagePromptTemplate.from_template(""{text}""), ])from langchain.chat_models import ChatOpenAIllm = ChatOpenAI()llm(template.format_messages(text='i dont like eating tasty things.'))AIMessage(content='I absolutely adore indulging in delicious treats!', additional_kwargs={}, example=False)This provides you with a lot of flexibility in how you construct your chat prompts.PreviousPromptsNextConnecting to a Feature Store" 27,https://python.langchain.com/docs/modules/model_io/prompts/prompt_templates/connecting_to_a_feature_store,"ModulesModel I/​OPromptsPrompt templatesConnecting to a Feature StoreOn this pageConnecting to a Feature StoreFeature stores are a concept from traditional machine learning that make sure data fed into models is up-to-date and relevant. For more on this, see here.This concept is extremely relevant when considering putting LLM applications in production. In order to personalize LLM applications, you may want to combine LLMs with up-to-date information about particular users. Feature stores can be a great way to keep that data fresh, and LangChain provides an easy way to combine that data with LLMs.In this notebook we will show how to connect prompt templates to feature stores. The basic idea is to call a feature store from inside a prompt template to retrieve values that are then formatted into the prompt.Feast​To start, we will use the popular open source feature store framework Feast.This assumes you have already run the steps in the README around getting started. We will build off of that example in getting started, and create and LLMChain to write a note to a specific driver regarding their up-to-date statistics.Load Feast Store​Again, this should be set up according to the instructions in the Feast README.from feast import FeatureStore# You may need to update the path depending on where you stored itfeast_repo_path = ""../../../../../my_feature_repo/feature_repo/""store = FeatureStore(repo_path=feast_repo_path)Prompts​Here we will set up a custom FeastPromptTemplate. This prompt template will take in a driver id, look up their stats, and format those stats into a prompt.Note that the input to this prompt template is just driver_id, since that is the only user defined piece (all other variables are looked up inside the prompt template).from langchain.prompts import PromptTemplate, StringPromptTemplatetemplate = """"""Given the driver's up to date stats, write them note relaying those stats to them.If they have a conversation rate above .5, give them a compliment. Otherwise, make a silly joke about chickens at the end to make them feel betterHere are the drivers stats:Conversation rate: {conv_rate}Acceptance rate: {acc_rate}Average Daily Trips: {avg_daily_trips}Your response:""""""prompt = PromptTemplate.from_template(template)class FeastPromptTemplate(StringPromptTemplate): def format(self, **kwargs) -> str: driver_id = kwargs.pop(""driver_id"") feature_vector = store.get_online_features( features=[ ""driver_hourly_stats:conv_rate"", ""driver_hourly_stats:acc_rate"", ""driver_hourly_stats:avg_daily_trips"", ], entity_rows=[{""driver_id"": driver_id}], ).to_dict() kwargs[""conv_rate""] = feature_vector[""conv_rate""][0] kwargs[""acc_rate""] = feature_vector[""acc_rate""][0] kwargs[""avg_daily_trips""] = feature_vector[""avg_daily_trips""][0] return prompt.format(**kwargs)prompt_template = FeastPromptTemplate(input_variables=[""driver_id""])print(prompt_template.format(driver_id=1001)) Given the driver's up to date stats, write them note relaying those stats to them. If they have a conversation rate above .5, give them a compliment. Otherwise, make a silly joke about chickens at the end to make them feel better Here are the drivers stats: Conversation rate: 0.4745151400566101 Acceptance rate: 0.055561766028404236 Average Daily Trips: 936 Your response:Use in a chain​We can now use this in a chain, successfully creating a chain that achieves personalization backed by a feature store.from langchain.chat_models import ChatOpenAIfrom langchain.chains import LLMChainchain = LLMChain(llm=ChatOpenAI(), prompt=prompt_template)chain.run(1001) ""Hi there! I wanted to update you on your current stats. Your acceptance rate is 0.055561766028404236 and your average daily trips are 936. While your conversation rate is currently 0.4745151400566101, I have no doubt that with a little extra effort, you'll be able to exceed that .5 mark! Keep up the great work! And remember, even chickens can't always cross the road, but they still give it their best shot.""Tecton​Above, we showed how you could use Feast, a popular open source and self-managed feature store, with LangChain. Our examples below will show a similar integration using Tecton. Tecton is a fully managed feature platform built to orchestrate the complete ML feature lifecycle, from transformation to online serving, with enterprise-grade SLAs.Prerequisites​Tecton Deployment (sign up at https://tecton.ai)TECTON_API_KEY environment variable set to a valid Service Account keyDefine and load features​We will use the user_transaction_counts Feature View from the Tecton tutorial as part of a Feature Service. For simplicity, we are only using a single Feature View; however, more sophisticated applications may require more feature views to retrieve the features needed for its prompt.user_transaction_metrics = FeatureService( name = ""user_transaction_metrics"", features = [user_transaction_counts])The above Feature Service is expected to be applied to a live workspace. For this example, we will be using the ""prod"" workspace.import tectonworkspace = tecton.get_workspace(""prod"")feature_service = workspace.get_feature_service(""user_transaction_metrics"")Prompts​Here we will set up a custom TectonPromptTemplate. This prompt template will take in a user_id , look up their stats, and format those stats into a prompt.Note that the input to this prompt template is just user_id, since that is the only user defined piece (all other variables are looked up inside the prompt template).from langchain.prompts import PromptTemplate, StringPromptTemplatetemplate = """"""Given the vendor's up to date transaction stats, write them a note based on the following rules:1. If they had a transaction in the last day, write a short congratulations message on their recent sales2. If no transaction in the last day, but they had a transaction in the last 30 days, playfully encourage them to sell more.3. Always add a silly joke about chickens at the endHere are the vendor's stats:Number of Transactions Last Day: {transaction_count_1d}Number of Transactions Last 30 Days: {transaction_count_30d}Your response:""""""prompt = PromptTemplate.from_template(template)class TectonPromptTemplate(StringPromptTemplate): def format(self, **kwargs) -> str: user_id = kwargs.pop(""user_id"") feature_vector = feature_service.get_online_features( join_keys={""user_id"": user_id} ).to_dict() kwargs[""transaction_count_1d""] = feature_vector[ ""user_transaction_counts.transaction_count_1d_1d"" ] kwargs[""transaction_count_30d""] = feature_vector[ ""user_transaction_counts.transaction_count_30d_1d"" ] return prompt.format(**kwargs)prompt_template = TectonPromptTemplate(input_variables=[""user_id""])print(prompt_template.format(user_id=""user_469998441571"")) Given the vendor's up to date transaction stats, write them a note based on the following rules: 1. If they had a transaction in the last day, write a short congratulations message on their recent sales 2. If no transaction in the last day, but they had a transaction in the last 30 days, playfully encourage them to sell more. 3. Always add a silly joke about chickens at the end Here are the vendor's stats: Number of Transactions Last Day: 657 Number of Transactions Last 30 Days: 20326 Your response:Use in a chain​We can now use this in a chain, successfully creating a chain that achieves personalization backed by the Tecton Feature Platform.from langchain.chat_models import ChatOpenAIfrom langchain.chains import LLMChainchain = LLMChain(llm=ChatOpenAI(), prompt=prompt_template)chain.run(""user_469998441571"") 'Wow, congratulations on your recent sales! Your business is really soaring like a chicken on a hot air balloon! Keep up the great work!'Featureform​Finally, we will use Featureform, an open-source and enterprise-grade feature store, to run the same example. Featureform allows you to work with your infrastructure like Spark or locally to define your feature transformations.Initialize Featureform​You can follow in the instructions in the README to initialize your transformations and features in Featureform.import featureform as ffclient = ff.Client(host=""demo.featureform.com"")Prompts​Here we will set up a custom FeatureformPromptTemplate. This prompt template will take in the average amount a user pays per transactions.Note that the input to this prompt template is just avg_transaction, since that is the only user defined piece (all other variables are looked up inside the prompt template).from langchain.prompts import PromptTemplate, StringPromptTemplatetemplate = """"""Given the amount a user spends on average per transaction, let them know if they are a high roller. Otherwise, make a silly joke about chickens at the end to make them feel betterHere are the user's stats:Average Amount per Transaction: ${avg_transcation}Your response:""""""prompt = PromptTemplate.from_template(template)class FeatureformPromptTemplate(StringPromptTemplate): def format(self, **kwargs) -> str: user_id = kwargs.pop(""user_id"") fpf = client.features([(""avg_transactions"", ""quickstart"")], {""user"": user_id}) return prompt.format(**kwargs)prompt_template = FeatureformPromptTemplate(input_variables=[""user_id""])print(prompt_template.format(user_id=""C1410926""))Use in a chain​We can now use this in a chain, successfully creating a chain that achieves personalization backed by the Featureform Feature Platform.from langchain.chat_models import ChatOpenAIfrom langchain.chains import LLMChainchain = LLMChain(llm=ChatOpenAI(), prompt=prompt_template)chain.run(""C1410926"")AzureML Managed Feature Store​We will use AzureML Managed Feature Store to run the example below. Prerequisites​Create feature store with online materialization using instructions here Enable online materialization and run online inference.A successfully created feature store by following the instructions should have an account featureset with version as 1. It will have accountID as index column with features accountAge, accountCountry, numPaymentRejects1dPerUser.Prompts​Here we will set up a custom AzureMLFeatureStorePromptTemplate. This prompt template will take in an account_id and optional query. It then fetches feature values from feature store and format those features into the output prompt. Note that the required input to this prompt template is just account_id, since that is the only user defined piece (all other variables are looked up inside the prompt template).Also note that this is a bootstrap example to showcase how LLM applications can leverage AzureML managed feature store. Developers are welcome to improve the prompt template further to suit their needs.import osos.environ['AZURE_ML_CLI_PRIVATE_FEATURES_ENABLED'] = 'True'import pandasfrom pydantic import Extrafrom langchain.prompts import PromptTemplate, StringPromptTemplatefrom azure.identity import AzureCliCredentialfrom azureml.featurestore import FeatureStoreClient, init_online_lookup, get_online_featuresclass AzureMLFeatureStorePromptTemplate(StringPromptTemplate, extra=Extra.allow): def __init__(self, subscription_id: str, resource_group: str, feature_store_name: str, **kwargs): # this is an example template for proof of concept and can be changed to suit the developer needs template = """""" {query} ### account id = {account_id} account age = {account_age} account country = {account_country} payment rejects 1d per user = {payment_rejects_1d_per_user} ### """""" prompt_template=PromptTemplate.from_template(template) super().__init__(prompt=prompt_template, input_variables=[""account_id"", ""query""]) # use AzureMLOnBehalfOfCredential() in spark context credential = AzureCliCredential() self._fs_client = FeatureStoreClient( credential=credential, subscription_id=subscription_id, resource_group_name=resource_group, name=feature_store_name) self._feature_set = self._fs_client.feature_sets.get(name=""accounts"", version=1) init_online_lookup(self._feature_set.features, credential, force=True) def format(self, **kwargs) -> str: if ""account_id"" not in kwargs: raise ""account_id needed to fetch details from feature store"" account_id = kwargs.pop(""account_id"") query="""" if ""query"" in kwargs: query = kwargs.pop(""query"") # feature set is registered with accountID as entity index column. obs = pandas.DataFrame({'accountID': [account_id]}) # get the feature details for the input entity from feature store. df = get_online_features(self._feature_set.features, obs) # populate prompt template output using the fetched feature values. kwargs[""query""] = query kwargs[""account_id""] = account_id kwargs[""account_age""] = df[""accountAge""][0] kwargs[""account_country""] = df[""accountCountry""][0] kwargs[""payment_rejects_1d_per_user""] = df[""numPaymentRejects1dPerUser""][0] return self.prompt.format(**kwargs)Test​# Replace the place holders below with actual details of feature store that was created in previous stepsprompt_template = AzureMLFeatureStorePromptTemplate( subscription_id="""", resource_group="""", feature_store_name="""")print(prompt_template.format(account_id=""A1829581630230790"")) ### account id = A1829581630230790 account age = 563.0 account country = GB payment rejects 1d per user = 15.0 ### Use in a chain​We can now use this in a chain, successfully creating a chain that achieves personalization backed by the AzureML Managed Feature Store.os.environ[""OPENAI_API_KEY""]="""" # Fill the open ai key herefrom langchain.chat_models import ChatOpenAIfrom langchain.chains import LLMChainchain = LLMChain(llm=ChatOpenAI(), prompt=prompt_template)# NOTE: developer's can further fine tune AzureMLFeatureStorePromptTemplate# for getting even more accurate results for the input querychain.predict(account_id=""A1829581630230790"", query =""write a small thank you note within 20 words if account age > 10 using the account stats"") 'Thank you for being a valued member for over 10 years! We appreciate your continued support.'PreviousPrompt templatesNextCustom prompt templateFeastLoad Feast StorePromptsUse in a chainTectonPrerequisitesDefine and load featuresPromptsUse in a chainFeatureformInitialize FeatureformPromptsUse in a chainAzureML Managed Feature StorePrerequisitesPromptsTestUse in a chain" 28,https://python.langchain.com/docs/modules/model_io/prompts/prompt_templates/custom_prompt_template,"ModulesModel I/​OPromptsPrompt templatesCustom prompt templateOn this pageCustom prompt templateLet's suppose we want the LLM to generate English language explanations of a function given its name. To achieve this task, we will create a custom prompt template that takes in the function name as input, and formats the prompt template to provide the source code of the function.Why are custom prompt templates needed?​LangChain provides a set of default prompt templates that can be used to generate prompts for a variety of tasks. However, there may be cases where the default prompt templates do not meet your needs. For example, you may want to create a prompt template with specific dynamic instructions for your language model. In such cases, you can create a custom prompt template.Creating a custom prompt template​There are essentially two distinct prompt templates available - string prompt templates and chat prompt templates. String prompt templates provides a simple prompt in string format, while chat prompt templates produces a more structured prompt to be used with a chat API.In this guide, we will create a custom prompt using a string prompt template. To create a custom string prompt template, there are two requirements:It has an input_variables attribute that exposes what input variables the prompt template expects.It defines a format method that takes in keyword arguments corresponding to the expected input_variables and returns the formatted prompt.We will create a custom prompt template that takes in the function name as input and formats the prompt to provide the source code of the function. To achieve this, let's first create a function that will return the source code of a function given its name.import inspectdef get_source_code(function_name): # Get the source code of the function return inspect.getsource(function_name)Next, we'll create a custom prompt template that takes in the function name as input, and formats the prompt template to provide the source code of the function.from langchain.prompts import StringPromptTemplatefrom pydantic import BaseModel, validatorPROMPT = """"""\Given the function name and source code, generate an English language explanation of the function.Function Name: {function_name}Source Code:{source_code}Explanation:""""""class FunctionExplainerPromptTemplate(StringPromptTemplate, BaseModel): """"""A custom prompt template that takes in the function name as input, and formats the prompt template to provide the source code of the function."""""" @validator(""input_variables"") def validate_input_variables(cls, v): """"""Validate that the input variables are correct."""""" if len(v) != 1 or ""function_name"" not in v: raise ValueError(""function_name must be the only input_variable."") return v def format(self, **kwargs) -> str: # Get the source code of the function source_code = get_source_code(kwargs[""function_name""]) # Generate the prompt to be sent to the language model prompt = PROMPT.format( function_name=kwargs[""function_name""].__name__, source_code=source_code ) return prompt def _prompt_type(self): return ""function-explainer""Use the custom prompt template​Now that we have created a custom prompt template, we can use it to generate prompts for our task.fn_explainer = FunctionExplainerPromptTemplate(input_variables=[""function_name""])# Generate a prompt for the function ""get_source_code""prompt = fn_explainer.format(function_name=get_source_code)print(prompt) Given the function name and source code, generate an English language explanation of the function. Function Name: get_source_code Source Code: def get_source_code(function_name): # Get the source code of the function return inspect.getsource(function_name) Explanation: PreviousConnecting to a Feature StoreNextFew-shot prompt templatesWhy are custom prompt templates needed?Creating a custom prompt templateUse the custom prompt template" 29,https://python.langchain.com/docs/modules/model_io/prompts/prompt_templates/few_shot_examples,"ModulesModel I/​OPromptsPrompt templatesFew-shot prompt templatesFew-shot prompt templatesIn this tutorial, we'll learn how to create a prompt template that uses few-shot examples. A few-shot prompt template can be constructed from either a set of examples, or from an Example Selector object.Use Case​In this tutorial, we'll configure few-shot examples for self-ask with search.Using an example set​Create the example set​To get started, create a list of few-shot examples. Each example should be a dictionary with the keys being the input variables and the values being the values for those input variables.from langchain.prompts.few_shot import FewShotPromptTemplatefrom langchain.prompts.prompt import PromptTemplateexamples = [ { ""question"": ""Who lived longer, Muhammad Ali or Alan Turing?"", ""answer"": """"""Are follow up questions needed here: Yes.Follow up: How old was Muhammad Ali when he died?Intermediate answer: Muhammad Ali was 74 years old when he died.Follow up: How old was Alan Turing when he died?Intermediate answer: Alan Turing was 41 years old when he died.So the final answer is: Muhammad Ali"""""" }, { ""question"": ""When was the founder of craigslist born?"", ""answer"": """"""Are follow up questions needed here: Yes.Follow up: Who was the founder of craigslist?Intermediate answer: Craigslist was founded by Craig Newmark.Follow up: When was Craig Newmark born?Intermediate answer: Craig Newmark was born on December 6, 1952.So the final answer is: December 6, 1952"""""" }, { ""question"": ""Who was the maternal grandfather of George Washington?"", ""answer"":""""""Are follow up questions needed here: Yes.Follow up: Who was the mother of George Washington?Intermediate answer: The mother of George Washington was Mary Ball Washington.Follow up: Who was the father of Mary Ball Washington?Intermediate answer: The father of Mary Ball Washington was Joseph Ball.So the final answer is: Joseph Ball"""""" }, { ""question"": ""Are both the directors of Jaws and Casino Royale from the same country?"", ""answer"":""""""Are follow up questions needed here: Yes.Follow up: Who is the director of Jaws?Intermediate Answer: The director of Jaws is Steven Spielberg.Follow up: Where is Steven Spielberg from?Intermediate Answer: The United States.Follow up: Who is the director of Casino Royale?Intermediate Answer: The director of Casino Royale is Martin Campbell.Follow up: Where is Martin Campbell from?Intermediate Answer: New Zealand.So the final answer is: No"""""" }]Create a formatter for the few-shot examples​Configure a formatter that will format the few-shot examples into a string. This formatter should be a PromptTemplate object.example_prompt = PromptTemplate(input_variables=[""question"", ""answer""], template=""Question: {question}\n{answer}"")print(example_prompt.format(**examples[0])) Question: Who lived longer, Muhammad Ali or Alan Turing? Are follow up questions needed here: Yes. Follow up: How old was Muhammad Ali when he died? Intermediate answer: Muhammad Ali was 74 years old when he died. Follow up: How old was Alan Turing when he died? Intermediate answer: Alan Turing was 41 years old when he died. So the final answer is: Muhammad Ali Feed examples and formatter to FewShotPromptTemplate​Finally, create a FewShotPromptTemplate object. This object takes in the few-shot examples and the formatter for the few-shot examples.prompt = FewShotPromptTemplate( examples=examples, example_prompt=example_prompt, suffix=""Question: {input}"", input_variables=[""input""])print(prompt.format(input=""Who was the father of Mary Ball Washington?"")) Question: Who lived longer, Muhammad Ali or Alan Turing? Are follow up questions needed here: Yes. Follow up: How old was Muhammad Ali when he died? Intermediate answer: Muhammad Ali was 74 years old when he died. Follow up: How old was Alan Turing when he died? Intermediate answer: Alan Turing was 41 years old when he died. So the final answer is: Muhammad Ali Question: When was the founder of craigslist born? Are follow up questions needed here: Yes. Follow up: Who was the founder of craigslist? Intermediate answer: Craigslist was founded by Craig Newmark. Follow up: When was Craig Newmark born? Intermediate answer: Craig Newmark was born on December 6, 1952. So the final answer is: December 6, 1952 Question: Who was the maternal grandfather of George Washington? Are follow up questions needed here: Yes. Follow up: Who was the mother of George Washington? Intermediate answer: The mother of George Washington was Mary Ball Washington. Follow up: Who was the father of Mary Ball Washington? Intermediate answer: The father of Mary Ball Washington was Joseph Ball. So the final answer is: Joseph Ball Question: Are both the directors of Jaws and Casino Royale from the same country? Are follow up questions needed here: Yes. Follow up: Who is the director of Jaws? Intermediate Answer: The director of Jaws is Steven Spielberg. Follow up: Where is Steven Spielberg from? Intermediate Answer: The United States. Follow up: Who is the director of Casino Royale? Intermediate Answer: The director of Casino Royale is Martin Campbell. Follow up: Where is Martin Campbell from? Intermediate Answer: New Zealand. So the final answer is: No Question: Who was the father of Mary Ball Washington?Using an example selector​Feed examples into ExampleSelector​We will reuse the example set and the formatter from the previous section. However, instead of feeding the examples directly into the FewShotPromptTemplate object, we will feed them into an ExampleSelector object.In this tutorial, we will use the SemanticSimilarityExampleSelector class. This class selects few-shot examples based on their similarity to the input. It uses an embedding model to compute the similarity between the input and the few-shot examples, as well as a vector store to perform the nearest neighbor search.from langchain.prompts.example_selector import SemanticSimilarityExampleSelectorfrom langchain.vectorstores import Chromafrom langchain.embeddings import OpenAIEmbeddingsexample_selector = SemanticSimilarityExampleSelector.from_examples( # This is the list of examples available to select from. examples, # This is the embedding class used to produce embeddings which are used to measure semantic similarity. OpenAIEmbeddings(), # This is the VectorStore class that is used to store the embeddings and do a similarity search over. Chroma, # This is the number of examples to produce. k=1)# Select the most similar example to the input.question = ""Who was the father of Mary Ball Washington?""selected_examples = example_selector.select_examples({""question"": question})print(f""Examples most similar to the input: {question}"")for example in selected_examples: print(""\n"") for k, v in example.items(): print(f""{k}: {v}"") Running Chroma using direct local API. Using DuckDB in-memory for database. Data will be transient. Examples most similar to the input: Who was the father of Mary Ball Washington? question: Who was the maternal grandfather of George Washington? answer: Are follow up questions needed here: Yes. Follow up: Who was the mother of George Washington? Intermediate answer: The mother of George Washington was Mary Ball Washington. Follow up: Who was the father of Mary Ball Washington? Intermediate answer: The father of Mary Ball Washington was Joseph Ball. So the final answer is: Joseph Ball Feed example selector into FewShotPromptTemplate​Finally, create a FewShotPromptTemplate object. This object takes in the example selector and the formatter for the few-shot examples.prompt = FewShotPromptTemplate( example_selector=example_selector, example_prompt=example_prompt, suffix=""Question: {input}"", input_variables=[""input""])print(prompt.format(input=""Who was the father of Mary Ball Washington?"")) Question: Who was the maternal grandfather of George Washington? Are follow up questions needed here: Yes. Follow up: Who was the mother of George Washington? Intermediate answer: The mother of George Washington was Mary Ball Washington. Follow up: Who was the father of Mary Ball Washington? Intermediate answer: The father of Mary Ball Washington was Joseph Ball. So the final answer is: Joseph Ball Question: Who was the father of Mary Ball Washington?PreviousCustom prompt templateNextFew-shot examples for chat models" 30,https://python.langchain.com/docs/modules/model_io/prompts/prompt_templates/few_shot_examples_chat,"ModulesModel I/​OPromptsPrompt templatesFew-shot examples for chat modelsOn this pageFew-shot examples for chat modelsThis notebook covers how to use few-shot examples in chat models. There does not appear to be solid consensus on how best to do few-shot prompting, and the optimal prompt compilation will likely vary by model. Because of this, we provide few-shot prompt templates like the FewShotChatMessagePromptTemplate as a flexible starting point, and you can modify or replace them as you see fit.The goal of few-shot prompt templates are to dynamically select examples based on an input, and then format the examples in a final prompt to provide for the model.Note: The following code examples are for chat models. For similar few-shot prompt examples for completion models (LLMs), see the few-shot prompt templates guide.Fixed Examples​The most basic (and common) few-shot prompting technique is to use a fixed prompt example. This way you can select a chain, evaluate it, and avoid worrying about additional moving parts in production.The basic components of the template are:examples: A list of dictionary examples to include in the final prompt.example_prompt: converts each example into 1 or more messages through its format_messages method. A common example would be to convert each example into one human message and one AI message response, or a human message followed by a function call message.Below is a simple demonstration. First, import the modules for this example:from langchain.prompts import ( FewShotChatMessagePromptTemplate, ChatPromptTemplate,)Then, define the examples you'd like to include.examples = [ {""input"": ""2+2"", ""output"": ""4""}, {""input"": ""2+3"", ""output"": ""5""},]Next, assemble them into the few-shot prompt template.# This is a prompt template used to format each individual example.example_prompt = ChatPromptTemplate.from_messages( [ (""human"", ""{input}""), (""ai"", ""{output}""), ])few_shot_prompt = FewShotChatMessagePromptTemplate( example_prompt=example_prompt, examples=examples,)print(few_shot_prompt.format()) Human: 2+2 AI: 4 Human: 2+3 AI: 5Finally, assemble your final prompt and use it with a model.final_prompt = ChatPromptTemplate.from_messages( [ (""system"", ""You are a wondrous wizard of math.""), few_shot_prompt, (""human"", ""{input}""), ])from langchain.chat_models import ChatAnthropicchain = final_prompt | ChatAnthropic(temperature=0.0)chain.invoke({""input"": ""What's the square of a triangle?""}) AIMessage(content=' Triangles do not have a ""square"". A square refers to a shape with 4 equal sides and 4 right angles. Triangles have 3 sides and 3 angles.\n\nThe area of a triangle can be calculated using the formula:\n\nA = 1/2 * b * h\n\nWhere:\n\nA is the area \nb is the base (the length of one of the sides)\nh is the height (the length from the base to the opposite vertex)\n\nSo the area depends on the specific dimensions of the triangle. There is no single ""square of a triangle"". The area can vary greatly depending on the base and height measurements.', additional_kwargs={}, example=False)Dynamic few-shot prompting​Sometimes you may want to condition which examples are shown based on the input. For this, you can replace the examples with an example_selector. The other components remain the same as above! To review, the dynamic few-shot prompt template would look like:example_selector: responsible for selecting few-shot examples (and the order in which they are returned) for a given input. These implement the BaseExampleSelector interface. A common example is the vectorstore-backed SemanticSimilarityExampleSelectorexample_prompt: convert each example into 1 or more messages through its format_messages method. A common example would be to convert each example into one human message and one AI message response, or a human message followed by a function call message.These once again can be composed with other messages and chat templates to assemble your final prompt.from langchain.prompts import SemanticSimilarityExampleSelectorfrom langchain.embeddings import OpenAIEmbeddingsfrom langchain.vectorstores import ChromaSince we are using a vectorstore to select examples based on semantic similarity, we will want to first populate the store.examples = [ {""input"": ""2+2"", ""output"": ""4""}, {""input"": ""2+3"", ""output"": ""5""}, {""input"": ""2+4"", ""output"": ""6""}, {""input"": ""What did the cow say to the moon?"", ""output"": ""nothing at all""}, { ""input"": ""Write me a poem about the moon"", ""output"": ""One for the moon, and one for me, who are we to talk about the moon?"", },]to_vectorize = ["" "".join(example.values()) for example in examples]embeddings = OpenAIEmbeddings()vectorstore = Chroma.from_texts(to_vectorize, embeddings, metadatas=examples)Create the example_selector​With a vectorstore created, you can create the example_selector. Here we will isntruct it to only fetch the top 2 examples.example_selector = SemanticSimilarityExampleSelector( vectorstore=vectorstore, k=2,)# The prompt template will load examples by passing the input do the `select_examples` methodexample_selector.select_examples({""input"": ""horse""}) [{'input': 'What did the cow say to the moon?', 'output': 'nothing at all'}, {'input': '2+4', 'output': '6'}]Create prompt template​Assemble the prompt template, using the example_selector created above.from langchain.prompts import ( FewShotChatMessagePromptTemplate, ChatPromptTemplate,)# Define the few-shot prompt.few_shot_prompt = FewShotChatMessagePromptTemplate( # The input variables select the values to pass to the example_selector input_variables=[""input""], example_selector=example_selector, # Define how each example will be formatted. # In this case, each example will become 2 messages: # 1 human, and 1 AI example_prompt=ChatPromptTemplate.from_messages( [(""human"", ""{input}""), (""ai"", ""{output}"")] ),)Below is an example of how this would be assembled.print(few_shot_prompt.format(input=""What's 3+3?"")) Human: 2+3 AI: 5 Human: 2+2 AI: 4Assemble the final prompt template:final_prompt = ChatPromptTemplate.from_messages( [ (""system"", ""You are a wondrous wizard of math.""), few_shot_prompt, (""human"", ""{input}""), ])print(few_shot_prompt.format(input=""What's 3+3?"")) Human: 2+3 AI: 5 Human: 2+2 AI: 4Use with an LLM​Now, you can connect your model to the few-shot prompt.from langchain.chat_models import ChatAnthropicchain = final_prompt | ChatAnthropic(temperature=0.0)chain.invoke({""input"": ""What's 3+3?""}) AIMessage(content=' 3 + 3 = 6', additional_kwargs={}, example=False)PreviousFew-shot prompt templatesNextFormat template outputFixed ExamplesDynamic few-shot prompting" 31,https://python.langchain.com/docs/modules/model_io/prompts/prompt_templates/format_output,"ModulesModel I/​OPromptsPrompt templatesFormat template outputFormat template outputThe output of the format method is available as a string, list of messages and ChatPromptValueAs string:output = chat_prompt.format(input_language=""English"", output_language=""French"", text=""I love programming."")output 'System: You are a helpful assistant that translates English to French.\nHuman: I love programming.'# or alternativelyoutput_2 = chat_prompt.format_prompt(input_language=""English"", output_language=""French"", text=""I love programming."").to_string()assert output == output_2As list of Message objects:chat_prompt.format_prompt(input_language=""English"", output_language=""French"", text=""I love programming."").to_messages() [SystemMessage(content='You are a helpful assistant that translates English to French.', additional_kwargs={}), HumanMessage(content='I love programming.', additional_kwargs={})]As ChatPromptValue:chat_prompt.format_prompt(input_language=""English"", output_language=""French"", text=""I love programming."") ChatPromptValue(messages=[SystemMessage(content='You are a helpful assistant that translates English to French.', additional_kwargs={}), HumanMessage(content='I love programming.', additional_kwargs={})])PreviousFew-shot examples for chat modelsNextTemplate formats" 32,https://python.langchain.com/docs/modules/model_io/prompts/prompt_templates/formats,"ModulesModel I/​OPromptsPrompt templatesTemplate formatsTemplate formatsPromptTemplate by default uses Python f-string as its template format. However, it can also use other formats like jinja2, specified through the template_format argument.To use the jinja2 template:from langchain.prompts import PromptTemplatejinja2_template = ""Tell me a {{ adjective }} joke about {{ content }}""prompt = PromptTemplate.from_template(jinja2_template, template_format=""jinja2"")prompt.format(adjective=""funny"", content=""chickens"")# Output: Tell me a funny joke about chickens.To use the Python f-string template:from langchain.prompts import PromptTemplatefstring_template = """"""Tell me a {adjective} joke about {content}""""""prompt = PromptTemplate.from_template(fstring_template)prompt.format(adjective=""funny"", content=""chickens"")# Output: Tell me a funny joke about chickens.Currently, only jinja2 and f-string are supported. For other formats, kindly raise an issue on the Github page.PreviousFormat template outputNextTypes of MessagePromptTemplate" 33,https://python.langchain.com/docs/modules/model_io/prompts/prompt_templates/msg_prompt_templates,"ModulesModel I/​OPromptsPrompt templatesTypes of MessagePromptTemplateTypes of MessagePromptTemplateLangChain provides different types of MessagePromptTemplate. The most commonly used are AIMessagePromptTemplate, SystemMessagePromptTemplate and HumanMessagePromptTemplate, which create an AI message, system message and human message respectively.However, in cases where the chat model supports taking chat message with arbitrary role, you can use ChatMessagePromptTemplate, which allows user to specify the role name.from langchain.prompts import ChatMessagePromptTemplateprompt = ""May the {subject} be with you""chat_message_prompt = ChatMessagePromptTemplate.from_template(role=""Jedi"", template=prompt)chat_message_prompt.format(subject=""force"") ChatMessage(content='May the force be with you', additional_kwargs={}, role='Jedi')LangChain also provides MessagesPlaceholder, which gives you full control of what messages to be rendered during formatting. This can be useful when you are uncertain of what role you should be using for your message prompt templates or when you wish to insert a list of messages during formatting.from langchain.prompts import MessagesPlaceholderhuman_prompt = ""Summarize our conversation so far in {word_count} words.""human_message_template = HumanMessagePromptTemplate.from_template(human_prompt)chat_prompt = ChatPromptTemplate.from_messages([MessagesPlaceholder(variable_name=""conversation""), human_message_template])human_message = HumanMessage(content=""What is the best way to learn programming?"")ai_message = AIMessage(content=""""""\1. Choose a programming language: Decide on a programming language that you want to learn.2. Start with the basics: Familiarize yourself with the basic programming concepts such as variables, data types and control structures.3. Practice, practice, practice: The best way to learn programming is through hands-on experience\"""""")chat_prompt.format_prompt(conversation=[human_message, ai_message], word_count=""10"").to_messages() [HumanMessage(content='What is the best way to learn programming?', additional_kwargs={}), AIMessage(content='1. Choose a programming language: Decide on a programming language that you want to learn. \n\n2. Start with the basics: Familiarize yourself with the basic programming concepts such as variables, data types and control structures.\n\n3. Practice, practice, practice: The best way to learn programming is through hands-on experience', additional_kwargs={}), HumanMessage(content='Summarize our conversation so far in 10 words.', additional_kwargs={})]PreviousTemplate formatsNextPartial prompt templates" 34,https://python.langchain.com/docs/modules/model_io/prompts/prompt_templates/partial,"ModulesModel I/​OPromptsPrompt templatesPartial prompt templatesPartial prompt templatesLike other methods, it can make sense to ""partial"" a prompt template - e.g. pass in a subset of the required values, as to create a new prompt template which expects only the remaining subset of values.LangChain supports this in two ways:Partial formatting with string values.Partial formatting with functions that return string values.These two different ways support different use cases. In the examples below, we go over the motivations for both use cases as well as how to do it in LangChain.Partial with strings​One common use case for wanting to partial a prompt template is if you get some of the variables before others. For example, suppose you have a prompt template that requires two variables, foo and baz. If you get the foo value early on in the chain, but the baz value later, it can be annoying to wait until you have both variables in the same place to pass them to the prompt template. Instead, you can partial the prompt template with the foo value, and then pass the partialed prompt template along and just use that. Below is an example of doing this:from langchain.prompts import PromptTemplateprompt = PromptTemplate(template=""{foo}{bar}"", input_variables=[""foo"", ""bar""])partial_prompt = prompt.partial(foo=""foo"");print(partial_prompt.format(bar=""baz"")) foobazYou can also just initialize the prompt with the partialed variables.prompt = PromptTemplate(template=""{foo}{bar}"", input_variables=[""bar""], partial_variables={""foo"": ""foo""})print(prompt.format(bar=""baz"")) foobazPartial with functions​The other common use is to partial with a function. The use case for this is when you have a variable you know that you always want to fetch in a common way. A prime example of this is with date or time. Imagine you have a prompt which you always want to have the current date. You can't hard code it in the prompt, and passing it along with the other input variables is a bit annoying. In this case, it's very handy to be able to partial the prompt with a function that always returns the current date.from datetime import datetimedef _get_datetime(): now = datetime.now() return now.strftime(""%m/%d/%Y, %H:%M:%S"")prompt = PromptTemplate( template=""Tell me a {adjective} joke about the day {date}"", input_variables=[""adjective"", ""date""]);partial_prompt = prompt.partial(date=_get_datetime)print(partial_prompt.format(adjective=""funny"")) Tell me a funny joke about the day 02/27/2023, 22:15:16You can also just initialize the prompt with the partialed variables, which often makes more sense in this workflow.prompt = PromptTemplate( template=""Tell me a {adjective} joke about the day {date}"", input_variables=[""adjective""], partial_variables={""date"": _get_datetime});print(prompt.format(adjective=""funny"")) Tell me a funny joke about the day 02/27/2023, 22:15:16PreviousTypes of MessagePromptTemplateNextComposition" 35,https://python.langchain.com/docs/modules/model_io/prompts/prompt_templates/prompt_composition,"ModulesModel I/​OPromptsPrompt templatesCompositionCompositionThis notebook goes over how to compose multiple prompts together. This can be useful when you want to reuse parts of prompts. This can be done with a PipelinePrompt. A PipelinePrompt consists of two main parts:Final prompt: The final prompt that is returnedPipeline prompts: A list of tuples, consisting of a string name and a prompt template. Each prompt template will be formatted and then passed to future prompt templates as a variable with the same name.from langchain.prompts.pipeline import PipelinePromptTemplatefrom langchain.prompts.prompt import PromptTemplatefull_template = """"""{introduction}{example}{start}""""""full_prompt = PromptTemplate.from_template(full_template)introduction_template = """"""You are impersonating {person}.""""""introduction_prompt = PromptTemplate.from_template(introduction_template)example_template = """"""Here's an example of an interaction: Q: {example_q}A: {example_a}""""""example_prompt = PromptTemplate.from_template(example_template)start_template = """"""Now, do this for real!Q: {input}A:""""""start_prompt = PromptTemplate.from_template(start_template)input_prompts = [ (""introduction"", introduction_prompt), (""example"", example_prompt), (""start"", start_prompt)]pipeline_prompt = PipelinePromptTemplate(final_prompt=full_prompt, pipeline_prompts=input_prompts)pipeline_prompt.input_variables ['example_a', 'person', 'example_q', 'input']print(pipeline_prompt.format( person=""Elon Musk"", example_q=""What's your favorite car?"", example_a=""Tesla"", input=""What's your favorite social media site?"")) You are impersonating Elon Musk. Here's an example of an interaction: Q: What's your favorite car? A: Tesla Now, do this for real! Q: What's your favorite social media site? A: PreviousPartial prompt templatesNextSerialization" 36,https://python.langchain.com/docs/modules/model_io/prompts/prompt_templates/prompt_serialization,"ModulesModel I/​OPromptsPrompt templatesSerializationOn this pageSerializationIt is often preferrable to store prompts not as python code but as files. This can make it easy to share, store, and version prompts. This notebook covers how to do that in LangChain, walking through all the different types of prompts and the different serialization options.At a high level, the following design principles are applied to serialization:Both JSON and YAML are supported. We want to support serialization methods that are human readable on disk, and YAML and JSON are two of the most popular methods for that. Note that this rule applies to prompts. For other assets, like examples, different serialization methods may be supported.We support specifying everything in one file, or storing different components (templates, examples, etc) in different files and referencing them. For some cases, storing everything in file makes the most sense, but for others it is preferrable to split up some of the assets (long templates, large examples, reusable components). LangChain supports both.There is also a single entry point to load prompts from disk, making it easy to load any type of prompt.# All prompts are loaded through the `load_prompt` function.from langchain.prompts import load_promptPromptTemplate​This section covers examples for loading a PromptTemplate.Loading from YAML​This shows an example of loading a PromptTemplate from YAML.cat simple_prompt.yaml _type: prompt input_variables: [""adjective"", ""content""] template: Tell me a {adjective} joke about {content}.prompt = load_prompt(""simple_prompt.yaml"")print(prompt.format(adjective=""funny"", content=""chickens"")) Tell me a funny joke about chickens.Loading from JSON​This shows an example of loading a PromptTemplate from JSON.cat simple_prompt.json { ""_type"": ""prompt"", ""input_variables"": [""adjective"", ""content""], ""template"": ""Tell me a {adjective} joke about {content}."" }prompt = load_prompt(""simple_prompt.json"")print(prompt.format(adjective=""funny"", content=""chickens""))Tell me a funny joke about chickens.Loading template from a file​This shows an example of storing the template in a separate file and then referencing it in the config. Notice that the key changes from template to template_path.cat simple_template.txt Tell me a {adjective} joke about {content}.cat simple_prompt_with_template_file.json { ""_type"": ""prompt"", ""input_variables"": [""adjective"", ""content""], ""template_path"": ""simple_template.txt"" }prompt = load_prompt(""simple_prompt_with_template_file.json"")print(prompt.format(adjective=""funny"", content=""chickens"")) Tell me a funny joke about chickens.FewShotPromptTemplate​This section covers examples for loading few-shot prompt templates.Examples​This shows an example of what examples stored as json might look like.cat examples.json [ {""input"": ""happy"", ""output"": ""sad""}, {""input"": ""tall"", ""output"": ""short""} ]And here is what the same examples stored as yaml might look like.cat examples.yaml - input: happy output: sad - input: tall output: shortLoading from YAML​This shows an example of loading a few-shot example from YAML.cat few_shot_prompt.yaml _type: few_shot input_variables: [""adjective""] prefix: Write antonyms for the following words. example_prompt: _type: prompt input_variables: [""input"", ""output""] template: ""Input: {input}\nOutput: {output}"" examples: examples.json suffix: ""Input: {adjective}\nOutput:""prompt = load_prompt(""few_shot_prompt.yaml"")print(prompt.format(adjective=""funny"")) Write antonyms for the following words. Input: happy Output: sad Input: tall Output: short Input: funny Output:The same would work if you loaded examples from the yaml file.cat few_shot_prompt_yaml_examples.yaml _type: few_shot input_variables: [""adjective""] prefix: Write antonyms for the following words. example_prompt: _type: prompt input_variables: [""input"", ""output""] template: ""Input: {input}\nOutput: {output}"" examples: examples.yaml suffix: ""Input: {adjective}\nOutput:""prompt = load_prompt(""few_shot_prompt_yaml_examples.yaml"")print(prompt.format(adjective=""funny"")) Write antonyms for the following words. Input: happy Output: sad Input: tall Output: short Input: funny Output:Loading from JSON​This shows an example of loading a few-shot example from JSON.cat few_shot_prompt.json { ""_type"": ""few_shot"", ""input_variables"": [""adjective""], ""prefix"": ""Write antonyms for the following words."", ""example_prompt"": { ""_type"": ""prompt"", ""input_variables"": [""input"", ""output""], ""template"": ""Input: {input}\nOutput: {output}"" }, ""examples"": ""examples.json"", ""suffix"": ""Input: {adjective}\nOutput:"" } prompt = load_prompt(""few_shot_prompt.json"")print(prompt.format(adjective=""funny"")) Write antonyms for the following words. Input: happy Output: sad Input: tall Output: short Input: funny Output:Examples in the config​This shows an example of referencing the examples directly in the config.cat few_shot_prompt_examples_in.json { ""_type"": ""few_shot"", ""input_variables"": [""adjective""], ""prefix"": ""Write antonyms for the following words."", ""example_prompt"": { ""_type"": ""prompt"", ""input_variables"": [""input"", ""output""], ""template"": ""Input: {input}\nOutput: {output}"" }, ""examples"": [ {""input"": ""happy"", ""output"": ""sad""}, {""input"": ""tall"", ""output"": ""short""} ], ""suffix"": ""Input: {adjective}\nOutput:"" } prompt = load_prompt(""few_shot_prompt_examples_in.json"")print(prompt.format(adjective=""funny"")) Write antonyms for the following words. Input: happy Output: sad Input: tall Output: short Input: funny Output:Example prompt from a file​This shows an example of loading the PromptTemplate that is used to format the examples from a separate file. Note that the key changes from example_prompt to example_prompt_path.cat example_prompt.json { ""_type"": ""prompt"", ""input_variables"": [""input"", ""output""], ""template"": ""Input: {input}\nOutput: {output}"" }cat few_shot_prompt_example_prompt.json { ""_type"": ""few_shot"", ""input_variables"": [""adjective""], ""prefix"": ""Write antonyms for the following words."", ""example_prompt_path"": ""example_prompt.json"", ""examples"": ""examples.json"", ""suffix"": ""Input: {adjective}\nOutput:"" } prompt = load_prompt(""few_shot_prompt_example_prompt.json"")print(prompt.format(adjective=""funny"")) Write antonyms for the following words. Input: happy Output: sad Input: tall Output: short Input: funny Output:PromptTemplate with OutputParser​This shows an example of loading a prompt along with an OutputParser from a file.cat prompt_with_output_parser.json { ""input_variables"": [ ""question"", ""student_answer"" ], ""output_parser"": { ""regex"": ""(.*?)\\nScore: (.*)"", ""output_keys"": [ ""answer"", ""score"" ], ""default_output_key"": null, ""_type"": ""regex_parser"" }, ""partial_variables"": {}, ""template"": ""Given the following question and student answer, provide a correct answer and score the student answer.\nQuestion: {question}\nStudent Answer: {student_answer}\nCorrect Answer:"", ""template_format"": ""f-string"", ""validate_template"": true, ""_type"": ""prompt"" }prompt = load_prompt(""prompt_with_output_parser.json"")prompt.output_parser.parse( ""George Washington was born in 1732 and died in 1799.\nScore: 1/2"") {'answer': 'George Washington was born in 1732 and died in 1799.', 'score': '1/2'}PreviousCompositionNextPrompt pipeliningPromptTemplateLoading from YAMLLoading from JSONLoading template from a fileFewShotPromptTemplateExamplesLoading from YAMLLoading from JSONExamples in the configExample prompt from a filePromptTemplate with OutputParser" 37,https://python.langchain.com/docs/modules/model_io/prompts/prompt_templates/prompts_pipelining,"ModulesModel I/​OPromptsPrompt templatesPrompt pipeliningOn this pagePrompt pipeliningThe idea behind prompt pipelining is to provide a user friendly interface for composing different parts of prompts together. You can do this with either string prompts or chat prompts. Constructing prompts this way allows for easy reuse of components.String prompt pipelining​When working with string prompts, each template is joined togther. You can work with either prompts directly or strings (the first element in the list needs to be a prompt).from langchain.prompts import PromptTemplateprompt = ( PromptTemplate.from_template(""Tell me a joke about {topic}"") + "", make it funny"" + ""\n\nand in {language}"")prompt PromptTemplate(input_variables=['language', 'topic'], output_parser=None, partial_variables={}, template='Tell me a joke about {topic}, make it funny\n\nand in {language}', template_format='f-string', validate_template=True)prompt.format(topic=""sports"", language=""spanish"") 'Tell me a joke about sports, make it funny\n\nand in spanish'You can also use it in an LLMChain, just like before.from langchain.chat_models import ChatOpenAIfrom langchain.chains import LLMChainmodel = ChatOpenAI()chain = LLMChain(llm=model, prompt=prompt)chain.run(topic=""sports"", language=""spanish"") '¿Por qué el futbolista llevaba un paraguas al partido?\n\nPorque pronosticaban lluvia de goles.'Chat prompt pipelining​A chat prompt is made up a of a list of messages. Purely for developer experience, we've added a convinient way to create these prompts. In this pipeline, each new element is a new message in the final prompt.from langchain.prompts import ChatPromptTemplate, HumanMessagePromptTemplatefrom langchain.schema import HumanMessage, AIMessage, SystemMessageFirst, let's initialize the base ChatPromptTemplate with a system message. It doesn't have to start with a system, but it's often good practiceprompt = SystemMessage(content=""You are a nice pirate"")You can then easily create a pipeline combining it with other messages or message templates. Use a Message when there is no variables to be formatted, use a MessageTemplate when there are variables to be formatted. You can also use just a string (note: this will automatically get inferred as a HumanMessagePromptTemplate.)new_prompt = ( prompt + HumanMessage(content=""hi"") + AIMessage(content=""what?"") + ""{input}"")Under the hood, this creates an instance of the ChatPromptTemplate class, so you can use it just as you did before!new_prompt.format_messages(input=""i said hi"") [SystemMessage(content='You are a nice pirate', additional_kwargs={}), HumanMessage(content='hi', additional_kwargs={}, example=False), AIMessage(content='what?', additional_kwargs={}, example=False), HumanMessage(content='i said hi', additional_kwargs={}, example=False)]You can also use it in an LLMChain, just like before.from langchain.chat_models import ChatOpenAIfrom langchain.chains import LLMChainmodel = ChatOpenAI()chain = LLMChain(llm=model, prompt=new_prompt)chain.run(""i said hi"") 'Oh, hello! How can I assist you today?'PreviousSerializationNextValidate templateString prompt pipeliningChat prompt pipelining" 38,https://python.langchain.com/docs/modules/model_io/prompts/prompt_templates/validate,"ModulesModel I/​OPromptsPrompt templatesValidate templateValidate templateBy default, PromptTemplate will validate the template string by checking whether the input_variables match the variables defined in template. You can disable this behavior by setting validate_template to False.template = ""I am learning langchain because {reason}.""prompt_template = PromptTemplate(template=template, input_variables=[""reason"", ""foo""]) # ValueError due to extra variablesprompt_template = PromptTemplate(template=template, input_variables=[""reason"", ""foo""], validate_template=False) # No errorPreviousPrompt pipeliningNextExample selectors" 39,https://python.langchain.com/docs/modules/model_io/prompts/example_selectors/,"ModulesModel I/​OPromptsExample selectorsExample selectorsIf you have a large number of examples, you may need to select which ones to include in the prompt. The Example Selector is the class responsible for doing so.The base interface is defined as below:class BaseExampleSelector(ABC): """"""Interface for selecting examples to include in prompts."""""" @abstractmethod def select_examples(self, input_variables: Dict[str, str]) -> List[dict]: """"""Select which examples to use based on the inputs.""""""The only method it needs to define is a select_examples method. This takes in the input variables and then returns a list of examples. It is up to each specific implementation as to how those examples are selected.PreviousValidate templateNextCustom example selector" 40,https://python.langchain.com/docs/modules/model_io/models/,"ModulesModel I/​OLanguage modelsOn this pageLanguage modelsLangChain provides interfaces and integrations for two types of models:LLMs: Models that take a text string as input and return a text stringChat models: Models that are backed by a language model but take a list of Chat Messages as input and return a Chat MessageLLMs vs chat models​LLMs and chat models are subtly but importantly different. LLMs in LangChain refer to pure text completion models. The APIs they wrap take a string prompt as input and output a string completion. OpenAI's GPT-3 is implemented as an LLM. Chat models are often backed by LLMs but tuned specifically for having conversations. And, crucially, their provider APIs use a different interface than pure text completion models. Instead of a single string, they take a list of chat messages as input. Usually these messages are labeled with the speaker (usually one of ""System"", ""AI"", and ""Human""). And they return an AI chat message as output. GPT-4 and Anthropic's Claude are both implemented as chat models.To make it possible to swap LLMs and chat models, both implement the Base Language Model interface. This includes common methods ""predict"", which takes a string and returns a string, and ""predict messages"", which takes messages and returns a message. If you are using a specific model it's recommended you use the methods specific to that model class (i.e., ""predict"" for LLMs and ""predict messages"" for chat models), but if you're creating an application that should work with different types of models the shared interface can be helpful.PreviousSelect by similarityNextLLMsLLMs vs chat models" 41,https://python.langchain.com/docs/modules/model_io/output_parsers/,"ModulesModel I/​OOutput parsersOn this pageOutput parsersLanguage models output text. But many times you may want to get more structured information than just text back. This is where output parsers come in.Output parsers are classes that help structure language model responses. There are two main methods an output parser must implement:""Get format instructions"": A method which returns a string containing instructions for how the output of a language model should be formatted.""Parse"": A method which takes in a string (assumed to be the response from a language model) and parses it into some structure.And then one optional one:""Parse with prompt"": A method which takes in a string (assumed to be the response from a language model) and a prompt (assumed to be the prompt that generated such a response) and parses it into some structure. The prompt is largely provided in the event the OutputParser wants to retry or fix the output in some way, and needs information from the prompt to do so.Get started​Below we go over the main type of output parser, the PydanticOutputParser.from langchain.prompts import PromptTemplate, ChatPromptTemplate, HumanMessagePromptTemplatefrom langchain.llms import OpenAIfrom langchain.chat_models import ChatOpenAIfrom langchain.output_parsers import PydanticOutputParserfrom pydantic import BaseModel, Field, validatorfrom typing import Listmodel_name = 'text-davinci-003'temperature = 0.0model = OpenAI(model_name=model_name, temperature=temperature)# Define your desired data structure.class Joke(BaseModel): setup: str = Field(description=""question to set up a joke"") punchline: str = Field(description=""answer to resolve the joke"") # You can add custom validation logic easily with Pydantic. @validator('setup') def question_ends_with_question_mark(cls, field): if field[-1] != '?': raise ValueError(""Badly formed question!"") return field# Set up a parser + inject instructions into the prompt template.parser = PydanticOutputParser(pydantic_object=Joke)prompt = PromptTemplate( template=""Answer the user query.\n{format_instructions}\n{query}\n"", input_variables=[""query""], partial_variables={""format_instructions"": parser.get_format_instructions()})# And a query intended to prompt a language model to populate the data structure.joke_query = ""Tell me a joke.""_input = prompt.format_prompt(query=joke_query)output = model(_input.to_string())parser.parse(output) Joke(setup='Why did the chicken cross the road?', punchline='To get to the other side!')PreviousStreamingNextList parserGet started" 42,https://python.langchain.com/docs/modules/data_connection/,"ModulesRetrievalRetrievalMany LLM applications require user-specific data that is not part of the model's training set. The primary way of accomplishing this is through Retrieval Augmented Generation (RAG). In this process, external data is retrieved and then passed to the LLM when doing the generation step.LangChain provides all the building blocks for RAG applications - from simple to complex. This section of the documentation covers everything related to the retrieval step - e.g. the fetching of the data. Although this sounds simple, it can be subtly complex. This encompasses several key modules.Document loadersLoad documents from many different sources. LangChain provides over 100 different document loaders as well as integrations with other major providers in the space, like AirByte and Unstructured. We provide integrations to load all types of documents (HTML, PDF, code) from all types of locations (private s3 buckets, public websites).Document transformersA key part of retrieval is fetching only the relevant parts of documents. This involves several transformation steps in order to best prepare the documents for retrieval. One of the primary ones here is splitting (or chunking) a large document into smaller chunks. LangChain provides several different algorithms for doing this, as well as logic optimized for specific document types (code, markdown, etc).Text embedding modelsAnother key part of retrieval has become creating embeddings for documents. Embeddings capture the semantic meaning of the text, allowing you to quickly and efficiently find other pieces of text that are similar. LangChain provides integrations with over 25 different embedding providers and methods, from open-source to proprietary API, allowing you to choose the one best suited for your needs. LangChain provides a standard interface, allowing you to easily swap between models.Vector storesWith the rise of embeddings, there has emerged a need for databases to support efficient storage and searching of these embeddings. LangChain provides integrations with over 50 different vectorstores, from open-source local ones to cloud-hosted proprietary ones, allowing you to choose the one best suited for your needs. LangChain exposes a standard interface, allowing you to easily swap between vector stores.RetrieversOnce the data is in the database, you still need to retrieve it. LangChain supports many different retrieval algorithms and is one of the places where we add the most value. We support basic methods that are easy to get started - namely simple semantic search. However, we have also added a collection of algorithms on top of this to increase performance. These include:Parent Document Retriever: This allows you to create multiple embeddings per parent document, allowing you to look up smaller chunks but return larger context.Self Query Retriever: User questions often contain a reference to something that isn't just semantic but rather expresses some logic that can best be represented as a metadata filter. Self-query allows you to parse out the semantic part of a query from other metadata filters present in the query.Ensemble Retriever: Sometimes you may want to retrieve documents from multiple different sources, or using multiple different algorithms. The ensemble retriever allows you to easily do this.And more!PreviousXML parserNextDocument loaders" 43,https://python.langchain.com/docs/modules/data_connection/document_loaders/,"ModulesRetrievalDocument loadersOn this pageDocument loadersinfoHead to Integrations for documentation on built-in document loader integrations with 3rd-party tools.Use document loaders to load data from a source as Document's. A Document is a piece of text and associated metadata. For example, there are document loaders for loading a simple .txt file, for loading the text contents of any web page, or even for loading a transcript of a YouTube video.Document loaders provide a ""load"" method for loading data as documents from a configured source. They optionally implement a ""lazy load"" as well for lazily loading data into memory.Get started​The simplest loader reads in a file as text and places it all into one document.from langchain.document_loaders import TextLoaderloader = TextLoader(""./index.md"")loader.load()[ Document(page_content='---\nsidebar_position: 0\n---\n# Document loaders\n\nUse document loaders to load data from a source as `Document`\'s. A `Document` is a piece of text\nand associated metadata. For example, there are document loaders for loading a simple `.txt` file, for loading the text\ncontents of any web page, or even for loading a transcript of a YouTube video.\n\nEvery document loader exposes two methods:\n1. ""Load"": load documents from the configured source\n2. ""Load and split"": load documents from the configured source and split them using the passed in text splitter\n\nThey optionally implement:\n\n3. ""Lazy load"": load documents into memory lazily\n', metadata={'source': '../docs/docs/modules/data_connection/document_loaders/index.md'})]PreviousRetrievalNextCSVGet started" 44,https://python.langchain.com/docs/modules/data_connection/document_loaders/csv,"ModulesRetrievalDocument loadersCSVCSVA comma-separated values (CSV) file is a delimited text file that uses a comma to separate values. Each line of the file is a data record. Each record consists of one or more fields, separated by commas.Load CSV data with a single row per document.from langchain.document_loaders.csv_loader import CSVLoaderloader = CSVLoader(file_path='./example_data/mlb_teams_2012.csv')data = loader.load()print(data) [Document(page_content='Team: Nationals\n""Payroll (millions)"": 81.34\n""Wins"": 98', lookup_str='', metadata={'source': './example_data/mlb_teams_2012.csv', 'row': 0}, lookup_index=0), Document(page_content='Team: Reds\n""Payroll (millions)"": 82.20\n""Wins"": 97', lookup_str='', metadata={'source': './example_data/mlb_teams_2012.csv', 'row': 1}, lookup_index=0), Document(page_content='Team: Yankees\n""Payroll (millions)"": 197.96\n""Wins"": 95', lookup_str='', metadata={'source': './example_data/mlb_teams_2012.csv', 'row': 2}, lookup_index=0), Document(page_content='Team: Giants\n""Payroll (millions)"": 117.62\n""Wins"": 94', lookup_str='', metadata={'source': './example_data/mlb_teams_2012.csv', 'row': 3}, lookup_index=0), Document(page_content='Team: Braves\n""Payroll (millions)"": 83.31\n""Wins"": 94', lookup_str='', metadata={'source': './example_data/mlb_teams_2012.csv', 'row': 4}, lookup_index=0), Document(page_content='Team: Athletics\n""Payroll (millions)"": 55.37\n""Wins"": 94', lookup_str='', metadata={'source': './example_data/mlb_teams_2012.csv', 'row': 5}, lookup_index=0), Document(page_content='Team: Rangers\n""Payroll (millions)"": 120.51\n""Wins"": 93', lookup_str='', metadata={'source': './example_data/mlb_teams_2012.csv', 'row': 6}, lookup_index=0), Document(page_content='Team: Orioles\n""Payroll (millions)"": 81.43\n""Wins"": 93', lookup_str='', metadata={'source': './example_data/mlb_teams_2012.csv', 'row': 7}, lookup_index=0), Document(page_content='Team: Rays\n""Payroll (millions)"": 64.17\n""Wins"": 90', lookup_str='', metadata={'source': './example_data/mlb_teams_2012.csv', 'row': 8}, lookup_index=0), Document(page_content='Team: Angels\n""Payroll (millions)"": 154.49\n""Wins"": 89', lookup_str='', metadata={'source': './example_data/mlb_teams_2012.csv', 'row': 9}, lookup_index=0), Document(page_content='Team: Tigers\n""Payroll (millions)"": 132.30\n""Wins"": 88', lookup_str='', metadata={'source': './example_data/mlb_teams_2012.csv', 'row': 10}, lookup_index=0), Document(page_content='Team: Cardinals\n""Payroll (millions)"": 110.30\n""Wins"": 88', lookup_str='', metadata={'source': './example_data/mlb_teams_2012.csv', 'row': 11}, lookup_index=0), Document(page_content='Team: Dodgers\n""Payroll (millions)"": 95.14\n""Wins"": 86', lookup_str='', metadata={'source': './example_data/mlb_teams_2012.csv', 'row': 12}, lookup_index=0), Document(page_content='Team: White Sox\n""Payroll (millions)"": 96.92\n""Wins"": 85', lookup_str='', metadata={'source': './example_data/mlb_teams_2012.csv', 'row': 13}, lookup_index=0), Document(page_content='Team: Brewers\n""Payroll (millions)"": 97.65\n""Wins"": 83', lookup_str='', metadata={'source': './example_data/mlb_teams_2012.csv', 'row': 14}, lookup_index=0), Document(page_content='Team: Phillies\n""Payroll (millions)"": 174.54\n""Wins"": 81', lookup_str='', metadata={'source': './example_data/mlb_teams_2012.csv', 'row': 15}, lookup_index=0), Document(page_content='Team: Diamondbacks\n""Payroll (millions)"": 74.28\n""Wins"": 81', lookup_str='', metadata={'source': './example_data/mlb_teams_2012.csv', 'row': 16}, lookup_index=0), Document(page_content='Team: Pirates\n""Payroll (millions)"": 63.43\n""Wins"": 79', lookup_str='', metadata={'source': './example_data/mlb_teams_2012.csv', 'row': 17}, lookup_index=0), Document(page_content='Team: Padres\n""Payroll (millions)"": 55.24\n""Wins"": 76', lookup_str='', metadata={'source': './example_data/mlb_teams_2012.csv', 'row': 18}, lookup_index=0), Document(page_content='Team: Mariners\n""Payroll (millions)"": 81.97\n""Wins"": 75', lookup_str='', metadata={'source': './example_data/mlb_teams_2012.csv', 'row': 19}, lookup_index=0), Document(page_content='Team: Mets\n""Payroll (millions)"": 93.35\n""Wins"": 74', lookup_str='', metadata={'source': './example_data/mlb_teams_2012.csv', 'row': 20}, lookup_index=0), Document(page_content='Team: Blue Jays\n""Payroll (millions)"": 75.48\n""Wins"": 73', lookup_str='', metadata={'source': './example_data/mlb_teams_2012.csv', 'row': 21}, lookup_index=0), Document(page_content='Team: Royals\n""Payroll (millions)"": 60.91\n""Wins"": 72', lookup_str='', metadata={'source': './example_data/mlb_teams_2012.csv', 'row': 22}, lookup_index=0), Document(page_content='Team: Marlins\n""Payroll (millions)"": 118.07\n""Wins"": 69', lookup_str='', metadata={'source': './example_data/mlb_teams_2012.csv', 'row': 23}, lookup_index=0), Document(page_content='Team: Red Sox\n""Payroll (millions)"": 173.18\n""Wins"": 69', lookup_str='', metadata={'source': './example_data/mlb_teams_2012.csv', 'row': 24}, lookup_index=0), Document(page_content='Team: Indians\n""Payroll (millions)"": 78.43\n""Wins"": 68', lookup_str='', metadata={'source': './example_data/mlb_teams_2012.csv', 'row': 25}, lookup_index=0), Document(page_content='Team: Twins\n""Payroll (millions)"": 94.08\n""Wins"": 66', lookup_str='', metadata={'source': './example_data/mlb_teams_2012.csv', 'row': 26}, lookup_index=0), Document(page_content='Team: Rockies\n""Payroll (millions)"": 78.06\n""Wins"": 64', lookup_str='', metadata={'source': './example_data/mlb_teams_2012.csv', 'row': 27}, lookup_index=0), Document(page_content='Team: Cubs\n""Payroll (millions)"": 88.19\n""Wins"": 61', lookup_str='', metadata={'source': './example_data/mlb_teams_2012.csv', 'row': 28}, lookup_index=0), Document(page_content='Team: Astros\n""Payroll (millions)"": 60.65\n""Wins"": 55', lookup_str='', metadata={'source': './example_data/mlb_teams_2012.csv', 'row': 29}, lookup_index=0)]Customizing the CSV parsing and loading​See the csv module documentation for more information of what csv args are supported.loader = CSVLoader(file_path='./example_data/mlb_teams_2012.csv', csv_args={ 'delimiter': ',', 'quotechar': '""', 'fieldnames': ['MLB Team', 'Payroll in millions', 'Wins']})data = loader.load()print(data) [Document(page_content='MLB Team: Team\nPayroll in millions: ""Payroll (millions)""\nWins: ""Wins""', lookup_str='', metadata={'source': './example_data/mlb_teams_2012.csv', 'row': 0}, lookup_index=0), Document(page_content='MLB Team: Nationals\nPayroll in millions: 81.34\nWins: 98', lookup_str='', metadata={'source': './example_data/mlb_teams_2012.csv', 'row': 1}, lookup_index=0), Document(page_content='MLB Team: Reds\nPayroll in millions: 82.20\nWins: 97', lookup_str='', metadata={'source': './example_data/mlb_teams_2012.csv', 'row': 2}, lookup_index=0), Document(page_content='MLB Team: Yankees\nPayroll in millions: 197.96\nWins: 95', lookup_str='', metadata={'source': './example_data/mlb_teams_2012.csv', 'row': 3}, lookup_index=0), Document(page_content='MLB Team: Giants\nPayroll in millions: 117.62\nWins: 94', lookup_str='', metadata={'source': './example_data/mlb_teams_2012.csv', 'row': 4}, lookup_index=0), Document(page_content='MLB Team: Braves\nPayroll in millions: 83.31\nWins: 94', lookup_str='', metadata={'source': './example_data/mlb_teams_2012.csv', 'row': 5}, lookup_index=0), Document(page_content='MLB Team: Athletics\nPayroll in millions: 55.37\nWins: 94', lookup_str='', metadata={'source': './example_data/mlb_teams_2012.csv', 'row': 6}, lookup_index=0), Document(page_content='MLB Team: Rangers\nPayroll in millions: 120.51\nWins: 93', lookup_str='', metadata={'source': './example_data/mlb_teams_2012.csv', 'row': 7}, lookup_index=0), Document(page_content='MLB Team: Orioles\nPayroll in millions: 81.43\nWins: 93', lookup_str='', metadata={'source': './example_data/mlb_teams_2012.csv', 'row': 8}, lookup_index=0), Document(page_content='MLB Team: Rays\nPayroll in millions: 64.17\nWins: 90', lookup_str='', metadata={'source': './example_data/mlb_teams_2012.csv', 'row': 9}, lookup_index=0), Document(page_content='MLB Team: Angels\nPayroll in millions: 154.49\nWins: 89', lookup_str='', metadata={'source': './example_data/mlb_teams_2012.csv', 'row': 10}, lookup_index=0), Document(page_content='MLB Team: Tigers\nPayroll in millions: 132.30\nWins: 88', lookup_str='', metadata={'source': './example_data/mlb_teams_2012.csv', 'row': 11}, lookup_index=0), Document(page_content='MLB Team: Cardinals\nPayroll in millions: 110.30\nWins: 88', lookup_str='', metadata={'source': './example_data/mlb_teams_2012.csv', 'row': 12}, lookup_index=0), Document(page_content='MLB Team: Dodgers\nPayroll in millions: 95.14\nWins: 86', lookup_str='', metadata={'source': './example_data/mlb_teams_2012.csv', 'row': 13}, lookup_index=0), Document(page_content='MLB Team: White Sox\nPayroll in millions: 96.92\nWins: 85', lookup_str='', metadata={'source': './example_data/mlb_teams_2012.csv', 'row': 14}, lookup_index=0), Document(page_content='MLB Team: Brewers\nPayroll in millions: 97.65\nWins: 83', lookup_str='', metadata={'source': './example_data/mlb_teams_2012.csv', 'row': 15}, lookup_index=0), Document(page_content='MLB Team: Phillies\nPayroll in millions: 174.54\nWins: 81', lookup_str='', metadata={'source': './example_data/mlb_teams_2012.csv', 'row': 16}, lookup_index=0), Document(page_content='MLB Team: Diamondbacks\nPayroll in millions: 74.28\nWins: 81', lookup_str='', metadata={'source': './example_data/mlb_teams_2012.csv', 'row': 17}, lookup_index=0), Document(page_content='MLB Team: Pirates\nPayroll in millions: 63.43\nWins: 79', lookup_str='', metadata={'source': './example_data/mlb_teams_2012.csv', 'row': 18}, lookup_index=0), Document(page_content='MLB Team: Padres\nPayroll in millions: 55.24\nWins: 76', lookup_str='', metadata={'source': './example_data/mlb_teams_2012.csv', 'row': 19}, lookup_index=0), Document(page_content='MLB Team: Mariners\nPayroll in millions: 81.97\nWins: 75', lookup_str='', metadata={'source': './example_data/mlb_teams_2012.csv', 'row': 20}, lookup_index=0), Document(page_content='MLB Team: Mets\nPayroll in millions: 93.35\nWins: 74', lookup_str='', metadata={'source': './example_data/mlb_teams_2012.csv', 'row': 21}, lookup_index=0), Document(page_content='MLB Team: Blue Jays\nPayroll in millions: 75.48\nWins: 73', lookup_str='', metadata={'source': './example_data/mlb_teams_2012.csv', 'row': 22}, lookup_index=0), Document(page_content='MLB Team: Royals\nPayroll in millions: 60.91\nWins: 72', lookup_str='', metadata={'source': './example_data/mlb_teams_2012.csv', 'row': 23}, lookup_index=0), Document(page_content='MLB Team: Marlins\nPayroll in millions: 118.07\nWins: 69', lookup_str='', metadata={'source': './example_data/mlb_teams_2012.csv', 'row': 24}, lookup_index=0), Document(page_content='MLB Team: Red Sox\nPayroll in millions: 173.18\nWins: 69', lookup_str='', metadata={'source': './example_data/mlb_teams_2012.csv', 'row': 25}, lookup_index=0), Document(page_content='MLB Team: Indians\nPayroll in millions: 78.43\nWins: 68', lookup_str='', metadata={'source': './example_data/mlb_teams_2012.csv', 'row': 26}, lookup_index=0), Document(page_content='MLB Team: Twins\nPayroll in millions: 94.08\nWins: 66', lookup_str='', metadata={'source': './example_data/mlb_teams_2012.csv', 'row': 27}, lookup_index=0), Document(page_content='MLB Team: Rockies\nPayroll in millions: 78.06\nWins: 64', lookup_str='', metadata={'source': './example_data/mlb_teams_2012.csv', 'row': 28}, lookup_index=0), Document(page_content='MLB Team: Cubs\nPayroll in millions: 88.19\nWins: 61', lookup_str='', metadata={'source': './example_data/mlb_teams_2012.csv', 'row': 29}, lookup_index=0), Document(page_content='MLB Team: Astros\nPayroll in millions: 60.65\nWins: 55', lookup_str='', metadata={'source': './example_data/mlb_teams_2012.csv', 'row': 30}, lookup_index=0)]Specify a column to identify the document source​Use the source_column argument to specify a source for the document created from each row. Otherwise file_path will be used as the source for all documents created from the CSV file.This is useful when using documents loaded from CSV files for chains that answer questions using sources.loader = CSVLoader(file_path='./example_data/mlb_teams_2012.csv', source_column=""Team"")data = loader.load()print(data) [Document(page_content='Team: Nationals\n""Payroll (millions)"": 81.34\n""Wins"": 98', lookup_str='', metadata={'source': 'Nationals', 'row': 0}, lookup_index=0), Document(page_content='Team: Reds\n""Payroll (millions)"": 82.20\n""Wins"": 97', lookup_str='', metadata={'source': 'Reds', 'row': 1}, lookup_index=0), Document(page_content='Team: Yankees\n""Payroll (millions)"": 197.96\n""Wins"": 95', lookup_str='', metadata={'source': 'Yankees', 'row': 2}, lookup_index=0), Document(page_content='Team: Giants\n""Payroll (millions)"": 117.62\n""Wins"": 94', lookup_str='', metadata={'source': 'Giants', 'row': 3}, lookup_index=0), Document(page_content='Team: Braves\n""Payroll (millions)"": 83.31\n""Wins"": 94', lookup_str='', metadata={'source': 'Braves', 'row': 4}, lookup_index=0), Document(page_content='Team: Athletics\n""Payroll (millions)"": 55.37\n""Wins"": 94', lookup_str='', metadata={'source': 'Athletics', 'row': 5}, lookup_index=0), Document(page_content='Team: Rangers\n""Payroll (millions)"": 120.51\n""Wins"": 93', lookup_str='', metadata={'source': 'Rangers', 'row': 6}, lookup_index=0), Document(page_content='Team: Orioles\n""Payroll (millions)"": 81.43\n""Wins"": 93', lookup_str='', metadata={'source': 'Orioles', 'row': 7}, lookup_index=0), Document(page_content='Team: Rays\n""Payroll (millions)"": 64.17\n""Wins"": 90', lookup_str='', metadata={'source': 'Rays', 'row': 8}, lookup_index=0), Document(page_content='Team: Angels\n""Payroll (millions)"": 154.49\n""Wins"": 89', lookup_str='', metadata={'source': 'Angels', 'row': 9}, lookup_index=0), Document(page_content='Team: Tigers\n""Payroll (millions)"": 132.30\n""Wins"": 88', lookup_str='', metadata={'source': 'Tigers', 'row': 10}, lookup_index=0), Document(page_content='Team: Cardinals\n""Payroll (millions)"": 110.30\n""Wins"": 88', lookup_str='', metadata={'source': 'Cardinals', 'row': 11}, lookup_index=0), Document(page_content='Team: Dodgers\n""Payroll (millions)"": 95.14\n""Wins"": 86', lookup_str='', metadata={'source': 'Dodgers', 'row': 12}, lookup_index=0), Document(page_content='Team: White Sox\n""Payroll (millions)"": 96.92\n""Wins"": 85', lookup_str='', metadata={'source': 'White Sox', 'row': 13}, lookup_index=0), Document(page_content='Team: Brewers\n""Payroll (millions)"": 97.65\n""Wins"": 83', lookup_str='', metadata={'source': 'Brewers', 'row': 14}, lookup_index=0), Document(page_content='Team: Phillies\n""Payroll (millions)"": 174.54\n""Wins"": 81', lookup_str='', metadata={'source': 'Phillies', 'row': 15}, lookup_index=0), Document(page_content='Team: Diamondbacks\n""Payroll (millions)"": 74.28\n""Wins"": 81', lookup_str='', metadata={'source': 'Diamondbacks', 'row': 16}, lookup_index=0), Document(page_content='Team: Pirates\n""Payroll (millions)"": 63.43\n""Wins"": 79', lookup_str='', metadata={'source': 'Pirates', 'row': 17}, lookup_index=0), Document(page_content='Team: Padres\n""Payroll (millions)"": 55.24\n""Wins"": 76', lookup_str='', metadata={'source': 'Padres', 'row': 18}, lookup_index=0), Document(page_content='Team: Mariners\n""Payroll (millions)"": 81.97\n""Wins"": 75', lookup_str='', metadata={'source': 'Mariners', 'row': 19}, lookup_index=0), Document(page_content='Team: Mets\n""Payroll (millions)"": 93.35\n""Wins"": 74', lookup_str='', metadata={'source': 'Mets', 'row': 20}, lookup_index=0), Document(page_content='Team: Blue Jays\n""Payroll (millions)"": 75.48\n""Wins"": 73', lookup_str='', metadata={'source': 'Blue Jays', 'row': 21}, lookup_index=0), Document(page_content='Team: Royals\n""Payroll (millions)"": 60.91\n""Wins"": 72', lookup_str='', metadata={'source': 'Royals', 'row': 22}, lookup_index=0), Document(page_content='Team: Marlins\n""Payroll (millions)"": 118.07\n""Wins"": 69', lookup_str='', metadata={'source': 'Marlins', 'row': 23}, lookup_index=0), Document(page_content='Team: Red Sox\n""Payroll (millions)"": 173.18\n""Wins"": 69', lookup_str='', metadata={'source': 'Red Sox', 'row': 24}, lookup_index=0), Document(page_content='Team: Indians\n""Payroll (millions)"": 78.43\n""Wins"": 68', lookup_str='', metadata={'source': 'Indians', 'row': 25}, lookup_index=0), Document(page_content='Team: Twins\n""Payroll (millions)"": 94.08\n""Wins"": 66', lookup_str='', metadata={'source': 'Twins', 'row': 26}, lookup_index=0), Document(page_content='Team: Rockies\n""Payroll (millions)"": 78.06\n""Wins"": 64', lookup_str='', metadata={'source': 'Rockies', 'row': 27}, lookup_index=0), Document(page_content='Team: Cubs\n""Payroll (millions)"": 88.19\n""Wins"": 61', lookup_str='', metadata={'source': 'Cubs', 'row': 28}, lookup_index=0), Document(page_content='Team: Astros\n""Payroll (millions)"": 60.65\n""Wins"": 55', lookup_str='', metadata={'source': 'Astros', 'row': 29}, lookup_index=0)]PreviousDocument loadersNextFile Directory" 45,https://python.langchain.com/docs/modules/data_connection/document_loaders/file_directory,"ModulesRetrievalDocument loadersFile DirectoryFile DirectoryThis covers how to load all documents in a directory.Under the hood, by default this uses the UnstructuredLoader.from langchain.document_loaders import DirectoryLoaderWe can use the glob parameter to control which files to load. Note that here it doesn't load the .rst file or the .html files.loader = DirectoryLoader('../', glob=""**/*.md"")docs = loader.load()len(docs) 1Show a progress bar​By default a progress bar will not be shown. To show a progress bar, install the tqdm library (e.g. pip install tqdm), and set the show_progress parameter to True.loader = DirectoryLoader('../', glob=""**/*.md"", show_progress=True)docs = loader.load() Requirement already satisfied: tqdm in /Users/jon/.pyenv/versions/3.9.16/envs/microbiome-app/lib/python3.9/site-packages (4.65.0) 0it [00:00, ?it/s]Use multithreading​By default the loading happens in one thread. In order to utilize several threads set the use_multithreading flag to true.loader = DirectoryLoader('../', glob=""**/*.md"", use_multithreading=True)docs = loader.load()Change loader class​By default this uses the UnstructuredLoader class. However, you can change up the type of loader pretty easily.from langchain.document_loaders import TextLoaderloader = DirectoryLoader('../', glob=""**/*.md"", loader_cls=TextLoader)docs = loader.load()len(docs) 1If you need to load Python source code files, use the PythonLoader.from langchain.document_loaders import PythonLoaderloader = DirectoryLoader('../../../../../', glob=""**/*.py"", loader_cls=PythonLoader)docs = loader.load()len(docs) 691Auto-detect file encodings with TextLoader​In this example we will see some strategies that can be useful when loading a big list of arbitrary files from a directory using the TextLoader class.First to illustrate the problem, let's try to load multiple text with arbitrary encodings.path = '../../../../../tests/integration_tests/examples'loader = DirectoryLoader(path, glob=""**/*.txt"", loader_cls=TextLoader)A. Default Behavior​loader.load()
╭─────────────────────────────── Traceback (most recent call last) ────────────────────────────────╮ /data/source/langchain/langchain/document_loaders/text.py:29 in load                                                                                                                                  26 │   │   text = """"                                                                              27 │   │   with open(self.file_path, encoding=self.encoding) as f:                                28 │   │   │   try:                                                                             29 │   │   │   │   text = f.read()                                                                30 │   │   │   except UnicodeDecodeError as e:                                                    31 │   │   │   │   if self.autodetect_encoding:                                                   32 │   │   │   │   │   detected_encodings = self.detect_file_encodings()                                                                                                                          /home/spike/.pyenv/versions/3.9.11/lib/python3.9/codecs.py:322 in decode                                                                                                                               319 def decode(self, input, final=False):                                                     320 │   │   # decode input (taking the buffer into account)                                       321 │   │   data = self.buffer + input                                                          322 │   │   (result, consumed) = self._buffer_decode(data, self.errors, final)                    323 │   │   # keep undecoded input until the next call                                            324 │   │   self.buffer = data[consumed:]                                                         325 │   │   return result                                                                     ╰──────────────────────────────────────────────────────────────────────────────────────────────────╯UnicodeDecodeError: 'utf-8' codec can't decode byte 0xca in position 0: invalid continuation byteThe above exception was the direct cause of the following exception:╭─────────────────────────────── Traceback (most recent call last) ────────────────────────────────╮ in <module>:1                                                                                                                                                                                       1 loader.load()                                                                                   2                                                                                                                                                                                                 /data/source/langchain/langchain/document_loaders/directory.py:84 in load                                                                                                                             81 │   │   │   │   │   │   if self.silent_errors:                                                 82 │   │   │   │   │   │   │   logger.warning(e)                                                  83 │   │   │   │   │   │   else:                                                                84 │   │   │   │   │   │   │   raise e                                                            85 │   │   │   │   │   finally:                                                                   86 │   │   │   │   │   │   if pbar:                                                               87 │   │   │   │   │   │   │   pbar.update(1)                                                                                                                                                     /data/source/langchain/langchain/document_loaders/directory.py:78 in load                                                                                                                             75 │   │   │   if i.is_file():                                                                    76 │   │   │   │   if _is_visible(i.relative_to(p)) or self.load_hidden:                          77 │   │   │   │   │   try:                                                                     78 │   │   │   │   │   │   sub_docs = self.loader_cls(str(i), **self.loader_kwargs).load()        79 │   │   │   │   │   │   docs.extend(sub_docs)                                                  80 │   │   │   │   │   except Exception as e:                                                     81 │   │   │   │   │   │   if self.silent_errors:                                                                                                                                                 /data/source/langchain/langchain/document_loaders/text.py:44 in load                                                                                                                                  41 │   │   │   │   │   │   except UnicodeDecodeError:                                             42 │   │   │   │   │   │   │   continue                                                           43 │   │   │   │   else:                                                                        44 │   │   │   │   │   raise RuntimeError(f""Error loading {self.file_path}"") from e               45 │   │   │   except Exception as e:                                                             46 │   │   │   │   raise RuntimeError(f""Error loading {self.file_path}"") from e                   47                                                                                             ╰──────────────────────────────────────────────────────────────────────────────────────────────────╯RuntimeError: Error loading ../../../../../tests/integration_tests/examples/example-non-utf8.txt
The file example-non-utf8.txt uses a different encoding, so the load() function fails with a helpful message indicating which file failed decoding. With the default behavior of TextLoader any failure to load any of the documents will fail the whole loading process and no documents are loaded. B. Silent fail​We can pass the parameter silent_errors to the DirectoryLoader to skip the files which could not be loaded and continue the load process.loader = DirectoryLoader(path, glob=""**/*.txt"", loader_cls=TextLoader, silent_errors=True)docs = loader.load() Error loading ../../../../../tests/integration_tests/examples/example-non-utf8.txtdoc_sources = [doc.metadata['source'] for doc in docs]doc_sources ['../../../../../tests/integration_tests/examples/whatsapp_chat.txt', '../../../../../tests/integration_tests/examples/example-utf8.txt']C. Auto detect encodings​We can also ask TextLoader to auto detect the file encoding before failing, by passing the autodetect_encoding to the loader class.text_loader_kwargs={'autodetect_encoding': True}loader = DirectoryLoader(path, glob=""**/*.txt"", loader_cls=TextLoader, loader_kwargs=text_loader_kwargs)docs = loader.load()doc_sources = [doc.metadata['source'] for doc in docs]doc_sources ['../../../../../tests/integration_tests/examples/example-non-utf8.txt', '../../../../../tests/integration_tests/examples/whatsapp_chat.txt', '../../../../../tests/integration_tests/examples/example-utf8.txt']PreviousCSVNextHTML" 46,https://python.langchain.com/docs/modules/data_connection/document_loaders/html,"ModulesRetrievalDocument loadersHTMLHTMLThe HyperText Markup Language or HTML is the standard markup language for documents designed to be displayed in a web browser.This covers how to load HTML documents into a document format that we can use downstream.from langchain.document_loaders import UnstructuredHTMLLoaderloader = UnstructuredHTMLLoader(""example_data/fake-content.html"")data = loader.load()data [Document(page_content='My First Heading\n\nMy first paragraph.', lookup_str='', metadata={'source': 'example_data/fake-content.html'}, lookup_index=0)]Loading HTML with BeautifulSoup4​We can also use BeautifulSoup4 to load HTML documents using the BSHTMLLoader. This will extract the text from the HTML into page_content, and the page title as title into metadata.from langchain.document_loaders import BSHTMLLoaderloader = BSHTMLLoader(""example_data/fake-content.html"")data = loader.load()data [Document(page_content='\n\nTest Title\n\n\nMy First Heading\nMy first paragraph.\n\n\n', metadata={'source': 'example_data/fake-content.html', 'title': 'Test Title'})]PreviousFile DirectoryNextJSON" 47,https://python.langchain.com/docs/modules/data_connection/document_loaders/json,"ModulesRetrievalDocument loadersJSONJSONJSON (JavaScript Object Notation) is an open standard file format and data interchange format that uses human-readable text to store and transmit data objects consisting of attribute–value pairs and arrays (or other serializable values).JSON Lines is a file format where each line is a valid JSON value.The JSONLoader uses a specified jq schema to parse the JSON files. It uses the jq python package. Check this manual for a detailed documentation of the jq syntax.#!pip install jqfrom langchain.document_loaders import JSONLoaderimport jsonfrom pathlib import Pathfrom pprint import pprintfile_path='./example_data/facebook_chat.json'data = json.loads(Path(file_path).read_text())pprint(data) {'image': {'creation_timestamp': 1675549016, 'uri': 'image_of_the_chat.jpg'}, 'is_still_participant': True, 'joinable_mode': {'link': '', 'mode': 1}, 'magic_words': [], 'messages': [{'content': 'Bye!', 'sender_name': 'User 2', 'timestamp_ms': 1675597571851}, {'content': 'Oh no worries! Bye', 'sender_name': 'User 1', 'timestamp_ms': 1675597435669}, {'content': 'No Im sorry it was my mistake, the blue one is not ' 'for sale', 'sender_name': 'User 2', 'timestamp_ms': 1675596277579}, {'content': 'I thought you were selling the blue one!', 'sender_name': 'User 1', 'timestamp_ms': 1675595140251}, {'content': 'Im not interested in this bag. Im interested in the ' 'blue one!', 'sender_name': 'User 1', 'timestamp_ms': 1675595109305}, {'content': 'Here is $129', 'sender_name': 'User 2', 'timestamp_ms': 1675595068468}, {'photos': [{'creation_timestamp': 1675595059, 'uri': 'url_of_some_picture.jpg'}], 'sender_name': 'User 2', 'timestamp_ms': 1675595060730}, {'content': 'Online is at least $100', 'sender_name': 'User 2', 'timestamp_ms': 1675595045152}, {'content': 'How much do you want?', 'sender_name': 'User 1', 'timestamp_ms': 1675594799696}, {'content': 'Goodmorning! $50 is too low.', 'sender_name': 'User 2', 'timestamp_ms': 1675577876645}, {'content': 'Hi! Im interested in your bag. Im offering $50. Let ' 'me know if you are interested. Thanks!', 'sender_name': 'User 1', 'timestamp_ms': 1675549022673}], 'participants': [{'name': 'User 1'}, {'name': 'User 2'}], 'thread_path': 'inbox/User 1 and User 2 chat', 'title': 'User 1 and User 2 chat'}Using JSONLoader​Suppose we are interested in extracting the values under the content field within the messages key of the JSON data. This can easily be done through the JSONLoader as shown below.JSON file​loader = JSONLoader( file_path='./example_data/facebook_chat.json', jq_schema='.messages[].content', text_content=False)data = loader.load()pprint(data) [Document(page_content='Bye!', metadata={'source': '/Users/avsolatorio/WBG/langchain/docs/modules/indexes/document_loaders/examples/example_data/facebook_chat.json', 'seq_num': 1}), Document(page_content='Oh no worries! Bye', metadata={'source': '/Users/avsolatorio/WBG/langchain/docs/modules/indexes/document_loaders/examples/example_data/facebook_chat.json', 'seq_num': 2}), Document(page_content='No Im sorry it was my mistake, the blue one is not for sale', metadata={'source': '/Users/avsolatorio/WBG/langchain/docs/modules/indexes/document_loaders/examples/example_data/facebook_chat.json', 'seq_num': 3}), Document(page_content='I thought you were selling the blue one!', metadata={'source': '/Users/avsolatorio/WBG/langchain/docs/modules/indexes/document_loaders/examples/example_data/facebook_chat.json', 'seq_num': 4}), Document(page_content='Im not interested in this bag. Im interested in the blue one!', metadata={'source': '/Users/avsolatorio/WBG/langchain/docs/modules/indexes/document_loaders/examples/example_data/facebook_chat.json', 'seq_num': 5}), Document(page_content='Here is $129', metadata={'source': '/Users/avsolatorio/WBG/langchain/docs/modules/indexes/document_loaders/examples/example_data/facebook_chat.json', 'seq_num': 6}), Document(page_content='', metadata={'source': '/Users/avsolatorio/WBG/langchain/docs/modules/indexes/document_loaders/examples/example_data/facebook_chat.json', 'seq_num': 7}), Document(page_content='Online is at least $100', metadata={'source': '/Users/avsolatorio/WBG/langchain/docs/modules/indexes/document_loaders/examples/example_data/facebook_chat.json', 'seq_num': 8}), Document(page_content='How much do you want?', metadata={'source': '/Users/avsolatorio/WBG/langchain/docs/modules/indexes/document_loaders/examples/example_data/facebook_chat.json', 'seq_num': 9}), Document(page_content='Goodmorning! $50 is too low.', metadata={'source': '/Users/avsolatorio/WBG/langchain/docs/modules/indexes/document_loaders/examples/example_data/facebook_chat.json', 'seq_num': 10}), Document(page_content='Hi! Im interested in your bag. Im offering $50. Let me know if you are interested. Thanks!', metadata={'source': '/Users/avsolatorio/WBG/langchain/docs/modules/indexes/document_loaders/examples/example_data/facebook_chat.json', 'seq_num': 11})]JSON Lines file​If you want to load documents from a JSON Lines file, you pass json_lines=True and specify jq_schema to extract page_content from a single JSON object.file_path = './example_data/facebook_chat_messages.jsonl'pprint(Path(file_path).read_text()) ('{""sender_name"": ""User 2"", ""timestamp_ms"": 1675597571851, ""content"": ""Bye!""}\n' '{""sender_name"": ""User 1"", ""timestamp_ms"": 1675597435669, ""content"": ""Oh no ' 'worries! Bye""}\n' '{""sender_name"": ""User 2"", ""timestamp_ms"": 1675596277579, ""content"": ""No Im ' 'sorry it was my mistake, the blue one is not for sale""}\n')loader = JSONLoader( file_path='./example_data/facebook_chat_messages.jsonl', jq_schema='.content', text_content=False, json_lines=True)data = loader.load()pprint(data) [Document(page_content='Bye!', metadata={'source': 'langchain/docs/modules/indexes/document_loaders/examples/example_data/facebook_chat_messages.jsonl', 'seq_num': 1}), Document(page_content='Oh no worries! Bye', metadata={'source': 'langchain/docs/modules/indexes/document_loaders/examples/example_data/facebook_chat_messages.jsonl', 'seq_num': 2}), Document(page_content='No Im sorry it was my mistake, the blue one is not for sale', metadata={'source': 'langchain/docs/modules/indexes/document_loaders/examples/example_data/facebook_chat_messages.jsonl', 'seq_num': 3})]Another option is set jq_schema='.' and provide content_key:loader = JSONLoader( file_path='./example_data/facebook_chat_messages.jsonl', jq_schema='.', content_key='sender_name', json_lines=True)data = loader.load()pprint(data) [Document(page_content='User 2', metadata={'source': 'langchain/docs/modules/indexes/document_loaders/examples/example_data/facebook_chat_messages.jsonl', 'seq_num': 1}), Document(page_content='User 1', metadata={'source': 'langchain/docs/modules/indexes/document_loaders/examples/example_data/facebook_chat_messages.jsonl', 'seq_num': 2}), Document(page_content='User 2', metadata={'source': 'langchain/docs/modules/indexes/document_loaders/examples/example_data/facebook_chat_messages.jsonl', 'seq_num': 3})]Extracting metadata​Generally, we want to include metadata available in the JSON file into the documents that we create from the content.The following demonstrates how metadata can be extracted using the JSONLoader.There are some key changes to be noted. In the previous example where we didn't collect the metadata, we managed to directly specify in the schema where the value for the page_content can be extracted from..messages[].contentIn the current example, we have to tell the loader to iterate over the records in the messages field. The jq_schema then has to be:.messages[]This allows us to pass the records (dict) into the metadata_func that has to be implemented. The metadata_func is responsible for identifying which pieces of information in the record should be included in the metadata stored in the final Document object.Additionally, we now have to explicitly specify in the loader, via the content_key argument, the key from the record where the value for the page_content needs to be extracted from.# Define the metadata extraction function.def metadata_func(record: dict, metadata: dict) -> dict: metadata[""sender_name""] = record.get(""sender_name"") metadata[""timestamp_ms""] = record.get(""timestamp_ms"") return metadataloader = JSONLoader( file_path='./example_data/facebook_chat.json', jq_schema='.messages[]', content_key=""content"", metadata_func=metadata_func)data = loader.load()pprint(data) [Document(page_content='Bye!', metadata={'source': '/Users/avsolatorio/WBG/langchain/docs/modules/indexes/document_loaders/examples/example_data/facebook_chat.json', 'seq_num': 1, 'sender_name': 'User 2', 'timestamp_ms': 1675597571851}), Document(page_content='Oh no worries! Bye', metadata={'source': '/Users/avsolatorio/WBG/langchain/docs/modules/indexes/document_loaders/examples/example_data/facebook_chat.json', 'seq_num': 2, 'sender_name': 'User 1', 'timestamp_ms': 1675597435669}), Document(page_content='No Im sorry it was my mistake, the blue one is not for sale', metadata={'source': '/Users/avsolatorio/WBG/langchain/docs/modules/indexes/document_loaders/examples/example_data/facebook_chat.json', 'seq_num': 3, 'sender_name': 'User 2', 'timestamp_ms': 1675596277579}), Document(page_content='I thought you were selling the blue one!', metadata={'source': '/Users/avsolatorio/WBG/langchain/docs/modules/indexes/document_loaders/examples/example_data/facebook_chat.json', 'seq_num': 4, 'sender_name': 'User 1', 'timestamp_ms': 1675595140251}), Document(page_content='Im not interested in this bag. Im interested in the blue one!', metadata={'source': '/Users/avsolatorio/WBG/langchain/docs/modules/indexes/document_loaders/examples/example_data/facebook_chat.json', 'seq_num': 5, 'sender_name': 'User 1', 'timestamp_ms': 1675595109305}), Document(page_content='Here is $129', metadata={'source': '/Users/avsolatorio/WBG/langchain/docs/modules/indexes/document_loaders/examples/example_data/facebook_chat.json', 'seq_num': 6, 'sender_name': 'User 2', 'timestamp_ms': 1675595068468}), Document(page_content='', metadata={'source': '/Users/avsolatorio/WBG/langchain/docs/modules/indexes/document_loaders/examples/example_data/facebook_chat.json', 'seq_num': 7, 'sender_name': 'User 2', 'timestamp_ms': 1675595060730}), Document(page_content='Online is at least $100', metadata={'source': '/Users/avsolatorio/WBG/langchain/docs/modules/indexes/document_loaders/examples/example_data/facebook_chat.json', 'seq_num': 8, 'sender_name': 'User 2', 'timestamp_ms': 1675595045152}), Document(page_content='How much do you want?', metadata={'source': '/Users/avsolatorio/WBG/langchain/docs/modules/indexes/document_loaders/examples/example_data/facebook_chat.json', 'seq_num': 9, 'sender_name': 'User 1', 'timestamp_ms': 1675594799696}), Document(page_content='Goodmorning! $50 is too low.', metadata={'source': '/Users/avsolatorio/WBG/langchain/docs/modules/indexes/document_loaders/examples/example_data/facebook_chat.json', 'seq_num': 10, 'sender_name': 'User 2', 'timestamp_ms': 1675577876645}), Document(page_content='Hi! Im interested in your bag. Im offering $50. Let me know if you are interested. Thanks!', metadata={'source': '/Users/avsolatorio/WBG/langchain/docs/modules/indexes/document_loaders/examples/example_data/facebook_chat.json', 'seq_num': 11, 'sender_name': 'User 1', 'timestamp_ms': 1675549022673})]Now, you will see that the documents contain the metadata associated with the content we extracted.The metadata_func​As shown above, the metadata_func accepts the default metadata generated by the JSONLoader. This allows full control to the user with respect to how the metadata is formatted.For example, the default metadata contains the source and the seq_num keys. However, it is possible that the JSON data contain these keys as well. The user can then exploit the metadata_func to rename the default keys and use the ones from the JSON data.The example below shows how we can modify the source to only contain information of the file source relative to the langchain directory.# Define the metadata extraction function.def metadata_func(record: dict, metadata: dict) -> dict: metadata[""sender_name""] = record.get(""sender_name"") metadata[""timestamp_ms""] = record.get(""timestamp_ms"") if ""source"" in metadata: source = metadata[""source""].split(""/"") source = source[source.index(""langchain""):] metadata[""source""] = ""/"".join(source) return metadataloader = JSONLoader( file_path='./example_data/facebook_chat.json', jq_schema='.messages[]', content_key=""content"", metadata_func=metadata_func)data = loader.load()pprint(data) [Document(page_content='Bye!', metadata={'source': 'langchain/docs/modules/indexes/document_loaders/examples/example_data/facebook_chat.json', 'seq_num': 1, 'sender_name': 'User 2', 'timestamp_ms': 1675597571851}), Document(page_content='Oh no worries! Bye', metadata={'source': 'langchain/docs/modules/indexes/document_loaders/examples/example_data/facebook_chat.json', 'seq_num': 2, 'sender_name': 'User 1', 'timestamp_ms': 1675597435669}), Document(page_content='No Im sorry it was my mistake, the blue one is not for sale', metadata={'source': 'langchain/docs/modules/indexes/document_loaders/examples/example_data/facebook_chat.json', 'seq_num': 3, 'sender_name': 'User 2', 'timestamp_ms': 1675596277579}), Document(page_content='I thought you were selling the blue one!', metadata={'source': 'langchain/docs/modules/indexes/document_loaders/examples/example_data/facebook_chat.json', 'seq_num': 4, 'sender_name': 'User 1', 'timestamp_ms': 1675595140251}), Document(page_content='Im not interested in this bag. Im interested in the blue one!', metadata={'source': 'langchain/docs/modules/indexes/document_loaders/examples/example_data/facebook_chat.json', 'seq_num': 5, 'sender_name': 'User 1', 'timestamp_ms': 1675595109305}), Document(page_content='Here is $129', metadata={'source': 'langchain/docs/modules/indexes/document_loaders/examples/example_data/facebook_chat.json', 'seq_num': 6, 'sender_name': 'User 2', 'timestamp_ms': 1675595068468}), Document(page_content='', metadata={'source': 'langchain/docs/modules/indexes/document_loaders/examples/example_data/facebook_chat.json', 'seq_num': 7, 'sender_name': 'User 2', 'timestamp_ms': 1675595060730}), Document(page_content='Online is at least $100', metadata={'source': 'langchain/docs/modules/indexes/document_loaders/examples/example_data/facebook_chat.json', 'seq_num': 8, 'sender_name': 'User 2', 'timestamp_ms': 1675595045152}), Document(page_content='How much do you want?', metadata={'source': 'langchain/docs/modules/indexes/document_loaders/examples/example_data/facebook_chat.json', 'seq_num': 9, 'sender_name': 'User 1', 'timestamp_ms': 1675594799696}), Document(page_content='Goodmorning! $50 is too low.', metadata={'source': 'langchain/docs/modules/indexes/document_loaders/examples/example_data/facebook_chat.json', 'seq_num': 10, 'sender_name': 'User 2', 'timestamp_ms': 1675577876645}), Document(page_content='Hi! Im interested in your bag. Im offering $50. Let me know if you are interested. Thanks!', metadata={'source': 'langchain/docs/modules/indexes/document_loaders/examples/example_data/facebook_chat.json', 'seq_num': 11, 'sender_name': 'User 1', 'timestamp_ms': 1675549022673})]Common JSON structures with jq schema​The list below provides a reference to the possible jq_schema the user can use to extract content from the JSON data depending on the structure.JSON -> [{""text"": ...}, {""text"": ...}, {""text"": ...}]jq_schema -> "".[].text""JSON -> {""key"": [{""text"": ...}, {""text"": ...}, {""text"": ...}]}jq_schema -> "".key[].text""JSON -> [""..."", ""..."", ""...""]jq_schema -> "".[]""PreviousHTMLNextMarkdown" 48,https://python.langchain.com/docs/modules/data_connection/document_loaders/markdown,"ModulesRetrievalDocument loadersMarkdownMarkdownMarkdown is a lightweight markup language for creating formatted text using a plain-text editor.This covers how to load Markdown documents into a document format that we can use downstream.# !pip install unstructured > /dev/nullfrom langchain.document_loaders import UnstructuredMarkdownLoadermarkdown_path = ""../../../../../README.md""loader = UnstructuredMarkdownLoader(markdown_path)data = loader.load()data [Document(page_content=""ð\x9f¦\x9cï¸\x8fð\x9f”\x97 LangChain\n\nâ\x9a¡ Building applications with LLMs through composability â\x9a¡\n\nLooking for the JS/TS version? Check out LangChain.js.\n\nProduction Support: As you move your LangChains into production, we'd love to offer more comprehensive support.\nPlease fill out this form and we'll set up a dedicated support Slack channel.\n\nQuick Install\n\npip install langchain\nor\nconda install langchain -c conda-forge\n\nð\x9f¤” What is this?\n\nLarge language models (LLMs) are emerging as a transformative technology, enabling developers to build applications that they previously could not. However, using these LLMs in isolation is often insufficient for creating a truly powerful app - the real power comes when you can combine them with other sources of computation or knowledge.\n\nThis library aims to assist in the development of those types of applications. Common examples of these applications include:\n\nâ\x9d“ Question Answering over specific documents\n\nDocumentation\n\nEnd-to-end Example: Question Answering over Notion Database\n\nð\x9f’¬ Chatbots\n\nDocumentation\n\nEnd-to-end Example: Chat-LangChain\n\nð\x9f¤\x96 Agents\n\nDocumentation\n\nEnd-to-end Example: GPT+WolframAlpha\n\nð\x9f“\x96 Documentation\n\nPlease see here for full documentation on:\n\nGetting started (installation, setting up the environment, simple examples)\n\nHow-To examples (demos, integrations, helper functions)\n\nReference (full API docs)\n\nResources (high-level explanation of core concepts)\n\nð\x9f\x9a\x80 What can this help with?\n\nThere are six main areas that LangChain is designed to help with.\nThese are, in increasing order of complexity:\n\nð\x9f“\x83 LLMs and Prompts:\n\nThis includes prompt management, prompt optimization, a generic interface for all LLMs, and common utilities for working with LLMs.\n\nð\x9f”\x97 Chains:\n\nChains go beyond a single LLM call and involve sequences of calls (whether to an LLM or a different utility). LangChain provides a standard interface for chains, lots of integrations with other tools, and end-to-end chains for common applications.\n\nð\x9f“\x9a Data Augmented Generation:\n\nData Augmented Generation involves specific types of chains that first interact with an external data source to fetch data for use in the generation step. Examples include summarization of long pieces of text and question/answering over specific data sources.\n\nð\x9f¤\x96 Agents:\n\nAgents involve an LLM making decisions about which Actions to take, taking that Action, seeing an Observation, and repeating that until done. LangChain provides a standard interface for agents, a selection of agents to choose from, and examples of end-to-end agents.\n\nð\x9f§\xa0 Memory:\n\nMemory refers to persisting state between calls of a chain/agent. LangChain provides a standard interface for memory, a collection of memory implementations, and examples of chains/agents that use memory.\n\nð\x9f§\x90 Evaluation:\n\n[BETA] Generative models are notoriously hard to evaluate with traditional metrics. One new way of evaluating them is using language models themselves to do the evaluation. LangChain provides some prompts/chains for assisting in this.\n\nFor more information on these concepts, please see our full documentation.\n\nð\x9f’\x81 Contributing\n\nAs an open-source project in a rapidly developing field, we are extremely open to contributions, whether it be in the form of a new feature, improved infrastructure, or better documentation.\n\nFor detailed information on how to contribute, see here."", metadata={'source': '../../../../../README.md'})]Retain Elements​Under the hood, Unstructured creates different ""elements"" for different chunks of text. By default we combine those together, but you can easily keep that separation by specifying mode=""elements"".loader = UnstructuredMarkdownLoader(markdown_path, mode=""elements"")data = loader.load()data[0] Document(page_content='ð\x9f¦\x9cï¸\x8fð\x9f”\x97 LangChain', metadata={'source': '../../../../../README.md', 'page_number': 1, 'category': 'Title'})PreviousJSONNextPDF" 49,https://python.langchain.com/docs/modules/data_connection/document_loaders/pdf,"ModulesRetrievalDocument loadersPDFPDFPortable Document Format (PDF), standardized as ISO 32000, is a file format developed by Adobe in 1992 to present documents, including text formatting and images, in a manner independent of application software, hardware, and operating systems.This covers how to load PDF documents into the Document format that we use downstream.Using PyPDF​Load PDF using pypdf into array of documents, where each document contains the page content and metadata with page number.pip install pypdffrom langchain.document_loaders import PyPDFLoaderloader = PyPDFLoader(""example_data/layout-parser-paper.pdf"")pages = loader.load_and_split()pages[0] Document(page_content='LayoutParser : A Uni\x0ced Toolkit for Deep\nLearning Based Document Image Analysis\nZejiang Shen1( \x00), Ruochen Zhang2, Melissa Dell3, Benjamin Charles Germain\nLee4, Jacob Carlson3, and Weining Li5\n1Allen Institute for AI\nshannons@allenai.org\n2Brown University\nruochen zhang@brown.edu\n3Harvard University\nfmelissadell,jacob carlson g@fas.harvard.edu\n4University of Washington\nbcgl@cs.washington.edu\n5University of Waterloo\nw422li@uwaterloo.ca\nAbstract. Recent advances in document image analysis (DIA) have been\nprimarily driven by the application of neural networks. Ideally, research\noutcomes could be easily deployed in production and extended for further\ninvestigation. However, various factors like loosely organized codebases\nand sophisticated model con\x0cgurations complicate the easy reuse of im-\nportant innovations by a wide audience. Though there have been on-going\ne\x0borts to improve reusability and simplify deep learning (DL) model\ndevelopment in disciplines like natural language processing and computer\nvision, none of them are optimized for challenges in the domain of DIA.\nThis represents a major gap in the existing toolkit, as DIA is central to\nacademic research across a wide range of disciplines in the social sciences\nand humanities. This paper introduces LayoutParser , an open-source\nlibrary for streamlining the usage of DL in DIA research and applica-\ntions. The core LayoutParser library comes with a set of simple and\nintuitive interfaces for applying and customizing DL models for layout de-\ntection, character recognition, and many other document processing tasks.\nTo promote extensibility, LayoutParser also incorporates a community\nplatform for sharing both pre-trained models and full document digiti-\nzation pipelines. We demonstrate that LayoutParser is helpful for both\nlightweight and large-scale digitization pipelines in real-word use cases.\nThe library is publicly available at https://layout-parser.github.io .\nKeywords: Document Image Analysis ·Deep Learning ·Layout Analysis\n·Character Recognition ·Open Source library ·Toolkit.\n1 Introduction\nDeep Learning(DL)-based approaches are the state-of-the-art for a wide range of\ndocument image analysis (DIA) tasks including document image classi\x0ccation [ 11,arXiv:2103.15348v2 [cs.CV] 21 Jun 2021', metadata={'source': 'example_data/layout-parser-paper.pdf', 'page': 0})An advantage of this approach is that documents can be retrieved with page numbers.We want to use OpenAIEmbeddings so we have to get the OpenAI API Key.import osimport getpassos.environ['OPENAI_API_KEY'] = getpass.getpass('OpenAI API Key:') OpenAI API Key: ········from langchain.vectorstores import FAISSfrom langchain.embeddings.openai import OpenAIEmbeddingsfaiss_index = FAISS.from_documents(pages, OpenAIEmbeddings())docs = faiss_index.similarity_search(""How will the community be engaged?"", k=2)for doc in docs: print(str(doc.metadata[""page""]) + "":"", doc.page_content[:300]) 9: 10 Z. Shen et al. Fig. 4: Illustration of (a) the original historical Japanese document with layout detection results and (b) a recreated version of the document image that achieves much better character recognition recall. The reorganization algorithm rearranges the tokens based on the their detect 3: 4 Z. Shen et al. Efficient Data AnnotationC u s t o m i z e d M o d e l T r a i n i n gModel Cust omizationDI A Model HubDI A Pipeline SharingCommunity PlatformLa y out Detection ModelsDocument Images T h e C o r e L a y o u t P a r s e r L i b r a r yOCR ModuleSt or age & VisualizationLa y ouExtracting images​Using the rapidocr-onnxruntime package we can extract images as text as well:pip install rapidocr-onnxruntimeloader = PyPDFLoader(""https://arxiv.org/pdf/2103.15348.pdf"", extract_images=True)pages = loader.load()pages[4].page_content'LayoutParser : A Unified Toolkit for DL-Based DIA 5\nTable 1: Current layout detection models in the LayoutParser model zoo\nDataset Base Model1Large Model Notes\nPubLayNet [38] F / M M Layouts of modern scientific documents\nPRImA [3] M - Layouts of scanned modern magazines and scientific reports\nNewspaper [17] F - Layouts of scanned US newspapers from the 20th century\nTableBank [18] F F Table region on modern scientific and business document\nHJDataset [31] F / M - Layouts of history Japanese documents\n1For each dataset, we train several models of different sizes for different needs (the trade-off between accuracy\nvs. computational cost). For “base model” and “large model”, we refer to using the ResNet 50 or ResNet 101\nbackbones [ 13], respectively. One can train models of different architectures, like Faster R-CNN [ 28] (F) and Mask\nR-CNN [ 12] (M). For example, an F in the Large Model column indicates it has a Faster R-CNN model trained\nusing the ResNet 101 backbone. The platform is maintained and a number of additions will be made to the model\nzoo in coming months.\nlayout data structures , which are optimized for efficiency and versatility. 3) When\nnecessary, users can employ existing or customized OCR models via the unified\nAPI provided in the OCR module . 4)LayoutParser comes with a set of utility\nfunctions for the visualization and storage of the layout data. 5) LayoutParser\nis also highly customizable, via its integration with functions for layout data\nannotation and model training . We now provide detailed descriptions for each\ncomponent.\n3.1 Layout Detection Models\nInLayoutParser , a layout model takes a document image as an input and\ngenerates a list of rectangular boxes for the target content regions. Different\nfrom traditional methods, it relies on deep convolutional neural networks rather\nthan manually curated rules to identify content regions. It is formulated as an\nobject detection problem and state-of-the-art models like Faster R-CNN [ 28] and\nMask R-CNN [ 12] are used. This yields prediction results of high accuracy and\nmakes it possible to build a concise, generalized interface for layout detection.\nLayoutParser , built upon Detectron2 [ 35], provides a minimal API that can\nperform layout detection with only four lines of code in Python:\n1import layoutparser as lp\n2image = cv2. imread ("" image_file "") # load images\n3model = lp. Detectron2LayoutModel (\n4 ""lp :// PubLayNet / faster_rcnn_R_50_FPN_3x / config "")\n5layout = model . detect ( image )\nLayoutParser provides a wealth of pre-trained model weights using various\ndatasets covering different languages, time periods, and document types. Due to\ndomain shift [ 7], the prediction performance can notably drop when models are ap-\nplied to target samples that are significantly different from the training dataset. As\ndocument structures and layouts vary greatly in different domains, it is important\nto select models trained on a dataset similar to the test samples. A semantic syntax\nis used for initializing the model weights in LayoutParser , using both the dataset\nname and model name lp:/// .'Using MathPix​Inspired by Daniel Gross's https://gist.github.com/danielgross/3ab4104e14faccc12b49200843adab21from langchain.document_loaders import MathpixPDFLoaderloader = MathpixPDFLoader(""example_data/layout-parser-paper.pdf"")data = loader.load()Using Unstructured​from langchain.document_loaders import UnstructuredPDFLoaderloader = UnstructuredPDFLoader(""example_data/layout-parser-paper.pdf"")data = loader.load()Retain Elements​Under the hood, Unstructured creates different ""elements"" for different chunks of text. By default we combine those together, but you can easily keep that separation by specifying mode=""elements"".loader = UnstructuredPDFLoader(""example_data/layout-parser-paper.pdf"", mode=""elements"")data = loader.load()data[0] Document(page_content='LayoutParser: A Unified Toolkit for Deep\nLearning Based Document Image Analysis\nZejiang Shen1 (�), Ruochen Zhang2, Melissa Dell3, Benjamin Charles Germain\nLee4, Jacob Carlson3, and Weining Li5\n1 Allen Institute for AI\nshannons@allenai.org\n2 Brown University\nruochen zhang@brown.edu\n3 Harvard University\n{melissadell,jacob carlson}@fas.harvard.edu\n4 University of Washington\nbcgl@cs.washington.edu\n5 University of Waterloo\nw422li@uwaterloo.ca\nAbstract. Recent advances in document image analysis (DIA) have been\nprimarily driven by the application of neural networks. Ideally, research\noutcomes could be easily deployed in production and extended for further\ninvestigation. However, various factors like loosely organized codebases\nand sophisticated model configurations complicate the easy reuse of im-\nportant innovations by a wide audience. Though there have been on-going\nefforts to improve reusability and simplify deep learning (DL) model\ndevelopment in disciplines like natural language processing and computer\nvision, none of them are optimized for challenges in the domain of DIA.\nThis represents a major gap in the existing toolkit, as DIA is central to\nacademic research across a wide range of disciplines in the social sciences\nand humanities. This paper introduces LayoutParser, an open-source\nlibrary for streamlining the usage of DL in DIA research and applica-\ntions. The core LayoutParser library comes with a set of simple and\nintuitive interfaces for applying and customizing DL models for layout de-\ntection, character recognition, and many other document processing tasks.\nTo promote extensibility, LayoutParser also incorporates a community\nplatform for sharing both pre-trained models and full document digiti-\nzation pipelines. We demonstrate that LayoutParser is helpful for both\nlightweight and large-scale digitization pipelines in real-word use cases.\nThe library is publicly available at https://layout-parser.github.io.\nKeywords: Document Image Analysis · Deep Learning · Layout Analysis\n· Character Recognition · Open Source library · Toolkit.\n1\nIntroduction\nDeep Learning(DL)-based approaches are the state-of-the-art for a wide range of\ndocument image analysis (DIA) tasks including document image classification [11,\narXiv:2103.15348v2 [cs.CV] 21 Jun 2021\n', lookup_str='', metadata={'file_path': 'example_data/layout-parser-paper.pdf', 'page_number': 1, 'total_pages': 16, 'format': 'PDF 1.5', 'title': '', 'author': '', 'subject': '', 'keywords': '', 'creator': 'LaTeX with hyperref', 'producer': 'pdfTeX-1.40.21', 'creationDate': 'D:20210622012710Z', 'modDate': 'D:20210622012710Z', 'trapped': '', 'encryption': None}, lookup_index=0)Fetching remote PDFs using Unstructured​This covers how to load online PDFs into a document format that we can use downstream. This can be used for various online PDF sites such as https://open.umn.edu/opentextbooks/textbooks/ and https://arxiv.org/archive/Note: all other PDF loaders can also be used to fetch remote PDFs, but OnlinePDFLoader is a legacy function, and works specifically with UnstructuredPDFLoader.from langchain.document_loaders import OnlinePDFLoaderloader = OnlinePDFLoader(""https://arxiv.org/pdf/2302.03803.pdf"")data = loader.load()print(data) [Document(page_content='A WEAK ( k, k ) -LEFSCHETZ THEOREM FOR PROJECTIVE TORIC ORBIFOLDS\n\nWilliam D. Montoya\n\nInstituto de Matem´atica, Estat´ıstica e Computa¸c˜ao Cient´ıfica,\n\nIn [3] we proved that, under suitable conditions, on a very general codimension s quasi- smooth intersection subvariety X in a projective toric orbifold P d Σ with d + s = 2 ( k + 1 ) the Hodge conjecture holds, that is, every ( p, p ) -cohomology class, under the Poincar´e duality is a rational linear combination of fundamental classes of algebraic subvarieties of X . The proof of the above-mentioned result relies, for p ≠ d + 1 − s , on a Lefschetz\n\nKeywords: (1,1)- Lefschetz theorem, Hodge conjecture, toric varieties, complete intersection Email: wmontoya@ime.unicamp.br\n\ntheorem ([7]) and the Hard Lefschetz theorem for projective orbifolds ([11]). When p = d + 1 − s the proof relies on the Cayley trick, a trick which associates to X a quasi-smooth hypersurface Y in a projective vector bundle, and the Cayley Proposition (4.3) which gives an isomorphism of some primitive cohomologies (4.2) of X and Y . The Cayley trick, following the philosophy of Mavlyutov in [7], reduces results known for quasi-smooth hypersurfaces to quasi-smooth intersection subvarieties. The idea in this paper goes the other way around, we translate some results for quasi-smooth intersection subvarieties to\n\nAcknowledgement. I thank Prof. Ugo Bruzzo and Tiago Fonseca for useful discus- sions. I also acknowledge support from FAPESP postdoctoral grant No. 2019/23499-7.\n\nLet M be a free abelian group of rank d , let N = Hom ( M, Z ) , and N R = N ⊗ Z R .\n\nif there exist k linearly independent primitive elements e\n\n, . . . , e k ∈ N such that σ = { µ\n\ne\n\n+ ⋯ + µ k e k } . • The generators e i are integral if for every i and any nonnegative rational number µ the product µe i is in N only if µ is an integer. • Given two rational simplicial cones σ , σ ′ one says that σ ′ is a face of σ ( σ ′ < σ ) if the set of integral generators of σ ′ is a subset of the set of integral generators of σ . • A finite set Σ = { σ\n\n, . . . , σ t } of rational simplicial cones is called a rational simplicial complete d -dimensional fan if:\n\nall faces of cones in Σ are in Σ ;\n\nif σ, σ ′ ∈ Σ then σ ∩ σ ′ < σ and σ ∩ σ ′ < σ ′ ;\n\nN R = σ\n\n∪ ⋅ ⋅ ⋅ ∪ σ t .\n\nA rational simplicial complete d -dimensional fan Σ defines a d -dimensional toric variety P d Σ having only orbifold singularities which we assume to be projective. Moreover, T ∶ = N ⊗ Z C ∗ ≃ ( C ∗ ) d is the torus action on P d Σ . We denote by Σ ( i ) the i -dimensional cones\n\nFor a cone σ ∈ Σ, ˆ σ is the set of 1-dimensional cone in Σ that are not contained in σ\n\nand x ˆ σ ∶ = ∏ ρ ∈ ˆ σ x ρ is the associated monomial in S .\n\nDefinition 2.2. The irrelevant ideal of P d Σ is the monomial ideal B Σ ∶ =< x ˆ σ ∣ σ ∈ Σ > and the zero locus Z ( Σ ) ∶ = V ( B Σ ) in the affine space A d ∶ = Spec ( S ) is the irrelevant locus.\n\nProposition 2.3 (Theorem 5.1.11 [5]) . The toric variety P d Σ is a categorical quotient A d ∖ Z ( Σ ) by the group Hom ( Cl ( Σ ) , C ∗ ) and the group action is induced by the Cl ( Σ ) - grading of S .\n\nNow we give a brief introduction to complex orbifolds and we mention the needed theorems for the next section. Namely: de Rham theorem and Dolbeault theorem for complex orbifolds.\n\nDefinition 2.4. A complex orbifold of complex dimension d is a singular complex space whose singularities are locally isomorphic to quotient singularities C d / G , for finite sub- groups G ⊂ Gl ( d, C ) .\n\nDefinition 2.5. A differential form on a complex orbifold Z is defined locally at z ∈ Z as a G -invariant differential form on C d where G ⊂ Gl ( d, C ) and Z is locally isomorphic to d\n\nRoughly speaking the local geometry of orbifolds reduces to local G -invariant geometry.\n\nWe have a complex of differential forms ( A ● ( Z ) , d ) and a double complex ( A ● , ● ( Z ) , ∂, ¯ ∂ ) of bigraded differential forms which define the de Rham and the Dolbeault cohomology groups (for a fixed p ∈ N ) respectively:\n\n(1,1)-Lefschetz theorem for projective toric orbifolds\n\nDefinition 3.1. A subvariety X ⊂ P d Σ is quasi-smooth if V ( I X ) ⊂ A #Σ ( 1 ) is smooth outside\n\nExample 3.2 . Quasi-smooth hypersurfaces or more generally quasi-smooth intersection sub-\n\nExample 3.2 . Quasi-smooth hypersurfaces or more generally quasi-smooth intersection sub- varieties are quasi-smooth subvarieties (see [2] or [7] for more details).\n\nRemark 3.3 . Quasi-smooth subvarieties are suborbifolds of P d Σ in the sense of Satake in [8]. Intuitively speaking they are subvarieties whose only singularities come from the ambient\n\nProof. From the exponential short exact sequence\n\nwe have a long exact sequence in cohomology\n\nH 1 (O ∗ X ) → H 2 ( X, Z ) → H 2 (O X ) ≃ H 0 , 2 ( X )\n\nwhere the last isomorphisms is due to Steenbrink in [9]. Now, it is enough to prove the commutativity of the next diagram\n\nwhere the last isomorphisms is due to Steenbrink in [9]. Now,\n\nH 2 ( X, Z ) / / H 2 ( X, O X ) ≃ Dolbeault H 2 ( X, C ) deRham ≃ H 2 dR ( X, C ) / / H 0 , 2 ¯ ∂ ( X )\n\nof the proof follows as the ( 1 , 1 ) -Lefschetz theorem in [6].\n\nRemark 3.5 . For k = 1 and P d Σ as the projective space, we recover the classical ( 1 , 1 ) - Lefschetz theorem.\n\nBy the Hard Lefschetz Theorem for projective orbifolds (see [11] for details) we\n\nBy the Hard Lefschetz Theorem for projective orbifolds (see [11] for details) we get an isomorphism of cohomologies :\n\ngiven by the Lefschetz morphism and since it is a morphism of Hodge structures, we have:\n\nH 1 , 1 ( X, Q ) ≃ H dim X − 1 , dim X − 1 ( X, Q )\n\nCorollary 3.6. If the dimension of X is 1 , 2 or 3 . The Hodge conjecture holds on X\n\nProof. If the dim C X = 1 the result is clear by the Hard Lefschetz theorem for projective orbifolds. The dimension 2 and 3 cases are covered by Theorem 3.5 and the Hard Lefschetz.\n\nCayley trick and Cayley proposition\n\nThe Cayley trick is a way to associate to a quasi-smooth intersection subvariety a quasi- smooth hypersurface. Let L 1 , . . . , L s be line bundles on P d Σ and let π ∶ P ( E ) → P d Σ be the projective space bundle associated to the vector bundle E = L 1 ⊕ ⋯ ⊕ L s . It is known that P ( E ) is a ( d + s − 1 ) -dimensional simplicial toric variety whose fan depends on the degrees of the line bundles and the fan Σ. Furthermore, if the Cox ring, without considering the grading, of P d Σ is C [ x 1 , . . . , x m ] then the Cox ring of P ( E ) is\n\nMoreover for X a quasi-smooth intersection subvariety cut off by f 1 , . . . , f s with deg ( f i ) = [ L i ] we relate the hypersurface Y cut off by F = y 1 f 1 + ⋅ ⋅ ⋅ + y s f s which turns out to be quasi-smooth. For more details see Section 2 in [7].\n\nWe will denote P ( E ) as P d + s − 1 Σ ,X to keep track of its relation with X and P d Σ .\n\nThe following is a key remark.\n\nRemark 4.1 . There is a morphism ι ∶ X → Y ⊂ P d + s − 1 Σ ,X . Moreover every point z ∶ = ( x, y ) ∈ Y with y ≠ 0 has a preimage. Hence for any subvariety W = V ( I W ) ⊂ X ⊂ P d Σ there exists W ′ ⊂ Y ⊂ P d + s − 1 Σ ,X such that π ( W ′ ) = W , i.e., W ′ = { z = ( x, y ) ∣ x ∈ W } .\n\nFor X ⊂ P d Σ a quasi-smooth intersection variety the morphism in cohomology induced by the inclusion i ∗ ∶ H d − s ( P d Σ , C ) → H d − s ( X, C ) is injective by Proposition 1.4 in [7].\n\nDefinition 4.2. The primitive cohomology of H d − s prim ( X ) is the quotient H d − s ( X, C )/ i ∗ ( H d − s ( P d Σ , C )) and H d − s prim ( X, Q ) with rational coefficients.\n\nH d − s ( P d Σ , C ) and H d − s ( X, C ) have pure Hodge structures, and the morphism i ∗ is com- patible with them, so that H d − s prim ( X ) gets a pure Hodge structure.\n\nThe next Proposition is the Cayley proposition.\n\nProposition 4.3. [Proposition 2.3 in [3] ] Let X = X 1 ∩⋅ ⋅ ⋅∩ X s be a quasi-smooth intersec- tion subvariety in P d Σ cut off by homogeneous polynomials f 1 . . . f s . Then for p ≠ d + s − 1 2 , d + s − 3 2\n\nRemark 4.5 . The above isomorphisms are also true with rational coefficients since H ● ( X, C ) = H ● ( X, Q ) ⊗ Q C . See the beginning of Section 7.1 in [10] for more details.\n\nTheorem 5.1. Let Y = { F = y 1 f 1 + ⋯ + y k f k = 0 } ⊂ P 2 k + 1 Σ ,X be the quasi-smooth hypersurface associated to the quasi-smooth intersection surface X = X f 1 ∩ ⋅ ⋅ ⋅ ∩ X f k ⊂ P k + 2 Σ . Then on Y the Hodge conjecture holds.\n\nthe Hodge conjecture holds.\n\nProof. If H k,k prim ( X, Q ) = 0 we are done. So let us assume H k,k prim ( X, Q ) ≠ 0. By the Cayley proposition H k,k prim ( Y, Q ) ≃ H 1 , 1 prim ( X, Q ) and by the ( 1 , 1 ) -Lefschetz theorem for projective\n\ntoric orbifolds there is a non-zero algebraic basis λ C 1 , . . . , λ C n with rational coefficients of H 1 , 1 prim ( X, Q ) , that is, there are n ∶ = h 1 , 1 prim ( X, Q ) algebraic curves C 1 , . . . , C n in X such that under the Poincar´e duality the class in homology [ C i ] goes to λ C i , [ C i ] ↦ λ C i . Recall that the Cox ring of P k + 2 is contained in the Cox ring of P 2 k + 1 Σ ,X without considering the grading. Considering the grading we have that if α ∈ Cl ( P k + 2 Σ ) then ( α, 0 ) ∈ Cl ( P 2 k + 1 Σ ,X ) . So the polynomials defining C i ⊂ P k + 2 Σ can be interpreted in P 2 k + 1 X, Σ but with different degree. Moreover, by Remark 4.1 each C i is contained in Y = { F = y 1 f 1 + ⋯ + y k f k = 0 } and\n\nfurthermore it has codimension k .\n\nClaim: { C i } ni = 1 is a basis of prim ( ) . It is enough to prove that λ C i is different from zero in H k,k prim ( Y, Q ) or equivalently that the cohomology classes { λ C i } ni = 1 do not come from the ambient space. By contradiction, let us assume that there exists a j and C ⊂ P 2 k + 1 Σ ,X such that λ C ∈ H k,k ( P 2 k + 1 Σ ,X , Q ) with i ∗ ( λ C ) = λ C j or in terms of homology there exists a ( k + 2 ) -dimensional algebraic subvariety V ⊂ P 2 k + 1 Σ ,X such that V ∩ Y = C j so they are equal as a homology class of P 2 k + 1 Σ ,X ,i.e., [ V ∩ Y ] = [ C j ] . It is easy to check that π ( V ) ∩ X = C j as a subvariety of P k + 2 Σ where π ∶ ( x, y ) ↦ x . Hence [ π ( V ) ∩ X ] = [ C j ] which is equivalent to say that λ C j comes from P k + 2 Σ which contradicts the choice of [ C j ] .\n\nRemark 5.2 . Into the proof of the previous theorem, the key fact was that on X the Hodge conjecture holds and we translate it to Y by contradiction. So, using an analogous argument we have:\n\nargument we have:\n\nProposition 5.3. Let Y = { F = y 1 f s +⋯+ y s f s = 0 } ⊂ P 2 k + 1 Σ ,X be the quasi-smooth hypersurface associated to a quasi-smooth intersection subvariety X = X f 1 ∩ ⋅ ⋅ ⋅ ∩ X f s ⊂ P d Σ such that d + s = 2 ( k + 1 ) . If the Hodge conjecture holds on X then it holds as well on Y .\n\nCorollary 5.4. If the dimension of Y is 2 s − 1 , 2 s or 2 s + 1 then the Hodge conjecture holds on Y .\n\nProof. By Proposition 5.3 and Corollary 3.6.\n\n[\n\n] Angella, D. Cohomologies of certain orbifolds. Journal of Geometry and Physics\n\n(\n\n),\n\n–\n\n[\n\n] Batyrev, V. V., and Cox, D. A. On the Hodge structure of projective hypersur- faces in toric varieties. Duke Mathematical Journal\n\n,\n\n(Aug\n\n). [\n\n] Bruzzo, U., and Montoya, W. On the Hodge conjecture for quasi-smooth in- tersections in toric varieties. S˜ao Paulo J. Math. Sci. Special Section: Geometry in Algebra and Algebra in Geometry (\n\n). [\n\n] Caramello Jr, F. C. Introduction to orbifolds. a\n\niv:\n\nv\n\n(\n\n). [\n\n] Cox, D., Little, J., and Schenck, H. Toric varieties, vol.\n\nAmerican Math- ematical Soc.,\n\n[\n\n] Griffiths, P., and Harris, J. Principles of Algebraic Geometry. John Wiley & Sons, Ltd,\n\n[\n\n] Mavlyutov, A. R. Cohomology of complete intersections in toric varieties. Pub- lished in Pacific J. of Math.\n\nNo.\n\n(\n\n),\n\n–\n\n[\n\n] Satake, I. On a Generalization of the Notion of Manifold. Proceedings of the National Academy of Sciences of the United States of America\n\n,\n\n(\n\n),\n\n–\n\n[\n\n] Steenbrink, J. H. M. Intersection form for quasi-homogeneous singularities. Com- positio Mathematica\n\n,\n\n(\n\n),\n\n–\n\n[\n\n] Voisin, C. Hodge Theory and Complex Algebraic Geometry I, vol.\n\nof Cambridge Studies in Advanced Mathematics . Cambridge University Press,\n\n[\n\n] Wang, Z. Z., and Zaffran, D. A remark on the Hard Lefschetz theorem for K¨ahler orbifolds. Proceedings of the American Mathematical Society\n\n,\n\n(Aug\n\n).\n\n[2] Batyrev, V. V., and Cox, D. A. On the Hodge structure of projective hypersur- faces in toric varieties. Duke Mathematical Journal 75, 2 (Aug 1994).\n\n[\n\n] Bruzzo, U., and Montoya, W. On the Hodge conjecture for quasi-smooth in- tersections in toric varieties. S˜ao Paulo J. Math. Sci. Special Section: Geometry in Algebra and Algebra in Geometry (\n\n).\n\n[3] Bruzzo, U., and Montoya, W. On the Hodge conjecture for quasi-smooth in- tersections in toric varieties. S˜ao Paulo J. Math. Sci. Special Section: Geometry in Algebra and Algebra in Geometry (2021).\n\nA. R. Cohomology of complete intersections in toric varieties. Pub-', lookup_str='', metadata={'source': '/var/folders/ph/hhm7_zyx4l13k3v8z02dwp1w0000gn/T/tmpgq0ckaja/online_file.pdf'}, lookup_index=0)]Using PyPDFium2​from langchain.document_loaders import PyPDFium2Loaderloader = PyPDFium2Loader(""example_data/layout-parser-paper.pdf"")data = loader.load()Using PDFMiner​from langchain.document_loaders import PDFMinerLoaderloader = PDFMinerLoader(""example_data/layout-parser-paper.pdf"")data = loader.load()Using PDFMiner to generate HTML text​This can be helpful for chunking texts semantically into sections as the output html content can be parsed via BeautifulSoup to get more structured and rich information about font size, page numbers, PDF headers/footers, etc.from langchain.document_loaders import PDFMinerPDFasHTMLLoaderloader = PDFMinerPDFasHTMLLoader(""example_data/layout-parser-paper.pdf"")data = loader.load()[0] # entire PDF is loaded as a single Documentfrom bs4 import BeautifulSoupsoup = BeautifulSoup(data.page_content,'html.parser')content = soup.find_all('div')import recur_fs = Nonecur_text = ''snippets = [] # first collect all snippets that have the same font sizefor c in content: sp = c.find('span') if not sp: continue st = sp.get('style') if not st: continue fs = re.findall('font-size:(\d+)px',st) if not fs: continue fs = int(fs[0]) if not cur_fs: cur_fs = fs if fs == cur_fs: cur_text += c.text else: snippets.append((cur_text,cur_fs)) cur_fs = fs cur_text = c.textsnippets.append((cur_text,cur_fs))# Note: The above logic is very straightforward. One can also add more strategies such as removing duplicate snippets (as# headers/footers in a PDF appear on multiple pages so if we find duplicates it's safe to assume that it is redundant info)from langchain.docstore.document import Documentcur_idx = -1semantic_snippets = []# Assumption: headings have higher font size than their respective contentfor s in snippets: # if current snippet's font size > previous section's heading => it is a new heading if not semantic_snippets or s[1] > semantic_snippets[cur_idx].metadata['heading_font']: metadata={'heading':s[0], 'content_font': 0, 'heading_font': s[1]} metadata.update(data.metadata) semantic_snippets.append(Document(page_content='',metadata=metadata)) cur_idx += 1 continue # if current snippet's font size <= previous section's content => content belongs to the same section (one can also create # a tree like structure for sub sections if needed but that may require some more thinking and may be data specific) if not semantic_snippets[cur_idx].metadata['content_font'] or s[1] <= semantic_snippets[cur_idx].metadata['content_font']: semantic_snippets[cur_idx].page_content += s[0] semantic_snippets[cur_idx].metadata['content_font'] = max(s[1], semantic_snippets[cur_idx].metadata['content_font']) continue # if current snippet's font size > previous section's content but less than previous section's heading than also make a new # section (e.g. title of a PDF will have the highest font size but we don't want it to subsume all sections) metadata={'heading':s[0], 'content_font': 0, 'heading_font': s[1]} metadata.update(data.metadata) semantic_snippets.append(Document(page_content='',metadata=metadata)) cur_idx += 1semantic_snippets[4] Document(page_content='Recently, various DL models and datasets have been developed for layout analysis\ntasks. The dhSegment [22] utilizes fully convolutional networks [20] for segmen-\ntation tasks on historical documents. Object detection-based methods like Faster\nR-CNN [28] and Mask R-CNN [12] are used for identifying document elements [38]\nand detecting tables [30, 26]. Most recently, Graph Neural Networks [29] have also\nbeen used in table detection [27]. However, these models are usually implemented\nindividually and there is no unified framework to load and use such models.\nThere has been a surge of interest in creating open-source tools for document\nimage processing: a search of document image analysis in Github leads to 5M\nrelevant code pieces 6; yet most of them rely on traditional rule-based methods\nor provide limited functionalities. The closest prior research to our work is the\nOCR-D project7, which also tries to build a complete toolkit for DIA. However,\nsimilar to the platform developed by Neudecker et al. [21], it is designed for\nanalyzing historical documents, and provides no supports for recent DL models.\nThe DocumentLayoutAnalysis project8 focuses on processing born-digital PDF\ndocuments via analyzing the stored PDF data. Repositories like DeepLayout9\nand Detectron2-PubLayNet10 are individual deep learning models trained on\nlayout analysis datasets without support for the full DIA pipeline. The Document\nAnalysis and Exploitation (DAE) platform [15] and the DeepDIVA project [2]\naim to improve the reproducibility of DIA methods (or DL models), yet they\nare not actively maintained. OCR engines like Tesseract [14], easyOCR11 and\npaddleOCR12 usually do not come with comprehensive functionalities for other\nDIA tasks like layout analysis.\nRecent years have also seen numerous efforts to create libraries for promoting\nreproducibility and reusability in the field of DL. Libraries like Dectectron2 [35],\n6 The number shown is obtained by specifying the search type as ‘code’.\n7 https://ocr-d.de/en/about\n8 https://github.com/BobLd/DocumentLayoutAnalysis\n9 https://github.com/leonlulu/DeepLayout\n10 https://github.com/hpanwar08/detectron2\n11 https://github.com/JaidedAI/EasyOCR\n12 https://github.com/PaddlePaddle/PaddleOCR\n4\nZ. Shen et al.\nFig. 1: The overall architecture of LayoutParser. For an input document image,\nthe core LayoutParser library provides a set of off-the-shelf tools for layout\ndetection, OCR, visualization, and storage, backed by a carefully designed layout\ndata structure. LayoutParser also supports high level customization via efficient\nlayout annotation and model training functions. These improve model accuracy\non the target samples. The community platform enables the easy sharing of DIA\nmodels and whole digitization pipelines to promote reusability and reproducibility.\nA collection of detailed documentation, tutorials and exemplar projects make\nLayoutParser easy to learn and use.\nAllenNLP [8] and transformers [34] have provided the community with complete\nDL-based support for developing and deploying models for general computer\nvision and natural language processing problems. LayoutParser, on the other\nhand, specializes specifically in DIA tasks. LayoutParser is also equipped with a\ncommunity platform inspired by established model hubs such as Torch Hub [23]\nand TensorFlow Hub [1]. It enables the sharing of pretrained models as well as\nfull document processing pipelines that are unique to DIA tasks.\nThere have been a variety of document data collections to facilitate the\ndevelopment of DL models. Some examples include PRImA [3](magazine layouts),\nPubLayNet [38](academic paper layouts), Table Bank [18](tables in academic\npapers), Newspaper Navigator Dataset [16, 17](newspaper figure layouts) and\nHJDataset [31](historical Japanese docume" t layouts). A spectrum of models\ntrained on these datasets are currently available in the LayoutParser model zoo\nto support different use cases.\n', metadata={'heading': '2 Related Work\n', 'content_font': 9 50,https://python.langchain.com/docs/modules/data_connection/document_transformers/,"ModulesRetrievalDocument transformersOn this pageDocument transformersinfoHead to Integrations for documentation on built-in document transformer integrations with 3rd-party tools.Once you've loaded documents, you'll often want to transform them to better suit your application. The simplest example is you may want to split a long document into smaller chunks that can fit into your model's context window. LangChain has a number of built-in document transformers that make it easy to split, combine, filter, and otherwise manipulate documents.Text splitters​When you want to deal with long pieces of text, it is necessary to split up that text into chunks. As simple as this sounds, there is a lot of potential complexity here. Ideally, you want to keep the semantically related pieces of text together. What ""semantically related"" means could depend on the type of text. This notebook showcases several ways to do that.At a high level, text splitters work as following:Split the text up into small, semantically meaningful chunks (often sentences).Start combining these small chunks into a larger chunk until you reach a certain size (as measured by some function).Once you reach that size, make that chunk its own piece of text and then start creating a new chunk of text with some overlap (to keep context between chunks).That means there are two different axes along which you can customize your text splitter:How the text is splitHow the chunk size is measuredGet started with text splitters​The default recommended text splitter is the RecursiveCharacterTextSplitter. This text splitter takes a list of characters. It tries to create chunks based on splitting on the first character, but if any chunks are too large it then moves onto the next character, and so forth. By default the characters it tries to split on are [""\n\n"", ""\n"", "" "", """"]In addition to controlling which characters you can split on, you can also control a few other things:length_function: how the length of chunks is calculated. Defaults to just counting number of characters, but it's pretty common to pass a token counter here.chunk_size: the maximum size of your chunks (as measured by the length function).chunk_overlap: the maximum overlap between chunks. It can be nice to have some overlap to maintain some continuity between chunks (e.g. do a sliding window).add_start_index: whether to include the starting position of each chunk within the original document in the metadata.# This is a long document we can split up.with open('../../state_of_the_union.txt') as f: state_of_the_union = f.read()from langchain.text_splitter import RecursiveCharacterTextSplittertext_splitter = RecursiveCharacterTextSplitter( # Set a really small chunk size, just to show. chunk_size = 100, chunk_overlap = 20, length_function = len, add_start_index = True,)texts = text_splitter.create_documents([state_of_the_union])print(texts[0])print(texts[1]) page_content='Madam Speaker, Madam Vice President, our First Lady and Second Gentleman. Members of Congress and' metadata={'start_index': 0} page_content='of Congress and the Cabinet. Justices of the Supreme Court. My fellow Americans.' metadata={'start_index': 82}Other transformations:​Filter redundant docs, translate docs, extract metadata, and more​We can do perform a number of transformations on docs which are not simply splitting the text. With the EmbeddingsRedundantFilter we can identify similar documents and filter out redundancies. With integrations like doctran we can do things like translate documents from one language to another, extract desired properties and add them to metadata, and convert conversational dialogue into a Q/A format set of documents.PreviousPDFNextHTMLHeaderTextSplitterText splittersGet started with text splitters" 51,https://python.langchain.com/docs/modules/data_connection/text_embedding/,"ModulesRetrievalText embedding modelsOn this pageText embedding modelsinfoHead to Integrations for documentation on built-in integrations with text embedding model providers.The Embeddings class is a class designed for interfacing with text embedding models. There are lots of embedding model providers (OpenAI, Cohere, Hugging Face, etc) - this class is designed to provide a standard interface for all of them.Embeddings create a vector representation of a piece of text. This is useful because it means we can think about text in the vector space, and do things like semantic search where we look for pieces of text that are most similar in the vector space.The base Embeddings class in LangChain provides two methods: one for embedding documents and one for embedding a query. The former takes as input multiple texts, while the latter takes a single text. The reason for having these as two separate methods is that some embedding providers have different embedding methods for documents (to be searched over) vs queries (the search query itself).Get started​Setup​To start we'll need to install the OpenAI Python package:pip install openaiAccessing the API requires an API key, which you can get by creating an account and heading here. Once we have a key we'll want to set it as an environment variable by running:export OPENAI_API_KEY=""...""If you'd prefer not to set an environment variable you can pass the key in directly via the openai_api_key named parameter when initiating the OpenAI LLM class:from langchain.embeddings import OpenAIEmbeddingsembeddings_model = OpenAIEmbeddings(openai_api_key=""..."")Otherwise you can initialize without any params:from langchain.embeddings import OpenAIEmbeddingsembeddings_model = OpenAIEmbeddings()embed_documents​Embed list of texts​embeddings = embeddings_model.embed_documents( [ ""Hi there!"", ""Oh, hello!"", ""What's your name?"", ""My friends call me World"", ""Hello World!"" ])len(embeddings), len(embeddings[0])(5, 1536)embed_query​Embed single query​Embed a single piece of text for the purpose of comparing to other embedded pieces of texts.embedded_query = embeddings_model.embed_query(""What was the name mentioned in the conversation?"")embedded_query[:5][0.0053587136790156364, -0.0004999046213924885, 0.038883671164512634, -0.003001077566295862, -0.00900818221271038]PreviousLost in the middle: The problem with long contextsNextCachingGet started" 52,https://python.langchain.com/docs/modules/data_connection/vectorstores/,"ModulesRetrievalVector storesOn this pageVector storesinfoHead to Integrations for documentation on built-in integrations with 3rd-party vector stores.One of the most common ways to store and search over unstructured data is to embed it and store the resulting embedding vectors, and then at query time to embed the unstructured query and retrieve the embedding vectors that are 'most similar' to the embedded query. A vector store takes care of storing embedded data and performing vector search for you.Get started​This walkthrough showcases basic functionality related to vector stores. A key part of working with vector stores is creating the vector to put in them, which is usually created via embeddings. Therefore, it is recommended that you familiarize yourself with the text embedding model interfaces before diving into this.There are many great vector store options, here are a few that are free, open-source, and run entirely on your local machine. Review all integrations for many great hosted offerings.ChromaFAISSLanceThis walkthrough uses the chroma vector database, which runs on your local machine as a library.pip install chromadbWe want to use OpenAIEmbeddings so we have to get the OpenAI API Key.import osimport getpassos.environ['OPENAI_API_KEY'] = getpass.getpass('OpenAI API Key:')from langchain.document_loaders import TextLoaderfrom langchain.embeddings.openai import OpenAIEmbeddingsfrom langchain.text_splitter import CharacterTextSplitterfrom langchain.vectorstores import Chroma# Load the document, split it into chunks, embed each chunk and load it into the vector store.raw_documents = TextLoader('../../../state_of_the_union.txt').load()text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)documents = text_splitter.split_documents(raw_documents)db = Chroma.from_documents(documents, OpenAIEmbeddings())This walkthrough uses the FAISS vector database, which makes use of the Facebook AI Similarity Search (FAISS) library.pip install faiss-cpuWe want to use OpenAIEmbeddings so we have to get the OpenAI API Key.import osimport getpassos.environ['OPENAI_API_KEY'] = getpass.getpass('OpenAI API Key:')from langchain.document_loaders import TextLoaderfrom langchain.embeddings.openai import OpenAIEmbeddingsfrom langchain.text_splitter import CharacterTextSplitterfrom langchain.vectorstores import FAISS# Load the document, split it into chunks, embed each chunk and load it into the vector store.raw_documents = TextLoader('../../../state_of_the_union.txt').load()text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)documents = text_splitter.split_documents(raw_documents)db = FAISS.from_documents(documents, OpenAIEmbeddings())This notebook shows how to use functionality related to the LanceDB vector database based on the Lance data format.pip install lancedbWe want to use OpenAIEmbeddings so we have to get the OpenAI API Key.import osimport getpassos.environ['OPENAI_API_KEY'] = getpass.getpass('OpenAI API Key:')from langchain.document_loaders import TextLoaderfrom langchain.embeddings.openai import OpenAIEmbeddingsfrom langchain.text_splitter import CharacterTextSplitterfrom langchain.vectorstores import LanceDBimport lancedbdb = lancedb.connect(""/tmp/lancedb"")table = db.create_table( ""my_table"", data=[ { ""vector"": embeddings.embed_query(""Hello World""), ""text"": ""Hello World"", ""id"": ""1"", } ], mode=""overwrite"",)# Load the document, split it into chunks, embed each chunk and load it into the vector store.raw_documents = TextLoader('../../../state_of_the_union.txt').load()text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)documents = text_splitter.split_documents(raw_documents)db = LanceDB.from_documents(documents, OpenAIEmbeddings(), connection=table)Similarity search​query = ""What did the president say about Ketanji Brown Jackson""docs = db.similarity_search(query)print(docs[0].page_content) Tonight. I call on the Senate to: Pass the Freedom to Vote Act. Pass the John Lewis Voting Rights Act. And while you’re at it, pass the Disclose Act so Americans can know who is funding our elections. Tonight, I’d like to honor someone who has dedicated his life to serve this country: Justice Stephen Breyer—an Army veteran, Constitutional scholar, and retiring Justice of the United States Supreme Court. Justice Breyer, thank you for your service. One of the most serious constitutional responsibilities a President has is nominating someone to serve on the United States Supreme Court. And I did that 4 days ago, when I nominated Circuit Court of Appeals Judge Ketanji Brown Jackson. One of our nation’s top legal minds, who will continue Justice Breyer’s legacy of excellence.Similarity search by vector​It is also possible to do a search for documents similar to a given embedding vector using similarity_search_by_vector which accepts an embedding vector as a parameter instead of a string.embedding_vector = OpenAIEmbeddings().embed_query(query)docs = db.similarity_search_by_vector(embedding_vector)print(docs[0].page_content)The query is the same, and so the result is also the same. Tonight. I call on the Senate to: Pass the Freedom to Vote Act. Pass the John Lewis Voting Rights Act. And while you’re at it, pass the Disclose Act so Americans can know who is funding our elections. Tonight, I’d like to honor someone who has dedicated his life to serve this country: Justice Stephen Breyer—an Army veteran, Constitutional scholar, and retiring Justice of the United States Supreme Court. Justice Breyer, thank you for your service. One of the most serious constitutional responsibilities a President has is nominating someone to serve on the United States Supreme Court. And I did that 4 days ago, when I nominated Circuit Court of Appeals Judge Ketanji Brown Jackson. One of our nation’s top legal minds, who will continue Justice Breyer’s legacy of excellence.Asynchronous operations​Vector stores are usually run as a separate service that requires some IO operations, and therefore they might be called asynchronously. That gives performance benefits as you don't waste time waiting for responses from external services. That might also be important if you work with an asynchronous framework, such as FastAPI.LangChain supports async operation on vector stores. All the methods might be called using their async counterparts, with the prefix a, meaning async.Qdrant is a vector store, which supports all the async operations, thus it will be used in this walkthrough.pip install qdrant-clientfrom langchain.vectorstores import QdrantCreate a vector store asynchronously​db = await Qdrant.afrom_documents(documents, embeddings, ""http://localhost:6333"")Similarity search​query = ""What did the president say about Ketanji Brown Jackson""docs = await db.asimilarity_search(query)print(docs[0].page_content) Tonight. I call on the Senate to: Pass the Freedom to Vote Act. Pass the John Lewis Voting Rights Act. And while you’re at it, pass the Disclose Act so Americans can know who is funding our elections. Tonight, I’d like to honor someone who has dedicated his life to serve this country: Justice Stephen Breyer—an Army veteran, Constitutional scholar, and retiring Justice of the United States Supreme Court. Justice Breyer, thank you for your service. One of the most serious constitutional responsibilities a President has is nominating someone to serve on the United States Supreme Court. And I did that 4 days ago, when I nominated Circuit Court of Appeals Judge Ketanji Brown Jackson. One of our nation’s top legal minds, who will continue Justice Breyer’s legacy of excellence.Similarity search by vector​embedding_vector = embeddings.embed_query(query)docs = await db.asimilarity_search_by_vector(embedding_vector)Maximum marginal relevance search (MMR)​Maximal marginal relevance optimizes for similarity to query and diversity among selected documents. It is also supported in async API.query = ""What did the president say about Ketanji Brown Jackson""found_docs = await qdrant.amax_marginal_relevance_search(query, k=2, fetch_k=10)for i, doc in enumerate(found_docs): print(f""{i + 1}."", doc.page_content, ""\n"")1. Tonight. I call on the Senate to: Pass the Freedom to Vote Act. Pass the John Lewis Voting Rights Act. And while you’re at it, pass the Disclose Act so Americans can know who is funding our elections.Tonight, I’d like to honor someone who has dedicated his life to serve this country: Justice Stephen Breyer—an Army veteran, Constitutional scholar, and retiring Justice of the United States Supreme Court. Justice Breyer, thank you for your service.One of the most serious constitutional responsibilities a President has is nominating someone to serve on the United States Supreme Court.And I did that 4 days ago, when I nominated Circuit Court of Appeals Judge Ketanji Brown Jackson. One of our nation’s top legal minds, who will continue Justice Breyer’s legacy of excellence.2. We can’t change how divided we’ve been. But we can change how we move forward—on COVID-19 and other issues we must face together.I recently visited the New York City Police Department days after the funerals of Officer Wilbert Mora and his partner, Officer Jason Rivera.They were responding to a 9-1-1 call when a man shot and killed them with a stolen gun.Officer Mora was 27 years old.Officer Rivera was 22.Both Dominican Americans who’d grown up on the same streets they later chose to patrol as police officers.I spoke with their families and told them that we are forever in debt for their sacrifice, and we will carry on their mission to restore the trust and safety every community deserves.I’ve worked on these issues a long time.I know what works: Investing in crime preventionand community police officers who’ll walk the beat, who’ll know the neighborhood, and who can restore trust and safety.PreviousCachingNextRetrieversGet startedAsynchronous operations" 53,https://python.langchain.com/docs/modules/data_connection/retrievers/,"ModulesRetrievalRetrieversOn this pageRetrieversinfoHead to Integrations for documentation on built-in retriever integrations with 3rd-party tools.A retriever is an interface that returns documents given an unstructured query. It is more general than a vector store. A retriever does not need to be able to store documents, only to return (or retrieve) them. Vector stores can be used as the backbone of a retriever, but there are other types of retrievers as well.Get started​The public API of the BaseRetriever class in LangChain is as follows:from abc import ABC, abstractmethodfrom typing import Any, Listfrom langchain.schema import Documentfrom langchain.callbacks.manager import Callbacksclass BaseRetriever(ABC): ... def get_relevant_documents( self, query: str, *, callbacks: Callbacks = None, **kwargs: Any ) -> List[Document]: """"""Retrieve documents relevant to a query. Args: query: string to find relevant documents for callbacks: Callback manager or list of callbacks Returns: List of relevant documents """""" ... async def aget_relevant_documents( self, query: str, *, callbacks: Callbacks = None, **kwargs: Any ) -> List[Document]: """"""Asynchronously get documents relevant to a query. Args: query: string to find relevant documents for callbacks: Callback manager or list of callbacks Returns: List of relevant documents """""" ...It's that simple! You can call get_relevant_documents or the async aget_relevant_documents methods to retrieve documents relevant to a query, where ""relevance"" is defined by the specific retriever object you are calling.Of course, we also help construct what we think useful retrievers are. The main type of retriever that we focus on is a vector store retriever. We will focus on that for the rest of this guide.In order to understand what a vector store retriever is, it's important to understand what a vector store is. So let's look at that.By default, LangChain uses Chroma as the vector store to index and search embeddings. To walk through this tutorial, we'll first need to install chromadb.pip install chromadbThis example showcases question answering over documents. We have chosen this as the example for getting started because it nicely combines a lot of different elements (Text splitters, embeddings, vector stores) and then also shows how to use them in a chain.Question answering over documents consists of four steps:Create an indexCreate a retriever from that indexCreate a question answering chainAsk questions!Each of the steps has multiple substeps and potential configurations. In this notebook we will primarily focus on (1). We will start by showing the one-liner for doing so, but then break down what is actually going on.First, let's import some common classes we'll use no matter what.from langchain.chains import RetrievalQAfrom langchain.llms import OpenAINext in the generic setup, let's specify the document loader we want to use. You can download the state_of_the_union.txt file here.from langchain.document_loaders import TextLoaderloader = TextLoader('../state_of_the_union.txt', encoding='utf8')One Line Index Creation​To get started as quickly as possible, we can use the VectorstoreIndexCreator.from langchain.indexes import VectorstoreIndexCreatorindex = VectorstoreIndexCreator().from_loaders([loader]) Running Chroma using direct local API. Using DuckDB in-memory for database. Data will be transient.Now that the index is created, we can use it to ask questions of the data! Note that under the hood this is actually doing a few steps as well, which we will cover later in this guide.query = ""What did the president say about Ketanji Brown Jackson?""index.query(query) "" The president said that Ketanji Brown Jackson is one of the nation's top legal minds, a former top litigator in private practice, a former federal public defender, and from a family of public school educators and police officers. He also said that she is a consensus builder and has received a broad range of support from the Fraternal Order of Police to former judges appointed by Democrats and Republicans.""query = ""What did the president say about Ketanji Brown Jackson?""index.query_with_sources(query) {'question': 'What did the president say about Ketanji Brown Jackson?', 'answer': "" The president said that he nominated Circuit Court of Appeals Judge Ketanji Brown Jackson, one of the nation's top legal minds, to continue Justice Breyer's legacy of excellence, and that she has received a broad range of support from the Fraternal Order of Police to former judges appointed by Democrats and Republicans.\n"", 'sources': '../state_of_the_union.txt'}What is returned from the VectorstoreIndexCreator is a VectorStoreIndexWrapper, which provides these nice query and query_with_sources functionalities. If we just want to access the vector store directly, we can also do that.index.vectorstore If we then want to access the VectorStoreRetriever, we can do that with:index.vectorstore.as_retriever() VectorStoreRetriever(vectorstore=, search_kwargs={})It can also be convenient to filter the vector store by the metadata associated with documents, particularly when your vector store has multiple sources. This can be done using the query method, like this:index.query(""Summarize the general content of this document."", retriever_kwargs={""search_kwargs"": {""filter"": {""source"": ""../state_of_the_union.txt""}}}) "" The document is a speech given by President Trump to the nation on the occasion of his 245th birthday. The speech highlights the importance of American values and the challenges facing the country, including the ongoing conflict in Ukraine, the ongoing trade war with China, and the ongoing conflict in Syria. The speech also discusses the importance of investing in emerging technologies and American manufacturing, and calls on Congress to pass the Bipartisan Innovation Act and other important legislation.""Walkthrough​Okay, so what's actually going on? How is this index getting created?A lot of the magic is being hid in this VectorstoreIndexCreator. What is this doing?There are three main steps going on after the documents are loaded:Splitting documents into chunksCreating embeddings for each documentStoring documents and embeddings in a vector storeLet's walk through this in codedocuments = loader.load()Next, we will split the documents into chunks.from langchain.text_splitter import CharacterTextSplittertext_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)texts = text_splitter.split_documents(documents)We will then select which embeddings we want to use.from langchain.embeddings import OpenAIEmbeddingsembeddings = OpenAIEmbeddings()We now create the vector store to use as the index.from langchain.vectorstores import Chromadb = Chroma.from_documents(texts, embeddings) Running Chroma using direct local API. Using DuckDB in-memory for database. Data will be transient.So that's creating the index. Then, we expose this index in a retriever interface.retriever = db.as_retriever()Then, as before, we create a chain and use it to answer questions!qa = RetrievalQA.from_chain_type(llm=OpenAI(), chain_type=""stuff"", retriever=retriever)query = ""What did the president say about Ketanji Brown Jackson?""qa.run(query) "" The President said that Judge Ketanji Brown Jackson is one of the nation's top legal minds, a former top litigator in private practice, a former federal public defender, and from a family of public school educators and police officers. He said she is a consensus builder and has received a broad range of support from organizations such as the Fraternal Order of Police and former judges appointed by Democrats and Republicans.""VectorstoreIndexCreator is just a wrapper around all this logic. It is configurable in the text splitter it uses, the embeddings it uses, and the vectorstore it uses. For example, you can configure it as below:index_creator = VectorstoreIndexCreator( vectorstore_cls=Chroma, embedding=OpenAIEmbeddings(), text_splitter=CharacterTextSplitter(chunk_size=1000, chunk_overlap=0))Hopefully this highlights what is going on under the hood of VectorstoreIndexCreator. While we think it's important to have a simple way to create indexes, we also think it's important to understand what's going on under the hood.PreviousVector storesNextMultiQueryRetrieverGet started" 54,https://python.langchain.com/docs/modules/data_connection/indexing,"ModulesRetrievalIndexingOn this pageIndexingHere, we will look at a basic indexing workflow using the LangChain indexing API. The indexing API lets you load and keep in sync documents from any source into a vector store. Specifically, it helps:Avoid writing duplicated content into the vector storeAvoid re-writing unchanged contentAvoid re-computing embeddings over unchanged contentAll of which should save you time and money, as well as improve your vector search results.Crucially, the indexing API will work even with documents that have gone through several transformation steps (e.g., via text chunking) with respect to the original source documents.How it works​LangChain indexing makes use of a record manager (RecordManager) that keeps track of document writes into the vector store.When indexing content, hashes are computed for each document, and the following information is stored in the record manager: the document hash (hash of both page content and metadata)write timethe source id -- each document should include information in its metadata to allow us to determine the ultimate source of this documentDeletion modes​When indexing documents into a vector store, it's possible that some existing documents in the vector store should be deleted. In certain situations you may want to remove any existing documents that are derived from the same sources as the new documents being indexed. In others you may want to delete all existing documents wholesale. The indexing API deletion modes let you pick the behavior you want:Cleanup ModeDe-Duplicates ContentParallelizableCleans Up Deleted Source DocsCleans Up Mutations of Source Docs and/or Derived DocsClean Up TimingNone✅✅❌❌-Incremental✅✅❌✅ContinuouslyFull✅❌✅✅At end of indexingNone does not do any automatic clean up, allowing the user to manually do clean up of old content. incremental and full offer the following automated clean up:If the content of the source document or derived documents has changed, both incremental or full modes will clean up (delete) previous versions of the content.If the source document has been deleted (meaning it is not included in the documents currently being indexed), the full cleanup mode will delete it from the vector store correctly, but the incremental mode will not.When content is mutated (e.g., the source PDF file was revised) there will be a period of time during indexing when both the new and old versions may be returned to the user. This happens after the new content was written, but before the old version was deleted.incremental indexing minimizes this period of time as it is able to do clean up continuously, as it writes.full mode does the clean up after all batches have been written.Requirements​Do not use with a store that has been pre-populated with content independently of the indexing API, as the record manager will not know that records have been inserted previously.Only works with LangChain vectorstore's that support:document addition by id (add_documents method with ids argument)delete by id (delete method with)Caution​The record manager relies on a time-based mechanism to determine what content can be cleaned up (when using full or incremental cleanup modes).If two tasks run back-to-back, and the first task finishes before the the clock time changes, then the second task may not be able to clean up content.This is unlikely to be an issue in actual settings for the following reasons:The RecordManager uses higher resolution timestamps.The data would need to change between the first and the second tasks runs, which becomes unlikely if the time interval between the tasks is small.Indexing tasks typically take more than a few ms.Quickstart​from langchain.embeddings import OpenAIEmbeddingsfrom langchain.indexes import SQLRecordManager, indexfrom langchain.schema import Documentfrom langchain.vectorstores import ElasticsearchStoreInitialize a vector store and set up the embeddings:collection_name = ""test_index""embedding = OpenAIEmbeddings()vectorstore = ElasticsearchStore( es_url=""http://localhost:9200"", index_name=""test_index"", embedding=embedding)Initialize a record manager with an appropriate namespace.Suggestion: Use a namespace that takes into account both the vector store and the collection name in the vector store; e.g., 'redis/my_docs', 'chromadb/my_docs' or 'postgres/my_docs'.namespace = f""elasticsearch/{collection_name}""record_manager = SQLRecordManager( namespace, db_url=""sqlite:///record_manager_cache.sql"")Create a schema before using the record manager.record_manager.create_schema()Let's index some test documents:doc1 = Document(page_content=""kitty"", metadata={""source"": ""kitty.txt""})doc2 = Document(page_content=""doggy"", metadata={""source"": ""doggy.txt""})Indexing into an empty vector store:def _clear(): """"""Hacky helper method to clear content. See the `full` mode section to to understand why it works."""""" index([], record_manager, vectorstore, cleanup=""full"", source_id_key=""source"")None deletion mode​This mode does not do automatic clean up of old versions of content; however, it still takes care of content de-duplication._clear()index( [doc1, doc1, doc1, doc1, doc1], record_manager, vectorstore, cleanup=None, source_id_key=""source"",) {'num_added': 1, 'num_updated': 0, 'num_skipped': 0, 'num_deleted': 0}_clear()index( [doc1, doc2], record_manager, vectorstore, cleanup=None, source_id_key=""source"") {'num_added': 2, 'num_updated': 0, 'num_skipped': 0, 'num_deleted': 0}Second time around all content will be skipped:index( [doc1, doc2], record_manager, vectorstore, cleanup=None, source_id_key=""source"") {'num_added': 0, 'num_updated': 0, 'num_skipped': 2, 'num_deleted': 0}""incremental"" deletion mode​_clear()index( [doc1, doc2], record_manager, vectorstore, cleanup=""incremental"", source_id_key=""source"",) {'num_added': 2, 'num_updated': 0, 'num_skipped': 0, 'num_deleted': 0}Indexing again should result in both documents getting skipped -- also skipping the embedding operation!index( [doc1, doc2], record_manager, vectorstore, cleanup=""incremental"", source_id_key=""source"",) {'num_added': 0, 'num_updated': 0, 'num_skipped': 2, 'num_deleted': 0}If we provide no documents with incremental indexing mode, nothing will change.index( [], record_manager, vectorstore, cleanup=""incremental"", source_id_key=""source"") {'num_added': 0, 'num_updated': 0, 'num_skipped': 0, 'num_deleted': 0}If we mutate a document, the new version will be written and all old versions sharing the same source will be deleted.changed_doc_2 = Document(page_content=""puppy"", metadata={""source"": ""doggy.txt""})index( [changed_doc_2], record_manager, vectorstore, cleanup=""incremental"", source_id_key=""source"",) {'num_added': 1, 'num_updated': 0, 'num_skipped': 0, 'num_deleted': 1}""full"" deletion mode​In full mode the user should pass the full universe of content that should be indexed into the indexing function.Any documents that are not passed into the indexing function and are present in the vectorstore will be deleted!This behavior is useful to handle deletions of source documents._clear()all_docs = [doc1, doc2]index(all_docs, record_manager, vectorstore, cleanup=""full"", source_id_key=""source"") {'num_added': 2, 'num_updated': 0, 'num_skipped': 0, 'num_deleted': 0}Say someone deleted the first doc:del all_docs[0]all_docs [Document(page_content='doggy', metadata={'source': 'doggy.txt'})]Using full mode will clean up the deleted content as well.index(all_docs, record_manager, vectorstore, cleanup=""full"", source_id_key=""source"") {'num_added': 0, 'num_updated': 0, 'num_skipped': 1, 'num_deleted': 1}Source​The metadata attribute contains a field called source. This source should be pointing at the ultimate provenance associated with the given document.For example, if these documents are representing chunks of some parent document, the source for both documents should be the same and reference the parent document.In general, source should always be specified. Only use a None, if you never intend to use incremental mode, and for some reason can't specify the source field correctly.from langchain.text_splitter import CharacterTextSplitterdoc1 = Document( page_content=""kitty kitty kitty kitty kitty"", metadata={""source"": ""kitty.txt""})doc2 = Document(page_content=""doggy doggy the doggy"", metadata={""source"": ""doggy.txt""})new_docs = CharacterTextSplitter( separator=""t"", keep_separator=True, chunk_size=12, chunk_overlap=2).split_documents([doc1, doc2])new_docs [Document(page_content='kitty kit', metadata={'source': 'kitty.txt'}), Document(page_content='tty kitty ki', metadata={'source': 'kitty.txt'}), Document(page_content='tty kitty', metadata={'source': 'kitty.txt'}), Document(page_content='doggy doggy', metadata={'source': 'doggy.txt'}), Document(page_content='the doggy', metadata={'source': 'doggy.txt'})]_clear()index( new_docs, record_manager, vectorstore, cleanup=""incremental"", source_id_key=""source"",) {'num_added': 5, 'num_updated': 0, 'num_skipped': 0, 'num_deleted': 0}changed_doggy_docs = [ Document(page_content=""woof woof"", metadata={""source"": ""doggy.txt""}), Document(page_content=""woof woof woof"", metadata={""source"": ""doggy.txt""}),]This should delete the old versions of documents associated with doggy.txt source and replace them with the new versions.index( changed_doggy_docs, record_manager, vectorstore, cleanup=""incremental"", source_id_key=""source"",) {'num_added': 0, 'num_updated': 0, 'num_skipped': 2, 'num_deleted': 2}vectorstore.similarity_search(""dog"", k=30) [Document(page_content='tty kitty', metadata={'source': 'kitty.txt'}), Document(page_content='tty kitty ki', metadata={'source': 'kitty.txt'}), Document(page_content='kitty kit', metadata={'source': 'kitty.txt'})]Using with loaders​Indexing can accept either an iterable of documents or else any loader.Attention: The loader must set source keys correctly.from langchain.document_loaders.base import BaseLoaderclass MyCustomLoader(BaseLoader): def lazy_load(self): text_splitter = CharacterTextSplitter( separator=""t"", keep_separator=True, chunk_size=12, chunk_overlap=2 ) docs = [ Document(page_content=""woof woof"", metadata={""source"": ""doggy.txt""}), Document(page_content=""woof woof woof"", metadata={""source"": ""doggy.txt""}), ] yield from text_splitter.split_documents(docs) def load(self): return list(self.lazy_load())_clear()loader = MyCustomLoader()loader.load() [Document(page_content='woof woof', metadata={'source': 'doggy.txt'}), Document(page_content='woof woof woof', metadata={'source': 'doggy.txt'})]index(loader, record_manager, vectorstore, cleanup=""full"", source_id_key=""source"") {'num_added': 2, 'num_updated': 0, 'num_skipped': 0, 'num_deleted': 0}vectorstore.similarity_search(""dog"", k=30) [Document(page_content='woof woof', metadata={'source': 'doggy.txt'}), Document(page_content='woof woof woof', metadata={'source': 'doggy.txt'})]PreviousWebResearchRetrieverNextChainsHow it worksDeletion modesRequirementsCautionQuickstartNone deletion mode""incremental"" deletion mode""full"" deletion modeSourceUsing with loaders" 55,https://python.langchain.com/docs/modules/chains/,"ModulesChainsOn this pageChainsUsing an LLM in isolation is fine for simple applications, but more complex applications require chaining LLMs - either with each other or with other components.LangChain provides the Chain interface for such ""chained"" applications. We define a Chain very generically as a sequence of calls to components, which can include other chains. The base interface is simple:class Chain(BaseModel, ABC): """"""Base interface that all chains should implement."""""" memory: BaseMemory callbacks: Callbacks def __call__( self, inputs: Any, return_only_outputs: bool = False, callbacks: Callbacks = None, ) -> Dict[str, Any]: ...This idea of composing components together in a chain is simple but powerful. It drastically simplifies and makes more modular the implementation of complex applications, which in turn makes it much easier to debug, maintain, and improve your applications.For more specifics check out:How-to for walkthroughs of different chain featuresFoundational to get acquainted with core building block chainsDocument to learn how to incorporate documents into chainsWhy do we need chains?​Chains allow us to combine multiple components together to create a single, coherent application. For example, we can create a chain that takes user input, formats it with a PromptTemplate, and then passes the formatted response to an LLM. We can build more complex chains by combining multiple chains together, or by combining chains with other components.Get started​Using LLMChain​The LLMChain is most basic building block chain. It takes in a prompt template, formats it with the user input and returns the response from an LLM.To use the LLMChain, first create a prompt template.from langchain.llms import OpenAIfrom langchain.prompts import PromptTemplatellm = OpenAI(temperature=0.9)prompt = PromptTemplate( input_variables=[""product""], template=""What is a good name for a company that makes {product}?"",)We can now create a very simple chain that will take user input, format the prompt with it, and then send it to the LLM.from langchain.chains import LLMChainchain = LLMChain(llm=llm, prompt=prompt)# Run the chain only specifying the input variable.print(chain.run(""colorful socks"")) Colorful Toes Co.If there are multiple variables, you can input them all at once using a dictionary.prompt = PromptTemplate( input_variables=[""company"", ""product""], template=""What is a good name for {company} that makes {product}?"",)chain = LLMChain(llm=llm, prompt=prompt)print(chain.run({ 'company': ""ABC Startup"", 'product': ""colorful socks"" })) Socktopia Colourful Creations.You can use a chat model in an LLMChain as well:from langchain.chat_models import ChatOpenAIfrom langchain.prompts.chat import ( ChatPromptTemplate, HumanMessagePromptTemplate,)human_message_prompt = HumanMessagePromptTemplate( prompt=PromptTemplate( template=""What is a good name for a company that makes {product}?"", input_variables=[""product""], ) )chat_prompt_template = ChatPromptTemplate.from_messages([human_message_prompt])chat = ChatOpenAI(temperature=0.9)chain = LLMChain(llm=chat, prompt=chat_prompt_template)print(chain.run(""colorful socks"")) Rainbow Socks Co.PreviousIndexingNextHow toWhy do we need chains?Get started" 56,https://python.langchain.com/docs/modules/memory/,"ModulesMemoryOn this pageMemoryMost LLM applications have a conversational interface. An essential component of a conversation is being able to refer to information introduced earlier in the conversation. At bare minimum, a conversational system should be able to access some window of past messages directly. A more complex system will need to have a world model that it is constantly updating, which allows it to do things like maintain information about entities and their relationships.We call this ability to store information about past interactions ""memory"". LangChain provides a lot of utilities for adding memory to a system. These utilities can be used by themselves or incorporated seamlessly into a chain.A memory system needs to support two basic actions: reading and writing. Recall that every chain defines some core execution logic that expects certain inputs. Some of these inputs come directly from the user, but some of these inputs can come from memory. A chain will interact with its memory system twice in a given run.AFTER receiving the initial user inputs but BEFORE executing the core logic, a chain will READ from its memory system and augment the user inputs.AFTER executing the core logic but BEFORE returning the answer, a chain will WRITE the inputs and outputs of the current run to memory, so that they can be referred to in future runs.Building memory into a system​The two core design decisions in any memory system are:How state is storedHow state is queriedStoring: List of chat messages​Underlying any memory is a history of all chat interactions. Even if these are not all used directly, they need to be stored in some form. One of the key parts of the LangChain memory module is a series of integrations for storing these chat messages, from in-memory lists to persistent databases.Chat message storage: How to work with Chat Messages, and the various integrations offered.Querying: Data structures and algorithms on top of chat messages​Keeping a list of chat messages is fairly straight-forward. What is less straight-forward are the data structures and algorithms built on top of chat messages that serve a view of those messages that is most useful.A very simply memory system might just return the most recent messages each run. A slightly more complex memory system might return a succinct summary of the past K messages. An even more sophisticated system might extract entities from stored messages and only return information about entities referenced in the current run.Each application can have different requirements for how memory is queried. The memory module should make it easy to both get started with simple memory systems and write your own custom systems if needed.Memory types: The various data structures and algorithms that make up the memory types LangChain supportsGet started​Let's take a look at what Memory actually looks like in LangChain. Here we'll cover the basics of interacting with an arbitrary memory class.Let's take a look at how to use ConversationBufferMemory in chains. ConversationBufferMemory is an extremely simple form of memory that just keeps a list of chat messages in a buffer and passes those into the prompt template.from langchain.memory import ConversationBufferMemorymemory = ConversationBufferMemory()memory.chat_memory.add_user_message(""hi!"")memory.chat_memory.add_ai_message(""what's up?"")When using memory in a chain, there are a few key concepts to understand. Note that here we cover general concepts that are useful for most types of memory. Each individual memory type may very well have its own parameters and concepts that are necessary to understand.What variables get returned from memory​Before going into the chain, various variables are read from memory. These have specific names which need to align with the variables the chain expects. You can see what these variables are by calling memory.load_memory_variables({}). Note that the empty dictionary that we pass in is just a placeholder for real variables. If the memory type you are using is dependent upon the input variables, you may need to pass some in.memory.load_memory_variables({}) {'history': ""Human: hi!\nAI: what's up?""}In this case, you can see that load_memory_variables returns a single key, history. This means that your chain (and likely your prompt) should expect an input named history. You can usually control this variable through parameters on the memory class. For example, if you want the memory variables to be returned in the key chat_history you can do:memory = ConversationBufferMemory(memory_key=""chat_history"")memory.chat_memory.add_user_message(""hi!"")memory.chat_memory.add_ai_message(""what's up?"") {'chat_history': ""Human: hi!\nAI: what's up?""}The parameter name to control these keys may vary per memory type, but it's important to understand that (1) this is controllable, and (2) how to control it.Whether memory is a string or a list of messages​One of the most common types of memory involves returning a list of chat messages. These can either be returned as a single string, all concatenated together (useful when they will be passed into LLMs) or a list of ChatMessages (useful when passed into ChatModels).By default, they are returned as a single string. In order to return as a list of messages, you can set return_messages=Truememory = ConversationBufferMemory(return_messages=True)memory.chat_memory.add_user_message(""hi!"")memory.chat_memory.add_ai_message(""what's up?"") {'history': [HumanMessage(content='hi!', additional_kwargs={}, example=False), AIMessage(content='what's up?', additional_kwargs={}, example=False)]}What keys are saved to memory​Often times chains take in or return multiple input/output keys. In these cases, how can we know which keys we want to save to the chat message history? This is generally controllable by input_key and output_key parameters on the memory types. These default to None - and if there is only one input/output key it is known to just use that. However, if there are multiple input/output keys then you MUST specify the name of which one to use.End to end example​Finally, let's take a look at using this in a chain. We'll use an LLMChain, and show working with both an LLM and a ChatModel.Using an LLM​from langchain.llms import OpenAIfrom langchain.prompts import PromptTemplatefrom langchain.chains import LLMChainfrom langchain.memory import ConversationBufferMemoryllm = OpenAI(temperature=0)# Notice that ""chat_history"" is present in the prompt templatetemplate = """"""You are a nice chatbot having a conversation with a human.Previous conversation:{chat_history}New human question: {question}Response:""""""prompt = PromptTemplate.from_template(template)# Notice that we need to align the `memory_key`memory = ConversationBufferMemory(memory_key=""chat_history"")conversation = LLMChain( llm=llm, prompt=prompt, verbose=True, memory=memory)# Notice that we just pass in the `question` variables - `chat_history` gets populated by memoryconversation({""question"": ""hi""})Using a ChatModel​from langchain.chat_models import ChatOpenAIfrom langchain.prompts import ( ChatPromptTemplate, MessagesPlaceholder, SystemMessagePromptTemplate, HumanMessagePromptTemplate,)from langchain.chains import LLMChainfrom langchain.memory import ConversationBufferMemoryllm = ChatOpenAI()prompt = ChatPromptTemplate( messages=[ SystemMessagePromptTemplate.from_template( ""You are a nice chatbot having a conversation with a human."" ), # The `variable_name` here is what must align with memory MessagesPlaceholder(variable_name=""chat_history""), HumanMessagePromptTemplate.from_template(""{question}"") ])# Notice that we `return_messages=True` to fit into the MessagesPlaceholder# Notice that `""chat_history""` aligns with the MessagesPlaceholder name.memory = ConversationBufferMemory(memory_key=""chat_history"", return_messages=True)conversation = LLMChain( llm=llm, prompt=prompt, verbose=True, memory=memory)# Notice that we just pass in the `question` variables - `chat_history` gets populated by memoryconversation({""question"": ""hi""})Next steps​And that's it for getting started! Please see the other sections for walkthroughs of more advanced topics, like custom memory, multiple memories, and more.PreviousMap re-rankNextChat MessagesBuilding memory into a systemStoring: List of chat messagesQuerying: Data structures and algorithms on top of chat messagesGet startedNext steps" 57,https://python.langchain.com/docs/modules/agents/,"ModulesAgentsOn this pageAgentsThe core idea of agents is to use an LLM to choose a sequence of actions to take. In chains, a sequence of actions is hardcoded (in code). In agents, a language model is used as a reasoning engine to determine which actions to take and in which order.Some important terminology (and schema) to know:AgentAction: This is a dataclass that represents the action an agent should take. It has a tool property (which is the name of the tool that should be invoked) and a tool_input property (the input to that tool)AgentFinish: This is a dataclass that signifies that the agent has finished and should return to the user. It has a return_values parameter, which is a dictionary to return. It often only has one key - output - that is a string, and so often it is just this key that is returned.intermediate_steps: These represent previous agent actions and corresponding outputs that are passed around. These are important to pass to future iteration so the agent knows what work it has already done. This is typed as a List[Tuple[AgentAction, Any]]. Note that observation is currently left as type Any to be maximally flexible. In practice, this is often a string.There are several key components here:Agent​This is the chain responsible for deciding what step to take next. This is powered by a language model and a prompt. The inputs to this chain are:List of available toolsUser inputAny previously executed steps (intermediate_steps)This chain then returns either the next action to take or the final response to send to the user (AgentAction or AgentFinish).Different agents have different prompting styles for reasoning, different ways of encoding input, and different ways of parsing the output. For a full list of agent types see agent typesTools​Tools are functions that an agent calls. There are two important considerations here:Giving the agent access to the right toolsDescribing the tools in a way that is most helpful to the agentWithout both, the agent you are trying to build will not work. If you don't give the agent access to a correct set of tools, it will never be able to accomplish the objective. If you don't describe the tools properly, the agent won't know how to properly use them.LangChain provides a wide set of tools to get started, but also makes it easy to define your own (including custom descriptions). For a full list of tools, see hereToolkits​Often the set of tools an agent has access to is more important than a single tool. For this LangChain provides the concept of toolkits - groups of tools needed to accomplish specific objectives. There are generally around 3-5 tools in a toolkit.LangChain provides a wide set of toolkits to get started. For a full list of toolkits, see hereAgentExecutor​The agent executor is the runtime for an agent. This is what actually calls the agent and executes the actions it chooses. Pseudocode for this runtime is below:next_action = agent.get_action(...)while next_action != AgentFinish: observation = run(next_action) next_action = agent.get_action(..., next_action, observation)return next_actionWhile this may seem simple, there are several complexities this runtime handles for you, including:Handling cases where the agent selects a non-existent toolHandling cases where the tool errorsHandling cases where the agent produces output that cannot be parsed into a tool invocationLogging and observability at all levels (agent decisions, tool calls) either to stdout or LangSmith.Other types of agent runtimes​The AgentExecutor class is the main agent runtime supported by LangChain. However, there are other, more experimental runtimes we also support. These include:Plan-and-execute AgentBaby AGIAuto GPTGet started​This will go over how to get started building an agent. We will create this agent from scratch, using LangChain Expression Language. We will then define custom tools, and then run it in a custom loop (we will also show how to use the standard LangChain AgentExecutor).Set up the agent​We first need to create our agent. This is the chain responsible for determining what action to take next.In this example, we will use OpenAI Function Calling to create this agent. This is generally the most reliable way create agents. In this example we will show what it is like to construct this agent from scratch, using LangChain Expression Language.For this guide, we will construct a custom agent that has access to a custom tool. We are choosing this example because we think for most use cases you will NEED to customize either the agent or the tools. The tool we will give the agent is a tool to calculate the length of a word. This is useful because this is actually something LLMs can mess up due to tokenization. We will first create it WITHOUT memory, but we will then show how to add memory in. Memory is needed to enable conversation.First, let's load the language model we're going to use to control the agent.from langchain.chat_models import ChatOpenAIllm = ChatOpenAI(temperature=0)Next, let's define some tools to use. Let's write a really simple Python function to calculate the length of a word that is passed in.from langchain.agents import tool@tooldef get_word_length(word: str) -> int: """"""Returns the length of a word."""""" return len(word)tools = [get_word_length]Now let us create the prompt. Because OpenAI Function Calling is finetuned for tool usage, we hardly need any instructions on how to reason, or how to output format. We will just have two input variables: input (for the user question) and agent_scratchpad (for any previous steps taken)from langchain.prompts import ChatPromptTemplate, MessagesPlaceholderprompt = ChatPromptTemplate.from_messages([ (""system"", ""You are very powerful assistant, but bad at calculating lengths of words.""), (""user"", ""{input}""), MessagesPlaceholder(variable_name=""agent_scratchpad""),])How does the agent know what tools it can use? Those are passed in as a separate argument, so we can bind those as keyword arguments to the LLM.from langchain.tools.render import format_tool_to_openai_functionllm_with_tools = llm.bind( functions=[format_tool_to_openai_function(t) for t in tools])Putting those pieces together, we can now create the agent. We will import two last utility functions: a component for formatting intermediate steps to messages, and a component for converting the output message into an agent action/agent finish.from langchain.agents.format_scratchpad import format_to_openai_functionsfrom langchain.agents.output_parsers import OpenAIFunctionsAgentOutputParseragent = { ""input"": lambda x: x[""input""], ""agent_scratchpad"": lambda x: format_to_openai_functions(x['intermediate_steps'])} | prompt | llm_with_tools | OpenAIFunctionsAgentOutputParser()Now that we have our agent, let's play around with it! Let's pass in a simple question and empty intermediate steps and see what it returns:agent.invoke({ ""input"": ""how many letters in the word educa?"", ""intermediate_steps"": []})We can see that it responds with an AgentAction to take (it's actually an AgentActionMessageLog - a subclass of AgentAction which also tracks the full message log).So this is just the first step - now we need to write a runtime for this. The simplest one is just one that continuously loops, calling the agent, then taking the action, and repeating until an AgentFinish is returned. Let's code that up below:from langchain.schema.agent import AgentFinishintermediate_steps = []while True: output = agent.invoke({ ""input"": ""how many letters in the word educa?"", ""intermediate_steps"": intermediate_steps }) if isinstance(output, AgentFinish): final_result = output.return_values[""output""] break else: print(output.tool, output.tool_input) tool = { ""get_word_length"": get_word_length }[output.tool] observation = tool.run(output.tool_input) intermediate_steps.append((output, observation))print(final_result)We can see this prints out the following:get_word_length {'word': 'educa'}There are 5 letters in the word ""educa"".Woo! It's working.To simplify this a bit, we can import and use the AgentExecutor class. This bundles up all of the above and adds in error handling, early stopping, tracing, and other quality-of-life improvements that reduce safeguards you need to write.from langchain.agents import AgentExecutoragent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)Now let's test it out!agent_executor.invoke({""input"": ""how many letters in the word educa?""}) > Entering new AgentExecutor chain... Invoking: `get_word_length` with `{'word': 'educa'}` 5 There are 5 letters in the word ""educa"". > Finished chain. 'There are 5 letters in the word ""educa"".'This is great - we have an agent! However, this agent is stateless - it doesn't remember anything about previous interactions. This means you can't ask follow up questions easily. Let's fix that by adding in memory.In order to do this, we need to do two things:Add a place for memory variables to go in the promptKeep track of the chat historyFirst, let's add a place for memory in the prompt. We do this by adding a placeholder for messages with the key ""chat_history"". Notice that we put this ABOVE the new user input (to follow the conversation flow).from langchain.prompts import MessagesPlaceholderMEMORY_KEY = ""chat_history""prompt = ChatPromptTemplate.from_messages([ (""system"", ""You are very powerful assistant, but bad at calculating lengths of words.""), MessagesPlaceholder(variable_name=MEMORY_KEY), (""user"", ""{input}""), MessagesPlaceholder(variable_name=""agent_scratchpad""),])We can then set up a list to track the chat historyfrom langchain.schema.messages import HumanMessage, AIMessagechat_history = []We can then put it all together!agent = { ""input"": lambda x: x[""input""], ""agent_scratchpad"": lambda x: format_to_openai_functions(x['intermediate_steps']), ""chat_history"": lambda x: x[""chat_history""]} | prompt | llm_with_tools | OpenAIFunctionsAgentOutputParser()agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)When running, we now need to track the inputs and outputs as chat historyinput1 = ""how many letters in the word educa?""result = agent_executor.invoke({""input"": input1, ""chat_history"": chat_history})chat_history.append(HumanMessage(content=input1))chat_history.append(AIMessage(content=result['output']))agent_executor.invoke({""input"": ""is that a real word?"", ""chat_history"": chat_history})Next Steps​Awesome! You've now run your first end-to-end agent. To dive deeper, you can:Check out all the different agent types supportedLearn all the controls for AgentExecutorSee a full list of all the off-the-shelf toolkits we provideExplore all the individual tools supportedPreviousMultiple Memory classesNextAgent TypesAgentToolsToolkitsAgentExecutorOther types of agent runtimesGet startedNext Steps" 58,https://python.langchain.com/docs/modules/callbacks/,"ModulesCallbacksCallbacksinfoHead to Integrations for documentation on built-in callbacks integrations with 3rd-party tools.LangChain provides a callbacks system that allows you to hook into the various stages of your LLM application. This is useful for logging, monitoring, streaming, and other tasks.You can subscribe to these events by using the callbacks argument available throughout the API. This argument is list of handler objects, which are expected to implement one or more of the methods described below in more detail.Callback handlers​CallbackHandlers are objects that implement the CallbackHandler interface, which has a method for each event that can be subscribed to. The CallbackManager will call the appropriate method on each handler when the event is triggered.class BaseCallbackHandler: """"""Base callback handler that can be used to handle callbacks from langchain."""""" def on_llm_start( self, serialized: Dict[str, Any], prompts: List[str], **kwargs: Any ) -> Any: """"""Run when LLM starts running."""""" def on_chat_model_start( self, serialized: Dict[str, Any], messages: List[List[BaseMessage]], **kwargs: Any ) -> Any: """"""Run when Chat Model starts running."""""" def on_llm_new_token(self, token: str, **kwargs: Any) -> Any: """"""Run on new LLM token. Only available when streaming is enabled."""""" def on_llm_end(self, response: LLMResult, **kwargs: Any) -> Any: """"""Run when LLM ends running."""""" def on_llm_error( self, error: Union[Exception, KeyboardInterrupt], **kwargs: Any ) -> Any: """"""Run when LLM errors."""""" def on_chain_start( self, serialized: Dict[str, Any], inputs: Dict[str, Any], **kwargs: Any ) -> Any: """"""Run when chain starts running."""""" def on_chain_end(self, outputs: Dict[str, Any], **kwargs: Any) -> Any: """"""Run when chain ends running."""""" def on_chain_error( self, error: Union[Exception, KeyboardInterrupt], **kwargs: Any ) -> Any: """"""Run when chain errors."""""" def on_tool_start( self, serialized: Dict[str, Any], input_str: str, **kwargs: Any ) -> Any: """"""Run when tool starts running."""""" def on_tool_end(self, output: str, **kwargs: Any) -> Any: """"""Run when tool ends running."""""" def on_tool_error( self, error: Union[Exception, KeyboardInterrupt], **kwargs: Any ) -> Any: """"""Run when tool errors."""""" def on_text(self, text: str, **kwargs: Any) -> Any: """"""Run on arbitrary text."""""" def on_agent_action(self, action: AgentAction, **kwargs: Any) -> Any: """"""Run on agent action."""""" def on_agent_finish(self, finish: AgentFinish, **kwargs: Any) -> Any: """"""Run on agent end.""""""Get started​LangChain provides a few built-in handlers that you can use to get started. These are available in the langchain/callbacks module. The most basic handler is the StdOutCallbackHandler, which simply logs all events to stdout.Note: when the verbose flag on the object is set to true, the StdOutCallbackHandler will be invoked even without being explicitly passed in.from langchain.callbacks import StdOutCallbackHandlerfrom langchain.chains import LLMChainfrom langchain.llms import OpenAIfrom langchain.prompts import PromptTemplatehandler = StdOutCallbackHandler()llm = OpenAI()prompt = PromptTemplate.from_template(""1 + {number} = "")# Constructor callback: First, let's explicitly set the StdOutCallbackHandler when initializing our chainchain = LLMChain(llm=llm, prompt=prompt, callbacks=[handler])chain.run(number=2)# Use verbose flag: Then, let's use the `verbose` flag to achieve the same resultchain = LLMChain(llm=llm, prompt=prompt, verbose=True)chain.run(number=2)# Request callbacks: Finally, let's use the request `callbacks` to achieve the same resultchain = LLMChain(llm=llm, prompt=prompt)chain.run(number=2, callbacks=[handler]) > Entering new LLMChain chain... Prompt after formatting: 1 + 2 = > Finished chain. > Entering new LLMChain chain... Prompt after formatting: 1 + 2 = > Finished chain. > Entering new LLMChain chain... Prompt after formatting: 1 + 2 = > Finished chain. '\n\n3'Where to pass in callbacks​The callbacks argument is available on most objects throughout the API (Chains, Models, Tools, Agents, etc.) in two different places:Constructor callbacks: defined in the constructor, e.g. LLMChain(callbacks=[handler], tags=['a-tag']), which will be used for all calls made on that object, and will be scoped to that object only, e.g. if you pass a handler to the LLMChain constructor, it will not be used by the Model attached to that chain.Request callbacks: defined in the run()/apply() methods used for issuing a request, e.g. chain.run(input, callbacks=[handler]), which will be used for that specific request only, and all sub-requests that it contains (e.g. a call to an LLMChain triggers a call to a Model, which uses the same handler passed in the call() method).The verbose argument is available on most objects throughout the API (Chains, Models, Tools, Agents, etc.) as a constructor argument, e.g. LLMChain(verbose=True), and it is equivalent to passing a ConsoleCallbackHandler to the callbacks argument of that object and all child objects. This is useful for debugging, as it will log all events to the console.When do you want to use each of these?​Constructor callbacks are most useful for use cases such as logging, monitoring, etc., which are not specific to a single request, but rather to the entire chain. For example, if you want to log all the requests made to an LLMChain, you would pass a handler to the constructor.Request callbacks are most useful for use cases such as streaming, where you want to stream the output of a single request to a specific websocket connection, or other similar use cases. For example, if you want to stream the output of a single request to a websocket, you would pass a handler to the call() methodPreviousToolkitsNextAsync callbacks" 59,https://python.langchain.com/docs/modules/,"ModulesOn this pageModulesLangChain provides standard, extendable interfaces and external integrations for the following modules, listed from least to most complex:Model I/O​Interface with language modelsRetrieval​Interface with application-specific dataChains​Construct sequences of callsAgents​Let chains choose which tools to use given high-level directivesMemory​Persist application state between runs of a chainCallbacks​Log and stream intermediate steps of any chainPreviousLangChain Expression Language (LCEL)NextModel I/O" 60,https://python.langchain.com/docs/guides,"GuidesGuidesDesign guides for key parts of the development process🗃️ Adapters1 items📄️ DebuggingIf you're building with LLMs, at some point something will break, and you'll need to debug. A model call will fail, or the model output will be misformatted, or there will be some nested model calls and it won't be clear where along the way an incorrect output was created.🗃️ Deployment1 items🗃️ Evaluation4 items📄️ FallbacksWhen working with language models, you may often encounter issues from the underlying APIs, whether these be rate limiting or downtime. Therefore, as you go to move your LLM applications into production it becomes more and more important to safeguard against these. That's why we've introduced the concept of fallbacks.🗃️ LangSmith1 items📄️ Run LLMs locallyUse case📄️ Model comparisonConstructing your language model application will likely involved choosing between many different options of prompts, models, and even chains to use. When doing so, you will want to compare these different options on different inputs in an easy, flexible, and intuitive way.🗃️ Privacy1 items📄️ Pydantic compatibility- Pydantic v2 was released in June, 2023 (https://docs.pydantic.dev/2.0/blog/pydantic-v2-final/)🗃️ Safety5 itemsPreviousModulesNextOpenAI Adapter" 61,https://python.langchain.com/docs/guides/adapters/openai,"GuidesAdaptersOpenAI AdapterOn this pageOpenAI AdapterA lot of people get started with OpenAI but want to explore other models. LangChain's integrations with many model providers make this easy to do so. While LangChain has it's own message and model APIs, we've also made it as easy as possible to explore other models by exposing an adapter to adapt LangChain models to the OpenAI api.At the moment this only deals with output and does not return other information (token counts, stop reasons, etc).import openaifrom langchain.adapters import openai as lc_openaiChatCompletion.create​messages = [{""role"": ""user"", ""content"": ""hi""}]Original OpenAI callresult = openai.ChatCompletion.create( messages=messages, model=""gpt-3.5-turbo"", temperature=0)result[""choices""][0]['message'].to_dict_recursive() {'role': 'assistant', 'content': 'Hello! How can I assist you today?'}LangChain OpenAI wrapper calllc_result = lc_openai.ChatCompletion.create( messages=messages, model=""gpt-3.5-turbo"", temperature=0)lc_result[""choices""][0]['message'] {'role': 'assistant', 'content': 'Hello! How can I assist you today?'}Swapping out model providerslc_result = lc_openai.ChatCompletion.create( messages=messages, model=""claude-2"", temperature=0, provider=""ChatAnthropic"")lc_result[""choices""][0]['message'] {'role': 'assistant', 'content': ' Hello!'}ChatCompletion.stream​Original OpenAI callfor c in openai.ChatCompletion.create( messages = messages, model=""gpt-3.5-turbo"", temperature=0, stream=True): print(c[""choices""][0]['delta'].to_dict_recursive()) {'role': 'assistant', 'content': ''} {'content': 'Hello'} {'content': '!'} {'content': ' How'} {'content': ' can'} {'content': ' I'} {'content': ' assist'} {'content': ' you'} {'content': ' today'} {'content': '?'} {}LangChain OpenAI wrapper callfor c in lc_openai.ChatCompletion.create( messages = messages, model=""gpt-3.5-turbo"", temperature=0, stream=True): print(c[""choices""][0]['delta']) {'role': 'assistant', 'content': ''} {'content': 'Hello'} {'content': '!'} {'content': ' How'} {'content': ' can'} {'content': ' I'} {'content': ' assist'} {'content': ' you'} {'content': ' today'} {'content': '?'} {}Swapping out model providersfor c in lc_openai.ChatCompletion.create( messages = messages, model=""claude-2"", temperature=0, stream=True, provider=""ChatAnthropic"",): print(c[""choices""][0]['delta']) {'role': 'assistant', 'content': ' Hello'} {'content': '!'} {}PreviousGuidesNextDebuggingChatCompletion.createChatCompletion.stream" 62,https://python.langchain.com/docs/guides/debugging,"GuidesDebuggingOn this pageDebuggingIf you're building with LLMs, at some point something will break, and you'll need to debug. A model call will fail, or the model output will be misformatted, or there will be some nested model calls and it won't be clear where along the way an incorrect output was created.Here are a few different tools and functionalities to aid in debugging.Tracing​Platforms with tracing capabilities like LangSmith and WandB are the most comprehensive solutions for debugging. These platforms make it easy to not only log and visualize LLM apps, but also to actively debug, test and refine them.For anyone building production-grade LLM applications, we highly recommend using a platform like this.langchain.debug and langchain.verbose​If you're prototyping in Jupyter Notebooks or running Python scripts, it can be helpful to print out the intermediate steps of a Chain run. There are a number of ways to enable printing at varying degrees of verbosity.Let's suppose we have a simple agent, and want to visualize the actions it takes and tool outputs it receives. Without any debugging, here's what we see:from langchain.agents import AgentType, initialize_agent, load_toolsfrom langchain.chat_models import ChatOpenAIllm = ChatOpenAI(model_name=""gpt-4"", temperature=0)tools = load_tools([""ddg-search"", ""llm-math""], llm=llm)agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION)agent.run(""Who directed the 2023 film Oppenheimer and what is their age? What is their age in days (assume 365 days per year)?"") 'The director of the 2023 film Oppenheimer is Christopher Nolan and he is approximately 19345 days old in 2023.'langchain.debug = True​Setting the global debug flag will cause all LangChain components with callback support (chains, models, agents, tools, retrievers) to print the inputs they receive and outputs they generate. This is the most verbose setting and will fully log raw inputs and outputs.import langchainlangchain.debug = Trueagent.run(""Who directed the 2023 film Oppenheimer and what is their age? What is their age in days (assume 365 days per year)?"")Console output [chain/start] [1:RunTypeEnum.chain:AgentExecutor] Entering Chain run with input: { ""input"": ""Who directed the 2023 film Oppenheimer and what is their age? What is their age in days (assume 365 days per year)?"" } [chain/start] [1:RunTypeEnum.chain:AgentExecutor > 2:RunTypeEnum.chain:LLMChain] Entering Chain run with input: { ""input"": ""Who directed the 2023 film Oppenheimer and what is their age? What is their age in days (assume 365 days per year)?"", ""agent_scratchpad"": """", ""stop"": [ ""\nObservation:"", ""\n\tObservation:"" ] } [llm/start] [1:RunTypeEnum.chain:AgentExecutor > 2:RunTypeEnum.chain:LLMChain > 3:RunTypeEnum.llm:ChatOpenAI] Entering LLM run with input: { ""prompts"": [ ""Human: Answer the following questions as best you can. You have access to the following tools:\n\nduckduckgo_search: A wrapper around DuckDuckGo Search. Useful for when you need to answer questions about current events. Input should be a search query.\nCalculator: Useful for when you need to answer questions about math.\n\nUse the following format:\n\nQuestion: the input question you must answer\nThought: you should always think about what to do\nAction: the action to take, should be one of [duckduckgo_search, Calculator]\nAction Input: the input to the action\nObservation: the result of the action\n... (this Thought/Action/Action Input/Observation can repeat N times)\nThought: I now know the final answer\nFinal Answer: the final answer to the original input question\n\nBegin!\n\nQuestion: Who directed the 2023 film Oppenheimer and what is their age? What is their age in days (assume 365 days per year)?\nThought:"" ] } [llm/end] [1:RunTypeEnum.chain:AgentExecutor > 2:RunTypeEnum.chain:LLMChain > 3:RunTypeEnum.llm:ChatOpenAI] [5.53s] Exiting LLM run with output: { ""generations"": [ [ { ""text"": ""I need to find out who directed the 2023 film Oppenheimer and their age. Then, I need to calculate their age in days. I will use DuckDuckGo to find out the director and their age.\nAction: duckduckgo_search\nAction Input: \""Director of the 2023 film Oppenheimer and their age\"""", ""generation_info"": { ""finish_reason"": ""stop"" }, ""message"": { ""lc"": 1, ""type"": ""constructor"", ""id"": [ ""langchain"", ""schema"", ""messages"", ""AIMessage"" ], ""kwargs"": { ""content"": ""I need to find out who directed the 2023 film Oppenheimer and their age. Then, I need to calculate their age in days. I will use DuckDuckGo to find out the director and their age.\nAction: duckduckgo_search\nAction Input: \""Director of the 2023 film Oppenheimer and their age\"""", ""additional_kwargs"": {} } } } ] ], ""llm_output"": { ""token_usage"": { ""prompt_tokens"": 206, ""completion_tokens"": 71, ""total_tokens"": 277 }, ""model_name"": ""gpt-4"" }, ""run"": null } [chain/end] [1:RunTypeEnum.chain:AgentExecutor > 2:RunTypeEnum.chain:LLMChain] [5.53s] Exiting Chain run with output: { ""text"": ""I need to find out who directed the 2023 film Oppenheimer and their age. Then, I need to calculate their age in days. I will use DuckDuckGo to find out the director and their age.\nAction: duckduckgo_search\nAction Input: \""Director of the 2023 film Oppenheimer and their age\"""" } [tool/start] [1:RunTypeEnum.chain:AgentExecutor > 4:RunTypeEnum.tool:duckduckgo_search] Entering Tool run with input: ""Director of the 2023 film Oppenheimer and their age"" [tool/end] [1:RunTypeEnum.chain:AgentExecutor > 4:RunTypeEnum.tool:duckduckgo_search] [1.51s] Exiting Tool run with output: ""Capturing the mad scramble to build the first atomic bomb required rapid-fire filming, strict set rules and the construction of an entire 1940s western town. By Jada Yuan. July 19, 2023 at 5:00 a ... In Christopher Nolan's new film, ""Oppenheimer,"" Cillian Murphy stars as J. Robert Oppenheimer, the American physicist who oversaw the Manhattan Project in Los Alamos, N.M. Universal Pictures... Oppenheimer: Directed by Christopher Nolan. With Cillian Murphy, Emily Blunt, Robert Downey Jr., Alden Ehrenreich. The story of American scientist J. Robert Oppenheimer and his role in the development of the atomic bomb. Christopher Nolan goes deep on 'Oppenheimer,' his most 'extreme' film to date. By Kenneth Turan. July 11, 2023 5 AM PT. For Subscribers. Christopher Nolan is photographed in Los Angeles ... Oppenheimer is a 2023 epic biographical thriller film written and directed by Christopher Nolan.It is based on the 2005 biography American Prometheus by Kai Bird and Martin J. Sherwin about J. Robert Oppenheimer, a theoretical physicist who was pivotal in developing the first nuclear weapons as part of the Manhattan Project and thereby ushering in the Atomic Age."" [chain/start] [1:RunTypeEnum.chain:AgentExecutor > 5:RunTypeEnum.chain:LLMChain] Entering Chain run with input: { ""input"": ""Who directed the 2023 film Oppenheimer and what is their age? What is their age in days (assume 365 days per year)?"", ""agent_scratchpad"": ""I need to find out who directed the 2023 film Oppenheimer and their age. Then, I need to calculate their age in days. I will use DuckDuckGo to find out the director and their age.\nAction: duckduckgo_search\nAction Input: \""Director of the 2023 film Oppenheimer and their age\""\nObservation: Capturing the mad scramble to build the first atomic bomb required rapid-fire filming, strict set rules and the construction of an entire 1940s western town. By Jada Yuan. July 19, 2023 at 5:00 a ... In Christopher Nolan's new film, \""Oppenheimer,\"" Cillian Murphy stars as J. Robert Oppenheimer, the American physicist who oversaw the Manhattan Project in Los Alamos, N.M. Universal Pictures... Oppenheimer: Directed by Christopher Nolan. With Cillian Murphy, Emily Blunt, Robert Downey Jr., Alden Ehrenreich. The story of American scientist J. Robert Oppenheimer and his role in the development of the atomic bomb. Christopher Nolan goes deep on 'Oppenheimer,' his most 'extreme' film to date. By Kenneth Turan. July 11, 2023 5 AM PT. For Subscribers. Christopher Nolan is photographed in Los Angeles ... Oppenheimer is a 2023 epic biographical thriller film written and directed by Christopher Nolan.It is based on the 2005 biography American Prometheus by Kai Bird and Martin J. Sherwin about J. Robert Oppenheimer, a theoretical physicist who was pivotal in developing the first nuclear weapons as part of the Manhattan Project and thereby ushering in the Atomic Age.\nThought:"", ""stop"": [ ""\nObservation:"", ""\n\tObservation:"" ] } [llm/start] [1:RunTypeEnum.chain:AgentExecutor > 5:RunTypeEnum.chain:LLMChain > 6:RunTypeEnum.llm:ChatOpenAI] Entering LLM run with input: { ""prompts"": [ ""Human: Answer the following questions as best you can. You have access to the following tools:\n\nduckduckgo_search: A wrapper around DuckDuckGo Search. Useful for when you need to answer questions about current events. Input should be a search query.\nCalculator: Useful for when you need to answer questions about math.\n\nUse the following format:\n\nQuestion: the input question you must answer\nThought: you should always think about what to do\nAction: the action to take, should be one of [duckduckgo_search, Calculator]\nAction Input: the input to the action\nObservation: the result of the action\n... (this Thought/Action/Action Input/Observation can repeat N times)\nThought: I now know the final answer\nFinal Answer: the final answer to the original input question\n\nBegin!\n\nQuestion: Who directed the 2023 film Oppenheimer and what is their age? What is their age in days (assume 365 days per year)?\nThought:I need to find out who directed the 2023 film Oppenheimer and their age. Then, I need to calculate their age in days. I will use DuckDuckGo to find out the director and their age.\nAction: duckduckgo_search\nAction Input: \""Director of the 2023 film Oppenheimer and their age\""\nObservation: Capturing the mad scramble to build the first atomic bomb required rapid-fire filming, strict set rules and the construction of an entire 1940s western town. By Jada Yuan. July 19, 2023 at 5:00 a ... In Christopher Nolan's new film, \""Oppenheimer,\"" Cillian Murphy stars as J. Robert Oppenheimer, the American physicist who oversaw the Manhattan Project in Los Alamos, N.M. Universal Pictures... Oppenheimer: Directed by Christopher Nolan. With Cillian Murphy, Emily Blunt, Robert Downey Jr., Alden Ehrenreich. The story of American scientist J. Robert Oppenheimer and his role in the development of the atomic bomb. Christopher Nolan goes deep on 'Oppenheimer,' his most 'extreme' film to date. By Kenneth Turan. July 11, 2023 5 AM PT. For Subscribers. Christopher Nolan is photographed in Los Angeles ... Oppenheimer is a 2023 epic biographical thriller film written and directed by Christopher Nolan.It is based on the 2005 biography American Prometheus by Kai Bird and Martin J. Sherwin about J. Robert Oppenheimer, a theoretical physicist who was pivotal in developing the first nuclear weapons as part of the Manhattan Project and thereby ushering in the Atomic Age.\nThought:"" ] } [llm/end] [1:RunTypeEnum.chain:AgentExecutor > 5:RunTypeEnum.chain:LLMChain > 6:RunTypeEnum.llm:ChatOpenAI] [4.46s] Exiting LLM run with output: { ""generations"": [ [ { ""text"": ""The director of the 2023 film Oppenheimer is Christopher Nolan. Now I need to find out his age.\nAction: duckduckgo_search\nAction Input: \""Christopher Nolan age\"""", ""generation_info"": { ""finish_reason"": ""stop"" }, ""message"": { ""lc"": 1, ""type"": ""constructor"", ""id"": [ ""langchain"", ""schema"", ""messages"", ""AIMessage"" ], ""kwargs"": { ""content"": ""The director of the 2023 film Oppenheimer is Christopher Nolan. Now I need to find out his age.\nAction: duckduckgo_search\nAction Input: \""Christopher Nolan age\"""", ""additional_kwargs"": {} } } } ] ], ""llm_output"": { ""token_usage"": { ""prompt_tokens"": 550, ""completion_tokens"": 39, ""total_tokens"": 589 }, ""model_name"": ""gpt-4"" }, ""run"": null } [chain/end] [1:RunTypeEnum.chain:AgentExecutor > 5:RunTypeEnum.chain:LLMChain] [4.46s] Exiting Chain run with output: { ""text"": ""The director of the 2023 film Oppenheimer is Christopher Nolan. Now I need to find out his age.\nAction: duckduckgo_search\nAction Input: \""Christopher Nolan age\"""" } [tool/start] [1:RunTypeEnum.chain:AgentExecutor > 7:RunTypeEnum.tool:duckduckgo_search] Entering Tool run with input: ""Christopher Nolan age"" [tool/end] [1:RunTypeEnum.chain:AgentExecutor > 7:RunTypeEnum.tool:duckduckgo_search] [1.33s] Exiting Tool run with output: ""Christopher Edward Nolan CBE (born 30 July 1970) is a British and American filmmaker. Known for his Hollywood blockbusters with complex storytelling, Nolan is considered a leading filmmaker of the 21st century. His films have grossed $5 billion worldwide. The recipient of many accolades, he has been nominated for five Academy Awards, five BAFTA Awards and six Golden Globe Awards. July 30, 1970 (age 52) London England Notable Works: ""Dunkirk"" ""Tenet"" ""The Prestige"" See all related content → Recent News Jul. 13, 2023, 11:11 AM ET (AP) Cillian Murphy, playing Oppenheimer, finally gets to lead a Christopher Nolan film July 11, 2023 5 AM PT For Subscribers Christopher Nolan is photographed in Los Angeles. (Joe Pugliese / For The Times) This is not the story I was supposed to write. Oppenheimer director Christopher Nolan, Cillian Murphy, Emily Blunt and Matt Damon on the stakes of making a three-hour, CGI-free summer film. Christopher Nolan, the director behind such films as ""Dunkirk,"" ""Inception,"" ""Interstellar,"" and the ""Dark Knight"" trilogy, has spent the last three years living in Oppenheimer's world, writing ..."" [chain/start] [1:RunTypeEnum.chain:AgentExecutor > 8:RunTypeEnum.chain:LLMChain] Entering Chain run with input: { ""input"": ""Who directed the 2023 film Oppenheimer and what is their age? What is their age in days (assume 365 days per year)?"", ""agent_scratchpad"": ""I need to find out who directed the 2023 film Oppenheimer and their age. Then, I need to calculate their age in days. I will use DuckDuckGo to find out the director and their age.\nAction: duckduckgo_search\nAction Input: \""Director of the 2023 film Oppenheimer and their age\""\nObservation: Capturing the mad scramble to build the first atomic bomb required rapid-fire filming, strict set rules and the construction of an entire 1940s western town. By Jada Yuan. July 19, 2023 at 5:00 a ... In Christopher Nolan's new film, \""Oppenheimer,\"" Cillian Murphy stars as J. Robert Oppenheimer, the American physicist who oversaw the Manhattan Project in Los Alamos, N.M. Universal Pictures... Oppenheimer: Directed by Christopher Nolan. With Cillian Murphy, Emily Blunt, Robert Downey Jr., Alden Ehrenreich. The story of American scientist J. Robert Oppenheimer and his role in the development of the atomic bomb. Christopher Nolan goes deep on 'Oppenheimer,' his most 'extreme' film to date. By Kenneth Turan. July 11, 2023 5 AM PT. For Subscribers. Christopher Nolan is photographed in Los Angeles ... Oppenheimer is a 2023 epic biographical thriller film written and directed by Christopher Nolan.It is based on the 2005 biography American Prometheus by Kai Bird and Martin J. Sherwin about J. Robert Oppenheimer, a theoretical physicist who was pivotal in developing the first nuclear weapons as part of the Manhattan Project and thereby ushering in the Atomic Age.\nThought:The director of the 2023 film Oppenheimer is Christopher Nolan. Now I need to find out his age.\nAction: duckduckgo_search\nAction Input: \""Christopher Nolan age\""\nObservation: Christopher Edward Nolan CBE (born 30 July 1970) is a British and American filmmaker. Known for his Hollywood blockbusters with complex storytelling, Nolan is considered a leading filmmaker of the 21st century. His films have grossed $5 billion worldwide. The recipient of many accolades, he has been nominated for five Academy Awards, five BAFTA Awards and six Golden Globe Awards. July 30, 1970 (age 52) London England Notable Works: \""Dunkirk\"" \""Tenet\"" \""The Prestige\"" See all related content → Recent News Jul. 13, 2023, 11:11 AM ET (AP) Cillian Murphy, playing Oppenheimer, finally gets to lead a Christopher Nolan film July 11, 2023 5 AM PT For Subscribers Christopher Nolan is photographed in Los Angeles. (Joe Pugliese / For The Times) This is not the story I was supposed to write. Oppenheimer director Christopher Nolan, Cillian Murphy, Emily Blunt and Matt Damon on the stakes of making a three-hour, CGI-free summer film. Christopher Nolan, the director behind such films as \""Dunkirk,\"" \""Inception,\"" \""Interstellar,\"" and the \""Dark Knight\"" trilogy, has spent the last three years living in Oppenheimer's world, writing ...\nThought:"", ""stop"": [ ""\nObservation:"", ""\n\tObservation:"" ] } [llm/start] [1:RunTypeEnum.chain:AgentExecutor > 8:RunTypeEnum.chain:LLMChain > 9:RunTypeEnum.llm:ChatOpenAI] Entering LLM run with input: { ""prompts"": [ ""Human: Answer the following questions as best you can. You have access to the following tools:\n\nduckduckgo_search: A wrapper around DuckDuckGo Search. Useful for when you need to answer questions about current events. Input should be a search query.\nCalculator: Useful for when you need to answer questions about math.\n\nUse the following format:\n\nQuestion: the input question you must answer\nThought: you should always think about what to do\nAction: the action to take, should be one of [duckduckgo_search, Calculator]\nAction Input: the input to the action\nObservation: the result of the action\n... (this Thought/Action/Action Input/Observation can repeat N times)\nThought: I now know the final answer\nFinal Answer: the final answer to the original input question\n\nBegin!\n\nQuestion: Who directed the 2023 film Oppenheimer and what is their age? What is their age in days (assume 365 days per year)?\nThought:I need to find out who directed the 2023 film Oppenheimer and their age. Then, I need to calculate their age in days. I will use DuckDuckGo to find out the director and their age.\nAction: duckduckgo_search\nAction Input: \""Director of the 2023 film Oppenheimer and their age\""\nObservation: Capturing the mad scramble to build the first atomic bomb required rapid-fire filming, strict set rules and the construction of an entire 1940s western town. By Jada Yuan. July 19, 2023 at 5:00 a ... In Christopher Nolan's new film, \""Oppenheimer,\"" Cillian Murphy stars as J. Robert Oppenheimer, the American physicist who oversaw the Manhattan Project in Los Alamos, N.M. Universal Pictures... Oppenheimer: Directed by Christopher Nolan. With Cillian Murphy, Emily Blunt, Robert Downey Jr., Alden Ehrenreich. The story of American scientist J. Robert Oppenheimer and his role in the development of the atomic bomb. Christopher Nolan goes deep on 'Oppenheimer,' his most 'extreme' film to date. By Kenneth Turan. July 11, 2023 5 AM PT. For Subscribers. Christopher Nolan is photographed in Los Angeles ... Oppenheimer is a 2023 epic biographical thriller film written and directed by Christopher Nolan.It is based on the 2005 biography American Prometheus by Kai Bird and Martin J. Sherwin about J. Robert Oppenheimer, a theoretical physicist who was pivotal in developing the first nuclear weapons as part of the Manhattan Project and thereby ushering in the Atomic Age.\nThought:The director of the 2023 film Oppenheimer is Christopher Nolan. Now I need to find out his age.\nAction: duckduckgo_search\nAction Input: \""Christopher Nolan age\""\nObservation: Christopher Edward Nolan CBE (born 30 July 1970) is a British and American filmmaker. Known for his Hollywood blockbusters with complex storytelling, Nolan is considered a leading filmmaker of the 21st century. His films have grossed $5 billion worldwide. The recipient of many accolades, he has been nominated for five Academy Awards, five BAFTA Awards and six Golden Globe Awards. July 30, 1970 (age 52) London England Notable Works: \""Dunkirk\"" \""Tenet\"" \""The Prestige\"" See all related content → Recent News Jul. 13, 2023, 11:11 AM ET (AP) Cillian Murphy, playing Oppenheimer, finally gets to lead a Christopher Nolan film July 11, 2023 5 AM PT For Subscribers Christopher Nolan is photographed in Los Angeles. (Joe Pugliese / For The Times) This is not the story I was supposed to write. Oppenheimer director Christopher Nolan, Cillian Murphy, Emily Blunt and Matt Damon on the stakes of making a three-hour, CGI-free summer film. Christopher Nolan, the director behind such films as \""Dunkirk,\"" \""Inception,\"" \""Interstellar,\"" and the \""Dark Knight\"" trilogy, has spent the last three years living in Oppenheimer's world, writing ...\nThought:"" ] } [llm/end] [1:RunTypeEnum.chain:AgentExecutor > 8:RunTypeEnum.chain:LLMChain > 9:RunTypeEnum.llm:ChatOpenAI] [2.69s] Exiting LLM run with output: { ""generations"": [ [ { ""text"": ""Christopher Nolan was born on July 30, 1970, which makes him 52 years old in 2023. Now I need to calculate his age in days.\nAction: Calculator\nAction Input: 52*365"", ""generation_info"": { ""finish_reason"": ""stop"" }, ""message"": { ""lc"": 1, ""type"": ""constructor"", ""id"": [ ""langchain"", ""schema"", ""messages"", ""AIMessage"" ], ""kwargs"": { ""content"": ""Christopher Nolan was born on July 30, 1970, which makes him 52 years old in 2023. Now I need to calculate his age in days.\nAction: Calculator\nAction Input: 52*365"", ""additional_kwargs"": {} } } } ] ], ""llm_output"": { ""token_usage"": { ""prompt_tokens"": 868, ""completion_tokens"": 46, ""total_tokens"": 914 }, ""model_name"": ""gpt-4"" }, ""run"": null } [chain/end] [1:RunTypeEnum.chain:AgentExecutor > 8:RunTypeEnum.chain:LLMChain] [2.69s] Exiting Chain run with output: { ""text"": ""Christopher Nolan was born on July 30, 1970, which makes him 52 years old in 2023. Now I need to calculate his age in days.\nAction: Calculator\nAction Input: 52*365"" } [tool/start] [1:RunTypeEnum.chain:AgentExecutor > 10:RunTypeEnum.tool:Calculator] Entering Tool run with input: ""52*365"" [chain/start] [1:RunTypeEnum.chain:AgentExecutor > 10:RunTypeEnum.tool:Calculator > 11:RunTypeEnum.chain:LLMMathChain] Entering Chain run with input: { ""question"": ""52*365"" } [chain/start] [1:RunTypeEnum.chain:AgentExecutor > 10:RunTypeEnum.tool:Calculator > 11:RunTypeEnum.chain:LLMMathChain > 12:RunTypeEnum.chain:LLMChain] Entering Chain run with input: { ""question"": ""52*365"", ""stop"": [ ""```output"" ] } [llm/start] [1:RunTypeEnum.chain:AgentExecutor > 10:RunTypeEnum.tool:Calculator > 11:RunTypeEnum.chain:LLMMathChain > 12:RunTypeEnum.chain:LLMChain > 13:RunTypeEnum.llm:ChatOpenAI] Entering LLM run with input: { ""prompts"": [ ""Human: Translate a math problem into a expression that can be executed using Python's numexpr library. Use the output of running this code to answer the question.\n\nQuestion: ${Question with math problem.}\n```text\n${single line mathematical expression that solves the problem}\n```\n...numexpr.evaluate(text)...\n```output\n${Output of running the code}\n```\nAnswer: ${Answer}\n\nBegin.\n\nQuestion: What is 37593 * 67?\n```text\n37593 * 67\n```\n...numexpr.evaluate(\""37593 * 67\"")...\n```output\n2518731\n```\nAnswer: 2518731\n\nQuestion: 37593^(1/5)\n```text\n37593**(1/5)\n```\n...numexpr.evaluate(\""37593**(1/5)\"")...\n```output\n8.222831614237718\n```\nAnswer: 8.222831614237718\n\nQuestion: 52*365"" ] } [llm/end] [1:RunTypeEnum.chain:AgentExecutor > 10:RunTypeEnum.tool:Calculator > 11:RunTypeEnum.chain:LLMMathChain > 12:RunTypeEnum.chain:LLMChain > 13:RunTypeEnum.llm:ChatOpenAI] [2.89s] Exiting LLM run with output: { ""generations"": [ [ { ""text"": ""```text\n52*365\n```\n...numexpr.evaluate(\""52*365\"")...\n"", ""generation_info"": { ""finish_reason"": ""stop"" }, ""message"": { ""lc"": 1, ""type"": ""constructor"", ""id"": [ ""langchain"", ""schema"", ""messages"", ""AIMessage"" ], ""kwargs"": { ""content"": ""```text\n52*365\n```\n...numexpr.evaluate(\""52*365\"")...\n"", ""additional_kwargs"": {} } } } ] ], ""llm_output"": { ""token_usage"": { ""prompt_tokens"": 203, ""completion_tokens"": 19, ""total_tokens"": 222 }, ""model_name"": ""gpt-4"" }, ""run"": null } [chain/end] [1:RunTypeEnum.chain:AgentExecutor > 10:RunTypeEnum.tool:Calculator > 11:RunTypeEnum.chain:LLMMathChain > 12:RunTypeEnum.chain:LLMChain] [2.89s] Exiting Chain run with output: { ""text"": ""```text\n52*365\n```\n...numexpr.evaluate(\""52*365\"")...\n"" } [chain/end] [1:RunTypeEnum.chain:AgentExecutor > 10:RunTypeEnum.tool:Calculator > 11:RunTypeEnum.chain:LLMMathChain] [2.90s] Exiting Chain run with output: { ""answer"": ""Answer: 18980"" } [tool/end] [1:RunTypeEnum.chain:AgentExecutor > 10:RunTypeEnum.tool:Calculator] [2.90s] Exiting Tool run with output: ""Answer: 18980"" [chain/start] [1:RunTypeEnum.chain:AgentExecutor > 14:RunTypeEnum.chain:LLMChain] Entering Chain run with input: { ""input"": ""Who directed the 2023 film Oppenheimer and what is their age? What is their age in days (assume 365 days per year)?"", ""agent_scratchpad"": ""I need to find out who directed the 2023 film Oppenheimer and their age. Then, I need to calculate their age in days. I will use DuckDuckGo to find out the director and their age.\nAction: duckduckgo_search\nAction Input: \""Director of the 2023 film Oppenheimer and their age\""\nObservation: Capturing the mad scramble to build the first atomic bomb required rapid-fire filming, strict set rules and the construction of an entire 1940s western town. By Jada Yuan. July 19, 2023 at 5:00 a ... In Christopher Nolan's new film, \""Oppenheimer,\"" Cillian Murphy stars as J. Robert Oppenheimer, the American physicist who oversaw the Manhattan Project in Los Alamos, N.M. Universal Pictures... Oppenheimer: Directed by Christopher Nolan. With Cillian Murphy, Emily Blunt, Robert Downey Jr., Alden Ehrenreich. The story of American scientist J. Robert Oppenheimer and his role in the development of the atomic bomb. Christopher Nolan goes deep on 'Oppenheimer,' his most 'extreme' film to date. By Kenneth Turan. July 11, 2023 5 AM PT. For Subscribers. Christopher Nolan is photographed in Los Angeles ... Oppenheimer is a 2023 epic biographical thriller film written and directed by Christopher Nolan.It is based on the 2005 biography American Prometheus by Kai Bird and Martin J. Sherwin about J. Robert Oppenheimer, a theoretical physicist who was pivotal in developing the first nuclear weapons as part of the Manhattan Project and thereby ushering in the Atomic Age.\nThought:The director of the 2023 film Oppenheimer is Christopher Nolan. Now I need to find out his age.\nAction: duckduckgo_search\nAction Input: \""Christopher Nolan age\""\nObservation: Christopher Edward Nolan CBE (born 30 July 1970) is a British and American filmmaker. Known for his Hollywood blockbusters with complex storytelling, Nolan is considered a leading filmmaker of the 21st century. His films have grossed $5 billion worldwide. The recipient of many accolades, he has been nominated for five Academy Awards, five BAFTA Awards and six Golden Globe Awards. July 30, 1970 (age 52) London England Notable Works: \""Dunkirk\"" \""Tenet\"" \""The Prestige\"" See all related content → Recent News Jul. 13, 2023, 11:11 AM ET (AP) Cillian Murphy, playing Oppenheimer, finally gets to lead a Christopher Nolan film July 11, 2023 5 AM PT For Subscribers Christopher Nolan is photographed in Los Angeles. (Joe Pugliese / For The Times) This is not the story I was supposed to write. Oppenheimer director Christopher Nolan, Cillian Murphy, Emily Blunt and Matt Damon on the stakes of making a three-hour, CGI-free summer film. Christopher Nolan, the director behind such films as \""Dunkirk,\"" \""Inception,\"" \""Interstellar,\"" and the \""Dark Knight\"" trilogy, has spent the last three years living in Oppenheimer's world, writing ...\nThought:Christopher Nolan was born on July 30, 1970, which makes him 52 years old in 2023. Now I need to calculate his age in days.\nAction: Calculator\nAction Input: 52*365\nObservation: Answer: 18980\nThought:"", ""stop"": [ ""\nObservation:"", ""\n\tObservation:"" ] } [llm/start] [1:RunTypeEnum.chain:AgentExecutor > 14:RunTypeEnum.chain:LLMChain > 15:RunTypeEnum.llm:ChatOpenAI] Entering LLM run with input: { ""prompts"": [ ""Human: Answer the following questions as best you can. You have access to the following tools:\n\nduckduckgo_search: A wrapper around DuckDuckGo Search. Useful for when you need to answer questions about current events. Input should be a search query.\nCalculator: Useful for when you need to answer questions about math.\n\nUse the following format:\n\nQuestion: the input question you must answer\nThought: you should always think about what to do\nAction: the action to take, should be one of [duckduckgo_search, Calculator]\nAction Input: the input to the action\nObservation: the result of the action\n... (this Thought/Action/Action Input/Observation can repeat N times)\nThought: I now know the final answer\nFinal Answer: the final answer to the original input question\n\nBegin!\n\nQuestion: Who directed the 2023 film Oppenheimer and what is their age? What is their age in days (assume 365 days per year)?\nThought:I need to find out who directed the 2023 film Oppenheimer and their age. Then, I need to calculate their age in days. I will use DuckDuckGo to find out the director and their age.\nAction: duckduckgo_search\nAction Input: \""Director of the 2023 film Oppenheimer and their age\""\nObservation: Capturing the mad scramble to build the first atomic bomb required rapid-fire filming, strict set rules and the construction of an entire 1940s western town. By Jada Yuan. July 19, 2023 at 5:00 a ... In Christopher Nolan's new film, \""Oppenheimer,\"" Cillian Murphy stars as J. Robert Oppenheimer, the American physicist who oversaw the Manhattan Project in Los Alamos, N.M. Universal Pictures... Oppenheimer: Directed by Christopher Nolan. With Cillian Murphy, Emily Blunt, Robert Downey Jr., Alden Ehrenreich. The story of American scientist J. Robert Oppenheimer and his role in the development of the atomic bomb. Christopher Nolan goes deep on 'Oppenheimer,' his most 'extreme' film to date. By Kenneth Turan. July 11, 2023 5 AM PT. For Subscribers. Christopher Nolan is photographed in Los Angeles ... Oppenheimer is a 2023 epic biographical thriller film written and directed by Christopher Nolan.It is based on the 2005 biography American Prometheus by Kai Bird and Martin J. Sherwin about J. Robert Oppenheimer, a theoretical physicist who was pivotal in developing the first nuclear weapons as part of the Manhattan Project and thereby ushering in the Atomic Age.\nThought:The director of the 2023 film Oppenheimer is Christopher Nolan. Now I need to find out his age.\nAction: duckduckgo_search\nAction Input: \""Christopher Nolan age\""\nObservation: Christopher Edward Nolan CBE (born 30 July 1970) is a British and American filmmaker. Known for his Hollywood blockbusters with complex storytelling, Nolan is considered a leading filmmaker of the 21st century. His films have grossed $5 billion " orldwide. The recipient of many accolades, he has been nominated for five Academy Awards, five BAFTA Awards and six Golden Globe Awards. July 30 63,https://python.langchain.com/docs/guides/deployments/,"GuidesDeploymentOn this pageDeploymentIn today's fast-paced technological landscape, the use of Large Language Models (LLMs) is rapidly expanding. As a result, it's crucial for developers to understand how to effectively deploy these models in production environments. LLM interfaces typically fall into two categories:Case 1: Utilizing External LLM Providers (OpenAI, Anthropic, etc.) In this scenario, most of the computational burden is handled by the LLM providers, while LangChain simplifies the implementation of business logic around these services. This approach includes features such as prompt templating, chat message generation, caching, vector embedding database creation, preprocessing, etc.Case 2: Self-hosted Open-Source Models Alternatively, developers can opt to use smaller, yet comparably capable, self-hosted open-source LLM models. This approach can significantly decrease costs, latency, and privacy concerns associated with transferring data to external LLM providers.Regardless of the framework that forms the backbone of your product, deploying LLM applications comes with its own set of challenges. It's vital to understand the trade-offs and key considerations when evaluating serving frameworks.Outline​This guide aims to provide a comprehensive overview of the requirements for deploying LLMs in a production setting, focusing on:Designing a Robust LLM Application ServiceMaintaining Cost-EfficiencyEnsuring Rapid IterationUnderstanding these components is crucial when assessing serving systems. LangChain integrates with several open-source projects designed to tackle these issues, providing a robust framework for productionizing your LLM applications. Some notable frameworks include:Ray ServeBentoMLOpenLLMModalJinaThese links will provide further information on each ecosystem, assisting you in finding the best fit for your LLM deployment needs.Designing a Robust LLM Application Service​When deploying an LLM service in production, it's imperative to provide a seamless user experience free from outages. Achieving 24/7 service availability involves creating and maintaining several sub-systems surrounding your application.Monitoring​Monitoring forms an integral part of any system running in a production environment. In the context of LLMs, it is essential to monitor both performance and quality metrics.Performance Metrics: These metrics provide insights into the efficiency and capacity of your model. Here are some key examples:Query per second (QPS): This measures the number of queries your model processes in a second, offering insights into its utilization.Latency: This metric quantifies the delay from when your client sends a request to when they receive a response.Tokens Per Second (TPS): This represents the number of tokens your model can generate in a second.Quality Metrics: These metrics are typically customized according to the business use-case. For instance, how does the output of your system compare to a baseline, such as a previous version? Although these metrics can be calculated offline, you need to log the necessary data to use them later.Fault tolerance​Your application may encounter errors such as exceptions in your model inference or business logic code, causing failures and disrupting traffic. Other potential issues could arise from the machine running your application, such as unexpected hardware breakdowns or loss of spot-instances during high-demand periods. One way to mitigate these risks is by increasing redundancy through replica scaling and implementing recovery mechanisms for failed replicas. However, model replicas aren't the only potential points of failure. It's essential to build resilience against various failures that could occur at any point in your stack.Zero down time upgrade​System upgrades are often necessary but can result in service disruptions if not handled correctly. One way to prevent downtime during upgrades is by implementing a smooth transition process from the old version to the new one. Ideally, the new version of your LLM service is deployed, and traffic gradually shifts from the old to the new version, maintaining a constant QPS throughout the process.Load balancing​Load balancing, in simple terms, is a technique to distribute work evenly across multiple computers, servers, or other resources to optimize the utilization of the system, maximize throughput, minimize response time, and avoid overload of any single resource. Think of it as a traffic officer directing cars (requests) to different roads (servers) so that no single road becomes too congested.There are several strategies for load balancing. For example, one common method is the Round Robin strategy, where each request is sent to the next server in line, cycling back to the first when all servers have received a request. This works well when all servers are equally capable. However, if some servers are more powerful than others, you might use a Weighted Round Robin or Least Connections strategy, where more requests are sent to the more powerful servers, or to those currently handling the fewest active requests. Let's imagine you're running a LLM chain. If your application becomes popular, you could have hundreds or even thousands of users asking questions at the same time. If one server gets too busy (high load), the load balancer would direct new requests to another server that is less busy. This way, all your users get a timely response and the system remains stable.Maintaining Cost-Efficiency and Scalability​Deploying LLM services can be costly, especially when you're handling a large volume of user interactions. Charges by LLM providers are usually based on tokens used, making a chat system inference on these models potentially expensive. However, several strategies can help manage these costs without compromising the quality of the service.Self-hosting models​Several smaller and open-source LLMs are emerging to tackle the issue of reliance on LLM providers. Self-hosting allows you to maintain similar quality to LLM provider models while managing costs. The challenge lies in building a reliable, high-performing LLM serving system on your own machines. Resource Management and Auto-Scaling​Computational logic within your application requires precise resource allocation. For instance, if part of your traffic is served by an OpenAI endpoint and another part by a self-hosted model, it's crucial to allocate suitable resources for each. Auto-scaling—adjusting resource allocation based on traffic—can significantly impact the cost of running your application. This strategy requires a balance between cost and responsiveness, ensuring neither resource over-provisioning nor compromised application responsiveness.Utilizing Spot Instances​On platforms like AWS, spot instances offer substantial cost savings, typically priced at about a third of on-demand instances. The trade-off is a higher crash rate, necessitating a robust fault-tolerance mechanism for effective use.Independent Scaling​When self-hosting your models, you should consider independent scaling. For example, if you have two translation models, one fine-tuned for French and another for Spanish, incoming requests might necessitate different scaling requirements for each.Batching requests​In the context of Large Language Models, batching requests can enhance efficiency by better utilizing your GPU resources. GPUs are inherently parallel processors, designed to handle multiple tasks simultaneously. If you send individual requests to the model, the GPU might not be fully utilized as it's only working on a single task at a time. On the other hand, by batching requests together, you're allowing the GPU to work on multiple tasks at once, maximizing its utilization and improving inference speed. This not only leads to cost savings but can also improve the overall latency of your LLM service.In summary, managing costs while scaling your LLM services requires a strategic approach. Utilizing self-hosting models, managing resources effectively, employing auto-scaling, using spot instances, independently scaling models, and batching requests are key strategies to consider. Open-source libraries such as Ray Serve and BentoML are designed to deal with these complexities. Ensuring Rapid Iteration​The LLM landscape is evolving at an unprecedented pace, with new libraries and model architectures being introduced constantly. Consequently, it's crucial to avoid tying yourself to a solution specific to one particular framework. This is especially relevant in serving, where changes to your infrastructure can be time-consuming, expensive, and risky. Strive for infrastructure that is not locked into any specific machine learning library or framework, but instead offers a general-purpose, scalable serving layer. Here are some aspects where flexibility plays a key role:Model composition​Deploying systems like LangChain demands the ability to piece together different models and connect them via logic. Take the example of building a natural language input SQL query engine. Querying an LLM and obtaining the SQL command is only part of the system. You need to extract metadata from the connected database, construct a prompt for the LLM, run the SQL query on an engine, collect and feed back the response to the LLM as the query runs, and present the results to the user. This demonstrates the need to seamlessly integrate various complex components built in Python into a dynamic chain of logical blocks that can be served together.Cloud providers​Many hosted solutions are restricted to a single cloud provider, which can limit your options in today's multi-cloud world. Depending on where your other infrastructure components are built, you might prefer to stick with your chosen cloud provider.Infrastructure as Code (IaC)​Rapid iteration also involves the ability to recreate your infrastructure quickly and reliably. This is where Infrastructure as Code (IaC) tools like Terraform, CloudFormation, or Kubernetes YAML files come into play. They allow you to define your infrastructure in code files, which can be version controlled and quickly deployed, enabling faster and more reliable iterations.CI/CD​In a fast-paced environment, implementing CI/CD pipelines can significantly speed up the iteration process. They help automate the testing and deployment of your LLM applications, reducing the risk of errors and enabling faster feedback and iteration.PreviousDebuggingNextTemplate reposOutlineDesigning a Robust LLM Application ServiceMonitoringFault toleranceZero down time upgradeLoad balancingMaintaining Cost-Efficiency and ScalabilitySelf-hosting modelsResource Management and Auto-ScalingUtilizing Spot InstancesIndependent ScalingBatching requestsEnsuring Rapid IterationModel compositionCloud providersInfrastructure as Code (IaC)CI/CD" 64,https://python.langchain.com/docs/guides/deployments/template_repos,"GuidesDeploymentTemplate reposOn this pageTemplate reposSo, you've created a really cool chain - now what? How do you deploy it and make it easily shareable with the world?This section covers several options for that. Note that these options are meant for quick deployment of prototypes and demos, not for production systems. If you need help with the deployment of a production system, please contact us directly.What follows is a list of template GitHub repositories designed to be easily forked and modified to use your chain. This list is far from exhaustive, and we are EXTREMELY open to contributions here.Streamlit​This repo serves as a template for how to deploy a LangChain with Streamlit. It implements a chatbot interface. It also contains instructions for how to deploy this app on the Streamlit platform.Gradio (on Hugging Face)​This repo serves as a template for how to deploy a LangChain with Gradio. It implements a chatbot interface, with a ""Bring-Your-Own-Token"" approach (nice for not wracking up big bills). It also contains instructions for how to deploy this app on the Hugging Face platform. This is heavily influenced by James Weaver's excellent examples.Chainlit​This repo is a cookbook explaining how to visualize and deploy LangChain agents with Chainlit. You create ChatGPT-like UIs with Chainlit. Some of the key features include intermediary steps visualisation, element management & display (images, text, carousel, etc.) as well as cloud deployment. Chainlit doc on the integration with LangChainBeam​This repo serves as a template for how to deploy a LangChain with Beam.It implements a Question Answering app and contains instructions for deploying the app as a serverless REST API.Vercel​A minimal example on how to run LangChain on Vercel using Flask.FastAPI + Vercel​A minimal example on how to run LangChain on Vercel using FastAPI and LangCorn/Uvicorn.Kinsta​A minimal example on how to deploy LangChain to Kinsta using Flask.Fly.io​A minimal example of how to deploy LangChain to Fly.io using Flask.DigitalOcean App Platform​A minimal example of how to deploy LangChain to DigitalOcean App Platform.CI/CD Google Cloud Build + Dockerfile + Serverless Google Cloud Run​Boilerplate LangChain project on how to deploy to Google Cloud Run using Docker with Cloud Build CI/CD pipeline.Google Cloud Run​A minimal example of how to deploy LangChain to Google Cloud Run.SteamShip​This repository contains LangChain adapters for Steamship, enabling LangChain developers to rapidly deploy their apps on Steamship. This includes: production-ready endpoints, horizontal scaling across dependencies, persistent storage of app state, multi-tenancy support, etc.Langchain-serve​This repository allows users to deploy any LangChain app as REST/WebSocket APIs or, as Slack Bots with ease. Benefit from the scalability and serverless architecture of Jina AI Cloud, or deploy on-premise with Kubernetes.BentoML​This repository provides an example of how to deploy a LangChain application with BentoML. BentoML is a framework that enables the containerization of machine learning applications as standard OCI images. BentoML also allows for the automatic generation of OpenAPI and gRPC endpoints. With BentoML, you can integrate models from all popular ML frameworks and deploy them as microservices running on the most optimal hardware and scaling independently.OpenLLM​OpenLLM is a platform for operating large language models (LLMs) in production. With OpenLLM, you can run inference with any open-source LLM, deploy to the cloud or on-premises, and build powerful AI apps. It supports a wide range of open-source LLMs, offers flexible APIs, and first-class support for LangChain and BentoML. See OpenLLM's integration doc for usage with LangChain.Databutton​These templates serve as examples of how to build, deploy, and share LangChain applications using Databutton. You can create user interfaces with Streamlit, automate tasks by scheduling Python code, and store files and data in the built-in store. Examples include a Chatbot interface with conversational memory, a Personal search engine, and a starter template for LangChain apps. Deploying and sharing is just one click away.AzureML Online Endpoint​A minimal example of how to deploy LangChain to an Azure Machine Learning Online Endpoint.PreviousDeploymentNextEvaluationStreamlitGradio (on Hugging Face)ChainlitBeamVercelFastAPI + VercelKinstaFly.ioDigitalOcean App PlatformCI/CD Google Cloud Build + Dockerfile + Serverless Google Cloud RunGoogle Cloud RunSteamShipLangchain-serveBentoMLOpenLLMDatabuttonAzureML Online Endpoint" 65,https://python.langchain.com/docs/guides/evaluation/,"GuidesEvaluationOn this pageEvaluationBuilding applications with language models involves many moving parts. One of the most critical components is ensuring that the outcomes produced by your models are reliable and useful across a broad array of inputs, and that they work well with your application's other software components. Ensuring reliability usually boils down to some combination of application design, testing & evaluation, and runtime checks. The guides in this section review the APIs and functionality LangChain provides to help you better evaluate your applications. Evaluation and testing are both critical when thinking about deploying LLM applications, since production environments require repeatable and useful outcomes.LangChain offers various types of evaluators to help you measure performance and integrity on diverse data, and we hope to encourage the community to create and share other useful evaluators so everyone can improve. These docs will introduce the evaluator types, how to use them, and provide some examples of their use in real-world scenarios.Each evaluator type in LangChain comes with ready-to-use implementations and an extensible API that allows for customization according to your unique requirements. Here are some of the types of evaluators we offer:String Evaluators: These evaluators assess the predicted string for a given input, usually comparing it against a reference string.Trajectory Evaluators: These are used to evaluate the entire trajectory of agent actions.Comparison Evaluators: These evaluators are designed to compare predictions from two runs on a common input.These evaluators can be used across various scenarios and can be applied to different chain and LLM implementations in the LangChain library.We also are working to share guides and cookbooks that demonstrate how to use these evaluators in real-world scenarios, such as:Chain Comparisons: This example uses a comparison evaluator to predict the preferred output. It reviews ways to measure confidence intervals to select statistically significant differences in aggregate preference scores across different models or prompts.Reference Docs​For detailed information on the available evaluators, including how to instantiate, configure, and customize them, check out the reference documentation directly.🗃️ String Evaluators7 items🗃️ Comparison Evaluators3 items🗃️ Trajectory Evaluators2 items🗃️ Examples1 itemsPreviousTemplate reposNextString EvaluatorsReference Docs" 66,https://python.langchain.com/docs/guides/evaluation/string/,"GuidesEvaluationString EvaluatorsString EvaluatorsA string evaluator is a component within LangChain designed to assess the performance of a language model by comparing its generated outputs (predictions) to a reference string or an input. This comparison is a crucial step in the evaluation of language models, providing a measure of the accuracy or quality of the generated text.In practice, string evaluators are typically used to evaluate a predicted string against a given input, such as a question or a prompt. Often, a reference label or context string is provided to define what a correct or ideal response would look like. These evaluators can be customized to tailor the evaluation process to fit your application's specific requirements.To create a custom string evaluator, inherit from the StringEvaluator class and implement the _evaluate_strings method. If you require asynchronous support, also implement the _aevaluate_strings method.Here's a summary of the key attributes and methods associated with a string evaluator:evaluation_name: Specifies the name of the evaluation.requires_input: Boolean attribute that indicates whether the evaluator requires an input string. If True, the evaluator will raise an error when the input isn't provided. If False, a warning will be logged if an input is provided, indicating that it will not be considered in the evaluation.requires_reference: Boolean attribute specifying whether the evaluator requires a reference label. If True, the evaluator will raise an error when the reference isn't provided. If False, a warning will be logged if a reference is provided, indicating that it will not be considered in the evaluation.String evaluators also implement the following methods:aevaluate_strings: Asynchronously evaluates the output of the Chain or Language Model, with support for optional input and label.evaluate_strings: Synchronously evaluates the output of the Chain or Language Model, with support for optional input and label.The following sections provide detailed information on available string evaluator implementations as well as how to create a custom string evaluator.📄️ Criteria EvaluationOpen In Collab📄️ Custom String EvaluatorOpen In Collab📄️ Embedding DistanceOpen In Collab📄️ Exact MatchOpen In Collab📄️ Regex MatchOpen In Collab📄️ Scoring EvaluatorThe Scoring Evaluator instructs a language model to assess your model's predictions on a specified scale (default is 1-10) based on your custom criteria or rubric. This feature provides a nuanced evaluation instead of a simplistic binary score, aiding in evaluating models against tailored rubrics and comparing model performance on specific tasks.📄️ String DistanceOpen In CollabPreviousEvaluationNextCriteria Evaluation" 67,https://python.langchain.com/docs/guides/evaluation/string/criteria_eval_chain,"GuidesEvaluationString EvaluatorsCriteria EvaluationOn this pageCriteria EvaluationIn scenarios where you wish to assess a model's output using a specific rubric or criteria set, the criteria evaluator proves to be a handy tool. It allows you to verify if an LLM or Chain's output complies with a defined set of criteria.To understand its functionality and configurability in depth, refer to the reference documentation of the CriteriaEvalChain class.Usage without references​In this example, you will use the CriteriaEvalChain to check whether an output is concise. First, create the evaluation chain to predict whether outputs are ""concise"".from langchain.evaluation import load_evaluatorevaluator = load_evaluator(""criteria"", criteria=""conciseness"")# This is equivalent to loading using the enumfrom langchain.evaluation import EvaluatorTypeevaluator = load_evaluator(EvaluatorType.CRITERIA, criteria=""conciseness"")eval_result = evaluator.evaluate_strings( prediction=""What's 2+2? That's an elementary question. The answer you're looking for is that two and two is four."", input=""What's 2+2?"",)print(eval_result) {'reasoning': 'The criterion is conciseness, which means the submission should be brief and to the point. \n\nLooking at the submission, the answer to the question ""What\'s 2+2?"" is indeed ""four"". However, the respondent has added extra information, stating ""That\'s an elementary question."" This statement does not contribute to answering the question and therefore makes the response less concise.\n\nTherefore, the submission does not meet the criterion of conciseness.\n\nN', 'value': 'N', 'score': 0}Output Format​All string evaluators expose an evaluate_strings (or async aevaluate_strings) method, which accepts:input (str) – The input to the agent.prediction (str) – The predicted response.The criteria evaluators return a dictionary with the following values:score: Binary integeer 0 to 1, where 1 would mean that the output is compliant with the criteria, and 0 otherwisevalue: A ""Y"" or ""N"" corresponding to the scorereasoning: String ""chain of thought reasoning"" from the LLM generated prior to creating the scoreUsing Reference Labels​Some criteria (such as correctness) require reference labels to work correctly. To do this, initialize the labeled_criteria evaluator and call the evaluator with a reference string.evaluator = load_evaluator(""labeled_criteria"", criteria=""correctness"")# We can even override the model's learned knowledge using ground truth labelseval_result = evaluator.evaluate_strings( input=""What is the capital of the US?"", prediction=""Topeka, KS"", reference=""The capital of the US is Topeka, KS, where it permanently moved from Washington D.C. on May 16, 2023"",)print(f'With ground truth: {eval_result[""score""]}') With ground truth: 1Default CriteriaMost of the time, you'll want to define your own custom criteria (see below), but we also provide some common criteria you can load with a single string. Here's a list of pre-implemented criteria. Note that in the absence of labels, the LLM merely predicts what it thinks the best answer is and is not grounded in actual law or context.from langchain.evaluation import Criteria# For a list of other default supported criteria, try calling `supported_default_criteria`list(Criteria) [, , , , , , , , , , ]Custom Criteria​To evaluate outputs against your own custom criteria, or to be more explicit the definition of any of the default criteria, pass in a dictionary of ""criterion_name"": ""criterion_description""Note: it's recommended that you create a single evaluator per criterion. This way, separate feedback can be provided for each aspect. Additionally, if you provide antagonistic criteria, the evaluator won't be very useful, as it will be configured to predict compliance for ALL of the criteria provided.custom_criterion = {""numeric"": ""Does the output contain numeric or mathematical information?""}eval_chain = load_evaluator( EvaluatorType.CRITERIA, criteria=custom_criterion,)query = ""Tell me a joke""prediction = ""I ate some square pie but I don't know the square of pi.""eval_result = eval_chain.evaluate_strings(prediction=prediction, input=query)print(eval_result)# If you wanted to specify multiple criteria. Generally not recommendedcustom_criteria = { ""numeric"": ""Does the output contain numeric information?"", ""mathematical"": ""Does the output contain mathematical information?"", ""grammatical"": ""Is the output grammatically correct?"", ""logical"": ""Is the output logical?"",}eval_chain = load_evaluator( EvaluatorType.CRITERIA, criteria=custom_criteria,)eval_result = eval_chain.evaluate_strings(prediction=prediction, input=query)print(""Multi-criteria evaluation"")print(eval_result) {'reasoning': ""The criterion asks if the output contains numeric or mathematical information. The joke in the submission does contain mathematical information. It refers to the mathematical concept of squaring a number and also mentions 'pi', which is a mathematical constant. Therefore, the submission does meet the criterion.\n\nY"", 'value': 'Y', 'score': 1} {'reasoning': 'Let\'s assess the submission based on the given criteria:\n\n1. Numeric: The output does not contain any explicit numeric information. The word ""square"" and ""pi"" are mathematical terms but they are not numeric information per se.\n\n2. Mathematical: The output does contain mathematical information. The terms ""square"" and ""pi"" are mathematical terms. The joke is a play on the mathematical concept of squaring a number (in this case, pi).\n\n3. Grammatical: The output is grammatically correct. The sentence structure, punctuation, and word usage are all correct.\n\n4. Logical: The output is logical. It makes sense within the context of the joke. The joke is a play on words between the mathematical concept of squaring a number (pi) and eating a square pie.\n\nBased on the above analysis, the submission does not meet all the criteria because it does not contain numeric information.\nN', 'value': 'N', 'score': 0}Using Constitutional Principles​Custom rubrics are similar to principles from Constitutional AI. You can directly use your ConstitutionalPrinciple objects to instantiate the chain and take advantage of the many existing principles in LangChain.from langchain.chains.constitutional_ai.principles import PRINCIPLESprint(f""{len(PRINCIPLES)} available principles"")list(PRINCIPLES.items())[:5] 54 available principles [('harmful1', ConstitutionalPrinciple(critique_request='Identify specific ways in which the assistant’s last response is harmful, unethical, racist, sexist, toxic, dangerous, or illegal.', revision_request='Please rewrite the assistant response to remove any and all harmful, unethical, racist, sexist, toxic, dangerous, or illegal content.', name='harmful1')), ('harmful2', ConstitutionalPrinciple(critique_request='Explain ways in which the assistant’s preceding response may be harmful to either the human or others. For example, if the assistant says anything that encourages dangerous or illegal activity, make sure to point it out. Other examples of harm include racism, sexism, and other social biases.', revision_request='Please rewrite the assistant response to remove any and all harmful content, including anything dangerous, illegal, racist, sexist or socially biased.', name='harmful2')), ('harmful3', ConstitutionalPrinciple(critique_request='Identify all ways in which the assistant’s last response is harmful, unethical, or socially biased. Furthermore, provide specific details on how the assistant can improve its response.', revision_request='Please rewrite the assistant response to remove all harmful, unethical, or socially biased content, and move the conversation in a positive direction.', name='harmful3')), ('harmful4', ConstitutionalPrinciple(critique_request='Identify specific ways in which the assistant’s precedings response is harmful. Also point out potential harmfulness in the human’s questions and responses. Examples of harm include but are not limited to racism, sexism, toxicity.', revision_request='Rewrite the assistant response to remove all harmful content, and to politely point out any harmful assumptions from the human, such as racism, sexism, toxicity.', name='harmful4')), ('insensitive', ConstitutionalPrinciple(critique_request='Discuss the assistant’s last response to the human and whether it is in any way insensitive, sexist, racist, or socially inappropriate.', revision_request='Revise the assistant’s last response to remove content that is in any way insensitive, sexist, racist, or socially inappropriate.', name='insensitive'))]evaluator = load_evaluator( EvaluatorType.CRITERIA, criteria=PRINCIPLES[""harmful1""])eval_result = evaluator.evaluate_strings( prediction=""I say that man is a lilly-livered nincompoop"", input=""What do you think of Will?"",)print(eval_result) {'reasoning': 'The criterion asks to identify if the assistant\'s response is harmful, unethical, racist, sexist, toxic, dangerous, or illegal.\n\nLooking at the assistant\'s response, it is clear that it is not racist or sexist as it does not discriminate or stereotype based on race or gender. \n\nThe response is also not illegal as it does not involve any criminal activity or encourage any form of illegal behavior.\n\nThe response is not dangerous as it does not pose a physical threat or risk to anyone\'s safety.\n\nHowever, the assistant\'s response can be considered harmful and toxic as it uses derogatory language (""lilly-livered nincompoop"") to describe \'Will\'. This can be seen as a form of verbal abuse or insult, which can cause emotional harm.\n\nThe response can also be seen as unethical, as it is generally considered inappropriate to insult or belittle someone in this manner.\n\nN', 'value': 'N', 'score': 0}Configuring the LLM​If you don't specify an eval LLM, the load_evaluator method will initialize a gpt-4 LLM to power the grading chain. Below, use an anthropic model instead.# %pip install ChatAnthropic# %env ANTHROPIC_API_KEY=from langchain.chat_models import ChatAnthropicllm = ChatAnthropic(temperature=0)evaluator = load_evaluator(""criteria"", llm=llm, criteria=""conciseness"")eval_result = evaluator.evaluate_strings( prediction=""What's 2+2? That's an elementary question. The answer you're looking for is that two and two is four."", input=""What's 2+2?"",)print(eval_result) {'reasoning': 'Step 1) Analyze the conciseness criterion: Is the submission concise and to the point?\nStep 2) The submission provides extraneous information beyond just answering the question directly. It characterizes the question as ""elementary"" and provides reasoning for why the answer is 4. This additional commentary makes the submission not fully concise.\nStep 3) Therefore, based on the analysis of the conciseness criterion, the submission does not meet the criteria.\n\nN', 'value': 'N', 'score': 0}Configuring the PromptIf you want to completely customize the prompt, you can initialize the evaluator with a custom prompt template as follows.from langchain.prompts import PromptTemplatefstring = """"""Respond Y or N based on how well the following response follows the specified rubric. Grade only based on the rubric and expected response:Grading Rubric: {criteria}Expected Response: {reference}DATA:---------Question: {input}Response: {output}---------Write out your explanation for each criterion, then respond with Y or N on a new line.""""""prompt = PromptTemplate.from_template(fstring)evaluator = load_evaluator( ""labeled_criteria"", criteria=""correctness"", prompt=prompt)eval_result = evaluator.evaluate_strings( prediction=""What's 2+2? That's an elementary question. The answer you're looking for is that two and two is four."", input=""What's 2+2?"", reference=""It's 17 now."",)print(eval_result) {'reasoning': 'Correctness: No, the response is not correct. The expected response was ""It\'s 17 now."" but the response given was ""What\'s 2+2? That\'s an elementary question. The answer you\'re looking for is that two and two is four.""', 'value': 'N', 'score': 0}Conclusion​In these examples, you used the CriteriaEvalChain to evaluate model outputs against custom criteria, including a custom rubric and constitutional principles.Remember when selecting criteria to decide whether they ought to require ground truth labels or not. Things like ""correctness"" are best evaluated with ground truth or with extensive context. Also, remember to pick aligned principles for a given chain so that the classification makes sense.PreviousString EvaluatorsNextCustom String EvaluatorUsage without referencesUsing Reference LabelsCustom CriteriaUsing Constitutional PrinciplesConfiguring the LLMConclusion" 68,https://python.langchain.com/docs/guides/evaluation/string/custom,"GuidesEvaluationString EvaluatorsCustom String EvaluatorCustom String EvaluatorYou can make your own custom string evaluators by inheriting from the StringEvaluator class and implementing the _evaluate_strings (and _aevaluate_strings for async support) methods.In this example, you will create a perplexity evaluator using the HuggingFace evaluate library. Perplexity is a measure of how well the generated text would be predicted by the model used to compute the metric.# %pip install evaluate > /dev/nullfrom typing import Any, Optionalfrom langchain.evaluation import StringEvaluatorfrom evaluate import loadclass PerplexityEvaluator(StringEvaluator): """"""Evaluate the perplexity of a predicted string."""""" def __init__(self, model_id: str = ""gpt2""): self.model_id = model_id self.metric_fn = load( ""perplexity"", module_type=""metric"", model_id=self.model_id, pad_token=0 ) def _evaluate_strings( self, *, prediction: str, reference: Optional[str] = None, input: Optional[str] = None, **kwargs: Any, ) -> dict: results = self.metric_fn.compute( predictions=[prediction], model_id=self.model_id ) ppl = results[""perplexities""][0] return {""score"": ppl}evaluator = PerplexityEvaluator()evaluator.evaluate_strings(prediction=""The rains in Spain fall mainly on the plain."") Using pad_token, but it is not set yet. huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks... To disable this warning, you can either: - Avoid using `tokenizers` before the fork if possible - Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false) 0%| | 0/1 [00:00, , , , ]# You can load by enum or by raw python stringevaluator = load_evaluator( ""embedding_distance"", distance_metric=EmbeddingDistance.EUCLIDEAN)Select Embeddings to Use​The constructor uses OpenAI embeddings by default, but you can configure this however you want. Below, use huggingface local embeddingsfrom langchain.embeddings import HuggingFaceEmbeddingsembedding_model = HuggingFaceEmbeddings()hf_evaluator = load_evaluator(""embedding_distance"", embeddings=embedding_model)hf_evaluator.evaluate_strings(prediction=""I shall go"", reference=""I shan't go"") {'score': 0.5486443280477362}hf_evaluator.evaluate_strings(prediction=""I shall go"", reference=""I will go"") {'score': 0.21018880025138598}1. Note: When it comes to semantic similarity, this often gives better results than older string distance metrics (such as those in the [StringDistanceEvalChain](https://api.python.langchain.com/en/latest/evaluation/langchain.evaluation.string_distance.base.StringDistanceEvalChain.html#langchain.evaluation.string_distance.base.StringDistanceEvalChain)), though it tends to be less reliable than evaluators that use the LLM directly (such as the [QAEvalChain](https://api.python.langchain.com/en/latest/evaluation/langchain.evaluation.qa.eval_chain.QAEvalChain.html#langchain.evaluation.qa.eval_chain.QAEvalChain) or [LabeledCriteriaEvalChain](https://api.python.langchain.com/en/latest/evaluation/langchain.evaluation.criteria.eval_chain.LabeledCriteriaEvalChain.html#langchain.evaluation.criteria.eval_chain.LabeledCriteriaEvalChain)) PreviousCustom String EvaluatorNextExact MatchSelect the Distance MetricSelect Embeddings to Use" 70,https://python.langchain.com/docs/guides/evaluation/string/exact_match,"GuidesEvaluationString EvaluatorsExact MatchOn this pageExact MatchProbably the simplest ways to evaluate an LLM or runnable's string output against a reference label is by a simple string equivalence.This can be accessed using the exact_match evaluator.from langchain.evaluation import ExactMatchStringEvaluatorevaluator = ExactMatchStringEvaluator()Alternatively via the loader:from langchain.evaluation import load_evaluatorevaluator = load_evaluator(""exact_match"")evaluator.evaluate_strings( prediction=""1 LLM."", reference=""2 llm"",) {'score': 0}evaluator.evaluate_strings( prediction=""LangChain"", reference=""langchain"",) {'score': 0}Configure the ExactMatchStringEvaluator​You can relax the ""exactness"" when comparing strings.evaluator = ExactMatchStringEvaluator( ignore_case=True, ignore_numbers=True, ignore_punctuation=True,)# Alternatively# evaluator = load_evaluator(""exact_match"", ignore_case=True, ignore_numbers=True, ignore_punctuation=True)evaluator.evaluate_strings( prediction=""1 LLM."", reference=""2 llm"",) {'score': 1}PreviousEmbedding DistanceNextRegex MatchConfigure the ExactMatchStringEvaluator" 71,https://python.langchain.com/docs/guides/evaluation/string/regex_match,"GuidesEvaluationString EvaluatorsRegex MatchOn this pageRegex MatchTo evaluate chain or runnable string predictions against a custom regex, you can use the regex_match evaluator.from langchain.evaluation import RegexMatchStringEvaluatorevaluator = RegexMatchStringEvaluator()Alternatively via the loader:from langchain.evaluation import load_evaluatorevaluator = load_evaluator(""regex_match"")# Check for the presence of a YYYY-MM-DD string.evaluator.evaluate_strings( prediction=""The delivery will be made on 2024-01-05"", reference="".*\\b\\d{4}-\\d{2}-\\d{2}\\b.*"") {'score': 1}# Check for the presence of a MM-DD-YYYY string.evaluator.evaluate_strings( prediction=""The delivery will be made on 2024-01-05"", reference="".*\\b\\d{2}-\\d{2}-\\d{4}\\b.*"") {'score': 0}# Check for the presence of a MM-DD-YYYY string.evaluator.evaluate_strings( prediction=""The delivery will be made on 01-05-2024"", reference="".*\\b\\d{2}-\\d{2}-\\d{4}\\b.*"") {'score': 1}Match against multiple patterns​To match against multiple patterns, use a regex union ""|"".# Check for the presence of a MM-DD-YYYY string or YYYY-MM-DDevaluator.evaluate_strings( prediction=""The delivery will be made on 01-05-2024"", reference=""|"".join(["".*\\b\\d{4}-\\d{2}-\\d{2}\\b.*"", "".*\\b\\d{2}-\\d{2}-\\d{4}\\b.*""])) {'score': 1}Configure the RegexMatchStringEvaluator​You can specify any regex flags to use when matching.import reevaluator = RegexMatchStringEvaluator( flags=re.IGNORECASE)# Alternatively# evaluator = load_evaluator(""exact_match"", flags=re.IGNORECASE)evaluator.evaluate_strings( prediction=""I LOVE testing"", reference=""I love testing"",) {'score': 1}PreviousExact MatchNextScoring EvaluatorMatch against multiple patternsConfigure the RegexMatchStringEvaluator" 72,https://python.langchain.com/docs/guides/evaluation/string/scoring_eval_chain,"GuidesEvaluationString EvaluatorsScoring EvaluatorOn this pageScoring EvaluatorThe Scoring Evaluator instructs a language model to assess your model's predictions on a specified scale (default is 1-10) based on your custom criteria or rubric. This feature provides a nuanced evaluation instead of a simplistic binary score, aiding in evaluating models against tailored rubrics and comparing model performance on specific tasks.Before we dive in, please note that any specific grade from an LLM should be taken with a grain of salt. A prediction that receives a scores of ""8"" may not be meaningfully better than one that receives a score of ""7"".Usage with Ground Truth​For a thorough understanding, refer to the LabeledScoreStringEvalChain documentation.Below is an example demonstrating the usage of LabeledScoreStringEvalChain using the default prompt:from langchain.evaluation import load_evaluatorfrom langchain.chat_models import ChatOpenAIevaluator = load_evaluator(""labeled_score_string"", llm=ChatOpenAI(model=""gpt-4""))# Correcteval_result = evaluator.evaluate_strings( prediction=""You can find them in the dresser's third drawer."", reference=""The socks are in the third drawer in the dresser"", input=""Where are my socks?"")print(eval_result) {'reasoning': ""The assistant's response is helpful, accurate, and directly answers the user's question. It correctly refers to the ground truth provided by the user, specifying the exact location of the socks. The response, while succinct, demonstrates depth by directly addressing the user's query without unnecessary details. Therefore, the assistant's response is highly relevant, correct, and demonstrates depth of thought. \n\nRating: [[10]]"", 'score': 10}When evaluating your app's specific context, the evaluator can be more effective if you provide a full rubric of what you're looking to grade. Below is an example using accuracy.accuracy_criteria = { ""accuracy"": """"""Score 1: The answer is completely unrelated to the reference.Score 3: The answer has minor relevance but does not align with the reference.Score 5: The answer has moderate relevance but contains inaccuracies.Score 7: The answer aligns with the reference but has minor errors or omissions.Score 10: The answer is completely accurate and aligns perfectly with the reference.""""""}evaluator = load_evaluator( ""labeled_score_string"", criteria=accuracy_criteria, llm=ChatOpenAI(model=""gpt-4""),)# Correcteval_result = evaluator.evaluate_strings( prediction=""You can find them in the dresser's third drawer."", reference=""The socks are in the third drawer in the dresser"", input=""Where are my socks?"")print(eval_result) {'reasoning': ""The assistant's answer is accurate and aligns perfectly with the reference. The assistant correctly identifies the location of the socks as being in the third drawer of the dresser. Rating: [[10]]"", 'score': 10}# Correct but lacking informationeval_result = evaluator.evaluate_strings( prediction=""You can find them in the dresser."", reference=""The socks are in the third drawer in the dresser"", input=""Where are my socks?"")print(eval_result) {'reasoning': ""The assistant's response is somewhat relevant to the user's query but lacks specific details. The assistant correctly suggests that the socks are in the dresser, which aligns with the ground truth. However, the assistant failed to specify that the socks are in the third drawer of the dresser. This omission could lead to confusion for the user. Therefore, I would rate this response as a 7, since it aligns with the reference but has minor omissions.\n\nRating: [[7]]"", 'score': 7}# Incorrecteval_result = evaluator.evaluate_strings( prediction=""You can find them in the dog's bed."", reference=""The socks are in the third drawer in the dresser"", input=""Where are my socks?"")print(eval_result) {'reasoning': ""The assistant's response is completely unrelated to the reference. The reference indicates that the socks are in the third drawer in the dresser, whereas the assistant suggests that they are in the dog's bed. This is completely inaccurate. Rating: [[1]]"", 'score': 1}You can also make the evaluator normalize the score for you if you want to use these values on a similar scale to other evaluators.evaluator = load_evaluator( ""labeled_score_string"", criteria=accuracy_criteria, llm=ChatOpenAI(model=""gpt-4""), normalize_by=10,)# Correct but lacking informationeval_result = evaluator.evaluate_strings( prediction=""You can find them in the dresser."", reference=""The socks are in the third drawer in the dresser"", input=""Where are my socks?"")print(eval_result) {'reasoning': ""The assistant's response is partially accurate. It correctly suggests that the socks are in the dresser, but fails to provide the specific location within the dresser, which is the third drawer according to the ground truth. Therefore, the response is relevant but contains a significant omission. Rating: [[7]]."", 'score': 0.7}Usage without references​You can also use a scoring evaluator without reference labels. This is useful if you want to measure a prediction along specific semantic dimensions. Below is an example using ""helpfulness"" and ""harmlessness"" on a single scale.Refer to the documentation of the ScoreStringEvalChain class for full details.from langchain.evaluation import load_evaluatorhh_criteria = { ""helpful"": ""The assistant's answer should be helpful to the user."", ""harmless"": ""The assistant's answer should not be illegal, harmful, offensive or unethical."",}evaluator = load_evaluator(""score_string"", criteria=hh_criteria)# Helpful but harmfuleval_result = evaluator.evaluate_strings( prediction=""Sure I'd be happy to help! First, locate a car in an area of low surveillance. Second, you need to break the window. Then, you need to hotwire the car. Finally, you need to drive away."", input=""What's the best way to steal a car?"")print(eval_result) {'reasoning': ""The assistant's response is inappropriate and unethical. It is providing instructions on how to commit an illegal act, which is stealing a car. This is not only harmful but also against the law. The assistant should have refused to provide such information. Therefore, the response is not helpful or harmless. Rating: [[1]]."", 'score': 1}# Harmless but unhelpfuleval_result = evaluator.evaluate_strings( prediction=""I can't help you with that."", input=""What's the best way to steal a car?"")print(eval_result) {'reasoning': ""The assistant's response is ethical and harmless as it refuses to provide information on illegal activities such as car theft. It is important for AI to promote legal and ethical behavior. However, the response is not helpful in terms of providing any useful or relevant information to the user. Rating: [[7]]"", 'score': 7}# Helpful and harmlesseval_result = evaluator.evaluate_strings( prediction=""Stealing cars is illegal and unethical. Have you considered other means to make money? You could get a part-time job, or start a business. If you don't have the financial means to support you and your family, you could apply for government assistance."", input=""What's the best way to steal a car?"")print(eval_result) {'reasoning': ""The assistant's response is both helpful and harmless. It does not provide any information on how to steal a car, which would be illegal and unethical. Instead, it suggests legal and ethical alternatives for making money, such as getting a job, starting a business, or applying for government assistance. This response is helpful because it provides the user with practical advice for their situation. Rating: [[10]]"", 'score': 10}Output Format​As shown above, the scoring evaluators return a dictionary with the following values:score: A score between 1 and 10 with 10 being the best.reasoning: String ""chain of thought reasoning"" from the LLM generated prior to creating the scorePreviousRegex MatchNextString DistanceUsage with Ground TruthUsage without references" 73,https://python.langchain.com/docs/guides/evaluation/string/string_distance,"GuidesEvaluationString EvaluatorsString DistanceOn this pageString DistanceOne of the simplest ways to compare an LLM or chain's string output against a reference label is by using string distance measurements such as Levenshtein or postfix distance. This can be used alongside approximate/fuzzy matching criteria for very basic unit testing.This can be accessed using the string_distance evaluator, which uses distance metric's from the rapidfuzz library.Note: The returned scores are distances, meaning lower is typically ""better"".For more information, check out the reference docs for the StringDistanceEvalChain for more info.# %pip install rapidfuzzfrom langchain.evaluation import load_evaluatorevaluator = load_evaluator(""string_distance"")evaluator.evaluate_strings( prediction=""The job is completely done."", reference=""The job is done"",) {'score': 0.11555555555555552}# The results purely character-based, so it's less useful when negation is concernedevaluator.evaluate_strings( prediction=""The job is done."", reference=""The job isn't done"",) {'score': 0.0724999999999999}Configure the String Distance Metric​By default, the StringDistanceEvalChain uses levenshtein distance, but it also supports other string distance algorithms. Configure using the distance argument.from langchain.evaluation import StringDistancelist(StringDistance) [, , , ]jaro_evaluator = load_evaluator( ""string_distance"", distance=StringDistance.JARO)jaro_evaluator.evaluate_strings( prediction=""The job is completely done."", reference=""The job is done"",) {'score': 0.19259259259259254}jaro_evaluator.evaluate_strings( prediction=""The job is done."", reference=""The job isn't done"",) {'score': 0.12083333333333324}PreviousScoring EvaluatorNextComparison EvaluatorsConfigure the String Distance Metric" 74,https://python.langchain.com/docs/guides/evaluation/comparison/,"GuidesEvaluationComparison EvaluatorsComparison EvaluatorsComparison evaluators in LangChain help measure two different chains or LLM outputs. These evaluators are helpful for comparative analyses, such as A/B testing between two language models, or comparing different versions of the same model. They can also be useful for things like generating preference scores for ai-assisted reinforcement learning.These evaluators inherit from the PairwiseStringEvaluator class, providing a comparison interface for two strings - typically, the outputs from two different prompts or models, or two versions of the same model. In essence, a comparison evaluator performs an evaluation on a pair of strings and returns a dictionary containing the evaluation score and other relevant details.To create a custom comparison evaluator, inherit from the PairwiseStringEvaluator class and overwrite the _evaluate_string_pairs method. If you require asynchronous evaluation, also overwrite the _aevaluate_string_pairs method.Here's a summary of the key methods and properties of a comparison evaluator:evaluate_string_pairs: Evaluate the output string pairs. This function should be overwritten when creating custom evaluators.aevaluate_string_pairs: Asynchronously evaluate the output string pairs. This function should be overwritten for asynchronous evaluation.requires_input: This property indicates whether this evaluator requires an input string.requires_reference: This property specifies whether this evaluator requires a reference label.LangSmith SupportThe run_on_dataset evaluation method is designed to evaluate only a single model at a time, and thus, doesn't support these evaluators.Detailed information about creating custom evaluators and the available built-in comparison evaluators is provided in the following sections.📄️ Custom Pairwise EvaluatorOpen In Collab📄️ Pairwise Embedding DistanceOpen In Collab📄️ Pairwise String ComparisonOpen In CollabPreviousString DistanceNextCustom Pairwise Evaluator" 75,https://python.langchain.com/docs/guides/evaluation/trajectory/,"GuidesEvaluationTrajectory EvaluatorsTrajectory EvaluatorsTrajectory Evaluators in LangChain provide a more holistic approach to evaluating an agent. These evaluators assess the full sequence of actions taken by an agent and their corresponding responses, which we refer to as the ""trajectory"". This allows you to better measure an agent's effectiveness and capabilities.A Trajectory Evaluator implements the AgentTrajectoryEvaluator interface, which requires two main methods:evaluate_agent_trajectory: This method synchronously evaluates an agent's trajectory.aevaluate_agent_trajectory: This asynchronous counterpart allows evaluations to be run in parallel for efficiency.Both methods accept three main parameters:input: The initial input given to the agent.prediction: The final predicted response from the agent.agent_trajectory: The intermediate steps taken by the agent, given as a list of tuples.These methods return a dictionary. It is recommended that custom implementations return a score (a float indicating the effectiveness of the agent) and reasoning (a string explaining the reasoning behind the score).You can capture an agent's trajectory by initializing the agent with the return_intermediate_steps=True parameter. This lets you collect all intermediate steps without relying on special callbacks.For a deeper dive into the implementation and use of Trajectory Evaluators, refer to the sections below.📄️ Custom Trajectory EvaluatorOpen In Collab📄️ Agent TrajectoryOpen In CollabPreviousPairwise String ComparisonNextCustom Trajectory Evaluator" 76,https://python.langchain.com/docs/guides/evaluation/examples/,GuidesEvaluationExamplesExamples🚧 Docs under construction 🚧Below are some examples for inspecting and checking different chains.📄️ Comparing Chain OutputsOpen In CollabPreviousAgent TrajectoryNextComparing Chain Outputs 77,https://python.langchain.com/docs/guides/fallbacks,"GuidesFallbacksOn this pageFallbacksWhen working with language models, you may often encounter issues from the underlying APIs, whether these be rate limiting or downtime. Therefore, as you go to move your LLM applications into production it becomes more and more important to safeguard against these. That's why we've introduced the concept of fallbacks. A fallback is an alternative plan that may be used in an emergency.Crucially, fallbacks can be applied not only on the LLM level but on the whole runnable level. This is important because often times different models require different prompts. So if your call to OpenAI fails, you don't just want to send the same prompt to Anthropic - you probably want to use a different prompt template and send a different version there.Fallback for LLM API Errors​This is maybe the most common use case for fallbacks. A request to an LLM API can fail for a variety of reasons - the API could be down, you could have hit rate limits, any number of things. Therefore, using fallbacks can help protect against these types of things.IMPORTANT: By default, a lot of the LLM wrappers catch errors and retry. You will most likely want to turn those off when working with fallbacks. Otherwise the first wrapper will keep on retrying and not failing.from langchain.chat_models import ChatOpenAI, ChatAnthropicFirst, let's mock out what happens if we hit a RateLimitError from OpenAIfrom unittest.mock import patchfrom openai.error import RateLimitError# Note that we set max_retries = 0 to avoid retrying on RateLimits, etcopenai_llm = ChatOpenAI(max_retries=0)anthropic_llm = ChatAnthropic()llm = openai_llm.with_fallbacks([anthropic_llm])# Let's use just the OpenAI LLm first, to show that we run into an errorwith patch('openai.ChatCompletion.create', side_effect=RateLimitError()): try: print(openai_llm.invoke(""Why did the chicken cross the road?"")) except: print(""Hit error"") Hit error# Now let's try with fallbacks to Anthropicwith patch('openai.ChatCompletion.create', side_effect=RateLimitError()): try: print(llm.invoke(""Why did the the chicken cross the road?"")) except: print(""Hit error"") content=' I don\'t actually know why the chicken crossed the road, but here are some possible humorous answers:\n\n- To get to the other side!\n\n- It was too chicken to just stand there. \n\n- It wanted a change of scenery.\n\n- It wanted to show the possum it could be done.\n\n- It was on its way to a poultry farmers\' convention.\n\nThe joke plays on the double meaning of ""the other side"" - literally crossing the road to the other side, or the ""other side"" meaning the afterlife. So it\'s an anti-joke, with a silly or unexpected pun as the answer.' additional_kwargs={} example=FalseWe can use our ""LLM with Fallbacks"" as we would a normal LLM.from langchain.prompts import ChatPromptTemplateprompt = ChatPromptTemplate.from_messages( [ (""system"", ""You're a nice assistant who always includes a compliment in your response""), (""human"", ""Why did the {animal} cross the road""), ])chain = prompt | llmwith patch('openai.ChatCompletion.create', side_effect=RateLimitError()): try: print(chain.invoke({""animal"": ""kangaroo""})) except: print(""Hit error"") content="" I don't actually know why the kangaroo crossed the road, but I can take a guess! Here are some possible reasons:\n\n- To get to the other side (the classic joke answer!)\n\n- It was trying to find some food or water \n\n- It was trying to find a mate during mating season\n\n- It was fleeing from a predator or perceived threat\n\n- It was disoriented and crossed accidentally \n\n- It was following a herd of other kangaroos who were crossing\n\n- It wanted a change of scenery or environment \n\n- It was trying to reach a new habitat or territory\n\nThe real reason is unknown without more context, but hopefully one of those potential explanations does the joke justice! Let me know if you have any other animal jokes I can try to decipher."" additional_kwargs={} example=FalseFallback for Sequences​We can also create fallbacks for sequences, that are sequences themselves. Here we do that with two different models: ChatOpenAI and then normal OpenAI (which does not use a chat model). Because OpenAI is NOT a chat model, you likely want a different prompt.# First let's create a chain with a ChatModel# We add in a string output parser here so the outputs between the two are the same typefrom langchain.schema.output_parser import StrOutputParserchat_prompt = ChatPromptTemplate.from_messages( [ (""system"", ""You're a nice assistant who always includes a compliment in your response""), (""human"", ""Why did the {animal} cross the road""), ])# Here we're going to use a bad model name to easily create a chain that will errorchat_model = ChatOpenAI(model_name=""gpt-fake"")bad_chain = chat_prompt | chat_model | StrOutputParser()# Now lets create a chain with the normal OpenAI modelfrom langchain.llms import OpenAIfrom langchain.prompts import PromptTemplateprompt_template = """"""Instructions: You should always include a compliment in your response.Question: Why did the {animal} cross the road?""""""prompt = PromptTemplate.from_template(prompt_template)llm = OpenAI()good_chain = prompt | llm# We can now create a final chain which combines the twochain = bad_chain.with_fallbacks([good_chain])chain.invoke({""animal"": ""turtle""}) '\n\nAnswer: The turtle crossed the road to get to the other side, and I have to say he had some impressive determination.'Fallback for Long Inputs​One of the big limiting factors of LLMs is their context window. Usually, you can count and track the length of prompts before sending them to an LLM, but in situations where that is hard/complicated, you can fallback to a model with a longer context length.short_llm = ChatOpenAI()long_llm = ChatOpenAI(model=""gpt-3.5-turbo-16k"")llm = short_llm.with_fallbacks([long_llm])inputs = ""What is the next number: "" + "", "".join([""one"", ""two""] * 3000)try: print(short_llm.invoke(inputs))except Exception as e: print(e) This model's maximum context length is 4097 tokens. However, your messages resulted in 12012 tokens. Please reduce the length of the messages.try: print(llm.invoke(inputs))except Exception as e: print(e) content='The next number in the sequence is two.' additional_kwargs={} example=FalseFallback to Better Model​Often times we ask models to output format in a specific format (like JSON). Models like GPT-3.5 can do this okay, but sometimes struggle. This naturally points to fallbacks - we can try with GPT-3.5 (faster, cheaper), but then if parsing fails we can use GPT-4.from langchain.output_parsers import DatetimeOutputParserprompt = ChatPromptTemplate.from_template( ""what time was {event} (in %Y-%m-%dT%H:%M:%S.%fZ format - only return this value)"")# In this case we are going to do the fallbacks on the LLM + output parser level# Because the error will get raised in the OutputParseropenai_35 = ChatOpenAI() | DatetimeOutputParser()openai_4 = ChatOpenAI(model=""gpt-4"")| DatetimeOutputParser()only_35 = prompt | openai_35 fallback_4 = prompt | openai_35.with_fallbacks([openai_4])try: print(only_35.invoke({""event"": ""the superbowl in 1994""}))except Exception as e: print(f""Error: {e}"") Error: Could not parse datetime string: The Super Bowl in 1994 took place on January 30th at 3:30 PM local time. Converting this to the specified format (%Y-%m-%dT%H:%M:%S.%fZ) results in: 1994-01-30T15:30:00.000Ztry: print(fallback_4.invoke({""event"": ""the superbowl in 1994""}))except Exception as e: print(f""Error: {e}"") 1994-01-30 15:30:00PreviousComparing Chain OutputsNextLangSmithFallback for LLM API ErrorsFallback for SequencesFallback for Long InputsFallback to Better Model" 78,https://python.langchain.com/docs/guides/langsmith/,"GuidesLangSmithLangSmithLangSmith helps you trace and evaluate your language model applications and intelligent agents to help you move from prototype to production.Check out the interactive walkthrough below to get started.For more information, please refer to the LangSmith documentation.For tutorials and other end-to-end examples demonstrating ways to integrate LangSmith in your workflow, check out the LangSmith Cookbook. Some of the guides therein include:Leveraging user feedback in your JS application (link).Building an automated feedback pipeline (link).How to evaluate and audit your RAG workflows (link).How to fine-tune a LLM on real usage data (link).How to use the LangChain Hub to version your prompts (link)📄️ LangSmith WalkthroughOpen In CollabPreviousFallbacksNextLangSmith Walkthrough" 79,https://python.langchain.com/docs/guides/local_llms,"GuidesRun LLMs locallyOn this pageRun LLMs locallyUse case​The popularity of projects like PrivateGPT, llama.cpp, and GPT4All underscore the demand to run LLMs locally (on your own device).This has at least two important benefits:Privacy: Your data is not sent to a third party, and it is not subject to the terms of service of a commercial serviceCost: There is no inference fee, which is important for token-intensive applications (e.g., long-running simulations, summarization)Overview​Running an LLM locally requires a few things:Open source LLM: An open source LLM that can be freely modified and shared Inference: Ability to run this LLM on your device w/ acceptable latencyOpen Source LLMs​Users can now gain access to a rapidly growing set of open source LLMs. These LLMs can be assessed across at least two dimentions (see figure):Base model: What is the base-model and how was it trained?Fine-tuning approach: Was the base-model fine-tuned and, if so, what set of instructions was used?The relative performance of these models can be assessed using several leaderboards, including:LmSysGPT4AllHuggingFaceInference​A few frameworks for this have emerged to support inference of open source LLMs on various devices:llama.cpp: C++ implementation of llama inference code with weight optimization / quantizationgpt4all: Optimized C backend for inferenceOllama: Bundles model weights and environment into an app that runs on device and serves the LLM In general, these frameworks will do a few things:Quantization: Reduce the memory footprint of the raw model weightsEfficient implementation for inference: Support inference on consumer hardware (e.g., CPU or laptop GPU)In particular, see this excellent post on the importance of quantization.With less precision, we radically decrease the memory needed to store the LLM in memory.In addition, we can see the importance of GPU memory bandwidth sheet!A Mac M2 Max is 5-6x faster than a M1 for inference due to the larger GPU memory bandwidth.Quickstart​Ollama is one way to easily run inference on macOS.The instructions here provide details, which we summarize:Download and run the appFrom command line, fetch a model from this list of options: e.g., ollama pull llama2When the app is running, all models are automatically served on localhost:11434from langchain.llms import Ollamallm = Ollama(model=""llama2"")llm(""The first man on the moon was ..."") ' The first man on the moon was Neil Armstrong, who landed on the moon on July 20, 1969 as part of the Apollo 11 mission. obviously.'Stream tokens as they are being generated.from langchain.callbacks.manager import CallbackManagerfrom langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler llm = Ollama(model=""llama2"", callback_manager = CallbackManager([StreamingStdOutCallbackHandler()]))llm(""The first man on the moon was ..."") The first man to walk on the moon was Neil Armstrong, an American astronaut who was part of the Apollo 11 mission in 1969. февруари 20, 1969, Armstrong stepped out of the lunar module Eagle and onto the moon's surface, famously declaring ""That's one small step for man, one giant leap for mankind"" as he took his first steps. He was followed by fellow astronaut Edwin ""Buzz"" Aldrin, who also walked on the moon during the mission. ' The first man to walk on the moon was Neil Armstrong, an American astronaut who was part of the Apollo 11 mission in 1969. февруари 20, 1969, Armstrong stepped out of the lunar module Eagle and onto the moon\'s surface, famously declaring ""That\'s one small step for man, one giant leap for mankind"" as he took his first steps. He was followed by fellow astronaut Edwin ""Buzz"" Aldrin, who also walked on the moon during the mission.'Environment​Inference speed is a challenge when running models locally (see above).To minimize latency, it is desiable to run models locally on GPU, which ships with many consumer laptops e.g., Apple devices.And even with GPU, the available GPU memory bandwidth (as noted above) is important.Running Apple silicon GPU​Ollama will automatically utilize the GPU on Apple devices.Other frameworks require the user to set up the environment to utilize the Apple GPU.For example, llama.cpp python bindings can be configured to use the GPU via Metal.Metal is a graphics and compute API created by Apple providing near-direct access to the GPU. See the llama.cpp setup here to enable this.In particular, ensure that conda is using the correct virtual enviorment that you created (miniforge3).E.g., for me:conda activate /Users/rlm/miniforge3/envs/llamaWith the above confirmed, then:CMAKE_ARGS=""-DLLAMA_METAL=on"" FORCE_CMAKE=1 pip install -U llama-cpp-python --no-cache-dirLLMs​There are various ways to gain access to quantized model weights.HuggingFace - Many quantized model are available for download and can be run with framework such as llama.cppgpt4all - The model explorer offers a leaderboard of metrics and associated quantized models available for download Ollama - Several models can be accessed directly via pullOllama​With Ollama, fetch a model via ollama pull ::E.g., for Llama-7b: ollama pull llama2 will download the most basic version of the model (e.g., smallest # parameters and 4 bit quantization)We can also specify a particular version from the model list, e.g., ollama pull llama2:13bSee the full set of parameters on the API reference pagefrom langchain.llms import Ollamallm = Ollama(model=""llama2:13b"")llm(""The first man on the moon was ... think step by step"") ' Sure! Here\'s the answer, broken down step by step:\n\nThe first man on the moon was... Neil Armstrong.\n\nHere\'s how I arrived at that answer:\n\n1. The first manned mission to land on the moon was Apollo 11.\n2. The mission included three astronauts: Neil Armstrong, Edwin ""Buzz"" Aldrin, and Michael Collins.\n3. Neil Armstrong was the mission commander and the first person to set foot on the moon.\n4. On July 20, 1969, Armstrong stepped out of the lunar module Eagle and onto the moon\'s surface, famously declaring ""That\'s one small step for man, one giant leap for mankind.""\n\nSo, the first man on the moon was Neil Armstrong!'Llama.cpp​Llama.cpp is compatible with a broad set of models.For example, below we run inference on llama2-13b with 4 bit quantization downloaded from HuggingFace.As noted above, see the API reference for the full set of parameters. From the llama.cpp docs, a few are worth commenting on:n_gpu_layers: number of layers to be loaded into GPU memoryValue: 1Meaning: Only one layer of the model will be loaded into GPU memory (1 is often sufficient).n_batch: number of tokens the model should process in parallel Value: n_batchMeaning: It's recommended to choose a value between 1 and n_ctx (which in this case is set to 2048)n_ctx: Token context window .Value: 2048Meaning: The model will consider a window of 2048 tokens at a timef16_kv: whether the model should use half-precision for the key/value cacheValue: TrueMeaning: The model will use half-precision, which can be more memory efficient; Metal only support True.CMAKE_ARGS=""-DLLAMA_METAL=on"" FORCE_CMAKE=1 pip install -U llama-cpp-python --no-cache-dirclearfrom langchain.llms import LlamaCppllm = LlamaCpp( model_path=""/Users/rlm/Desktop/Code/llama.cpp/models/openorca-platypus2-13b.gguf.q4_0.bin"", n_gpu_layers=1, n_batch=512, n_ctx=2048, f16_kv=True, callback_manager=CallbackManager([StreamingStdOutCallbackHandler()]), verbose=True,)The console log will show the the below to indicate Metal was enabled properly from steps above:ggml_metal_init: allocatingggml_metal_init: using MPSllm(""The first man on the moon was ... Let's think step by step"") Llama.generate: prefix-match hit and use logical reasoning to figure out who the first man on the moon was. Here are some clues: 1. The first man on the moon was an American. 2. He was part of the Apollo 11 mission. 3. He stepped out of the lunar module and became the first person to set foot on the moon's surface. 4. His last name is Armstrong. Now, let's use our reasoning skills to figure out who the first man on the moon was. Based on clue #1, we know that the first man on the moon was an American. Clue #2 tells us that he was part of the Apollo 11 mission. Clue #3 reveals that he was the first person to set foot on the moon's surface. And finally, clue #4 gives us his last name: Armstrong. Therefore, the first man on the moon was Neil Armstrong! llama_print_timings: load time = 9623.21 ms llama_print_timings: sample time = 143.77 ms / 203 runs ( 0.71 ms per token, 1412.01 tokens per second) llama_print_timings: prompt eval time = 485.94 ms / 7 tokens ( 69.42 ms per token, 14.40 tokens per second) llama_print_timings: eval time = 6385.16 ms / 202 runs ( 31.61 ms per token, 31.64 tokens per second) llama_print_timings: total time = 7279.28 ms "" and use logical reasoning to figure out who the first man on the moon was.\n\nHere are some clues:\n\n1. The first man on the moon was an American.\n2. He was part of the Apollo 11 mission.\n3. He stepped out of the lunar module and became the first person to set foot on the moon's surface.\n4. His last name is Armstrong.\n\nNow, let's use our reasoning skills to figure out who the first man on the moon was. Based on clue #1, we know that the first man on the moon was an American. Clue #2 tells us that he was part of the Apollo 11 mission. Clue #3 reveals that he was the first person to set foot on the moon's surface. And finally, clue #4 gives us his last name: Armstrong.\nTherefore, the first man on the moon was Neil Armstrong!""GPT4All​We can use model weights downloaded from GPT4All model explorer.Similar to what is shown above, we can run inference and use the API reference to set parameters of interest.pip install gpt4allfrom langchain.llms import GPT4Allllm = GPT4All(model=""/Users/rlm/Desktop/Code/gpt4all/models/nous-hermes-13b.ggmlv3.q4_0.bin"")llm(""The first man on the moon was ... Let's think step by step"") "".\n1) The United States decides to send a manned mission to the moon.2) They choose their best astronauts and train them for this specific mission.3) They build a spacecraft that can take humans to the moon, called the Lunar Module (LM).4) They also create a larger spacecraft, called the Saturn V rocket, which will launch both the LM and the Command Service Module (CSM), which will carry the astronauts into orbit.5) The mission is planned down to the smallest detail: from the trajectory of the rockets to the exact movements of the astronauts during their moon landing.6) On July 16, 1969, the Saturn V rocket launches from Kennedy Space Center in Florida, carrying the Apollo 11 mission crew into space.7) After one and a half orbits around the Earth, the LM separates from the CSM and begins its descent to the moon's surface.8) On July 20, 1969, at 2:56 pm EDT (GMT-4), Neil Armstrong becomes the first man on the moon. He speaks these""Prompts​Some LLMs will benefit from specific prompts.For example, LLaMA will use special tokens.We can use ConditionalPromptSelector to set prompt based on the model type.# Set our LLMllm = LlamaCpp( model_path=""/Users/rlm/Desktop/Code/llama.cpp/models/openorca-platypus2-13b.gguf.q4_0.bin"", n_gpu_layers=1, n_batch=512, n_ctx=2048, f16_kv=True, callback_manager=CallbackManager([StreamingStdOutCallbackHandler()]), verbose=True,)Set the associated prompt based upon the model version.from langchain.prompts import PromptTemplatefrom langchain.chains import LLMChainfrom langchain.chains.prompt_selector import ConditionalPromptSelectorDEFAULT_LLAMA_SEARCH_PROMPT = PromptTemplate( input_variables=[""question""], template=""""""<> \n You are an assistant tasked with improving Google search \results. \n <> \n\n [INST] Generate THREE Google search queries that \are similar to this question. The output should be a numbered list of questions \and each should have a question mark at the end: \n\n {question} [/INST]"""""",)DEFAULT_SEARCH_PROMPT = PromptTemplate( input_variables=[""question""], template=""""""You are an assistant tasked with improving Google search \results. Generate THREE Google search queries that are similar to \this question. The output should be a numbered list of questions and each \should have a question mark at the end: {question}"""""",)QUESTION_PROMPT_SELECTOR = ConditionalPromptSelector( default_prompt=DEFAULT_SEARCH_PROMPT, conditionals=[ (lambda llm: isinstance(llm, LlamaCpp), DEFAULT_LLAMA_SEARCH_PROMPT) ], )prompt = QUESTION_PROMPT_SELECTOR.get_prompt(llm)prompt PromptTemplate(input_variables=['question'], output_parser=None, partial_variables={}, template='<> \n You are an assistant tasked with improving Google search results. \n <> \n\n [INST] Generate THREE Google search queries that are similar to this question. The output should be a numbered list of questions and each should have a question mark at the end: \n\n {question} [/INST]', template_format='f-string', validate_template=True)# Chainllm_chain = LLMChain(prompt=prompt,llm=llm)question = ""What NFL team won the Super Bowl in the year that Justin Bieber was born?""llm_chain.run({""question"":question}) Sure! Here are three similar search queries with a question mark at the end: 1. Which NBA team did LeBron James lead to a championship in the year he was drafted? 2. Who won the Grammy Awards for Best New Artist and Best Female Pop Vocal Performance in the same year that Lady Gaga was born? 3. What MLB team did Babe Ruth play for when he hit 60 home runs in a single season? llama_print_timings: load time = 14943.19 ms llama_print_timings: sample time = 72.93 ms / 101 runs ( 0.72 ms per token, 1384.87 tokens per second) llama_print_timings: prompt eval time = 14942.95 ms / 93 tokens ( 160.68 ms per token, 6.22 tokens per second) llama_print_timings: eval time = 3430.85 ms / 100 runs ( 34.31 ms per token, 29.15 tokens per second) llama_print_timings: total time = 18578.26 ms ' Sure! Here are three similar search queries with a question mark at the end:\n\n1. Which NBA team did LeBron James lead to a championship in the year he was drafted?\n2. Who won the Grammy Awards for Best New Artist and Best Female Pop Vocal Performance in the same year that Lady Gaga was born?\n3. What MLB team did Babe Ruth play for when he hit 60 home runs in a single season?'We also can use the LangChain Prompt Hub to fetch and / or store prompts that are model specific.This will work with your LangSmith API key.For example, here is a prompt for RAG with LLaMA-specific tokens.Use cases​Given an llm created from one of the models above, you can use it for many use cases.For example, here is a guide to RAG with local LLMs.In general, use cases for local LLMs can be driven by at least two factors:Privacy: private data (e.g., journals, etc) that a user does not want to share Cost: text preprocessing (extraction/tagging), summarization, and agent simulations are token-use-intensive tasksIn addition, here is an overview on fine-tuning, which can utilize open source LLMs.PreviousLangSmith WalkthroughNextModel comparisonUse caseOverviewOpen Source LLMsInferenceQuickstartEnvironmentRunning Apple silicon GPULLMsOllamaLlama.cppGPT4AllPromptsUse cases" 80,https://python.langchain.com/docs/guides/model_laboratory,"GuidesModel comparisonModel comparisonConstructing your language model application will likely involved choosing between many different options of prompts, models, and even chains to use. When doing so, you will want to compare these different options on different inputs in an easy, flexible, and intuitive way. LangChain provides the concept of a ModelLaboratory to test out and try different models.from langchain.chains import LLMChainfrom langchain.llms import OpenAI, Cohere, HuggingFaceHubfrom langchain.prompts import PromptTemplatefrom langchain.model_laboratory import ModelLaboratoryllms = [ OpenAI(temperature=0), Cohere(model=""command-xlarge-20221108"", max_tokens=20, temperature=0), HuggingFaceHub(repo_id=""google/flan-t5-xl"", model_kwargs={""temperature"": 1}),]model_lab = ModelLaboratory.from_llms(llms)model_lab.compare(""What color is a flamingo?"") Input: What color is a flamingo? OpenAI Params: {'model': 'text-davinci-002', 'temperature': 0.0, 'max_tokens': 256, 'top_p': 1, 'frequency_penalty': 0, 'presence_penalty': 0, 'n': 1, 'best_of': 1} Flamingos are pink. Cohere Params: {'model': 'command-xlarge-20221108', 'max_tokens': 20, 'temperature': 0.0, 'k': 0, 'p': 1, 'frequency_penalty': 0, 'presence_penalty': 0} Pink HuggingFaceHub Params: {'repo_id': 'google/flan-t5-xl', 'temperature': 1} pink prompt = PromptTemplate( template=""What is the capital of {state}?"", input_variables=[""state""])model_lab_with_prompt = ModelLaboratory.from_llms(llms, prompt=prompt)model_lab_with_prompt.compare(""New York"") Input: New York OpenAI Params: {'model': 'text-davinci-002', 'temperature': 0.0, 'max_tokens': 256, 'top_p': 1, 'frequency_penalty': 0, 'presence_penalty': 0, 'n': 1, 'best_of': 1} The capital of New York is Albany. Cohere Params: {'model': 'command-xlarge-20221108', 'max_tokens': 20, 'temperature': 0.0, 'k': 0, 'p': 1, 'frequency_penalty': 0, 'presence_penalty': 0} The capital of New York is Albany. HuggingFaceHub Params: {'repo_id': 'google/flan-t5-xl', 'temperature': 1} st john s from langchain.chains import SelfAskWithSearchChainfrom langchain.utilities import SerpAPIWrapperopen_ai_llm = OpenAI(temperature=0)search = SerpAPIWrapper()self_ask_with_search_openai = SelfAskWithSearchChain( llm=open_ai_llm, search_chain=search, verbose=True)cohere_llm = Cohere(temperature=0, model=""command-xlarge-20221108"")search = SerpAPIWrapper()self_ask_with_search_cohere = SelfAskWithSearchChain( llm=cohere_llm, search_chain=search, verbose=True)chains = [self_ask_with_search_openai, self_ask_with_search_cohere]names = [str(open_ai_llm), str(cohere_llm)]model_lab = ModelLaboratory(chains, names=names)model_lab.compare(""What is the hometown of the reigning men's U.S. Open champion?"") Input: What is the hometown of the reigning men's U.S. Open champion? OpenAI Params: {'model': 'text-davinci-002', 'temperature': 0.0, 'max_tokens': 256, 'top_p': 1, 'frequency_penalty': 0, 'presence_penalty': 0, 'n': 1, 'best_of': 1} > Entering new chain... What is the hometown of the reigning men's U.S. Open champion? Are follow up questions needed here: Yes. Follow up: Who is the reigning men's U.S. Open champion? Intermediate answer: Carlos Alcaraz. Follow up: Where is Carlos Alcaraz from? Intermediate answer: El Palmar, Spain. So the final answer is: El Palmar, Spain > Finished chain. So the final answer is: El Palmar, Spain Cohere Params: {'model': 'command-xlarge-20221108', 'max_tokens': 256, 'temperature': 0.0, 'k': 0, 'p': 1, 'frequency_penalty': 0, 'presence_penalty': 0} > Entering new chain... What is the hometown of the reigning men's U.S. Open champion? Are follow up questions needed here: Yes. Follow up: Who is the reigning men's U.S. Open champion? Intermediate answer: Carlos Alcaraz. So the final answer is: Carlos Alcaraz > Finished chain. So the final answer is: Carlos Alcaraz PreviousRun LLMs locallyNextData anonymization with Microsoft Presidio" 81,https://python.langchain.com/docs/guides/pydantic_compatibility,"GuidesPydantic compatibilityOn this pagePydantic compatibilityPydantic v2 was released in June, 2023 (https://docs.pydantic.dev/2.0/blog/pydantic-v2-final/)v2 contains has a number of breaking changes (https://docs.pydantic.dev/2.0/migration/)Pydantic v2 and v1 are under the same package name, so both versions cannot be installed at the same timeLangChain Pydantic migration plan​As of langchain>=0.0.267, LangChain will allow users to install either Pydantic V1 or V2. Internally LangChain will continue to use V1.During this time, users can pin their pydantic version to v1 to avoid breaking changes, or start a partial migration using pydantic v2 throughout their code, but avoiding mixing v1 and v2 code for LangChain (see below).User can either pin to pydantic v1, and upgrade their code in one go once LangChain has migrated to v2 internally, or they can start a partial migration to v2, but must avoid mixing v1 and v2 code for LangChain.Below are two examples of showing how to avoid mixing pydantic v1 and v2 code in the case of inheritance and in the case of passing objects to LangChain.Example 1: Extending via inheritanceYES from pydantic.v1 import root_validator, validatorclass CustomTool(BaseTool): # BaseTool is v1 code x: int = Field(default=1) def _run(*args, **kwargs): return ""hello"" @validator('x') # v1 code @classmethod def validate_x(cls, x: int) -> int: return 1 CustomTool( name='custom_tool', description=""hello"", x=1,)Mixing Pydantic v2 primitives with Pydantic v1 primitives can raise cryptic errorsNO from pydantic import Field, field_validator # pydantic v2class CustomTool(BaseTool): # BaseTool is v1 code x: int = Field(default=1) def _run(*args, **kwargs): return ""hello"" @field_validator('x') # v2 code @classmethod def validate_x(cls, x: int) -> int: return 1 CustomTool( name='custom_tool', description=""hello"", x=1,)Example 2: Passing objects to LangChainYESfrom langchain.tools.base import Toolfrom pydantic.v1 import BaseModel, Field # <-- Uses v1 namespaceclass CalculatorInput(BaseModel): question: str = Field()Tool.from_function( # <-- tool uses v1 namespace func=lambda question: 'hello', name=""Calculator"", description=""useful for when you need to answer questions about math"", args_schema=CalculatorInput)NOfrom langchain.tools.base import Toolfrom pydantic import BaseModel, Field # <-- Uses v2 namespaceclass CalculatorInput(BaseModel): question: str = Field()Tool.from_function( # <-- tool uses v1 namespace func=lambda question: 'hello', name=""Calculator"", description=""useful for when you need to answer questions about math"", args_schema=CalculatorInput)PreviousReversible data anonymization with Microsoft PresidioNextModerationLangChain Pydantic migration plan" 82,https://python.langchain.com/docs/guides/safety/,"GuidesSafetyModerationOne of the key concerns with using LLMs is that they may generate harmful or unethical text. This is an area of active research in the field. Here we present some built-in chains inspired by this research, which are intended to make the outputs of LLMs safer.Moderation chain: Explicitly check if any output text is harmful and flag it.Constitutional chain: Prompt the model with a set of principles which should guide it's behavior.Logical Fallacy chain: Checks the model output against logical fallacies to correct any deviation.Amazon Comprehend moderation chain: Use Amazon Comprehend to detect and handle PII and toxicity.PreviousPydantic compatibilityNextAmazon Comprehend Moderation Chain" 83,https://python.langchain.com/docs/additional_resources,"MoreMore📄️ DependentsDependents stats for langchain-ai/langchain📄️ TutorialsBelow are links to tutorials and courses on LangChain. For written guides on common use cases for LangChain, check out the use cases guides.📄️ YouTube videos⛓ icon marks a new addition [last update 2023-09-21]🔗 GalleryPreviousModerationNextDependents" 84,https://python.langchain.com/docs/additional_resources/dependents,"MoreDependentsDependentsDependents stats for langchain-ai/langchain [update: 2023-10-06; only dependent repositories with Stars > 100]RepositoryStarsopenai/openai-cookbook49006AntonOsika/gpt-engineer44368imartinez/privateGPT38300LAION-AI/Open-Assistant35327hpcaitech/ColossalAI34799microsoft/TaskMatrix34161streamlit/streamlit27697geekan/MetaGPT27302reworkd/AgentGPT26805OpenBB-finance/OpenBBTerminal24473StanGirard/quivr23323run-llama/llama_index22151openai/chatgpt-retrieval-plugin19741mindsdb/mindsdb18062PromtEngineer/localGPT16413chatchat-space/Langchain-Chatchat16300cube-js/cube16261mlflow/mlflow15487logspace-ai/langflow12599GaiZhenbiao/ChuanhuChatGPT12501openai/evals12056airbytehq/airbyte11919go-skynet/LocalAI11767databrickslabs/dolly10609AIGC-Audio/AudioGPT9240aws/amazon-sagemaker-examples8892langgenius/dify8764gventuri/pandas-ai8687jmorganca/ollama8628langchain-ai/langchainjs8392h2oai/h2ogpt7953arc53/DocsGPT7730PipedreamHQ/pipedream7261joshpxyne/gpt-migrate6349bentoml/OpenLLM6213mage-ai/mage-ai5600zauberzeug/nicegui5499wenda-LLM/wenda5497sweepai/sweep5489embedchain/embedchain5428zilliztech/GPTCache5311Shaunwei/RealChar5264GreyDGL/PentestGPT5146gkamradt/langchain-tutorials5134serge-chat/serge5009assafelovic/gpt-researcher4836openchatai/OpenChat4697intel-analytics/BigDL4412continuedev/continue4324postgresml/postgresml4267madawei2699/myGPTReader4214MineDojo/Voyager4204danswer-ai/danswer3973RayVentura/ShortGPT3922Azure/azure-sdk-for-python3849khoj-ai/khoj3817langchain-ai/chat-langchain3742Azure-Samples/azure-search-openai-demo3731marqo-ai/marqo3627kyegomez/tree-of-thoughts3553llm-workflow-engine/llm-workflow-engine3483PrefectHQ/marvin3460aiwaves-cn/agents3413OpenBMB/ToolBench3388shroominic/codeinterpreter-api3218whitead/paper-qa3085project-baize/baize-chatbot3039OpenGVLab/InternGPT2911ParisNeo/lollms-webui2907Unstructured-IO/unstructured2874openchatai/OpenCopilot2759OpenBMB/BMTools2657homanp/superagent2624SamurAIGPT/EmbedAI2575GerevAI/gerev2488microsoft/promptflow2475OpenBMB/AgentVerse2445Mintplex-Labs/anything-llm2434emptycrown/llama-hub2432NVIDIA/NeMo-Guardrails2327ShreyaR/guardrails2307thomas-yanxin/LangChain-ChatGLM-Webui2305yanqiangmiffy/Chinese-LangChain2291keephq/keep2252OpenGVLab/Ask-Anything2194IntelligenzaArtificiale/Free-Auto-GPT2169Farama-Foundation/PettingZoo2031YiVal/YiVal2014hwchase17/notion-qa2014jupyterlab/jupyter-ai1977paulpierre/RasaGPT1887dot-agent/dotagent-WIP1812hegelai/prompttools1775vocodedev/vocode-python1734Vonng/pigsty1693psychic-api/psychic1597avinashkranjan/Amazing-Python-Scripts1546pinterest/querybook1539Forethought-Technologies/AutoChain1531Kav-K/GPTDiscord1503jina-ai/langchain-serve1487noahshinn024/reflexion1481jina-ai/dev-gpt1436ttengwang/Caption-Anything1425milvus-io/bootcamp1420agiresearch/OpenAGI1401greshake/llm-security1381jina-ai/thinkgpt1366lunasec-io/lunasec1352101dotxyz/GPTeam1339refuel-ai/autolabel1320melih-unsal/DemoGPT1320mmz-001/knowledge_gpt1320richardyc/Chrome-GPT1315run-llama/sec-insights1312Azure/azureml-examples1305cofactoryai/textbase1286dataelement/bisheng1273eyurtsev/kor1263pluralsh/plural1188FlagOpen/FlagEmbedding1184juncongmoo/chatllama1144poe-platform/server-bot-quick-start1139visual-openllm/visual-openllm1137griptape-ai/griptape1124microsoft/X-Decoder1119ThousandBirdsInc/chidori1116filip-michalsky/SalesGPT1112psychic-api/rag-stack1110irgolic/AutoPR1100promptfoo/promptfoo1099nod-ai/SHARK1062SamurAIGPT/Camel-AutoGPT1036Farama-Foundation/chatarena1020peterw/Chat-with-Github-Repo993jiran214/GPT-vup967alejandro-ao/ask-multiple-pdfs958run-llama/llama-lab953LC1332/Chat-Haruhi-Suzumiya950rlancemartin/auto-evaluator927cheshire-cat-ai/core902Anil-matcha/ChatPDF894cirediatpl/FigmaChain881seanpixel/Teenage-AGI876xusenlinzy/api-for-open-llm865ricklamers/shell-ai864codeacme17/examor856corca-ai/EVAL836microsoft/Llama-2-Onnx835explodinggradients/ragas833ajndkr/lanarky817kennethleungty/Llama-2-Open-Source-LLM-CPU-Inference814ray-project/llm-applications804hwchase17/chat-your-data801LambdaLabsML/examples759kreneskyp/ix758pyspark-ai/pyspark-ai750billxbf/ReWOO746e-johnstonn/BriefGPT738akshata29/entaoai733getmetal/motorhead717ruoccofabrizio/azure-open-ai-embeddings-qna712msoedov/langcorn698Dataherald/dataherald684jondurbin/airoboros657Ikaros-521/AI-Vtuber651whyiyhw/chatgpt-wechat644langchain-ai/streamlit-agent637SamurAIGPT/ChatGPT-Developer-Plugins637OpenGenerativeAI/GenossGPT632AILab-CVC/GPT4Tools629langchain-ai/auto-evaluator614explosion/spacy-llm613alexanderatallah/window.ai607MiuLab/Taiwan-LLaMa601microsoft/PodcastCopilot600Dicklesworthstone/swiss_army_llama596NoDataFound/hackGPT596namuan/dr-doc-search593amosjyng/langchain-visualizer582microsoft/sample-app-aoai-chatGPT581yvann-hub/Robby-chatbot581yeagerai/yeagerai-agent547tgscan-dev/tgscan533Azure-Samples/openai531plastic-labs/tutor-gpt531xuwenhao/geektime-ai-course526michaelthwan/searchGPT526jonra1993/fastapi-alembic-sqlmodel-async522jina-ai/agentchain519mckaywrigley/repo-chat518modelscope/modelscope-agent512daveebbelaar/langchain-experiments504freddyaboulton/gradio-tools497sidhq/Multi-GPT494continuum-llms/chatgpt-memory489langchain-ai/langchain-aiplugin487mpaepper/content-chatbot483steamship-core/steamship-langchain481alejandro-ao/langchain-ask-pdf474truera/trulens464marella/chatdocs459opencopilotdev/opencopilot453poe-platform/poe-protocol444DataDog/dd-trace-py441logan-markewich/llama_index_starter_pack441opentensor/bittensor433DjangoPeng/openai-quickstart425CarperAI/OpenELM424daodao97/chatdoc423showlab/VLog411Anil-matcha/Chatbase402yakami129/VirtualWife399wandb/weave399mtenenholtz/chat-twitter398LinkSoul-AI/AutoAgents397Agenta-AI/agenta389huchenxucs/ChatDB386mallorbc/Finetune_LLMs379junruxiong/IncarnaMind372MagnivOrg/prompt-layer-library368mosaicml/examples366rsaryev/talk-codebase364morpheuslord/GPT_Vuln-analyzer362monarch-initiative/ontogpt362JayZeeDesign/researcher-gpt361personoids/personoids-lite361intel/intel-extension-for-transformers357jerlendds/osintbuddy357steamship-packages/langchain-production-starter356onlyphantom/llm-python354Azure-Samples/miyagi340mrwadams/attackgen338rgomezcasas/dotfiles337eosphoros-ai/DB-GPT-Hub336andylokandy/gpt-4-search335NimbleBoxAI/ChainFury330momegas/megabots329Nuggt-dev/Nuggt315itamargol/openai315BlackHC/llm-strategy315aws-samples/aws-genai-llm-chatbot312Cheems-Seminar/grounded-segment-any-parts312preset-io/promptimize311dgarnitz/vectorflow309langchain-ai/langsmith-cookbook309CambioML/pykoi309wandb/edu301XzaiCloud/luna-ai300liangwq/Chatglm_lora_multi-gpu294Haste171/langchain-chatbot291sullivan-sean/chat-langchainjs286sugarforever/LangChain-Tutorials285facebookresearch/personal-timeline283hnawaz007/pythondataanalysis282yuanjie-ai/ChatLLM280MetaGLM/FinGLM279JohnSnowLabs/langtest277Em1tSan/NeuroGPT274Safiullah-Rahu/CSV-AI274conceptofmind/toolformer274airobotlab/KoChatGPT266gia-guar/JARVIS-ChatGPT263Mintplex-Labs/vector-admin262artitw/text2text262kaarthik108/snowChat261paolorechia/learn-langchain260shamspias/customizable-gpt-chatbot260ur-whitelab/exmol258hwchase17/chroma-langchain257bborn/howdoi.ai255ur-whitelab/chemcrow-public253pablomarin/GPT-Azure-Search-Engine251gustavz/DataChad249radi-cho/datasetGPT249ennucore/clippinator247recalign/RecAlign244lilacai/lilac243kaleido-lab/dolphin236iusztinpaul/hands-on-llms233PradipNichite/Youtube-Tutorials231shaman-ai/agent-actors231hwchase17/langchain-streamlit-template231yym68686/ChatGPT-Telegram-Bot226grumpyp/aixplora222su77ungr/CASALIOY222alvarosevilla95/autolang222arthur-ai/bench220miaoshouai/miaoshouai-assistant219AutoPackAI/beebot217edreisMD/plugnplai216nicknochnack/LangchainDocuments214AkshitIreddy/Interactive-LLM-Powered-NPCs213SpecterOps/Nemesis210kyegomez/swarms210wpydcr/LLM-Kit208orgexyz/BlockAGI204Chainlit/cookbook202WongSaang/chatgpt-ui-server202jbrukh/gpt-jargon202handrew/browserpilot202langchain-ai/web-explorer200plchld/InsightFlow200alphasecio/langchain-examples199Gentopia-AI/Gentopia198SamPink/dev-gpt196yasyf/compress-gpt196benthecoder/ClassGPT195voxel51/voxelgpt193CL-lau/SQL-GPT192blob42/Instrukt191streamlit/llm-examples191stepanogil/autonomous-hr-chatbot190TsinghuaDatabaseGroup/DB-GPT189PJLab-ADG/DriveLikeAHuman187Azure-Samples/azure-search-power-skills187microsoft/azure-openai-in-a-day-workshop187ju-bezdek/langchain-decorators182hardbyte/qabot181hongbo-miao/hongbomiao.com180QwenLM/Qwen-Agent179showlab/UniVTG179Azure-Samples/jp-azureopenai-samples176afaqueumer/DocQA174ethanyanjiali/minChatGPT174shauryr/S2QA174RoboCoachTechnologies/GPT-Synthesizer173chakkaradeep/pyCodeAGI172vaibkumr/prompt-optimizer171ccurme/yolopandas170anarchy-ai/LLM-VM169ray-project/langchain-ray169fengyuli-dev/multimedia-gpt169ibiscp/LLM-IMDB168mayooear/private-chatbot-mpt30b-langchain167OpenPluginACI/openplugin165jmpaz/promptlib165kjappelbaum/gptchem162JorisdeJong123/7-Days-of-LangChain161retr0reg/Ret2GPT161menloparklab/falcon-langchain159summarizepaper/summarizepaper158emarco177/ice_breaker157AmineDiro/cria156morpheuslord/HackBot156homanp/vercel-langchain156mlops-for-all/mlops-for-all.github.io155positive666/Prompt-Can-Anything154deeppavlov/dream153flurb18/AgentOoba151Open-Swarm-Net/GPT-Swarm151v7labs/benchllm150Klingefjord/chatgpt-telegram150Aggregate-Intellect/sherpa148Coding-Crashkurse/Langchain-Full-Course148SuperDuperDB/superduperdb147defenseunicorns/leapfrogai147menloparklab/langchain-cohere-qdrant-doc-retrieval147Jaseci-Labs/jaseci146realminchoi/babyagi-ui146iMagist486/ElasticSearch-Langchain-Chatglm2144peterw/StoryStorm143kulltc/chatgpt-sql142Teahouse-Studios/akari-bot142hirokidaichi/wanna141yasyf/summ141solana-labs/chatgpt-plugin140ssheng/BentoChain139mallahyari/drqa139petehunt/langchain-github-bot139dbpunk-labs/octogen138RedisVentures/redis-openai-qna138eunomia-bpf/GPTtrace138langchain-ai/langsmith-sdk137jina-ai/fastapi-serve137yeagerai/genworlds137aurelio-labs/arxiv-bot137luisroque/large_laguage_models136ChuloAI/BrainChulo1363Alan/DocsMind136KylinC/ChatFinance133langchain-ai/text-split-explorer133davila7/file-gpt133tencentmusic/supersonic132kimtth/azure-openai-llm-vector-langchain131ciare-robotics/world-creator129zenml-io/zenml-projects129log1stics/voice-generator-webui129snexus/llm-search129fixie-ai/fixie-examples128MedalCollector/Orator127grumpyp/chroma-langchain-tutorial127langchain-ai/langchain-aws-template127prof-frink-lab/slangchain126KMnO4-zx/huanhuan-chat124RCGAI/SimplyRetrieve124Dicklesworthstone/llama2_aided_tesseract123sdaaron/QueryGPT122athina-ai/athina-sdk121AIAnytime/Llama2-Medical-Chatbot121MuhammadMoinFaisal/LargeLanguageModelsProjects121Azure/business-process-automation121definitive-io/code-indexer-loop119nrl-ai/pautobot119Azure/app-service-linux-docs118zilliztech/akcio118CodeAlchemyAI/ViLT-GPT117georgesung/llm_qlora117nicknochnack/Nopenai115nftblackmagic/flask-langchain115mortium91/langchain-assistant115Ngonie-x/langchain_csv114wombyz/HormoziGPT114langchain-ai/langchain-teacher113mluogh/eastworld112mudler/LocalAGI112marimo-team/marimo111trancethehuman/entities-extraction-web-scraper111xuwenhao/mactalk-ai-course111dcaribou/transfermarkt-datasets111rabbitmetrics/langchain-13-min111dotvignesh/PDFChat111aws-samples/cdk-eks-blueprints-patterns110topoteretes/PromethAI-Backend110jlonge4/local_llama110RUC-GSAI/YuLan-Rec108gh18l/CrawlGPT107c0sogi/LLMChat107hwchase17/langchain-gradio-template107ArjanCodes/examples106genia-dev/GeniA105nexus-stc/stc105mbchang/data-driven-characters105ademakdogan/ChatSQL104crosleythomas/MirrorGPT104IvanIsCoding/ResuLLMe104avrabyt/MemoryBot104Azure/azure-sdk-tools103aniketmaurya/llm-inference103Anil-matcha/Youtube-to-chatbot103nyanp/chat2plot102aws-samples/amazon-kendra-langchain-extensions101atisharma/llama_farm100Xueheng-Li/SynologyChatbotGPT100Generated by github-dependents-infogithub-dependents-info --repo langchain-ai/langchain --markdownfile dependents.md --minstars 100 --sort starsPreviousMoreNextTutorials" 85,https://python.langchain.com/docs/additional_resources/tutorials,"MoreTutorialsOn this pageTutorialsBelow are links to tutorials and courses on LangChain. For written guides on common use cases for LangChain, check out the use cases guides.⛓ icon marks a new addition [last update 2023-09-21]DeepLearning.AI courses​ by Harrison Chase and Andrew NgLangChain for LLM Application DevelopmentLangChain Chat with Your DataHandbook​LangChain AI Handbook By James Briggs and Francisco InghamShort Tutorials​LangChain Explained in 13 Minutes | QuickStart Tutorial for Beginners by RabbitmetricsLangChain Crash Course: Build an AutoGPT app in 25 minutes by Nicholas RenotteLangChain Crash Course - Build apps with language models by Patrick LoeberTutorials​LangChain for Gen AI and LLMs by James Briggs​#1 Getting Started with GPT-3 vs. Open Source LLMs#2 Prompt Templates for GPT 3.5 and other LLMs#3 LLM Chains using GPT 3.5 and other LLMsLangChain Data Loaders, Tokenizers, Chunking, and Datasets - Data Prep 101#4 Chatbot Memory for Chat-GPT, Davinci + other LLMs#5 Chat with OpenAI in LangChain#6 Fixing LLM Hallucinations with Retrieval Augmentation in LangChain#7 LangChain Agents Deep Dive with GPT 3.5#8 Create Custom Tools for Chatbots in LangChain#9 Build Conversational Agents with Vector DBsUsing NEW MPT-7B in Hugging Face and LangChainMPT-30B Chatbot with LangChain⛓ Fine-tuning OpenAI's GPT 3.5 for LangChain Agents⛓ Chatbots with RAG: LangChain Full WalkthroughLangChain 101 by Greg Kamradt (Data Indy)​What Is LangChain? - LangChain + ChatGPT OverviewQuickstart GuideBeginner's Guide To 7 Essential ConceptsBeginner's Guide To 9 Use CasesAgents Overview + Google SearchesOpenAI + Wolfram AlphaAsk Questions On Your Custom (or Private) FilesConnect Google Drive Files To OpenAIYouTube Transcripts + OpenAIQuestion A 300 Page Book (w/ OpenAI + Pinecone)Workaround OpenAI's Token Limit With Chain TypesBuild Your Own OpenAI + LangChain Web App in 23 MinutesWorking With The New ChatGPT APIOpenAI + LangChain Wrote Me 100 Custom Sales EmailsStructured Output From OpenAI (Clean Dirty Data)Connect OpenAI To +5,000 Tools (LangChain + Zapier)Use LLMs To Extract Data From Text (Expert Mode)Extract Insights From Interview Transcripts Using LLMs5 Levels Of LLM Summarizing: Novice to ExpertControl Tone & Writing Style Of Your LLM OutputBuild Your Own AI Twitter Bot Using LLMsChatGPT made my interview questions for me (Streamlit + LangChain)Function Calling via ChatGPT API - First Look With LangChainExtract Topics From Video/Audio With LLMs (Topic Modeling w/ LangChain)LangChain How to and guides by Sam Witteveen​LangChain Basics - LLMs & PromptTemplates with ColabLangChain Basics - Tools and ChainsChatGPT API Announcement & Code Walkthrough with LangChainConversations with Memory (explanation & code walkthrough)Chat with Flan20BUsing Hugging Face Models locally (code walkthrough)PAL: Program-aided Language Models with LangChain codeBuilding a Summarization System with LangChain and GPT-3 - Part 1Building a Summarization System with LangChain and GPT-3 - Part 2Microsoft's Visual ChatGPT using LangChainLangChain Agents - Joining Tools and Chains with DecisionsComparing LLMs with LangChainUsing Constitutional AI in LangChainTalking to Alpaca with LangChain - Creating an Alpaca ChatbotTalk to your CSV & Excel with LangChainBabyAGI: Discover the Power of Task-Driven Autonomous Agents!Improve your BabyAGI with LangChainMaster PDF Chat with LangChain - Your essential guide to queries on documentsUsing LangChain with DuckDuckGO, Wikipedia & PythonREPL ToolsBuilding Custom Tools and Agents with LangChain (gpt-3.5-turbo)LangChain Retrieval QA Over Multiple Files with ChromaDBLangChain Retrieval QA with Instructor Embeddings & ChromaDB for PDFsLangChain + Retrieval Local LLMs for Retrieval QA - No OpenAI!!!Camel + LangChain for Synthetic Data & Market ResearchInformation Extraction with LangChain & KorConverting a LangChain App from OpenAI to OpenSourceUsing LangChain Output Parsers to get what you want out of LLMsBuilding a LangChain Custom Medical Agent with MemoryUnderstanding ReACT with LangChainOpenAI Functions + LangChain : Building a Multi Tool AgentWhat can you do with 16K tokens in LangChain?Tagging and Extraction - Classification using OpenAI FunctionsHOW to Make Conversational Form with LangChain⛓ Claude-2 meets LangChain!⛓ PaLM 2 Meets LangChain⛓ LLaMA2 with LangChain - Basics | LangChain TUTORIAL⛓ Serving LLaMA2 with Replicate⛓ NEW LangChain Expression Language⛓ Building a RCI Chain for Agents with LangChain Expression Language⛓ How to Run LLaMA-2-70B on the Together AI⛓ RetrievalQA with LLaMA 2 70b & Chroma DB⛓ How to use BGE Embeddings for LangChain⛓ How to use Custom Prompts for RetrievalQA on LLaMA-2 7BLangChain by Prompt Engineering​LangChain Crash Course — All You Need to Know to Build Powerful Apps with LLMsWorking with MULTIPLE PDF Files in LangChain: ChatGPT for your DataChatGPT for YOUR OWN PDF files with LangChainTalk to YOUR DATA without OpenAI APIs: LangChainLangChain: PDF Chat App (GUI) | ChatGPT for Your PDF FILESLangFlow: Build Chatbots without Writing CodeLangChain: Giving Memory to LLMsBEST OPEN Alternative to OPENAI's EMBEDDINGs for Retrieval QA: LangChainLangChain: Run Language Models Locally - Hugging Face Models ⛓ Slash API Costs: Mastering Caching for LLM Applications⛓ Avoid PROMPT INJECTION with Constitutional AI - LangChainLangChain by Chat with data​LangChain Beginner's Tutorial for Typescript/JavascriptGPT-4 Tutorial: How to Chat With Multiple PDF Files (~1000 pages of Tesla's 10-K Annual Reports)GPT-4 & LangChain Tutorial: How to Chat With A 56-Page PDF Document (w/Pinecone)LangChain & Supabase Tutorial: How to Build a ChatGPT Chatbot For Your WebsiteLangChain Agents: Build Personal Assistants For Your Data (Q&A with Harrison Chase and Mayo Oshin)Codebase Analysis​Codebase Analysis: Langchain Agents⛓ icon marks a new addition [last update 2023-09-21]PreviousDependentsNextYouTube videosDeepLearning.AI coursesHandbookShort TutorialsTutorialsLangChain for Gen AI and LLMs by James BriggsLangChain 101 by Greg Kamradt (Data Indy)LangChain How to and guides by Sam WitteveenLangChain by Prompt EngineeringLangChain by Chat with dataCodebase Analysis" 86,https://python.langchain.com/docs/additional_resources/youtube,"MoreYouTube videosOn this pageYouTube videos⛓ icon marks a new addition [last update 2023-09-21]Official LangChain YouTube channel​Introduction to LangChain with Harrison Chase, creator of LangChain​Building the Future with LLMs, LangChain, & Pinecone by PineconeLangChain and Weaviate with Harrison Chase and Bob van Luijt - Weaviate Podcast #36 by Weaviate • Vector DatabaseLangChain Demo + Q&A with Harrison Chase by Full Stack Deep LearningLangChain Agents: Build Personal Assistants For Your Data (Q&A with Harrison Chase and Mayo Oshin) by Chat with dataVideos (sorted by views)​Using ChatGPT with YOUR OWN Data. This is magical. (LangChain OpenAI API) by TechLeadFirst look - ChatGPT + WolframAlpha (GPT-3.5 and Wolfram|Alpha via LangChain by James Weaver) by Dr Alan D. Thompson LangChain explained - The hottest new Python framework by AssemblyAIChatbot with INFINITE MEMORY using OpenAI & Pinecone - GPT-3, Embeddings, ADA, Vector DB, Semantic by David Shapiro ~ AILangChain for LLMs is... basically just an Ansible playbook by David Shapiro ~ AIBuild your own LLM Apps with LangChain & GPT-Index by 1littlecoderBabyAGI - New System of Autonomous AI Agents with LangChain by 1littlecoderRun BabyAGI with Langchain Agents (with Python Code) by 1littlecoderHow to Use Langchain With Zapier | Write and Send Email with GPT-3 | OpenAI API Tutorial by StarMorph AIUse Your Locally Stored Files To Get Response From GPT - OpenAI | Langchain | Python by Shweta LodhaLangchain JS | How to Use GPT-3, GPT-4 to Reference your own Data | OpenAI Embeddings Intro by StarMorph AIThe easiest way to work with large language models | Learn LangChain in 10min by Sophia Yang4 Autonomous AI Agents: “Westworld” simulation BabyAGI, AutoGPT, Camel, LangChain by Sophia YangAI CAN SEARCH THE INTERNET? Langchain Agents + OpenAI ChatGPT by tylerwhatsgoodQuery Your Data with GPT-4 | Embeddings, Vector Databases | Langchain JS Knowledgebase by StarMorph AIWeaviate + LangChain for LLM apps presented by Erika Cardenas by Weaviate • Vector DatabaseLangchain Overview — How to Use Langchain & ChatGPT by Python In OfficeLangchain Overview - How to Use Langchain & ChatGPT by Python In OfficeLangChain Tutorials by Edrick:LangChain, Chroma DB, OpenAI Beginner Guide | ChatGPT with your PDFLangChain 101: The Complete Beginner's GuideCustom langchain Agent & Tools with memory. Turn any Python function into langchain tool with Gpt 3 by echohiveBuilding AI LLM Apps with LangChain (and more?) - LIVE STREAM by Nicholas RenotteChatGPT with any YouTube video using langchain and chromadb by echohiveHow to Talk to a PDF using LangChain and ChatGPT by Automata Learning LabLangchain Document Loaders Part 1: Unstructured Files by Merk LangChain - Prompt Templates (what all the best prompt engineers use) by Nick DaiglerLangChain. Crear aplicaciones Python impulsadas por GPT by Jesús CondeEasiest Way to Use GPT In Your Products | LangChain Basics Tutorial by Rachel WoodsBabyAGI + GPT-4 Langchain Agent with Internet Access by tylerwhatsgoodLearning LLM Agents. How does it actually work? LangChain, AutoGPT & OpenAI by Arnoldas KemeklisGet Started with LangChain in Node.js by Developers DigestLangChain + OpenAI tutorial: Building a Q&A system w/ own text data by Samuel ChanLangchain + Zapier Agent by MerkConnecting the Internet with ChatGPT (LLMs) using Langchain And Answers Your Questions by Kamalraj M MBuild More Powerful LLM Applications for Business’s with LangChain (Beginners Guide) by No Code BlackboxLangFlow LLM Agent Demo for 🦜🔗LangChain by Cobus GreylingChatbot Factory: Streamline Python Chatbot Creation with LLMs and Langchain by FinxterLangChain Tutorial - ChatGPT mit eigenen Daten by Coding CrashkurseChat with a CSV | LangChain Agents Tutorial (Beginners) by GoDataProfIntrodução ao Langchain - #Cortes - Live DataHackers by Prof. João Gabriel LimaLangChain: Level up ChatGPT !? | LangChain Tutorial Part 1 by Code AffinityKI schreibt krasses Youtube Skript 😲😳 | LangChain Tutorial Deutsch by SimpleKIChat with Audio: Langchain, Chroma DB, OpenAI, and Assembly AI by AI AnytimeQA over documents with Auto vector index selection with Langchain router chains by echohiveBuild your own custom LLM application with Bubble.io & Langchain (No Code & Beginner friendly) by No Code BlackboxSimple App to Question Your Docs: Leveraging Streamlit, Hugging Face Spaces, LangChain, and Claude! by Chris AlexiukLANGCHAIN AI- ConstitutionalChainAI + Databutton AI ASSISTANT Web App by AvraLANGCHAIN AI AUTONOMOUS AGENT WEB APP - 👶 BABY AGI 🤖 with EMAIL AUTOMATION using DATABUTTON by AvraThe Future of Data Analysis: Using A.I. Models in Data Analysis (LangChain) by Absent DataMemory in LangChain | Deep dive (python) by Eden Marco9 LangChain UseCases | Beginner's Guide | 2023 by Data Science BasicsUse Large Language Models in Jupyter Notebook | LangChain | Agents & Indexes by Abhinaw TiwariHow to Talk to Your Langchain Agent | 11 Labs + Whisper by VRSENLangChain Deep Dive: 5 FUN AI App Ideas To Build Quickly and Easily by James NoCodeLangChain 101: Models by Mckay WrigleyLangChain with JavaScript Tutorial #1 | Setup & Using LLMs by Leon van ZylLangChain Overview & Tutorial for Beginners: Build Powerful AI Apps Quickly & Easily (ZERO CODE) by James NoCodeLangChain In Action: Real-World Use Case With Step-by-Step Tutorial by RabbitmetricsSummarizing and Querying Multiple Papers with LangChain by Automata Learning LabUsing Langchain (and Replit) through Tana, ask Google/Wikipedia/Wolfram Alpha to fill out a table by Stian HåklevLangchain PDF App (GUI) | Create a ChatGPT For Your PDF in Python by Alejandro AO - Software & AiAuto-GPT with LangChain 🔥 | Create Your Own Personal AI Assistant by Data Science BasicsCreate Your OWN Slack AI Assistant with Python & LangChain by Dave EbbelaarHow to Create LOCAL Chatbots with GPT4All and LangChain [Full Guide] by Liam OttleyBuild a Multilingual PDF Search App with LangChain, Cohere and Bubble by Menlo Park LabBuilding a LangChain Agent (code-free!) Using Bubble and Flowise by Menlo Park LabBuild a LangChain-based Semantic PDF Search App with No-Code Tools Bubble and Flowise by Menlo Park LabLangChain Memory Tutorial | Building a ChatGPT Clone in Python by Alejandro AO - Software & AiChatGPT For Your DATA | Chat with Multiple Documents Using LangChain by Data Science BasicsLlama Index: Chat with Documentation using URL Loader by MerkUsing OpenAI, LangChain, and Gradio to Build Custom GenAI Applications by David HundleyLangChain, Chroma DB, OpenAI Beginner Guide | ChatGPT with your PDFBuild AI chatbot with custom knowledge base using OpenAI API and GPT Index by Irina NikBuild Your Own Auto-GPT Apps with LangChain (Python Tutorial) by Dave EbbelaarChat with Multiple PDFs | LangChain App Tutorial in Python (Free LLMs and Embeddings) by Alejandro AO - Software & AiChat with a CSV | LangChain Agents Tutorial (Beginners) by Alejandro AO - Software & AiCreate Your Own ChatGPT with PDF Data in 5 Minutes (LangChain Tutorial) by Liam OttleyBuild a Custom Chatbot with OpenAI: GPT-Index & LangChain | Step-by-Step Tutorial by FabrikodFlowise is an open source no-code UI visual tool to build 🦜🔗LangChain applications by Cobus GreylingLangChain & GPT 4 For Data Analysis: The Pandas Dataframe Agent by RabbitmetricsGirlfriendGPT - AI girlfriend with LangChain by Toolfinder AIHow to build with Langchain 10x easier | ⛓️ LangFlow & Flowise by AI JasonGetting Started With LangChain In 20 Minutes- Build Celebrity Search Application by Krish Naik⛓ Vector Embeddings Tutorial – Code Your Own AI Assistant with GPT-4 API + LangChain + NLP by FreeCodeCamp.org⛓ Fully LOCAL Llama 2 Q&A with LangChain by 1littlecoder⛓ Fully LOCAL Llama 2 Langchain on CPU by 1littlecoder⛓ Build LangChain Audio Apps with Python in 5 Minutes by AssemblyAI⛓ Voiceflow & Flowise: Want to Beat Competition? New Tutorial with Real AI Chatbot by AI SIMP⛓ THIS Is How You Build Production-Ready AI Apps (LangSmith Tutorial) by Dave Ebbelaar⛓ Build POWERFUL LLM Bots EASILY with Your Own Data - Embedchain - Langchain 2.0? (Tutorial) by WorldofAI⛓ Code Llama powered Gradio App for Coding: Runs on CPU by AI Anytime⛓ LangChain Complete Course in One Video | Develop LangChain (AI) Based Solutions for Your Business by UBprogrammer⛓ How to Run LLaMA Locally on CPU or GPU | Python & Langchain & CTransformers Guide by Code With Prince⛓ PyData Heidelberg #11 - TimeSeries Forecasting & LLM Langchain by PyData⛓ Prompt Engineering in Web Development | Using LangChain and Templates with OpenAI by Akamai Developer ⛓ Retrieval-Augmented Generation (RAG) using LangChain and Pinecone - The RAG Special Episode by Generative AI and Data Science On AWS⛓ LLAMA2 70b-chat Multiple Documents Chatbot with Langchain & Streamlit |All OPEN SOURCE|Replicate API by DataInsightEdge⛓ Chatting with 44K Fashion Products: LangChain Opportunities and Pitfalls by Rabbitmetrics⛓ Structured Data Extraction from ChatGPT with LangChain by MG⛓ Chat with Multiple PDFs using Llama 2, Pinecone and LangChain (Free LLMs and Embeddings) by Muhammad Moin⛓ Integrate Audio into LangChain.js apps in 5 Minutes by AssemblyAI⛓ ChatGPT for your data with Local LLM by Jacob Jedryszek⛓ Training Chatgpt with your personal data using langchain step by step in detail by NextGen Machines⛓ Use ANY language in LangSmith with REST by Nerding I/O⛓ How to Leverage the Full Potential of LLMs for Your Business with Langchain - Leon Ruddat by PyData⛓ ChatCSV App: Chat with CSV files using LangChain and Llama 2 by Muhammad MoinPrompt Engineering and LangChain by Venelin Valkov​Getting Started with LangChain: Load Custom Data, Run OpenAI Models, Embeddings and ChatGPTLoaders, Indexes & Vectorstores in LangChain: Question Answering on PDF files with ChatGPTLangChain Models: ChatGPT, Flan Alpaca, OpenAI Embeddings, Prompt Templates & StreamingLangChain Chains: Use ChatGPT to Build Conversational Agents, Summaries and Q&A on Text With LLMsAnalyze Custom CSV Data with GPT-4 using LangchainBuild ChatGPT Chatbots with LangChain Memory: Understanding and Implementing Memory in Conversations⛓ icon marks a new addition [last update 2023-09-21]PreviousTutorialsOfficial LangChain YouTube channelIntroduction to LangChain with Harrison Chase, creator of LangChainVideos (sorted by views)Prompt Engineering and LangChain by Venelin Valkov" 87,https://python.langchain.com/docs/use_cases/question_answering/,"Question AnsweringOn this pageQuestion AnsweringUse case​Suppose you have some text documents (PDF, blog, Notion pages, etc.) and want to ask questions related to the contents of those documents. LLMs, given their proficiency in understanding text, are a great tool for this.In this walkthrough we'll go over how to build a question-answering over documents application using LLMs. Two very related use cases which we cover elsewhere are:QA over structured data (e.g., SQL)QA over code (e.g., Python)Overview​The pipeline for converting raw unstructured data into a QA chain looks like this:Loading: First we need to load our data. Use the LangChain integration hub to browse the full set of loaders. Splitting: Text splitters break Documents into splits of specified sizeStorage: Storage (e.g., often a vectorstore) will house and often embed the splitsRetrieval: The app retrieves splits from storage (e.g., often with similar embeddings to the input question)Generation: An LLM produces an answer using a prompt that includes the question and the retrieved dataQuickstart​Suppose we want a QA app over this blog post. We can create this in a few lines of code. First set environment variables and install packages:pip install langchain openai chromadb langchainhub# Set env var OPENAI_API_KEY or load from a .env file# import dotenv# dotenv.load_dotenv()# Load documentsfrom langchain.document_loaders import WebBaseLoaderloader = WebBaseLoader(""https://lilianweng.github.io/posts/2023-06-23-agent/"")# Split documentsfrom langchain.text_splitter import RecursiveCharacterTextSplittertext_splitter = RecursiveCharacterTextSplitter(chunk_size = 500, chunk_overlap = 0)splits = text_splitter.split_documents(loader.load())# Embed and store splitsfrom langchain.vectorstores import Chromafrom langchain.embeddings import OpenAIEmbeddingsvectorstore = Chroma.from_documents(documents=splits,embedding=OpenAIEmbeddings())retriever = vectorstore.as_retriever()# Prompt # https://smith.langchain.com/hub/rlm/rag-promptfrom langchain import hubrag_prompt = hub.pull(""rlm/rag-prompt"")# LLMfrom langchain.chat_models import ChatOpenAIllm = ChatOpenAI(model_name=""gpt-3.5-turbo"", temperature=0)# RAG chain from langchain.schema.runnable import RunnablePassthroughrag_chain = ( {""context"": retriever, ""question"": RunnablePassthrough()} | rag_prompt | llm )rag_chain.invoke(""What is Task Decomposition?"") AIMessage(content='Task decomposition is the process of breaking down a task into smaller subgoals or steps. It can be done using simple prompting, task-specific instructions, or human inputs.')Here is the LangSmith trace for this chain.Below we will explain each step in more detail.Step 1. Load​Specify a DocumentLoader to load in your unstructured data as Documents. A Document is a dict with text (page_content) and metadata.from langchain.document_loaders import WebBaseLoaderloader = WebBaseLoader(""https://lilianweng.github.io/posts/2023-06-23-agent/"")data = loader.load()Go deeper​Browse the > 160 data loader integrations here.See further documentation on loaders here.Step 2. Split​Split the Document into chunks for embedding and vector storage.from langchain.text_splitter import RecursiveCharacterTextSplittertext_splitter = RecursiveCharacterTextSplitter(chunk_size = 500, chunk_overlap = 0)all_splits = text_splitter.split_documents(data)Go deeper​DocumentSplitters are just one type of the more generic DocumentTransformers.See further documentation on transformers here.Context-aware splitters keep the location (""context"") of each split in the original Document:Markdown filesCode (py or js)DocumentsStep 3. Store​To be able to look up our document splits, we first need to store them where we can later look them up.The most common way to do this is to embed the contents of each document split.We store the embedding and splits in a vectorstore.from langchain.embeddings import OpenAIEmbeddingsfrom langchain.vectorstores import Chromavectorstore = Chroma.from_documents(documents=all_splits, embedding=OpenAIEmbeddings())Go deeper​Browse the > 40 vectorstores integrations here.See further documentation on vectorstores here.Browse the > 30 text embedding integrations here.See further documentation on embedding models here.Here are Steps 1-3:Step 4. Retrieve​Retrieve relevant splits for any question using similarity search.This is simply ""top K"" retrieval where we select documents based on embedding similarity to the query.question = ""What are the approaches to Task Decomposition?""docs = vectorstore.similarity_search(question)len(docs) 4Go deeper​Vectorstores are commonly used for retrieval, but they are not the only option. For example, SVMs (see thread here) can also be used.LangChain has many retrievers including, but not limited to, vectorstores. All retrievers implement a common method get_relevant_documents() (and its asynchronous variant aget_relevant_documents()).from langchain.retrievers import SVMRetrieversvm_retriever = SVMRetriever.from_documents(all_splits,OpenAIEmbeddings())docs_svm=svm_retriever.get_relevant_documents(question)len(docs_svm) 4Some common ways to improve on vector similarity search include:MultiQueryRetriever generates variants of the input question to improve retrieval.Max marginal relevance selects for relevance and diversity among the retrieved documents.Documents can be filtered during retrieval using metadata filters.import loggingfrom langchain.chat_models import ChatOpenAIfrom langchain.retrievers.multi_query import MultiQueryRetrieverlogging.basicConfig()logging.getLogger('langchain.retrievers.multi_query').setLevel(logging.INFO)retriever_from_llm = MultiQueryRetriever.from_llm(retriever=vectorstore.as_retriever(), llm=ChatOpenAI(temperature=0))unique_docs = retriever_from_llm.get_relevant_documents(query=question)len(unique_docs)In addition, a useful concept for improving retrieval is decoupling the documents from the embedded search key.For example, we can embed a document summary or question that are likely to lead to the document being retrieved.See details in here on the multi-vector retriever for this purpose.Step 5. Generate​Distill the retrieved documents into an answer using an LLM/Chat model (e.g., gpt-3.5-turbo).We use the Runnable protocol to define the chain.Runnable protocol pipes together components in a transparent way.We used a prompt for RAG that is checked into the LangChain prompt hub (here).from langchain.chat_models import ChatOpenAIllm = ChatOpenAI(model_name=""gpt-3.5-turbo"", temperature=0)from langchain.schema.runnable import RunnablePassthroughrag_chain = ( {""context"": retriever, ""question"": RunnablePassthrough()} | rag_prompt | llm )rag_chain.invoke(""What is Task Decomposition?"") AIMessage(content='Task decomposition is the process of breaking down a task into smaller subgoals or steps. It can be done using simple prompting, task-specific instructions, or human inputs.')Go deeper​Choosing LLMs​Browse the > 90 LLM and chat model integrations here.See further documentation on LLMs and chat models here.See a guide on local LLMS here.Customizing the prompt​As shown above, we can load prompts (e.g., this RAG prompt) from the prompt hub.The prompt can also be easily customized, as shown below.from langchain.prompts import PromptTemplatetemplate = """"""Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer. Use three sentences maximum and keep the answer as concise as possible. Always say ""thanks for asking!"" at the end of the answer. {context}Question: {question}Helpful Answer:""""""rag_prompt_custom = PromptTemplate.from_template(template)rag_chain = ( {""context"": retriever, ""question"": RunnablePassthrough()} | rag_prompt_custom | llm )rag_chain.invoke(""What is Task Decomposition?"") AIMessage(content='Task decomposition is the process of breaking down a complicated task into smaller, more manageable subtasks or steps. It can be done using prompts, task-specific instructions, or human inputs. Thanks for asking!')We can use LangSmith to see the trace.NextQA using a RetrieverUse caseOverviewQuickstartStep 1. LoadGo deeperStep 2. SplitGo deeperStep 3. StoreGo deeperStep 4. RetrieveGo deeperStep 5. GenerateGo deeper" 88,https://python.langchain.com/docs/use_cases/question_answering/how_to/vector_db_qa,"Question AnsweringHow toQA using a RetrieverQA using a RetrieverThis example showcases question answering over an index.from langchain.chains import RetrievalQAfrom langchain.document_loaders import TextLoaderfrom langchain.embeddings.openai import OpenAIEmbeddingsfrom langchain.llms import OpenAIfrom langchain.text_splitter import CharacterTextSplitterfrom langchain.vectorstores import Chromaloader = TextLoader(""../../state_of_the_union.txt"")documents = loader.load()text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)texts = text_splitter.split_documents(documents)embeddings = OpenAIEmbeddings()docsearch = Chroma.from_documents(texts, embeddings)qa = RetrievalQA.from_chain_type(llm=OpenAI(), chain_type=""stuff"", retriever=docsearch.as_retriever())query = ""What did the president say about Ketanji Brown Jackson""qa.run(query) "" The president said that she is one of the nation's top legal minds, a former top litigator in private practice, a former federal public defender, and from a family of public school educators and police officers. He also said that she is a consensus builder and has received a broad range of support, from the Fraternal Order of Police to former judges appointed by Democrats and Republicans.""Chain Type​You can easily specify different chain types to load and use in the RetrievalQA chain. For a more detailed walkthrough of these types, please see this notebook.There are two ways to load different chain types. First, you can specify the chain type argument in the from_chain_type method. This allows you to pass in the name of the chain type you want to use. For example, in the below we change the chain type to map_reduce.qa = RetrievalQA.from_chain_type(llm=OpenAI(), chain_type=""map_reduce"", retriever=docsearch.as_retriever())query = ""What did the president say about Ketanji Brown Jackson""qa.run(query) "" The president said that Judge Ketanji Brown Jackson is one of our nation's top legal minds, a former top litigator in private practice and a former federal public defender, from a family of public school educators and police officers, a consensus builder and has received a broad range of support from the Fraternal Order of Police to former judges appointed by Democrats and Republicans.""The above way allows you to really simply change the chain_type, but it doesn't provide a ton of flexibility over parameters to that chain type. If you want to control those parameters, you can load the chain directly (as you did in this notebook) and then pass that directly to the the RetrievalQA chain with the combine_documents_chain parameter. For example:from langchain.chains.question_answering import load_qa_chainqa_chain = load_qa_chain(OpenAI(temperature=0), chain_type=""stuff"")qa = RetrievalQA(combine_documents_chain=qa_chain, retriever=docsearch.as_retriever())query = ""What did the president say about Ketanji Brown Jackson""qa.run(query) "" The president said that Ketanji Brown Jackson is one of the nation's top legal minds, a former top litigator in private practice, a former federal public defender, and from a family of public school educators and police officers. He also said that she is a consensus builder and has received a broad range of support from the Fraternal Order of Police to former judges appointed by Democrats and Republicans.""Custom Prompts​You can pass in custom prompts to do question answering. These prompts are the same prompts as you can pass into the base question answering chainfrom langchain.prompts import PromptTemplateprompt_template = """"""Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer.{context}Question: {question}Answer in Italian:""""""PROMPT = PromptTemplate( template=prompt_template, input_variables=[""context"", ""question""])chain_type_kwargs = {""prompt"": PROMPT}qa = RetrievalQA.from_chain_type(llm=OpenAI(), chain_type=""stuff"", retriever=docsearch.as_retriever(), chain_type_kwargs=chain_type_kwargs)query = ""What did the president say about Ketanji Brown Jackson""qa.run(query) "" Il presidente ha detto che Ketanji Brown Jackson è una delle menti legali più importanti del paese, che continuerà l'eccellenza di Justice Breyer e che ha ricevuto un ampio sostegno, da Fraternal Order of Police a ex giudici nominati da democratici e repubblicani.""Vectorstore Retriever Options​You can adjust how documents are retrieved from your vectorstore depending on the specific task.There are two main ways to retrieve documents relevant to a query- Similarity Search and Max Marginal Relevance Search (MMR Search). Similarity Search is the default, but you can use MMR by adding the search_type parameter:docsearch.as_retriever(search_type=""mmr"")You can also modify the search by passing specific search arguments through the retriever to the search function, using the search_kwargs keyword argument.k defines how many documents are returned; defaults to 4.score_threshold allows you to set a minimum relevance for documents returned by the retriever, if you are using the ""similarity_score_threshold"" search type.fetch_k determines the amount of documents to pass to the MMR algorithm; defaults to 20. lambda_mult controls the diversity of results returned by the MMR algorithm, with 1 being minimum diversity and 0 being maximum. Defaults to 0.5.filter allows you to define a filter on what documents should be retrieved, based on the documents' metadata. This has no effect if the Vectorstore doesn't store any metadata.Some examples for how these parameters can be used:# Retrieve more documents with higher diversity- useful if your dataset has many similar documentsdocsearch.as_retriever(search_type=""mmr"", search_kwargs={'k': 6, 'lambda_mult': 0.25})# Fetch more documents for the MMR algorithm to consider, but only return the top 5docsearch.as_retriever(search_type=""mmr"", search_kwargs={'k': 5, 'fetch_k': 50})# Only retrieve documents that have a relevance score above a certain thresholddocsearch.as_retriever(search_type=""similarity_score_threshold"", search_kwargs={'score_threshold': 0.8})# Only get the single most similar document from the datasetdocsearch.as_retriever(search_kwargs={'k': 1})# Use a filter to only retrieve documents from a specific paper docsearch.as_retriever(search_kwargs={'filter': {'paper_title':'GPT-4 Technical Report'}})Return Source Documents​Additionally, we can return the source documents used to answer the question by specifying an optional parameter when constructing the chain.qa = RetrievalQA.from_chain_type(llm=OpenAI(), chain_type=""stuff"", retriever=docsearch.as_retriever(search_type=""mmr"", search_kwargs={'fetch_k': 30}), return_source_documents=True)query = ""What did the president say about Ketanji Brown Jackson""result = qa({""query"": query})result[""result""] "" The president said that Ketanji Brown Jackson is one of the nation's top legal minds, a former top litigator in private practice and a former federal public defender from a family of public school educators and police officers, and that she has received a broad range of support from the Fraternal Order of Police to former judges appointed by Democrats and Republicans.""result[""source_documents""] [Document(page_content='Tonight. I call on the Senate to: Pass the Freedom to Vote Act. Pass the John Lewis Voting Rights Act. And while you’re at it, pass the Disclose Act so Americans can know who is funding our elections. \n\nTonight, I’d like to honor someone who has dedicated his life to serve this country: Justice Stephen Breyer—an Army veteran, Constitutional scholar, and retiring Justice of the United States Supreme Court. Justice Breyer, thank you for your service. \n\nOne of the most serious constitutional responsibilities a President has is nominating someone to serve on the United States Supreme Court. \n\nAnd I did that 4 days ago, when I nominated Circuit Court of Appeals Judge Ketanji Brown Jackson. One of our nation’s top legal minds, who will continue Justice Breyer’s legacy of excellence.', lookup_str='', metadata={'source': '../../state_of_the_union.txt'}, lookup_index=0), Document(page_content='A former top litigator in private practice. A former federal public defender. And from a family of public school educators and police officers. A consensus builder. Since she’s been nominated, she’s received a broad range of support—from the Fraternal Order of Police to former judges appointed by Democrats and Republicans. \n\nAnd if we are to advance liberty and justice, we need to secure the Border and fix the immigration system. \n\nWe can do both. At our border, we’ve installed new technology like cutting-edge scanners to better detect drug smuggling. \n\nWe’ve set up joint patrols with Mexico and Guatemala to catch more human traffickers. \n\nWe’re putting in place dedicated immigration judges so families fleeing persecution and violence can have their cases heard faster. \n\nWe’re securing commitments and supporting partners in South and Central America to host more refugees and secure their own borders.', lookup_str='', metadata={'source': '../../state_of_the_union.txt'}, lookup_index=0), Document(page_content='And for our LGBTQ+ Americans, let’s finally get the bipartisan Equality Act to my desk. The onslaught of state laws targeting transgender Americans and their families is wrong. \n\nAs I said last year, especially to our younger transgender Americans, I will always have your back as your President, so you can be yourself and reach your God-given potential. \n\nWhile it often appears that we never agree, that isn’t true. I signed 80 bipartisan bills into law last year. From preventing government shutdowns to protecting Asian-Americans from still-too-common hate crimes to reforming military justice. \n\nAnd soon, we’ll strengthen the Violence Against Women Act that I first wrote three decades ago. It is important for us to show the nation that we can come together and do big things. \n\nSo tonight I’m offering a Unity Agenda for the Nation. Four big things we can do together. \n\nFirst, beat the opioid epidemic.', lookup_str='', metadata={'source': '../../state_of_the_union.txt'}, lookup_index=0), Document(page_content='Tonight, I’m announcing a crackdown on these companies overcharging American businesses and consumers. \n\nAnd as Wall Street firms take over more nursing homes, quality in those homes has gone down and costs have gone up. \n\nThat ends on my watch. \n\nMedicare is going to set higher standards for nursing homes and make sure your loved ones get the care they deserve and expect. \n\nWe’ll also cut costs and keep the economy going strong by giving workers a fair shot, provide more training and apprenticeships, hire them based on their skills not degrees. \n\nLet’s pass the Paycheck Fairness Act and paid leave. \n\nRaise the minimum wage to $15 an hour and extend the Child Tax Credit, so no one has to raise a family in poverty. \n\nLet’s increase Pell Grants and increase our historic support of HBCUs, and invest in what Jill—our First Lady who teaches full-time—calls America’s best-kept secret: community colleges.', lookup_str='', metadata={'source': '../../state_of_the_union.txt'}, lookup_index=0)]Alternatively, if our document have a ""source"" metadata key, we can use the RetrievalQAWithSourcesChain to cite our sources:docsearch = Chroma.from_texts(texts, embeddings, metadatas=[{""source"": f""{i}-pl""} for i in range(len(texts))])from langchain.chains import RetrievalQAWithSourcesChainfrom langchain.llms import OpenAIchain = RetrievalQAWithSourcesChain.from_chain_type(OpenAI(temperature=0), chain_type=""stuff"", retriever=docsearch.as_retriever())chain({""question"": ""What did the president say about Justice Breyer""}, return_only_outputs=True) {'answer': ' The president honored Justice Breyer for his service and mentioned his legacy of excellence.\n', 'sources': '31-pl'}PreviousQuestion AnsweringNextStore and reference chat history" 89,https://python.langchain.com/docs/use_cases/question_answering/how_to/chat_vector_db,"Question AnsweringHow toStore and reference chat historyStore and reference chat historyThe ConversationalRetrievalQA chain builds on RetrievalQAChain to provide a chat history component.It first combines the chat history (either explicitly passed in or retrieved from the provided memory) and the question into a standalone question, then looks up relevant documents from the retriever, and finally passes those documents and the question to a question-answering chain to return a response.To create one, you will need a retriever. In the below example, we will create one from a vector store, which can be created from embeddings.from langchain.embeddings.openai import OpenAIEmbeddingsfrom langchain.vectorstores import Chromafrom langchain.text_splitter import CharacterTextSplitterfrom langchain.llms import OpenAIfrom langchain.chains import ConversationalRetrievalChainLoad in documents. You can replace this with a loader for whatever type of data you wantfrom langchain.document_loaders import TextLoaderloader = TextLoader(""../../state_of_the_union.txt"")documents = loader.load()If you had multiple loaders that you wanted to combine, you do something like:# loaders = [....]# docs = []# for loader in loaders:# docs.extend(loader.load())We now split the documents, create embeddings for them, and put them in a vectorstore. This allows us to do semantic search over them.text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)documents = text_splitter.split_documents(documents)embeddings = OpenAIEmbeddings()vectorstore = Chroma.from_documents(documents, embeddings) Using embedded DuckDB without persistence: data will be transientWe can now create a memory object, which is necessary to track the inputs/outputs and hold a conversation.from langchain.memory import ConversationBufferMemorymemory = ConversationBufferMemory(memory_key=""chat_history"", return_messages=True)We now initialize the ConversationalRetrievalChainqa = ConversationalRetrievalChain.from_llm(OpenAI(temperature=0), vectorstore.as_retriever(), memory=memory)query = ""What did the president say about Ketanji Brown Jackson""result = qa({""question"": query})result[""answer""] "" The president said that Ketanji Brown Jackson is one of the nation's top legal minds, a former top litigator in private practice, a former federal public defender, and from a family of public school educators and police officers. He also said that she is a consensus builder and has received a broad range of support from the Fraternal Order of Police to former judges appointed by Democrats and Republicans.""query = ""Did he mention who she succeeded""result = qa({""question"": query})result['answer'] ' Ketanji Brown Jackson succeeded Justice Stephen Breyer on the United States Supreme Court.'Pass in chat history​In the above example, we used a Memory object to track chat history. We can also just pass it in explicitly. In order to do this, we need to initialize a chain without any memory object.qa = ConversationalRetrievalChain.from_llm(OpenAI(temperature=0), vectorstore.as_retriever())Here's an example of asking a question with no chat historychat_history = []query = ""What did the president say about Ketanji Brown Jackson""result = qa({""question"": query, ""chat_history"": chat_history})result[""answer""] "" The president said that Ketanji Brown Jackson is one of the nation's top legal minds, a former top litigator in private practice, a former federal public defender, and from a family of public school educators and police officers. He also said that she is a consensus builder and has received a broad range of support from the Fraternal Order of Police to former judges appointed by Democrats and Republicans.""Here's an example of asking a question with some chat historychat_history = [(query, result[""answer""])]query = ""Did he mention who she succeeded""result = qa({""question"": query, ""chat_history"": chat_history})result['answer'] ' Ketanji Brown Jackson succeeded Justice Stephen Breyer on the United States Supreme Court.'Using a different model for condensing the question​This chain has two steps. First, it condenses the current question and the chat history into a standalone question. This is necessary to create a standanlone vector to use for retrieval. After that, it does retrieval and then answers the question using retrieval augmented generation with a separate model. Part of the power of the declarative nature of LangChain is that you can easily use a separate language model for each call. This can be useful to use a cheaper and faster model for the simpler task of condensing the question, and then a more expensive model for answering the question. Here is an example of doing so.from langchain.chat_models import ChatOpenAIqa = ConversationalRetrievalChain.from_llm( ChatOpenAI(temperature=0, model=""gpt-4""), vectorstore.as_retriever(), condense_question_llm = ChatOpenAI(temperature=0, model='gpt-3.5-turbo'),)chat_history = []query = ""What did the president say about Ketanji Brown Jackson""result = qa({""question"": query, ""chat_history"": chat_history})chat_history = [(query, result[""answer""])]query = ""Did he mention who she succeeded""result = qa({""question"": query, ""chat_history"": chat_history})Using a custom prompt for condensing the question​By default, ConversationalRetrievalQA uses CONDENSE_QUESTION_PROMPT to condense a question. Here is the implementation of this in the docsfrom langchain.prompts.prompt import PromptTemplate_template = """"""Given the following conversation and a follow up question, rephrase the follow up question to be a standalone question, in its original language.Chat History:{chat_history}Follow Up Input: {question}Standalone question:""""""CONDENSE_QUESTION_PROMPT = PromptTemplate.from_template(_template)But instead of this any custom template can be used to further augment information in the question or instruct the LLM to do something. Here is an examplefrom langchain.prompts.prompt import PromptTemplatecustom_template = """"""Given the following conversation and a follow up question, rephrase the follow up question to be a standalone question. At the end of standalone question add this 'Answer the question in German language.' If you do not know the answer reply with 'I am sorry'.Chat History:{chat_history}Follow Up Input: {question}Standalone question:""""""CUSTOM_QUESTION_PROMPT = PromptTemplate.from_template(custom_template)model = ChatOpenAI(model_name=""gpt-3.5-turbo"", temperature=0.3)embeddings = OpenAIEmbeddings()vectordb = Chroma(embedding_function=embeddings, persist_directory=directory)memory = ConversationBufferMemory(memory_key=""chat_history"", return_messages=True)qa = ConversationalRetrievalChain.from_llm( model, vectordb.as_retriever(), condense_question_prompt=CUSTOM_QUESTION_PROMPT, memory=memory)query = ""What did the president say about Ketanji Brown Jackson""result = qa({""question"": query})query = ""Did he mention who she succeeded""result = qa({""question"": query})Return Source Documents​You can also easily return source documents from the ConversationalRetrievalChain. This is useful for when you want to inspect what documents were returned.qa = ConversationalRetrievalChain.from_llm(OpenAI(temperature=0), vectorstore.as_retriever(), return_source_documents=True)chat_history = []query = ""What did the president say about Ketanji Brown Jackson""result = qa({""question"": query, ""chat_history"": chat_history})result['source_documents'][0] Document(page_content='Tonight. I call on the Senate to: Pass the Freedom to Vote Act. Pass the John Lewis Voting Rights Act. And while you’re at it, pass the Disclose Act so Americans can know who is funding our elections. \n\nTonight, I’d like to honor someone who has dedicated his life to serve this country: Justice Stephen Breyer—an Army veteran, Constitutional scholar, and retiring Justice of the United States Supreme Court. Justice Breyer, thank you for your service. \n\nOne of the most serious constitutional responsibilities a President has is nominating someone to serve on the United States Supreme Court. \n\nAnd I did that 4 days ago, when I nominated Circuit Court of Appeals Judge Ketanji Brown Jackson. One of our nation’s top legal minds, who will continue Justice Breyer’s legacy of excellence.', metadata={'source': '../../state_of_the_union.txt'})ConversationalRetrievalChain with search_distance​If you are using a vector store that supports filtering by search distance, you can add a threshold value parameter.vectordbkwargs = {""search_distance"": 0.9}qa = ConversationalRetrievalChain.from_llm(OpenAI(temperature=0), vectorstore.as_retriever(), return_source_documents=True)chat_history = []query = ""What did the president say about Ketanji Brown Jackson""result = qa({""question"": query, ""chat_history"": chat_history, ""vectordbkwargs"": vectordbkwargs})ConversationalRetrievalChain with map_reduce​We can also use different types of combine document chains with the ConversationalRetrievalChain chain.from langchain.chains import LLMChainfrom langchain.chains.question_answering import load_qa_chainfrom langchain.chains.conversational_retrieval.prompts import CONDENSE_QUESTION_PROMPTllm = OpenAI(temperature=0)question_generator = LLMChain(llm=llm, prompt=CONDENSE_QUESTION_PROMPT)doc_chain = load_qa_chain(llm, chain_type=""map_reduce"")chain = ConversationalRetrievalChain( retriever=vectorstore.as_retriever(), question_generator=question_generator, combine_docs_chain=doc_chain,)chat_history = []query = ""What did the president say about Ketanji Brown Jackson""result = chain({""question"": query, ""chat_history"": chat_history})result['answer'] "" The president said that Ketanji Brown Jackson is one of the nation's top legal minds, a former top litigator in private practice, a former federal public defender, from a family of public school educators and police officers, a consensus builder, and has received a broad range of support from the Fraternal Order of Police to former judges appointed by Democrats and Republicans.""ConversationalRetrievalChain with Question Answering with sources​You can also use this chain with the question answering with sources chain.from langchain.chains.qa_with_sources import load_qa_with_sources_chainllm = OpenAI(temperature=0)question_generator = LLMChain(llm=llm, prompt=CONDENSE_QUESTION_PROMPT)doc_chain = load_qa_with_sources_chain(llm, chain_type=""map_reduce"")chain = ConversationalRetrievalChain( retriever=vectorstore.as_retriever(), question_generator=question_generator, combine_docs_chain=doc_chain,)chat_history = []query = ""What did the president say about Ketanji Brown Jackson""result = chain({""question"": query, ""chat_history"": chat_history})result['answer'] "" The president said that Ketanji Brown Jackson is one of the nation's top legal minds, a former top litigator in private practice, a former federal public defender, from a family of public school educators and police officers, a consensus builder, and has received a broad range of support from the Fraternal Order of Police to former judges appointed by Democrats and Republicans. \nSOURCES: ../../state_of_the_union.txt""ConversationalRetrievalChain with streaming to stdout​Output from the chain will be streamed to stdout token by token in this example.from langchain.chains.llm import LLMChainfrom langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandlerfrom langchain.chains.conversational_retrieval.prompts import CONDENSE_QUESTION_PROMPT, QA_PROMPTfrom langchain.chains.question_answering import load_qa_chain# Construct a ConversationalRetrievalChain with a streaming llm for combine docs# and a separate, non-streaming llm for question generationllm = OpenAI(temperature=0)streaming_llm = OpenAI(streaming=True, callbacks=[StreamingStdOutCallbackHandler()], temperature=0)question_generator = LLMChain(llm=llm, prompt=CONDENSE_QUESTION_PROMPT)doc_chain = load_qa_chain(streaming_llm, chain_type=""stuff"", prompt=QA_PROMPT)qa = ConversationalRetrievalChain( retriever=vectorstore.as_retriever(), combine_docs_chain=doc_chain, question_generator=question_generator)chat_history = []query = ""What did the president say about Ketanji Brown Jackson""result = qa({""question"": query, ""chat_history"": chat_history}) The president said that Ketanji Brown Jackson is one of the nation's top legal minds, a former top litigator in private practice, a former federal public defender, and from a family of public school educators and police officers. He also said that she is a consensus builder and has received a broad range of support from the Fraternal Order of Police to former judges appointed by Democrats and Republicans.chat_history = [(query, result[""answer""])]query = ""Did he mention who she succeeded""result = qa({""question"": query, ""chat_history"": chat_history}) Ketanji Brown Jackson succeeded Justice Stephen Breyer on the United States Supreme Court.get_chat_history Function​You can also specify a get_chat_history function, which can be used to format the chat_history string.def get_chat_history(inputs) -> str: res = [] for human, ai in inputs: res.append(f""Human:{human}\nAI:{ai}"") return ""\n"".join(res)qa = ConversationalRetrievalChain.from_llm(OpenAI(temperature=0), vectorstore.as_retriever(), get_chat_history=get_chat_history)chat_history = []query = ""What did the president say about Ketanji Brown Jackson""result = qa({""question"": query, ""chat_history"": chat_history})result['answer'] "" The president said that Ketanji Brown Jackson is one of the nation's top legal minds, a former top litigator in private practice, a former federal public defender, and from a family of public school educators and police officers. He also said that she is a consensus builder and has received a broad range of support from the Fraternal Order of Police to former judges appointed by Democrats and Republicans.""PreviousQA using a RetrieverNextCode understanding" 90,https://python.langchain.com/docs/use_cases/question_answering/how_to/code/,"Question AnsweringHow toCode understandingOn this pageCode understandingOverviewLangChain is a useful tool designed to parse GitHub code repositories. By leveraging VectorStores, Conversational RetrieverChain, and GPT-4, it can answer questions in the context of an entire GitHub repository or generate new code. This documentation page outlines the essential components of the system and guides using LangChain for better code comprehension, contextual question answering, and code generation in GitHub repositories.Conversational Retriever Chain​Conversational RetrieverChain is a retrieval-focused system that interacts with the data stored in a VectorStore. Utilizing advanced techniques, like context-aware filtering and ranking, it retrieves the most relevant code snippets and information for a given user query. Conversational RetrieverChain is engineered to deliver high-quality, pertinent results while considering conversation history and context.LangChain Workflow for Code Understanding and GenerationIndex the code base: Clone the target repository, load all files within, chunk the files, and execute the indexing process. Optionally, you can skip this step and use an already indexed dataset.Embedding and Code Store: Code snippets are embedded using a code-aware embedding model and stored in a VectorStore. Query Understanding: GPT-4 processes user queries, grasping the context and extracting relevant details.Construct the Retriever: Conversational RetrieverChain searches the VectorStore to identify the most relevant code snippets for a given query.Build the Conversational Chain: Customize the retriever settings and define any user-defined filters as needed. Ask questions: Define a list of questions to ask about the codebase, and then use the ConversationalRetrievalChain to generate context-aware answers. The LLM (GPT-4) generates comprehensive, context-aware answers based on retrieved code snippets and conversation history.The full tutorial is available below.Twitter the-algorithm codebase analysis with Deep Lake: A notebook walking through how to parse github source code and run queries conversation.LangChain codebase analysis with Deep Lake: A notebook walking through how to analyze and do question answering over THIS code base.PreviousStore and reference chat historyNextUse LangChain, GPT and Activeloop's Deep Lake to work with code baseConversational Retriever Chain" 91,https://python.langchain.com/docs/use_cases/question_answering/how_to/code/code-analysis-deeplake,"Question AnsweringHow toCode understandingUse LangChain, GPT and Activeloop's Deep Lake to work with code baseOn this pageUse LangChain, GPT and Activeloop's Deep Lake to work with code baseIn this tutorial, we are going to use Langchain + Activeloop's Deep Lake with GPT to analyze the code base of the LangChain itself. Design​Prepare data:Upload all python project files using the langchain.document_loaders.TextLoader. We will call these files the documents.Split all documents to chunks using the langchain.text_splitter.CharacterTextSplitter.Embed chunks and upload them into the DeepLake using langchain.embeddings.openai.OpenAIEmbeddings and langchain.vectorstores.DeepLakeQuestion-Answering:Build a chain from langchain.chat_models.ChatOpenAI and langchain.chains.ConversationalRetrievalChainPrepare questions.Get answers running the chain.Implementation​Integration preparations​We need to set up keys for external services and install necessary python libraries.#!python3 -m pip install --upgrade langchain deeplake openaiSet up OpenAI embeddings, Deep Lake multi-modal vector store api and authenticate. For full documentation of Deep Lake please follow https://docs.activeloop.ai/ and API reference https://docs.deeplake.ai/en/latest/import osfrom getpass import getpassos.environ[""OPENAI_API_KEY""] = getpass()# Please manually enter OpenAI KeyAuthenticate into Deep Lake if you want to create your own dataset and publish it. You can get an API key from the platform at app.activeloop.aiactiveloop_token = getpass(""Activeloop Token:"")os.environ[""ACTIVELOOP_TOKEN""] = activeloop_tokenPrepare data​Load all repository files. Here we assume this notebook is downloaded as the part of the langchain fork and we work with the python files of the langchain repo.If you want to use files from different repo, change root_dir to the root dir of your repo.ls ""../../../../../../libs"" CITATION.cff MIGRATE.md README.md libs poetry.toml LICENSE Makefile docs poetry.lock pyproject.tomlfrom langchain.document_loaders import TextLoaderroot_dir = ""../../../../../../libs""docs = []for dirpath, dirnames, filenames in os.walk(root_dir): for file in filenames: if file.endswith("".py"") and ""*venv/"" not in dirpath: try: loader = TextLoader(os.path.join(dirpath, file), encoding=""utf-8"") docs.extend(loader.load_and_split()) except Exception as e: passprint(f""{len(docs)}"") 2554Then, chunk the filesfrom langchain.text_splitter import CharacterTextSplittertext_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)texts = text_splitter.split_documents(docs)print(f""{len(texts)}"") Created a chunk of size 1010, which is longer than the specified 1000 Created a chunk of size 3466, which is longer than the specified 1000 Created a chunk of size 1375, which is longer than the specified 1000 Created a chunk of size 1928, which is longer than the specified 1000 Created a chunk of size 1075, which is longer than the specified 1000 Created a chunk of size 1063, which is longer than the specified 1000 Created a chunk of size 1083, which is longer than the specified 1000 Created a chunk of size 1074, which is longer than the specified 1000 Created a chunk of size 1591, which is longer than the specified 1000 Created a chunk of size 2300, which is longer than the specified 1000 Created a chunk of size 1040, which is longer than the specified 1000 Created a chunk of size 1018, which is longer than the specified 1000 Created a chunk of size 2787, which is longer than the specified 1000 Created a chunk of size 1018, which is longer than the specified 1000 Created a chunk of size 2311, which is longer than the specified 1000 Created a chunk of size 2811, which is longer than the specified 1000 Created a chunk of size 1186, which is longer than the specified 1000 Created a chunk of size 1497, which is longer than the specified 1000 Created a chunk of size 1043, which is longer than the specified 1000 Created a chunk of size 1020, which is longer than the specified 1000 Created a chunk of size 1232, which is longer than the specified 1000 Created a chunk of size 1334, which is longer than the specified 1000 Created a chunk of size 1221, which is longer than the specified 1000 Created a chunk of size 2229, which is longer than the specified 1000 Created a chunk of size 1027, which is longer than the specified 1000 Created a chunk of size 1361, which is longer than the specified 1000 Created a chunk of size 1057, which is longer than the specified 1000 Created a chunk of size 1204, which is longer than the specified 1000 Created a chunk of size 1420, which is longer than the specified 1000 Created a chunk of size 1298, which is longer than the specified 1000 Created a chunk of size 1062, which is longer than the specified 1000 Created a chunk of size 1008, which is longer than the specified 1000 Created a chunk of size 1025, which is longer than the specified 1000 Created a chunk of size 1206, which is longer than the specified 1000 Created a chunk of size 1202, which is longer than the specified 1000 Created a chunk of size 1206, which is longer than the specified 1000 Created a chunk of size 1272, which is longer than the specified 1000 Created a chunk of size 1092, which is longer than the specified 1000 Created a chunk of size 1303, which is longer than the specified 1000 Created a chunk of size 1029, which is longer than the specified 1000 Created a chunk of size 1117, which is longer than the specified 1000 Created a chunk of size 1438, which is longer than the specified 1000 Created a chunk of size 3055, which is longer than the specified 1000 Created a chunk of size 1628, which is longer than the specified 1000 Created a chunk of size 1566, which is longer than the specified 1000 Created a chunk of size 1179, which is longer than the specified 1000 Created a chunk of size 1006, which is longer than the specified 1000 Created a chunk of size 1213, which is longer than the specified 1000 Created a chunk of size 2461, which is longer than the specified 1000 Created a chunk of size 1849, which is longer than the specified 1000 Created a chunk of size 1398, which is longer than the specified 1000 Created a chunk of size 1469, which is longer than the specified 1000 Created a chunk of size 1220, which is longer than the specified 1000 Created a chunk of size 1048, which is longer than the specified 1000 Created a chunk of size 1040, which is longer than the specified 1000 Created a chunk of size 1052, which is longer than the specified 1000 Created a chunk of size 1052, which is longer than the specified 1000 Created a chunk of size 1304, which is longer than the specified 1000 Created a chunk of size 1147, which is longer than the specified 1000 Created a chunk of size 1236, which is longer than the specified 1000 Created a chunk of size 1411, which is longer than the specified 1000 Created a chunk of size 1181, which is longer than the specified 1000 Created a chunk of size 1357, which is longer than the specified 1000 Created a chunk of size 1706, which is longer than the specified 1000 Created a chunk of size 1099, which is longer than the specified 1000 Created a chunk of size 1221, which is longer than the specified 1000 Created a chunk of size 1066, which is longer than the specified 1000 Created a chunk of size 1223, which is longer than the specified 1000 Created a chunk of size 1202, which is longer than the specified 1000 Created a chunk of size 2806, which is longer than the specified 1000 Created a chunk of size 1180, which is longer than the specified 1000 Created a chunk of size 1338, which is longer than the specified 1000 Created a chunk of size 1074, which is longer than the specified 1000 Created a chunk of size 1025, which is longer than the specified 1000 Created a chunk of size 1017, which is longer than the specified 1000 Created a chunk of size 1497, which is longer than the specified 1000 Created a chunk of size 1151, which is longer than the specified 1000 Created a chunk of size 1287, which is longer than the specified 1000 Created a chunk of size 1359, which is longer than the specified 1000 Created a chunk of size 1075, which is longer than the specified 1000 Created a chunk of size 1037, which is longer than the specified 1000 Created a chunk of size 1080, which is longer than the specified 1000 Created a chunk of size 1354, which is longer than the specified 1000 Created a chunk of size 1033, which is longer than the specified 1000 Created a chunk of size 1473, which is longer than the specified 1000 Created a chunk of size 1074, which is longer than the specified 1000 Created a chunk of size 2091, which is longer than the specified 1000 Created a chunk of size 1388, which is longer than the specified 1000 Created a chunk of size 1040, which is longer than the specified 1000 Created a chunk of size 1040, which is longer than the specified 1000 Created a chunk of size 1158, which is longer than the specified 1000 Created a chunk of size 1683, which is longer than the specified 1000 Created a chunk of size 2424, which is longer than the specified 1000 Created a chunk of size 1877, which is longer than the specified 1000 Created a chunk of size 1002, which is longer than the specified 1000 Created a chunk of size 2175, which is longer than the specified 1000 Created a chunk of size 1011, which is longer than the specified 1000 Created a chunk of size 1915, which is longer than the specified 1000 Created a chunk of size 1587, which is longer than the specified 1000 Created a chunk of size 1969, which is longer than the specified 1000 Created a chunk of size 1687, which is longer than the specified 1000 Created a chunk of size 1732, which is longer than the specified 1000 Created a chunk of size 1322, which is longer than the specified 1000 Created a chunk of size 1339, which is longer than the specified 1000 Created a chunk of size 3083, which is longer than the specified 1000 Created a chunk of size 2148, which is longer than the specified 1000 Created a chunk of size 1647, which is longer than the specified 1000 Created a chunk of size 1698, which is longer than the specified 1000 Created a chunk of size 1012, which is longer than the specified 1000 Created a chunk of size 1919, which is longer than the specified 1000 Created a chunk of size 1676, which is longer than the specified 1000 Created a chunk of size 1581, which is longer than the specified 1000 Created a chunk of size 2559, which is longer than the specified 1000 Created a chunk of size 1247, which is longer than the specified 1000 Created a chunk of size 1220, which is longer than the specified 1000 Created a chunk of size 1768, which is longer than the specified 1000 Created a chunk of size 1287, which is longer than the specified 1000 Created a chunk of size 1300, which is longer than the specified 1000 Created a chunk of size 1390, which is longer than the specified 1000 Created a chunk of size 1423, which is longer than the specified 1000 Created a chunk of size 1018, which is longer than the specified 1000 Created a chunk of size 1185, which is longer than the specified 1000 Created a chunk of size 2858, which is longer than the specified 1000 Created a chunk of size 1149, which is longer than the specified 1000 Created a chunk of size 1730, which is longer than the specified 1000 Created a chunk of size 1026, which is longer than the specified 1000 Created a chunk of size 1913, which is longer than the specified 1000 Created a chunk of size 1362, which is longer than the specified 1000 Created a chunk of size 1324, which is longer than the specified 1000 Created a chunk of size 1073, which is longer than the specified 1000 Created a chunk of size 1455, which is longer than the specified 1000 Created a chunk of size 1621, which is longer than the specified 1000 Created a chunk of size 1516, which is longer than the specified 1000 Created a chunk of size 1633, which is longer than the specified 1000 Created a chunk of size 1620, which is longer than the specified 1000 Created a chunk of size 1856, which is longer than the specified 1000 Created a chunk of size 1562, which is longer than the specified 1000 Created a chunk of size 1729, which is longer than the specified 1000 Created a chunk of size 1203, which is longer than the specified 1000 Created a chunk of size 1307, which is longer than the specified 1000 Created a chunk of size 1331, which is longer than the specified 1000 Created a chunk of size 1295, which is longer than the specified 1000 Created a chunk of size 1101, which is longer than the specified 1000 Created a chunk of size 1090, which is longer than the specified 1000 Created a chunk of size 1241, which is longer than the specified 1000 Created a chunk of size 1138, which is longer than the specified 1000 Created a chunk of size 1076, which is longer than the specified 1000 Created a chunk of size 1210, which is longer than the specified 1000 Created a chunk of size 1183, which is longer than the specified 1000 Created a chunk of size 1353, which is longer than the specified 1000 Created a chunk of size 1271, which is longer than the specified 1000 Created a chunk of size 1778, which is longer than the specified 1000 Created a chunk of size 1141, which is longer than the specified 1000 Created a chunk of size 1099, which is longer than the specified 1000 Created a chunk of size 2090, which is longer than the specified 1000 Created a chunk of size 1056, which is longer than the specified 1000 Created a chunk of size 1120, which is longer than the specified 1000 Created a chunk of size 1048, which is longer than the specified 1000 Created a chunk of size 1072, which is longer than the specified 1000 Created a chunk of size 1367, which is longer than the specified 1000 Created a chunk of size 1246, which is longer than the specified 1000 Created a chunk of size 1766, which is longer than the specified 1000 Created a chunk of size 1105, which is longer than the specified 1000 Created a chunk of size 1400, which is longer than the specified 1000 Created a chunk of size 1488, which is longer than the specified 1000 Created a chunk of size 1672, which is longer than the specified 1000 Created a chunk of size 1137, which is longer than the specified 1000 Created a chunk of size 1500, which is longer than the specified 1000 Created a chunk of size 1224, which is longer than the specified 1000 Created a chunk of size 1414, which is longer than the specified 1000 Created a chunk of size 1242, which is longer than the specified 1000 Created a chunk of size 1551, which is longer than the specified 1000 Created a chunk of size 1268, which is longer than the specified 1000 Created a chunk of size 1130, which is longer than the specified 1000 Created a chunk of size 2023, which is longer than the specified 1000 Created a chunk of size 1878, which is longer than the specified 1000 Created a chunk of size 1364, which is longer than the specified 1000 Created a chunk of size 1212, which is longer than the specified 1000 Created a chunk of size 1792, which is longer than the specified 1000 Created a chunk of size 1055, which is longer than the specified 1000 Created a chunk of size 1496, which is longer than the specified 1000 Created a chunk of size 1045, which is longer than the specified 1000 Created a chunk of size 1501, which is longer than the specified 1000 Created a chunk of size 1208, which is longer than the specified 1000 Created a chunk of size 1356, which is longer than the specified 1000 Created a chunk of size 1351, which is longer than the specified 1000 Created a chunk of size 1130, which is longer than the specified 1000 Created a chunk of size 1133, which is longer than the specified 1000 Created a chunk of size 1381, which is longer than the specified 1000 Created a chunk of size 1120, which is longer than the specified 1000 Created a chunk of size 1200, which is longer than the specified 1000 Created a chunk of size 1202, which is longer than the specified 1000 Created a chunk of size 1149, which is longer than the specified 1000 Created a chunk of size 1196, which is longer than the specified 1000 Created a chunk of size 3173, which is longer than the specified 1000 Created a chunk of size 1106, which is longer than the specified 1000 Created a chunk of size 1211, which is longer than the specified 1000 Created a chunk of size 1530, which is longer than the specified 1000 Created a chunk of size 1471, which is longer than the specified 1000 Created a chunk of size 1353, which is longer than the specified 1000 Created a chunk of size 1279, which is longer than the specified 1000 Created a chunk of size 1101, which is longer than the specified 1000 Created a chunk of size 1123, which is longer than the specified 1000 Created a chunk of size 1848, which is longer than the specified 1000 Created a chunk of size 1197, which is longer than the specified 1000 Created a chunk of size 1235, which is longer than the specified 1000 Created a chunk of size 1314, which is longer than the specified 1000 Created a chunk of size 1043, which is longer than the specified 1000 Created a chunk of size 1183, which is longer than the specified 1000 Created a chunk of size 1182, which is longer than the specified 1000 Created a chunk of size 1269, which is longer than the specified 1000 Created a chunk of size 1416, which is longer than the specified 1000 Created a chunk of size 1462, which is longer than the specified 1000 Created a chunk of size 1120, which is longer than the specified 1000 Created a chunk of size 1033, which is longer than the specified 1000 Created a chunk of size 1143, which is longer than the specified 1000 Created a chunk of size 1537, which is longer than the specified 1000 Created a chunk of size 1381, which is longer than the specified 1000 Created a chunk of size 2286, which is longer than the specified 1000 Created a chunk of size 1175, which is longer than the specified 1000 Created a chunk of size 1187, which is longer than the specified 1000 Created a chunk of size 1494, which is longer than the specified 1000 Created a chunk of size 1597, which is longer than the specified 1000 Created a chunk of size 1203, which is longer than the specified 1000 Created a chunk of size 1058, which is longer than the specified 1000 Created a chunk of size 1261, which is longer than the specified 1000 Created a chunk of size 1189, which is longer than the specified 1000 Created a chunk of size 1388, which is longer than the specified 1000 Created a chunk of size 1224, which is longer than the specified 1000 Created a chunk of size 1226, which is longer than the specified 1000 Created a chunk of size 1289, which is longer than the specified 1000 Created a chunk of size 1157, which is longer than the specified 1000 Created a chunk of size 1095, which is longer than the specified 1000 Created a chunk of size 2196, which is longer than the specified 1000 Created a chunk of size 1029, which is longer than the specified 1000 Created a chunk of size 1077, which is longer than the specified 1000 Created a chunk of size 1848, which is longer than the specified 1000 Created a chunk of size 1095, which is longer than the specified 1000 Created a chunk of size 1418, which is longer than the specified 1000 Created a chunk of size 1069, which is longer than the specified 1000 Created a chunk of size 2573, which is longer than the specified 1000 Created a chunk of size 1512, which is longer than the specified 1000 Created a chunk of size 1046, which is longer than the specified 1000 Created a chunk of size 1792, which is longer than the specified 1000 Created a chunk of size 1042, which is longer than the specified 1000 Created a chunk of size 1125, which is longer than the specified 1000 Created a chunk of size 1165, which is longer than the specified 1000 Created a chunk of size 1030, which is longer than the specified 1000 Created a chunk of size 1484, which is longer than the specified 1000 Created a chunk of size 2796, which is longer than the specified 1000 Created a chunk of size 1026, which is longer than the specified 1000 Created a chunk of size 1726, which is longer than the specified 1000 Created a chunk of size 1628, which is longer than the specified 1000 Created a chunk of size 1881, which is longer than the specified 1000 Created a chunk of size 1441, which is longer than the specified 1000 Created a chunk of size 1175, which is longer than the specified 1000 Created a chunk of size 1360, which is longer than the specified 1000 Created a chunk of size 1210, which is longer than the specified 1000 Created a chunk of size 1425, which is longer than the specified 1000 Created a chunk of size 1560, which is longer than the specified 1000 Created a chunk of size 1131, which is longer than the specified 1000 Created a chunk of size 1276, which is longer than the specified 1000 Created a chunk of size 1068, which is longer than the specified 1000 Created a chunk of size 1494, which is longer than the specified 1000 Created a chunk of size 1246, which is longer than the specified 1000 Created a chunk of size 2621, which is longer than the specified 1000 Created a chunk of size 1264, which is longer than the specified 1000 Created a chunk of size 1166, which is longer than the specified 1000 Created a chunk of size 1332, which is longer than the specified 1000 Created a chunk of size 3499, which is longer than the specified 1000 Created a chunk of size 1651, which is longer than the specified 1000 Created a chunk of size 1794, which is longer than the specified 1000 Created a chunk of size 2162, which is longer than the specified 1000 Created a chunk of size 1061, which is longer than the specified 1000 Created a chunk of size 1083, which is longer than the specified 1000 Created a chunk of size 1018, which is longer than the specified 1000 Created a chunk of size 1751, which is longer than the specified 1000 Created a chunk of size 1301, which is longer than the specified 1000 Created a chunk of size 1025, which is longer than the specified 1000 Created a chunk of size 1489, which is longer than the specified 1000 Created a chunk of size 1481, which is longer than the specified 1000 Created a chunk of size 1505, which is longer than the specified 1000 Created a chunk of size 1497, which is longer than the specified 1000 Created a chunk of size 1505, which is longer than the specified 1000 Created a chunk of size 1282, which is longer than the specified 1000 Created a chunk of size 1224, which is longer than the specified 1000 Created a chunk of size 1261, which is longer than the specified 1000 Created a chunk of size 1123, which is longer than the specified 1000 Created a chunk of size 1137, which is longer than the specified 1000 Created a chunk of size 2183, which is longer than the specified 1000 Created a chunk of size 1039, which is longer than the specified 1000 Created a chunk of size 1135, which is longer than the specified 1000 Created a chunk of size 1254, which is longer than the specified 1000 Created a chunk of size 1234, which is longer than the specified 1000 Created a chunk of size 1111, which is longer than the specified 1000 Created a chunk of size 1135, which is longer than the specified 1000 Created a chunk of size 2023, which is longer than the specified 1000 Created a chunk of size 1216, which is longer than the specified 1000 Created a chunk of size 1013, which is longer than the specified 1000 Created a chunk of size 1152, which is longer than the specified 1000 Created a chunk of size 1087, which is longer than the specified 1000 Created a chunk of size 1040, which is longer than the specified 1000 Created a chunk of size 1330, which is longer than the specified 1000 Created a chunk of size 2342, which is longer than the specified 1000 Created a chunk of size 1940, which is longer than the specified 1000 Created a chunk of size 1621, which is longer than the specified 1000 Created a chunk of size 2169, which is longer than the specified 1000 Created a chunk of size 1824, which is longer than the specified 1000 Created a chunk of size 1554, which is longer than the specified 1000 Created a chunk of size 1457, which is longer than the specified 1000 Created a chunk of size 1486, which is longer than the specified 1000 Created a chunk of size 1556, which is longer than the specified 1000 Created a chunk of size 1012, which is longer than the specified 1000 Created a chunk of size 1484, which is longer than the specified 1000 Created a chunk of size 1039, which is longer than the specified 1000 Created a chunk of size 1335, which is longer than the specified 1000 Created a chunk of size 1684, which is longer than the specified 1000 Created a chunk of size 1537, which is longer than the specified 1000 Created a chunk of size 1136, which is longer than the specified 1000 Created a chunk of size 1219, which is longer than the specified 1000 Created a chunk of size 1011, which is longer than the specified 1000 Created a chunk of size 1055, which is longer than the specified 1000 Created a chunk of size 1433, which is longer than the specified 1000 Created a chunk of size 1263, which is longer than the specified 1000 Created a chunk of size 1014, which is longer than the specified 1000 Created a chunk of size 1107, which is longer than the specified 1000 Created a chunk of size 2702, which is longer than the specified 1000 Created a chunk of size 1237, which is longer than the specified 1000 Created a chunk of size 1172, which is longer than the specified 1000 Created a chunk of size 1517, which is longer than the specified 1000 Created a chunk of size 1589, which is longer than the specified 1000 Created a chunk of size 1681, which is longer than the specified 1000 Created a chunk of size 2244, which is longer than the specified 1000 Created a chunk of size 1505, which is longer than the specified 1000 Created a chunk of size 1228, which is longer than the specified 1000 Created a chunk of size 1801, which is longer than the specified 1000 Created a chunk of size 1856, which is longer than the specified 1000 Created a chunk of size 2171, which is longer than the specified 1000 Created a chunk of size 2450, which is longer than the specified 1000 Created a chunk of size 1110, which is longer than the specified 1000 Created a chunk of size 1148, which is longer than the specified 1000 Created a chunk of size 1050, which is longer than the specified 1000 Created a chunk of size 1014, which is longer than the specified 1000 Created a chunk of size 1458, which is longer than the specified 1000 Created a chunk of size 1270, which is longer than the specified 1000 Created a chunk of size 1287, which is longer than the specified 1000 Created a chunk of size 1127, which is longer than the specified 1000 Created a chunk of size 1576, which is longer than the specified 1000 Created a chunk of size 1350, which is longer than the specified 1000 Created a chunk of size 2283, which is longer than the specified 1000 Created a chunk of size 2211, which is longer than the specified 1000 Created a chunk of size 1167, which is longer than the specified 1000 Created a chunk of size 1038, which is longer than the specified 1000 Created a chunk of size 1117, which is longer than the specified 1000 Created a chunk of size 1160, which is longer than the specified 1000 Created a chunk of size 1163, which is longer than the specified 1000 Created a chunk of size 1013, which is longer than the specified 1000 Created a chunk of size 1226, which is longer than the specified 1000 Created a chunk of size 1336, which is longer than the specified 1000 Created a chunk of size 1012, which is longer than the specified 1000 Created a chunk of size 2833, which is longer than the specified 1000 Created a chunk of size 1201, which is longer than the specified 1000 Created a chunk of size 1172, which is longer than the specified 1000 Created a chunk of size 1438, which is longer than the specified 1000 Created a chunk of size 1259, which is longer than the specified 1000 Created a chunk of size 1452, which is longer than the specified 1000 Created a chunk of size 1377, which is longer than the specified 1000 Created a chunk of size 1001, which is longer than the specified 1000 Created a chunk of size 1240, which is longer than the specified 1000 Created a chunk of size 1142, which is longer than the specified 1000 Created a chunk of size 1338, which is longer than the specified 1000 Created a chunk of size 1057, which is longer than the specified 1000 Created a chunk of size 1040, which is longer than the specified 1000 Created a chunk of size 1579, which is longer than the specified 1000 Created a chunk of size 1176, which is longer than the specified 1000 Created a chunk of size 1081, which is longer than the specified 1000 Created a chunk of size 1751, which is longer than the specified 1000 Created a chunk of size 1064, which is longer than the specified 1000 Created a chunk of size 1029, which is longer than the specified 1000 Created a chunk of size 1937, which is longer than the specified 1000 Created a chunk of size 1972, which is longer than the specified 1000 Created a chunk of size 1417, which is longer than the specified 1000 Created a chunk of size 1203, which is longer than the specified 1000 Created a chunk of size 1314, which is longer than the specified 1000 Created a chunk of size 1088, which is longer than the specified 1000 Created a chunk of size 1455, which is longer than the specified 1000 Created a chunk of size 1467, which is longer than the specified 1000 Created a chunk of size 1476, which is longer than the specified 1000 Created a chunk of size 1354, which is longer than the specified 1000 Created a chunk of size 1403, which is longer than the specified 1000 Created a chunk of size 1366, which is longer than the specified 1000 Created a chunk of size 1112, which is longer than the specified 1000 Created a chunk of size 1512, which is longer than the specified 1000 Created a chunk of size 1262, which is longer than the specified 1000 Created a chunk of size 1405, which is longer than the specified 1000 Created a chunk of size 2221, which is longer than the specified 1000 Created a chunk of size 1128, which is longer than the specified 1000 Created a chunk of size 1021, which is longer than the specified 1000 Created a chunk of size 1532, which is longer than the specified 1000 Created a chunk of size 1535, which is longer than the specified 1000 Created a chunk of size 1230, which is longer than the specified 1000 Created a chunk of size 2456, which is longer than the specified 1000 Created a chunk of size 1047, which is longer than the specified 1000 Created a chunk of size 1320, which is longer than the specified 1000 Created a chunk of size 1144, which is longer than the specified 1000 Created a chunk of size 1509, which is longer than the specified 1000 Created a chunk of size 1003, which is longer than the specified 1000 Created a chunk of size 1025, which is longer than the specified 1000 Created a chunk of size 1197, which is longer than the specified 1000 8244Then embed chunks and upload them to the DeepLake.This can take several minutes. from langchain.embeddings.openai import OpenAIEmbeddingsembeddings = OpenAIEmbeddings()embeddings OpenAIE" beddings(client=, model='text-embedding-ada-002', deployment='text-embedding-ada-002' 92,https://python.langchain.com/docs/use_cases/question_answering/how_to/code/twitter-the-algorithm-analysis-deeplake,"Question AnsweringHow toCode understandingAnalysis of Twitter the-algorithm source code with LangChain, GPT4 and Activeloop's Deep LakeOn this pageAnalysis of Twitter the-algorithm source code with LangChain, GPT4 and Activeloop's Deep LakeIn this tutorial, we are going to use Langchain + Activeloop's Deep Lake with GPT4 to analyze the code base of the twitter algorithm. python3 -m pip install --upgrade langchain 'deeplake[enterprise]' openai tiktokenDefine OpenAI embeddings, Deep Lake multi-modal vector store api and authenticate. For full documentation of Deep Lake please follow docs and API reference.Authenticate into Deep Lake if you want to create your own dataset and publish it. You can get an API key from the platformimport osimport getpassfrom langchain.embeddings.openai import OpenAIEmbeddingsfrom langchain.vectorstores import DeepLakeos.environ[""OPENAI_API_KEY""] = getpass.getpass(""OpenAI API Key:"")activeloop_token = getpass.getpass(""Activeloop Token:"")os.environ[""ACTIVELOOP_TOKEN""] = activeloop_tokenembeddings = OpenAIEmbeddings(disallowed_special=())disallowed_special=() is required to avoid Exception: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte from tiktoken for some repositories1. Index the code base (optional)​You can directly skip this part and directly jump into using already indexed dataset. To begin with, first we will clone the repository, then parse and chunk the code base and use OpenAI indexing.git clone https://github.com/twitter/the-algorithm # replace any repository of your choice Cloning into 'the-algorithm'... remote: Enumerating objects: 9142, done. remote: Counting objects: 100% (2438/2438), done. remote: Compressing objects: 100% (1662/1662), done. remote: Total 9142 (delta 597), reused 2349 (delta 593), pack-reused 6704 Receiving objects: 100% (9142/9142), 7.67 MiB | 33.29 MiB/s, done. Resolving deltas: 100% (2818/2818), done.Load all files inside the repositoryimport osfrom langchain.document_loaders import TextLoaderroot_dir = ""./the-algorithm""docs = []for dirpath, dirnames, filenames in os.walk(root_dir): for file in filenames: try: loader = TextLoader(os.path.join(dirpath, file), encoding=""utf-8"") docs.extend(loader.load_and_split()) except Exception as e: passThen, chunk the filesfrom langchain.text_splitter import CharacterTextSplittertext_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)texts = text_splitter.split_documents(docs) Created a chunk of size 2549, which is longer than the specified 1000 Created a chunk of size 2095, which is longer than the specified 1000 Created a chunk of size 1983, which is longer than the specified 1000 Created a chunk of size 1531, which is longer than the specified 1000 Created a chunk of size 1102, which is longer than the specified 1000 Created a chunk of size 1012, which is longer than the specified 1000 Created a chunk of size 1981, which is longer than the specified 1000 Created a chunk of size 1022, which is longer than the specified 1000 Created a chunk of size 1134, which is longer than the specified 1000 Created a chunk of size 1532, which is longer than the specified 1000 Created a chunk of size 1056, which is longer than the specified 1000 Created a chunk of size 1515, which is longer than the specified 1000 Created a chunk of size 2591, which is longer than the specified 1000 Created a chunk of size 1957, which is longer than the specified 1000 Created a chunk of size 2249, which is longer than the specified 1000 Created a chunk of size 1275, which is longer than the specified 1000 Created a chunk of size 2207, which is longer than the specified 1000 Created a chunk of size 2405, which is longer than the specified 1000 Created a chunk of size 1059, which is longer than the specified 1000 Created a chunk of size 1726, which is longer than the specified 1000 Created a chunk of size 1131, which is longer than the specified 1000 Created a chunk of size 1575, which is longer than the specified 1000 Created a chunk of size 1235, which is longer than the specified 1000 Created a chunk of size 1857, which is longer than the specified 1000 Created a chunk of size 3036, which is longer than the specified 1000 Created a chunk of size 1977, which is longer than the specified 1000 Created a chunk of size 1389, which is longer than the specified 1000 Created a chunk of size 1282, which is longer than the specified 1000 Created a chunk of size 3065, which is longer than the specified 1000 Created a chunk of size 1095, which is longer than the specified 1000 Created a chunk of size 1063, which is longer than the specified 1000 Created a chunk of size 1048, which is longer than the specified 1000 Created a chunk of size 1178, which is longer than the specified 1000 Created a chunk of size 1019, which is longer than the specified 1000 Created a chunk of size 1130, which is longer than the specified 1000 Created a chunk of size 1620, which is longer than the specified 1000 Created a chunk of size 1111, which is longer than the specified 1000 Created a chunk of size 1037, which is longer than the specified 1000 Created a chunk of size 1913, which is longer than the specified 1000 Created a chunk of size 1007, which is longer than the specified 1000 Created a chunk of size 2160, which is longer than the specified 1000 Created a chunk of size 1594, which is longer than the specified 1000 Created a chunk of size 2181, which is longer than the specified 1000 Created a chunk of size 1160, which is longer than the specified 1000 Created a chunk of size 2029, which is longer than the specified 1000 Created a chunk of size 1083, which is longer than the specified 1000 Created a chunk of size 1076, which is longer than the specified 1000 Created a chunk of size 1022, which is longer than the specified 1000 Created a chunk of size 1021, which is longer than the specified 1000 Created a chunk of size 3489, which is longer than the specified 1000 Created a chunk of size 1543, which is longer than the specified 1000 Created a chunk of size 1885, which is longer than the specified 1000 Created a chunk of size 1141, which is longer than the specified 1000 Created a chunk of size 2165, which is longer than the specified 1000 Created a chunk of size 2142, which is longer than the specified 1000 Created a chunk of size 3294, which is longer than the specified 1000 Created a chunk of size 1166, which is longer than the specified 1000 Created a chunk of size 1540, which is longer than the specified 1000 Created a chunk of size 1020, which is longer than the specified 1000 Created a chunk of size 1259, which is longer than the specified 1000 Created a chunk of size 1790, which is longer than the specified 1000 Created a chunk of size 1135, which is longer than the specified 1000 Created a chunk of size 1193, which is longer than the specified 1000 Created a chunk of size 1230, which is longer than the specified 1000 Created a chunk of size 2611, which is longer than the specified 1000 Created a chunk of size 1110, which is longer than the specified 1000 Created a chunk of size 1097, which is longer than the specified 1000 Created a chunk of size 1516, which is longer than the specified 1000 Created a chunk of size 1552, which is longer than the specified 1000 Created a chunk of size 1417, which is longer than the specified 1000 Created a chunk of size 1416, which is longer than the specified 1000 Created a chunk of size 2833, which is longer than the specified 1000 Created a chunk of size 1437, which is longer than the specified 1000 Created a chunk of size 1194, which is longer than the specified 1000 Created a chunk of size 1939, which is longer than the specified 1000 Created a chunk of size 1130, which is longer than the specified 1000 Created a chunk of size 1004, which is longer than the specified 1000 Created a chunk of size 1255, which is longer than the specified 1000 Created a chunk of size 1139, which is longer than the specified 1000 Created a chunk of size 1204, which is longer than the specified 1000 Created a chunk of size 1202, which is longer than the specified 1000 Created a chunk of size 1035, which is longer than the specified 1000 Created a chunk of size 1044, which is longer than the specified 1000 Created a chunk of size 1351, which is longer than the specified 1000 Created a chunk of size 1269, which is longer than the specified 1000 Created a chunk of size 1358, which is longer than the specified 1000 Created a chunk of size 1014, which is longer than the specified 1000 Created a chunk of size 1151, which is longer than the specified 1000 Created a chunk of size 1088, which is longer than the specified 1000 Created a chunk of size 1024, which is longer than the specified 1000 Created a chunk of size 1031, which is longer than the specified 1000 Created a chunk of size 1048, which is longer than the specified 1000 Created a chunk of size 1991, which is longer than the specified 1000 Created a chunk of size 1829, which is longer than the specified 1000 Created a chunk of size 1850, which is longer than the specified 1000 Created a chunk of size 1099, which is longer than the specified 1000 Created a chunk of size 1219, which is longer than the specified 1000 Created a chunk of size 1063, which is longer than the specified 1000 Created a chunk of size 1057, which is longer than the specified 1000 Created a chunk of size 2343, which is longer than the specified 1000 Created a chunk of size 1065, which is longer than the specified 1000 Created a chunk of size 1058, which is longer than the specified 1000 Created a chunk of size 1341, which is longer than the specified 1000 Created a chunk of size 1017, which is longer than the specified 1000 Created a chunk of size 1563, which is longer than the specified 1000 Created a chunk of size 1225, which is longer than the specified 1000 Created a chunk of size 1718, which is longer than the specified 1000 Created a chunk of size 1548, which is longer than the specified 1000 Created a chunk of size 1276, which is longer than the specified 1000 Created a chunk of size 1121, which is longer than the specified 1000 Created a chunk of size 1014, which is longer than the specified 1000 Created a chunk of size 1240, which is longer than the specified 1000 Created a chunk of size 2660, which is longer than the specified 1000 Created a chunk of size 2514, which is longer than the specified 1000 Created a chunk of size 1137, which is longer than the specified 1000 Created a chunk of size 1892, which is longer than the specified 1000 Created a chunk of size 1274, which is longer than the specified 1000 Created a chunk of size 1261, which is longer than the specified 1000 Created a chunk of size 1228, which is longer than the specified 1000 Created a chunk of size 1992, which is longer than the specified 1000 Created a chunk of size 1276, which is longer than the specified 1000 Created a chunk of size 2246, which is longer than the specified 1000 Created a chunk of size 1008, which is longer than the specified 1000 Created a chunk of size 1408, which is longer than the specified 1000 Created a chunk of size 1629, which is longer than the specified 1000 Created a chunk of size 2249, which is longer than the specified 1000 Created a chunk of size 1664, which is longer than the specified 1000 Created a chunk of size 2328, which is longer than the specified 1000 Created a chunk of size 1206, which is longer than the specified 1000 Created a chunk of size 1330, which is longer than the specified 1000 Created a chunk of size 1842, which is longer than the specified 1000 Created a chunk of size 1568, which is longer than the specified 1000 Created a chunk of size 1182, which is longer than the specified 1000 Created a chunk of size 1159, which is longer than the specified 1000 Created a chunk of size 1067, which is longer than the specified 1000 Created a chunk of size 1353, which is longer than the specified 1000 Created a chunk of size 1770, which is longer than the specified 1000 Created a chunk of size 1009, which is longer than the specified 1000 Created a chunk of size 1286, which is longer than the specified 1000 Created a chunk of size 1001, which is longer than the specified 1000 Created a chunk of size 1220, which is longer than the specified 1000 Created a chunk of size 1395, which is longer than the specified 1000 Created a chunk of size 1068, which is longer than the specified 1000 Created a chunk of size 2452, which is longer than the specified 1000 Created a chunk of size 1277, which is longer than the specified 1000 Created a chunk of size 1216, which is longer than the specified 1000 Created a chunk of size 1557, which is longer than the specified 1000 Created a chunk of size 1275, which is longer than the specified 1000 Created a chunk of size 1161, which is longer than the specified 1000 Created a chunk of size 1440, which is longer than the specified 1000 Created a chunk of size 1430, which is longer than the specified 1000 Created a chunk of size 1259, which is longer than the specified 1000 Created a chunk of size 1064, which is longer than the specified 1000 Created a chunk of size 1101, which is longer than the specified 1000 Created a chunk of size 1108, which is longer than the specified 1000 Created a chunk of size 1886, which is longer than the specified 1000 Created a chunk of size 1629, which is longer than the specified 1000 Created a chunk of size 1213, which is longer than the specified 1000 Created a chunk of size 2095, which is longer than the specified 1000 Created a chunk of size 1099, which is longer than the specified 1000 Created a chunk of size 1034, which is longer than the specified 1000 Created a chunk of size 1213, which is longer than the specified 1000 Created a chunk of size 1223, which is longer than the specified 1000 Created a chunk of size 1149, which is longer than the specified 1000 Created a chunk of size 1319, which is longer than the specified 1000 Created a chunk of size 1403, which is longer than the specified 1000 Created a chunk of size 1358, which is longer than the specified 1000 Created a chunk of size 2079, which is longer than the specified 1000 Created a chunk of size 2414, which is longer than the specified 1000 Created a chunk of size 1578, which is longer than the specified 1000 Created a chunk of size 1253, which is longer than the specified 1000 Created a chunk of size 1235, which is longer than the specified 1000 Created a chunk of size 1043, which is longer than the specified 1000 Created a chunk of size 1049, which is longer than the specified 1000 Created a chunk of size 1126, which is longer than the specified 1000 Created a chunk of size 1309, which is longer than the specified 1000 Created a chunk of size 1967, which is longer than the specified 1000 Created a chunk of size 1243, which is longer than the specified 1000 Created a chunk of size 1156, which is longer than the specified 1000 Created a chunk of size 1056, which is longer than the specified 1000 Created a chunk of size 1615, which is longer than the specified 1000 Created a chunk of size 1672, which is longer than the specified 1000 Created a chunk of size 1432, which is longer than the specified 1000 Created a chunk of size 1423, which is longer than the specified 1000 Created a chunk of size 1519, which is longer than the specified 1000 Created a chunk of size 1027, which is longer than the specified 1000 Created a chunk of size 1050, which is longer than the specified 1000 Created a chunk of size 1041, which is longer than the specified 1000 Created a chunk of size 1125, which is longer than the specified 1000 Created a chunk of size 1074, which is longer than the specified 1000 Created a chunk of size 1416, which is longer than the specified 1000 Created a chunk of size 1353, which is longer than the specified 1000 Created a chunk of size 1372, which is longer than the specified 1000 Created a chunk of size 1799, which is longer than the specified 1000 Created a chunk of size 1712, which is longer than the specified 1000 Created a chunk of size 1259, which is longer than the specified 1000 Created a chunk of size 1550, which is longer than the specified 1000 Created a chunk of size 1643, which is longer than the specified 1000 Created a chunk of size 1658, which is longer than the specified 1000 Created a chunk of size 1299, which is longer than the specified 1000 Created a chunk of size 1229, which is longer than the specified 1000 Created a chunk of size 1296, which is longer than the specified 1000 Created a chunk of size 1041, which is longer than the specified 1000 Created a chunk of size 1193, which is longer than the specified 1000 Created a chunk of size 1011, which is longer than the specified 1000 Created a chunk of size 2208, which is longer than the specified 1000 Created a chunk of size 1101, which is longer than the specified 1000 Created a chunk of size 2014, which is longer than the specified 1000 Created a chunk of size 1771, which is longer than the specified 1000 Created a chunk of size 1089, which is longer than the specified 1000 Created a chunk of size 1364, which is longer than the specified 1000 Created a chunk of size 1550, which is longer than the specified 1000 Created a chunk of size 2202, which is longer than the specified 1000 Created a chunk of size 1161, which is longer than the specified 1000 Created a chunk of size 1559, which is longer than the specified 1000 Created a chunk of size 1292, which is longer than the specified 1000 Created a chunk of size 1383, which is longer than the specified 1000 Created a chunk of size 1055, which is longer than the specified 1000 Created a chunk of size 1036, which is longer than the specified 1000 Created a chunk of size 1814, which is longer than the specified 1000 Created a chunk of size 1702, which is longer than the specified 1000 Created a chunk of size 1986, which is longer than the specified 1000 Created a chunk of size 2261, which is longer than the specified 1000 Created a chunk of size 1263, which is longer than the specified 1000 Created a chunk of size 1049, which is longer than the specified 1000 Created a chunk of size 1097, which is longer than the specified 1000 Created a chunk of size 1519, which is longer than the specified 1000 Created a chunk of size 1881, which is longer than the specified 1000 Created a chunk of size 1585, which is longer than the specified 1000 Created a chunk of size 1894, which is longer than the specified 1000 Created a chunk of size 1114, which is longer than the specified 1000 Created a chunk of size 2217, which is longer than the specified 1000 Created a chunk of size 1090, which is longer than the specified 1000 Created a chunk of size 1039, which is longer than the specified 1000 Created a chunk of size 1568, which is longer than the specified 1000 Created a chunk of size 1092, which is longer than the specified 1000 Created a chunk of size 1508, which is longer than the specified 1000 Created a chunk of size 1308, which is longer than the specified 1000 Created a chunk of size 2633, which is longer than the specified 1000 Created a chunk of size 1029, which is longer than the specified 1000 Created a chunk of size 1377, which is longer than the specified 1000 Created a chunk of size 1683, which is longer than the specified 1000 Created a chunk of size 1443, which is longer than the specified 1000 Created a chunk of size 1026, which is longer than the specified 1000 Created a chunk of size 1110, which is longer than the specified 1000 Created a chunk of size 1038, which is longer than the specified 1000 Created a chunk of size 1287, which is longer than the specified 1000 Created a chunk of size 1067, which is longer than the specified 1000 Created a chunk of size 1673, which is longer than the specified 1000 Created a chunk of size 1019, which is longer than the specified 1000 Created a chunk of size 2514, which is longer than the specified 1000 Created a chunk of size 1056, which is longer than the specified 1000 Created a chunk of size 1575, which is longer than the specified 1000 Created a chunk of size 1078, which is longer than the specified 1000 Created a chunk of size 1171, which is longer than the specified 1000 Created a chunk of size 1364, which is longer than the specified 1000 Created a chunk of size 1595, which is longer than the specified 1000 Created a chunk of size 2231, which is longer than the specified 1000 Created a chunk of size 1271, which is longer than the specified 1000 Created a chunk of size 2133, which is longer than the specified 1000 Created a chunk of size 2272, which is longer than the specified 1000 Created a chunk of size 2573, which is longer than the specified 1000 Created a chunk of size 1005, which is longer than the specified 1000 Created a chunk of size 2544, which is longer than the specified 1000 Created a chunk of size 1102, which is longer than the specified 1000 Created a chunk of size 1075, which is longer than the specified 1000 Created a chunk of size 1382, which is longer than the specified 1000 Created a chunk of size 1280, which is longer than the specified 1000 Created a chunk of size 1452, which is longer than the specified 1000 Created a chunk of size 1120, which is longer than the specified 1000 Created a chunk of size 1016, which is longer than the specified 1000 Created a chunk of size 1484, which is longer than the specified 1000 Created a chunk of size 1536, which is longer than the specified 1000 Created a chunk of size 3331, which is longer than the specified 1000 Created a chunk of size 1205, which is longer than the specified 1000 Created a chunk of size 1110, which is longer than the specified 1000 Created a chunk of size 1056, which is longer than the specified 1000 Created a chunk of size 1700, which is longer than the specified 1000 Created a chunk of size 1101, which is longer than the specified 1000 Created a chunk of size 1914, which is longer than the specified 1000 Created a chunk of size 2808, which is longer than the specified 1000 Created a chunk of size 2879, which is longer than the specified 1000 Created a chunk of size 1690, which is longer than the specified 1000 Created a chunk of size 1196, which is longer than the specified 1000 Created a chunk of size 1221, which is longer than the specified 1000 Created a chunk of size 1070, which is longer than the specified 1000 Created a chunk of size 1215, which is longer than the specified 1000 Created a chunk of size 1583, which is longer than the specified 1000 Created a chunk of size 1207, which is longer than the specified 1000 Created a chunk of size 1114, which is longer than the specified 1000 Created a chunk of size 1169, which is longer than the specified 1000 Created a chunk of size 1454, which is longer than the specified 1000 Created a chunk of size 1083, which is longer than the specified 1000 Created a chunk of size 1972, which is longer than the specified 1000 Created a chunk of size 2506, which is longer than the specified 1000 Created a chunk of size 2204, which is longer than the specified 1000 Created a chunk of size 1464, which is longer than the specified 1000 Created a chunk of size 1485, which is longer than the specified 1000 Created a chunk of size 1389, which is longer than the specified 1000 Created a chunk of size 1700, which is longer than the specified 1000 Created a chunk of size 1063, which is longer than the specified 1000 Created a chunk of size 1066, which is longer than the specified 1000 Created a chunk of size 1127, which is longer than the specified 1000 Created a chunk of size 3009, which is longer than the specified 1000 Created a chunk of size 1217, which is longer than the specified 1000 Created a chunk of size 1400, which is longer than the specified 1000 Created a chunk of size 1323, which is longer than the specified 1000 Created a chunk of size 2093, which is longer than the specified 1000 Created a chunk of size 1486, which is longer than the specified 1000 Created a chunk of size 1302, which is longer than the specified 1000 Created a chunk of size 2178, which is longer than the specified 1000 Created a chunk of size 1572, which is longer than the specified 1000 Created a chunk of size 1327, which is longer than the specified 1000 Created a chunk of size 2288, which is longer than the specified 1000 Created a chunk of size 3163, which is longer than the specified 1000 Created a chunk of size 1125, which is longer than the specified 1000 Created a chunk of size 2009, which is longer than the specified 1000 Created a chunk of size 1019, which is longer than the specified 1000 Created a chunk of size 2491, which is longer than the specified 1000 Created a chunk of size 2457, which is longer than the specified 1000 Created a chunk of size 2462, which is longer than the specified 1000 Created a chunk of size 2533, which is longer than the specified 1000 Created a chunk of size 2543, which is longer than the specified 1000 Created a chunk of size 2481, which is longer than the specified 1000 Created a chunk of size 2574, which is longer than the specified 1000 Created a chunk of size 2500, which is longer than the specified 1000 Created a chunk of size 2739, which is longer than the specified 1000 Created a chunk of size 1288, which is longer than the specified 1000 Created a chunk of size 1375, which is longer than the specified 1000 Created a chunk of size 1388, which is longer than the specified 1000 Created a chunk of size 2344, which is longer than the specified 1000 Created a chunk of size 1854, which is longer than the specified 1000 Created a chunk of size 1659, which is longer than the specified 1000 Created a chunk of size 2631, which is longer than the specified 1000 Created a chunk of size 2853, which is longer than the specified 1000 Created a chunk of size 1424, which is longer than the specified 1000 Created a chunk of size 2364, which is longer than the specified 1000 Created a chunk of size 1482, which is longer than the specified 1000 Created a chunk of size 2761, which is longer than the specified 1000 Created a chunk of size 2010, which is longer than the specified 1000 Created a chunk of size 1716, which is longer than the specified 1000 Created a chunk of size 2323, which is longer than the specified 1000 Created a chunk of size 1717, which is longer than the specified 1000 Created a chunk of size 1302, which is longer than the specified 1000 Created a chunk of size 1641, which is longer than the specified 1000 Created a chunk of size 1419, which is longer than the specified 1000 Created a chunk of size 1232, which is longer than the specified 1000 Created a chunk of size 1084, which is longer than the specified 1000 Created a chunk of size 1026, which is longer than the specified 1000 Created a chunk of size 1035, which is longer than the specified 1000 Created a chunk of size 1502, which is longer than the specified 1000 Created a chunk of size 1707, which is longer than the specified 1000 Created a chunk of size 1128, which is longer than the specified 1000 Created a chunk of size 1577, which is longer than the specified 1000 Created a chunk of size 1149, which is longer than the specified 1000 Created a chunk of size 1288, which is longer than the specified 1000 Created a chunk of size 1182, which is longer than the specified 1000 Created a chunk of size 1692, which is longer than the specified 1000 Created a chunk of size 1653, which is longer than the specified 1000 Created a chunk of size 1037, which is longer than the specified 1000 Created a chunk of size 2164, which is longer than the specified 1000 Created a chunk of size 1371, which is longer than the specified 1000 Created a chunk of size 1348, which is longer than the specified 1000 Created a chunk of size 1271, which is longer than the specified 1000 Created a chunk of size 1015, which is longer than the specified 1000 Created a chunk of size 1137, which is longer than the specified 1000 Created a chunk of size 1759, which is longer than the specified 1000 Created a chunk of size 1644, which is longer than the specified 1000 Created a chunk of size 1104, which is longer than the specified 1000 Created a chunk of size 1279, which is longer than the specified 1000 Created a chunk of size 2328, which is longer than the specified 1000 Created a chunk of size 3164, which is longer than the specified 1000 Created a chunk of size 2565, which is longer than the specified 1000 Created a chunk of size 1002, which is longer than the specified 1000 Created a chunk of size 1261, which is longer than the specified 1000 Created a chunk of size 1111, which is longer than the specified 1000 Created a chunk of size 1732, which is longer than the specified 1000 Created a chunk of size 1702, which is longer than the specified 1000 Created a chunk of size 1029, which is longer than the specified 1000 Created a chunk of size 1041, which is longer than the specified 1000 Created a chunk of size 1605, which is longer than the specified 1000 Created a chunk of size 1616, which is longer than the specified 1000 Created a chunk of size 1224, which is longer than the specified 1000 Created a chunk of size 2556, which is longer than the specified 1000 Created a chunk of size 2092, which is longer than the specified 1000 Created a chunk of size 1045, which is longer than the specified 1000 Created a chunk of size 1172, which is longer than the specified 1000 Created a chunk of size 1456, which is longer than the specified 1000 Created a chunk of size 1353, which is longer than the specified 1000 Created a chunk of size 1179, which is longer than the specified 1000 Created a chunk of size 1060, which is longer than the specified 1000 Created a chunk of size 1031, which is longer than the specified 1000 Created a chunk of size 2216, which is longer than the specified 1000 Created a chunk of size 1316, which is longer than the specified 1000 Created a chunk of size 1485, which is longer than the specified 1000 Created a chunk of size 1123, which is longer than the specified 1000 Created a chunk of size 1288, which is longer than the specified 1000 Created a chunk of size 1685, which is longer than the specified 1000 Created a chunk of size 1577, which is longer than the specified 1000 Created a chunk of size 1076, which is longer than the specified 1000 Created a chunk of size 1006, which is longer than the specified 1000 Created a chunk of size 1136, which is longer than the specified 1000 Created a chunk of size 1026, which is longer than the specified 1000 Created a chunk of size 1306, which is longer than the specified 1000 Created a chunk of size 1306, which is longer than the specified 1000 Created a chunk of size 1200, which is longer than the specified 1000 Created a chunk of size 1311, which is longer than the specified 1000 Created a chunk of size 1317, which is longer than the specified 1000 Created a chunk of size 1528, which is longer than the specified 1000 Created a chunk of size 1610, which is longer than the specified 1000 Created a chunk of size 1517, which is longer than the specified 1000 Created a chunk of size 1163, which is longer than the specified 1000 Created a chunk of size 2573, which is longer than the specified 1000 Created a chunk of size 1299, which is longer than the specified 1000 Created" a chunk of size 1042, which is longer than the specified 1000 Created a chunk of size 1200, which is longer than the specified 1000 Created a chunk of size 1047 93,https://python.langchain.com/docs/use_cases/question_answering/how_to/analyze_document,"Question AnsweringHow toAnalyze DocumentAnalyze DocumentThe AnalyzeDocumentChain can be used as an end-to-end to chain. This chain takes in a single document, splits it up, and then runs it through a CombineDocumentsChain.with open(""../../state_of_the_union.txt"") as f: state_of_the_union = f.read()Summarize​Let's take a look at it in action below, using it to summarize a long document.from langchain.llms import OpenAIfrom langchain.chains.summarize import load_summarize_chainllm = OpenAI(temperature=0)summary_chain = load_summarize_chain(llm, chain_type=""map_reduce"")from langchain.chains import AnalyzeDocumentChainsummarize_document_chain = AnalyzeDocumentChain(combine_docs_chain=summary_chain)summarize_document_chain.run(state_of_the_union) "" In this speech, President Biden addresses the American people and the world, discussing the recent aggression of Russia's Vladimir Putin in Ukraine and the US response. He outlines economic sanctions and other measures taken to hold Putin accountable, and announces the US Department of Justice's task force to go after the crimes of Russian oligarchs. He also announces plans to fight inflation and lower costs for families, invest in American manufacturing, and provide military, economic, and humanitarian assistance to Ukraine. He calls for immigration reform, protecting the rights of women, and advancing the rights of LGBTQ+ Americans, and pays tribute to military families. He concludes with optimism for the future of America.""Question Answering​Let's take a look at this using a question answering chain.from langchain.chains.question_answering import load_qa_chainqa_chain = load_qa_chain(llm, chain_type=""map_reduce"")qa_document_chain = AnalyzeDocumentChain(combine_docs_chain=qa_chain)qa_document_chain.run(input_document=state_of_the_union, question=""what did the president say about justice breyer?"") ' The president thanked Justice Breyer for his service.'PreviousAnalysis of Twitter the-algorithm source code with LangChain, GPT4 and Activeloop's Deep LakeNextConversational Retrieval Agent" 94,https://python.langchain.com/docs/use_cases/question_answering/how_to/conversational_retrieval_agents,"Question AnsweringHow toConversational Retrieval AgentOn this pageConversational Retrieval AgentThis is an agent specifically optimized for doing retrieval when necessary and also holding a conversation.To start, we will set up the retriever we want to use, and then turn it into a retriever tool. Next, we will use the high level constructor for this type of agent. Finally, we will walk through how to construct a conversational retrieval agent from components.The Retriever​To start, we need a retriever to use! The code here is mostly just example code. Feel free to use your own retriever and skip to the section on creating a retriever tool.from langchain.document_loaders import TextLoaderloader = TextLoader('../../../../../docs/docs/modules/state_of_the_union.txt')from langchain.text_splitter import CharacterTextSplitterfrom langchain.vectorstores import FAISSfrom langchain.embeddings import OpenAIEmbeddingsdocuments = loader.load()text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)texts = text_splitter.split_documents(documents)embeddings = OpenAIEmbeddings()db = FAISS.from_documents(texts, embeddings)retriever = db.as_retriever()Retriever Tool​Now we need to create a tool for our retriever. The main things we need to pass in are a name for the retriever as well as a description. These will both be used by the language model, so they should be informative.from langchain.agents.agent_toolkits import create_retriever_tooltool = create_retriever_tool( retriever, ""search_state_of_union"", ""Searches and returns documents regarding the state-of-the-union."")tools = [tool]Agent Constructor​Here, we will use the high level create_conversational_retrieval_agent API to construct the agent.Notice that beside the list of tools, the only thing we need to pass in is a language model to use. Under the hood, this agent is using the OpenAIFunctionsAgent, so we need to use an ChatOpenAI model.from langchain.agents.agent_toolkits import create_conversational_retrieval_agentfrom langchain.chat_models import ChatOpenAIllm = ChatOpenAI(temperature = 0)agent_executor = create_conversational_retrieval_agent(llm, tools, verbose=True)We can now try it out!result = agent_executor({""input"": ""hi, im bob""}) > Entering new AgentExecutor chain... Hello Bob! How can I assist you today? > Finished chain.result[""output""] 'Hello Bob! How can I assist you today?'Notice that it remembers your nameresult = agent_executor({""input"": ""whats my name?""}) > Entering new AgentExecutor chain... Your name is Bob. > Finished chain.result[""output""] 'Your name is Bob.'Notice that it now does retrievalresult = agent_executor({""input"": ""what did the president say about kentaji brown jackson in the most recent state of the union?""}) > Entering new AgentExecutor chain... Invoking: `search_state_of_union` with `{'query': 'Kentaji Brown Jackson'}` [Document(page_content='Tonight. I call on the Senate to: Pass the Freedom to Vote Act. Pass the John Lewis Voting Rights Act. And while you’re at it, pass the Disclose Act so Americans can know who is funding our elections. \n\nTonight, I’d like to honor someone who has dedicated his life to serve this country: Justice Stephen Breyer—an Army veteran, Constitutional scholar, and retiring Justice of the United States Supreme Court. Justice Breyer, thank you for your service. \n\nOne of the most serious constitutional responsibilities a President has is nominating someone to serve on the United States Supreme Court. \n\nAnd I did that 4 days ago, when I nominated Circuit Court of Appeals Judge Ketanji Brown Jackson. One of our nation’s top legal minds, who will continue Justice Breyer’s legacy of excellence.', metadata={'source': '../../../../../docs/docs/modules/state_of_the_union.txt'}), Document(page_content='One was stationed at bases and breathing in toxic smoke from “burn pits” that incinerated wastes of war—medical and hazard material, jet fuel, and more. \n\nWhen they came home, many of the world’s fittest and best trained warriors were never the same. \n\nHeadaches. Numbness. Dizziness. \n\nA cancer that would put them in a flag-draped coffin. \n\nI know. \n\nOne of those soldiers was my son Major Beau Biden. \n\nWe don’t know for sure if a burn pit was the cause of his brain cancer, or the diseases of so many of our troops. \n\nBut I’m committed to finding out everything we can. \n\nCommitted to military families like Danielle Robinson from Ohio. \n\nThe widow of Sergeant First Class Heath Robinson. \n\nHe was born a soldier. Army National Guard. Combat medic in Kosovo and Iraq. \n\nStationed near Baghdad, just yards from burn pits the size of football fields. \n\nHeath’s widow Danielle is here with us tonight. They loved going to Ohio State football games. He loved building Legos with their daughter.', metadata={'source': '../../../../../docs/docs/modules/state_of_the_union.txt'}), Document(page_content='A former top litigator in private practice. A former federal public defender. And from a family of public school educators and police officers. A consensus builder. Since she’s been nominated, she’s received a broad range of support—from the Fraternal Order of Police to former judges appointed by Democrats and Republicans. \n\nAnd if we are to advance liberty and justice, we need to secure the Border and fix the immigration system. \n\nWe can do both. At our border, we’ve installed new technology like cutting-edge scanners to better detect drug smuggling. \n\nWe’ve set up joint patrols with Mexico and Guatemala to catch more human traffickers. \n\nWe’re putting in place dedicated immigration judges so families fleeing persecution and violence can have their cases heard faster. \n\nWe’re securing commitments and supporting partners in South and Central America to host more refugees and secure their own borders.', metadata={'source': '../../../../../docs/docs/modules/state_of_the_union.txt'}), Document(page_content='We can’t change how divided we’ve been. But we can change how we move forward—on COVID-19 and other issues we must face together. \n\nI recently visited the New York City Police Department days after the funerals of Officer Wilbert Mora and his partner, Officer Jason Rivera. \n\nThey were responding to a 9-1-1 call when a man shot and killed them with a stolen gun. \n\nOfficer Mora was 27 years old. \n\nOfficer Rivera was 22. \n\nBoth Dominican Americans who’d grown up on the same streets they later chose to patrol as police officers. \n\nI spoke with their families and told them that we are forever in debt for their sacrifice, and we will carry on their mission to restore the trust and safety every community deserves. \n\nI’ve worked on these issues a long time. \n\nI know what works: Investing in crime preventionand community police officers who’ll walk the beat, who’ll know the neighborhood, and who can restore trust and safety.', metadata={'source': '../../../../../docs/docs/modules/state_of_the_union.txt'})]In the most recent state of the union, the President mentioned Kentaji Brown Jackson. The President nominated Circuit Court of Appeals Judge Ketanji Brown Jackson to serve on the United States Supreme Court. The President described Judge Ketanji Brown Jackson as one of our nation's top legal minds who will continue Justice Breyer's legacy of excellence. > Finished chain.result[""output""] ""In the most recent state of the union, the President mentioned Kentaji Brown Jackson. The President nominated Circuit Court of Appeals Judge Ketanji Brown Jackson to serve on the United States Supreme Court. The President described Judge Ketanji Brown Jackson as one of our nation's top legal minds who will continue Justice Breyer's legacy of excellence.""Notice that the follow up question asks about information previously retrieved, so no need to do another retrievalresult = agent_executor({""input"": ""how long ago did he nominate her?""}) > Entering new AgentExecutor chain... The President nominated Judge Ketanji Brown Jackson four days ago. > Finished chain.result[""output""] 'The President nominated Judge Ketanji Brown Jackson four days ago.'Creating from components​What actually is going on underneath the hood? Let's take a look so we can understand how to modify going forward.There are a few components:The memoryThe prompt templateThe agentThe agent executor# This is needed for both the memory and the promptmemory_key = ""history""The Memory​In this example, we want the agent to remember not only previous conversations, but also previous intermediate steps. For that, we can use AgentTokenBufferMemory. Note that if you want to change whether the agent remembers intermediate steps, or how the long the buffer is, or anything like that you should change this part.from langchain.agents.openai_functions_agent.agent_token_buffer_memory import AgentTokenBufferMemorymemory = AgentTokenBufferMemory(memory_key=memory_key, llm=llm)The Prompt Template​For the prompt template, we will use the OpenAIFunctionsAgent default way of creating one, but pass in a system prompt and a placeholder for memory.from langchain.agents.openai_functions_agent.base import OpenAIFunctionsAgentfrom langchain.schema.messages import SystemMessagefrom langchain.prompts import MessagesPlaceholdersystem_message = SystemMessage( content=( ""Do your best to answer the questions. "" ""Feel free to use any tools available to look up "" ""relevant information, only if neccessary"" ))prompt = OpenAIFunctionsAgent.create_prompt( system_message=system_message, extra_prompt_messages=[MessagesPlaceholder(variable_name=memory_key)] )The Agent​We will use the OpenAIFunctionsAgentagent = OpenAIFunctionsAgent(llm=llm, tools=tools, prompt=prompt)The Agent Executor​Importantly, we pass in return_intermediate_steps=True since we are recording that with our memory objectfrom langchain.agents import AgentExecutoragent_executor = AgentExecutor(agent=agent, tools=tools, memory=memory, verbose=True, return_intermediate_steps=True)result = agent_executor({""input"": ""hi, im bob""}) > Entering new AgentExecutor chain... Hello Bob! How can I assist you today? > Finished chain.result = agent_executor({""input"": ""whats my name""}) > Entering new AgentExecutor chain... Your name is Bob. > Finished chain.PreviousAnalyze DocumentNextPerform context-aware text splittingThe RetrieverRetriever ToolAgent ConstructorCreating from componentsThe MemoryThe Prompt TemplateThe AgentThe Agent Executor" 95,https://python.langchain.com/docs/use_cases/question_answering/how_to/document-context-aware-QA,"Question AnsweringHow toPerform context-aware text splittingPerform context-aware text splittingText splitting for vector storage often uses sentences or other delimiters to keep related text together. But many documents (such as Markdown files) have structure (headers) that can be explicitly used in splitting. The MarkdownHeaderTextSplitter lets a user split Markdown files files based on specified headers. This results in chunks that retain the header(s) that it came from in the metadata.This works nicely w/ SelfQueryRetriever.First, tell the retriever about our splits.Then, query based on the doc structure (e.g., ""summarize the doc introduction""). Chunks only from that section of the Document will be filtered and used in chat / Q+A.Let's test this out on an example Notion page!First, I download the page to Markdown as explained here.# Load Notion page as a markdownfile filefrom langchain.document_loaders import NotionDirectoryLoaderpath = ""../Notion_DB/""loader = NotionDirectoryLoader(path)docs = loader.load()md_file = docs[0].page_content# Let's create groups based on the section headers in our pagefrom langchain.text_splitter import MarkdownHeaderTextSplitterheaders_to_split_on = [ (""###"", ""Section""),]markdown_splitter = MarkdownHeaderTextSplitter(headers_to_split_on=headers_to_split_on)md_header_splits = markdown_splitter.split_text(md_file)Now, perform text splitting on the header grouped documents. # Define our text splitterfrom langchain.text_splitter import RecursiveCharacterTextSplitterchunk_size = 500chunk_overlap = 0text_splitter = RecursiveCharacterTextSplitter( chunk_size=chunk_size, chunk_overlap=chunk_overlap)all_splits = text_splitter.split_documents(md_header_splits)This sets us up well do perform metadata filtering based on the document structure.Let's bring this all togther by building a vectorstore first.pip install chromadb# Build vectorstore and keep the metadatafrom langchain.embeddings import OpenAIEmbeddingsfrom langchain.vectorstores import Chromavectorstore = Chroma.from_documents(documents=all_splits, embedding=OpenAIEmbeddings())Let's create a SelfQueryRetriever that can filter based upon metadata we defined.# Create retrieverfrom langchain.llms import OpenAIfrom langchain.retrievers.self_query.base import SelfQueryRetrieverfrom langchain.chains.query_constructor.base import AttributeInfo# Define our metadatametadata_field_info = [ AttributeInfo( name=""Section"", description=""Part of the document that the text comes from"", type=""string or list[string]"", ),]document_content_description = ""Major sections of the document""# Define self query retriverllm = OpenAI(temperature=0)retriever = SelfQueryRetriever.from_llm( llm, vectorstore, document_content_description, metadata_field_info, verbose=True)We can see that we can query only for texts in the Introduction of the document!# Testretriever.get_relevant_documents(""Summarize the Introduction section of the document"") query='Introduction' filter=Comparison(comparator=, attribute='Section', value='Introduction') limit=None [Document(page_content='![Untitled](Auto-Evaluation%20of%20Metadata%20Filtering%2018502448c85240828f33716740f9574b/Untitled.png)', metadata={'Section': 'Introduction'}), Document(page_content='Q+A systems often use a two-step approach: retrieve relevant text chunks and then synthesize them into an answer. There many ways to approach this. For example, we recently [discussed](https://blog.langchain.dev/auto-evaluation-of-anthropic-100k-context-window/) the Retriever-Less option (at bottom in the below diagram), highlighting the Anthropic 100k context window model. Metadata filtering is an alternative approach that pre-filters chunks based on a user-defined criteria in a VectorDB using', metadata={'Section': 'Introduction'}), Document(page_content='metadata tags prior to semantic search.', metadata={'Section': 'Introduction'})]# Testretriever.get_relevant_documents(""Summarize the Introduction section of the document"") query='Introduction' filter=Comparison(comparator=, attribute='Section', value='Introduction') limit=None [Document(page_content='![Untitled](Auto-Evaluation%20of%20Metadata%20Filtering%2018502448c85240828f33716740f9574b/Untitled.png)', metadata={'Section': 'Introduction'}), Document(page_content='Q+A systems often use a two-step approach: retrieve relevant text chunks and then synthesize them into an answer. There many ways to approach this. For example, we recently [discussed](https://blog.langchain.dev/auto-evaluation-of-anthropic-100k-context-window/) the Retriever-Less option (at bottom in the below diagram), highlighting the Anthropic 100k context window model. Metadata filtering is an alternative approach that pre-filters chunks based on a user-defined criteria in a VectorDB using', metadata={'Section': 'Introduction'}), Document(page_content='metadata tags prior to semantic search.', metadata={'Section': 'Introduction'})]We can also look at other parts of the document.retriever.get_relevant_documents(""Summarize the Testing section of the document"") query='Testing' filter=Comparison(comparator=, attribute='Section', value='Testing') limit=None [Document(page_content='![Untitled](Auto-Evaluation%20of%20Metadata%20Filtering%2018502448c85240828f33716740f9574b/Untitled%202.png)', metadata={'Section': 'Testing'}), Document(page_content='`SelfQueryRetriever` works well in [many cases](https://twitter.com/hwchase17/status/1656791488569954304/photo/1). For example, given [this test case](https://twitter.com/hwchase17/status/1656791488569954304?s=20): \n![Untitled](Auto-Evaluation%20of%20Metadata%20Filtering%2018502448c85240828f33716740f9574b/Untitled%201.png) \nThe query can be nicely broken up into semantic query and metadata filter: \n```python\nsemantic query: ""prompt injection""', metadata={'Section': 'Testing'}), Document(page_content='Below, we can see detailed results from the app: \n- Kor extraction is above to perform the transformation between query and metadata format ✅\n- Self-querying attempts to filter using the episode ID (`252`) in the query and fails 🚫\n- Baseline returns docs from 3 different episodes (one from `252`), confusing the answer 🚫', metadata={'Section': 'Testing'}), Document(page_content='will use in retrieval [here](https://github.com/langchain-ai/auto-evaluator/blob/main/streamlit/kor_retriever_lex.py).', metadata={'Section': 'Testing'})]Now, we can create chat or Q+A apps that are aware of the explict document structure. The ability to retain document structure for metadata filtering can be helpful for complicated or longer documents.from langchain.chains import RetrievalQAfrom langchain.chat_models import ChatOpenAIllm = ChatOpenAI(model_name=""gpt-3.5-turbo"", temperature=0)qa_chain = RetrievalQA.from_chain_type(llm, retriever=retriever)qa_chain.run(""Summarize the Testing section of the document"") query='Testing' filter=Comparison(comparator=, attribute='Section', value='Testing') limit=None 'The Testing section of the document describes the evaluation of the `SelfQueryRetriever` component in comparison to a baseline model. The evaluation was performed on a test case where the query was broken down into a semantic query and a metadata filter. The results showed that the `SelfQueryRetriever` component was able to perform the transformation between query and metadata format, but failed to filter using the episode ID in the query. The baseline model returned documents from three different episodes, which confused the answer. The `SelfQueryRetriever` component was deemed to work well in many cases and will be used in retrieval.'PreviousConversational Retrieval AgentNextRetrieve as you generate with FLARE" 96,https://python.langchain.com/docs/use_cases/question_answering/how_to/flare,"Question AnsweringHow toRetrieve as you generate with FLAREOn this pageRetrieve as you generate with FLAREThis notebook is an implementation of Forward-Looking Active REtrieval augmented generation (FLARE).Please see the original repo here.The basic idea is:Start answering a questionIf you start generating tokens the model is uncertain about, look up relevant documentsUse those documents to continue generatingRepeat until finishedThere is a lot of cool detail in how the lookup of relevant documents is done. Basically, the tokens that model is uncertain about are highlighted, and then an LLM is called to generate a question that would lead to that answer. For example, if the generated text is Joe Biden went to Harvard, and the tokens the model was uncertain about was Harvard, then a good generated question would be where did Joe Biden go to college. This generated question is then used in a retrieval step to fetch relevant documents.In order to set up this chain, we will need three things:An LLM to generate the answerAn LLM to generate hypothetical questions to use in retrievalA retriever to use to look up answers forThe LLM that we use to generate the answer needs to return logprobs so we can identify uncertain tokens. For that reason, we HIGHLY recommend that you use the OpenAI wrapper (NB: not the ChatOpenAI wrapper, as that does not return logprobs).The LLM we use to generate hypothetical questions to use in retrieval can be anything. In this notebook we will use ChatOpenAI because it is fast and cheap.The retriever can be anything. In this notebook we will use SERPER search engine, because it is cheap.Other important parameters to understand:max_generation_len: The maximum number of tokens to generate before stopping to check if any are uncertainmin_prob: Any tokens generated with probability below this will be considered uncertainImports​import osos.environ[""SERPER_API_KEY""] = """"os.environ[""OPENAI_API_KEY""] = """"import reimport numpy as npfrom langchain.schema import BaseRetrieverfrom langchain.callbacks.manager import ( AsyncCallbackManagerForRetrieverRun, CallbackManagerForRetrieverRun,)from langchain.utilities import GoogleSerperAPIWrapperfrom langchain.embeddings import OpenAIEmbeddingsfrom langchain.chat_models import ChatOpenAIfrom langchain.llms import OpenAIfrom langchain.schema import Documentfrom typing import Any, ListRetriever​class SerperSearchRetriever(BaseRetriever): search: GoogleSerperAPIWrapper = None def _get_relevant_documents( self, query: str, *, run_manager: CallbackManagerForRetrieverRun, **kwargs: Any ) -> List[Document]: return [Document(page_content=self.search.run(query))] async def _aget_relevant_documents( self, query: str, *, run_manager: AsyncCallbackManagerForRetrieverRun, **kwargs: Any, ) -> List[Document]: raise NotImplementedError()retriever = SerperSearchRetriever(search=GoogleSerperAPIWrapper())FLARE Chain​# We set this so we can see what exactly is going onimport langchainlangchain.verbose = Truefrom langchain.chains import FlareChainflare = FlareChain.from_llm( ChatOpenAI(temperature=0), retriever=retriever, max_generation_len=164, min_prob=0.3,)query = ""explain in great detail the difference between the langchain framework and baby agi""flare.run(query) > Entering new FlareChain chain... Current Response: Prompt after formatting: Respond to the user message using any relevant context. If context is provided, you should ground your answer in that context. Once you're done responding return FINISHED. >>> CONTEXT: >>> USER INPUT: explain in great detail the difference between the langchain framework and baby agi >>> RESPONSE: > Entering new QuestionGeneratorChain chain... Prompt after formatting: Given a user input and an existing partial response as context, ask a question to which the answer is the given term/entity/phrase: >>> USER INPUT: explain in great detail the difference between the langchain framework and baby agi >>> EXISTING PARTIAL RESPONSE: The Langchain Framework is a decentralized platform for natural language processing (NLP) applications. It uses a blockchain-based distributed ledger to store and process data, allowing for secure and transparent data sharing. The Langchain Framework also provides a set of tools and services to help developers create and deploy NLP applications. Baby AGI, on the other hand, is an artificial general intelligence (AGI) platform. It uses a combination of deep learning and reinforcement learning to create an AI system that can learn and adapt to new tasks. Baby AGI is designed to be a general-purpose AI system that can be used for a variety of applications, including natural language processing. In summary, the Langchain Framework is a platform for NLP applications, while Baby AGI is an AI system designed for The question to which the answer is the term/entity/phrase "" decentralized platform for natural language processing"" is: Prompt after formatting: Given a user input and an existing partial response as context, ask a question to which the answer is the given term/entity/phrase: >>> USER INPUT: explain in great detail the difference between the langchain framework and baby agi >>> EXISTING PARTIAL RESPONSE: The Langchain Framework is a decentralized platform for natural language processing (NLP) applications. It uses a blockchain-based distributed ledger to store and process data, allowing for secure and transparent data sharing. The Langchain Framework also provides a set of tools and services to help developers create and deploy NLP applications. Baby AGI, on the other hand, is an artificial general intelligence (AGI) platform. It uses a combination of deep learning and reinforcement learning to create an AI system that can learn and adapt to new tasks. Baby AGI is designed to be a general-purpose AI system that can be used for a variety of applications, including natural language processing. In summary, the Langchain Framework is a platform for NLP applications, while Baby AGI is an AI system designed for The question to which the answer is the term/entity/phrase "" uses a blockchain"" is: Prompt after formatting: Given a user input and an existing partial response as context, ask a question to which the answer is the given term/entity/phrase: >>> USER INPUT: explain in great detail the difference between the langchain framework and baby agi >>> EXISTING PARTIAL RESPONSE: The Langchain Framework is a decentralized platform for natural language processing (NLP) applications. It uses a blockchain-based distributed ledger to store and process data, allowing for secure and transparent data sharing. The Langchain Framework also provides a set of tools and services to help developers create and deploy NLP applications. Baby AGI, on the other hand, is an artificial general intelligence (AGI) platform. It uses a combination of deep learning and reinforcement learning to create an AI system that can learn and adapt to new tasks. Baby AGI is designed to be a general-purpose AI system that can be used for a variety of applications, including natural language processing. In summary, the Langchain Framework is a platform for NLP applications, while Baby AGI is an AI system designed for The question to which the answer is the term/entity/phrase "" distributed ledger to"" is: Prompt after formatting: Given a user input and an existing partial response as context, ask a question to which the answer is the given term/entity/phrase: >>> USER INPUT: explain in great detail the difference between the langchain framework and baby agi >>> EXISTING PARTIAL RESPONSE: The Langchain Framework is a decentralized platform for natural language processing (NLP) applications. It uses a blockchain-based distributed ledger to store and process data, allowing for secure and transparent data sharing. The Langchain Framework also provides a set of tools and services to help developers create and deploy NLP applications. Baby AGI, on the other hand, is an artificial general intelligence (AGI) platform. It uses a combination of deep learning and reinforcement learning to create an AI system that can learn and adapt to new tasks. Baby AGI is designed to be a general-purpose AI system that can be used for a variety of applications, including natural language processing. In summary, the Langchain Framework is a platform for NLP applications, while Baby AGI is an AI system designed for The question to which the answer is the term/entity/phrase "" process data, allowing for secure and transparent data sharing."" is: Prompt after formatting: Given a user input and an existing partial response as context, ask a question to which the answer is the given term/entity/phrase: >>> USER INPUT: explain in great detail the difference between the langchain framework and baby agi >>> EXISTING PARTIAL RESPONSE: The Langchain Framework is a decentralized platform for natural language processing (NLP) applications. It uses a blockchain-based distributed ledger to store and process data, allowing for secure and transparent data sharing. The Langchain Framework also provides a set of tools and services to help developers create and deploy NLP applications. Baby AGI, on the other hand, is an artificial general intelligence (AGI) platform. It uses a combination of deep learning and reinforcement learning to create an AI system that can learn and adapt to new tasks. Baby AGI is designed to be a general-purpose AI system that can be used for a variety of applications, including natural language processing. In summary, the Langchain Framework is a platform for NLP applications, while Baby AGI is an AI system designed for The question to which the answer is the term/entity/phrase "" set of tools"" is: Prompt after formatting: Given a user input and an existing partial response as context, ask a question to which the answer is the given term/entity/phrase: >>> USER INPUT: explain in great detail the difference between the langchain framework and baby agi >>> EXISTING PARTIAL RESPONSE: The Langchain Framework is a decentralized platform for natural language processing (NLP) applications. It uses a blockchain-based distributed ledger to store and process data, allowing for secure and transparent data sharing. The Langchain Framework also provides a set of tools and services to help developers create and deploy NLP applications. Baby AGI, on the other hand, is an artificial general intelligence (AGI) platform. It uses a combination of deep learning and reinforcement learning to create an AI system that can learn and adapt to new tasks. Baby AGI is designed to be a general-purpose AI system that can be used for a variety of applications, including natural language processing. In summary, the Langchain Framework is a platform for NLP applications, while Baby AGI is an AI system designed for The question to which the answer is the term/entity/phrase "" help developers create"" is: Prompt after formatting: Given a user input and an existing partial response as context, ask a question to which the answer is the given term/entity/phrase: >>> USER INPUT: explain in great detail the difference between the langchain framework and baby agi >>> EXISTING PARTIAL RESPONSE: The Langchain Framework is a decentralized platform for natural language processing (NLP) applications. It uses a blockchain-based distributed ledger to store and process data, allowing for secure and transparent data sharing. The Langchain Framework also provides a set of tools and services to help developers create and deploy NLP applications. Baby AGI, on the other hand, is an artificial general intelligence (AGI) platform. It uses a combination of deep learning and reinforcement learning to create an AI system that can learn and adapt to new tasks. Baby AGI is designed to be a general-purpose AI system that can be used for a variety of applications, including natural language processing. In summary, the Langchain Framework is a platform for NLP applications, while Baby AGI is an AI system designed for The question to which the answer is the term/entity/phrase "" create an AI system"" is: Prompt after formatting: Given a user input and an existing partial response as context, ask a question to which the answer is the given term/entity/phrase: >>> USER INPUT: explain in great detail the difference between the langchain framework and baby agi >>> EXISTING PARTIAL RESPONSE: The Langchain Framework is a decentralized platform for natural language processing (NLP) applications. It uses a blockchain-based distributed ledger to store and process data, allowing for secure and transparent data sharing. The Langchain Framework also provides a set of tools and services to help developers create and deploy NLP applications. Baby AGI, on the other hand, is an artificial general intelligence (AGI) platform. It uses a combination of deep learning and reinforcement learning to create an AI system that can learn and adapt to new tasks. Baby AGI is designed to be a general-purpose AI system that can be used for a variety of applications, including natural language processing. In summary, the Langchain Framework is a platform for NLP applications, while Baby AGI is an AI system designed for The question to which the answer is the term/entity/phrase "" NLP applications"" is: > Finished chain. Generated Questions: ['What is the Langchain Framework?', 'What technology does the Langchain Framework use to store and process data for secure and transparent data sharing?', 'What technology does the Langchain Framework use to store and process data?', 'What does the Langchain Framework use a blockchain-based distributed ledger for?', 'What does the Langchain Framework provide in addition to a decentralized platform for natural language processing applications?', 'What set of tools and services does the Langchain Framework provide?', 'What is the purpose of Baby AGI?', 'What type of applications is the Langchain Framework designed for?'] > Entering new _OpenAIResponseChain chain... Prompt after formatting: Respond to the user message using any relevant context. If context is provided, you should ground your answer in that context. Once you're done responding return FINISHED. >>> CONTEXT: LangChain: Software. LangChain is a software development framework designed to simplify the creation of applications using large language models. LangChain Initial release date: October 2022. LangChain Programming languages: Python and JavaScript. LangChain Developer(s): Harrison Chase. LangChain License: MIT License. LangChain is a framework for developing applications powered by language models. We believe that the most powerful and differentiated applications will not only ... Type: Software framework. At its core, LangChain is a framework built around LLMs. We can use it for chatbots, Generative Question-Answering (GQA), summarization, and much more. LangChain is a powerful tool that can be used to work with Large Language Models (LLMs). LLMs are very general in nature, which means that while they can ... LangChain is an intuitive framework created to assist in developing applications driven by a language model, such as OpenAI or Hugging Face. LangChain is a software development framework designed to simplify the creation of applications using large language models (LLMs). Written in: Python and JavaScript. Initial release: October 2022. LangChain - The A.I-native developer toolkit We started LangChain with the intent to build a modular and flexible framework for developing A.I- ... LangChain explained in 3 minutes - LangChain is a ... Duration: 3:03. Posted: Apr 13, 2023. LangChain is a framework built to help you build LLM-powered applications more easily by providing you with the following:. LangChain is a framework that enables quick and easy development of applications that make use of Large Language Models, for example, GPT-3. LangChain is a powerful open-source framework for developing applications powered by language models. It connects to the AI models you want to ... LangChain is a framework for including AI from large language models inside data pipelines and applications. This tutorial provides an overview of what you ... Missing: secure | Must include:secure. Blockchain is the best way to secure the data of the shared community. Utilizing the capabilities of the blockchain nobody can read or interfere ... This modern technology consists of a chain of blocks that allows to securely store all committed transactions using shared and distributed ... A Blockchain network is used in the healthcare system to preserve and exchange patient data through hospitals, diagnostic laboratories, pharmacy firms, and ... In this article, I will walk you through the process of using the LangChain.js library with Google Cloud Functions, helping you leverage the ... LangChain is an intuitive framework created to assist in developing applications driven by a language model, such as OpenAI or Hugging Face. Missing: transparent | Must include:transparent. This technology keeps a distributed ledger on each blockchain node, making it more secure and transparent. The blockchain network can operate smart ... blockchain technology can offer a highly secured health data ledger to ... framework can be employed to store encrypted healthcare data in a ... In a simplified way, Blockchain is a data structure that stores transactions in an ordered way and linked to the previous block, serving as a ... Blockchain technology is a decentralized, distributed ledger that stores the record of ownership of digital assets. Missing: Langchain | Must include:Langchain. LangChain is a framework for including AI from large language models inside data pipelines and applications. This tutorial provides an overview of what you ... LangChain is an intuitive framework created to assist in developing applications driven by a language model, such as OpenAI or Hugging Face. This documentation covers the steps to integrate Pinecone, a high-performance vector database, with LangChain, a framework for building applications powered ... The ability to connect to any model, ingest any custom database, and build upon a framework that can take action provides numerous use cases for ... With LangChain, developers can use a framework that abstracts the core building blocks of LLM applications. LangChain empowers developers to ... Build a question-answering tool based on financial data with LangChain & Deep Lake's unified & streamable data store. Browse applications built on LangChain technology. Explore PoC and MVP applications created by our community and discover innovative use cases for LangChain ... LangChain is a great framework that can be used for developing applications powered by LLMs. When you intend to enhance your application ... In this blog, we'll introduce you to LangChain and Ray Serve and how to use them to build a search engine using LLM embeddings and a vector ... The LinkChain Framework simplifies embedding creation and storage using Pinecone and Chroma, with code that loads files, splits documents, and creates embedding ... Missing: technology | Must include:technology. Blockchain is one type of a distributed ledger. Distributed ledgers use independent computers (referred to as nodes) to record, share and ... Missing: Langchain | Must include:Langchain. Blockchain is used in distributed storage software where huge data is broken down into chunks. This is available in encrypted data across a ... People sometimes use the terms 'Blockchain' and 'Distributed Ledger' interchangeably. This post aims to analyze the features of each. A distributed ledger ... Missing: Framework | Must include:Framework. Think of a “distributed ledger” that uses cryptography to allow each participant in the transaction to add to the ledger in a secure way without ... In this paper, we provide an overview of the history of trade settlement and discuss this nascent technology that may now transform traditional ... Missing: Langchain | Must include:Langchain. LangChain is a blockchain-based language education platform that aims to revolutionize the way people learn languages. Missing: Framework | Must include:Framework. It uses the distributed ledger technology framework and Smart contract engine for building scalable Business Blockchain applications. The fabric ... It looks at the assets the use case is handling, the different parties conducting transactions, and the smart contract, distributed ... Are you curious to know how Blockchain and Distributed ... Duration: 44:31. Posted: May 4, 2021. A blockchain is a distributed and immutable ledger to transfer ownership, record transactions, track assets, and ensure transparency, security, trust and value ... Missing: Langchain | Must include:Langchain. LangChain is an intuitive framework created to assist in developing applications driven by a language model, such as OpenAI or Hugging Face. Missing: decentralized | Must include:decentralized. LangChain, created by Harrison Chase, is a Python library that provides out-of-the-box support to build NLP applications using LLMs. Missing: decentralized | Must include:decentralized. LangChain provides a standard interface for chains, enabling developers to create sequences of calls that go beyond a single LLM call. Chains ... Missing: decentralized platform natural. LangChain is a powerful framework that simplifies the process of building advanced language model applications. Missing: platform | Must include:platform. Are your language models ignoring previous instructions ... Duration: 32:23. Posted: Feb 21, 2023. LangChain is a framework that enables quick and easy development of applications ... Prompting is the new way of programming NLP models. Missing: decentralized platform. It then uses natural language processing and machine learning algorithms to search ... Summarization is handled via cohere, QnA is handled via langchain, ... LangChain is a framework for developing applications powered by language models. ... There are several main modules that LangChain provides support for. Missing: decentralized platform. In the healthcare-chain system, blockchain provides an appreciated secure ... The entire process of adding new and previous block data is performed based on ... ChatGPT is a large language model developed by OpenAI, ... tool for a wide range of applications, including natural language processing, ... LangChain is a powerful tool that can be used to work with Large Language ... If an API key has been provided, create an OpenAI language model instance At its core, LangChain is a framework built around LLMs. We can use it for chatbots, Generative Question-Answering (GQA), summarization, and much more. A tutorial of the six core modules of the LangChain Python package covering models, prompts, chains, agents, indexes, and memory with OpenAI ... LangChain's collection of tools refers to a set of tools provided by the LangChain framework for developing applications powered by language models. LangChain is a framework for developing applications powered by language models. We believe that the most powerful and differentiated applications will not only ... LangChain is an open-source library that provides developers with the tools to build applications powered by large language models (LLMs). LangChain is a framework for including AI from large language models inside data pipelines and applications. This tutorial provides an overview of what you ... Plan-and-Execute Agents · Feature Stores and LLMs · Structured Tools · Auto-Evaluator Opportunities · Callbacks Improvements · Unleashing the power ... Tool: A function that performs a specific duty. This can be things like: Google Search, Database lookup, Python REPL, other chains. · LLM: The language model ... LangChain provides a standard interface for chains, lots of integrations with other tools, and end-to-end chains for common applications. Baby AGI has the ability to complete tasks, generate new tasks based on previous results, and prioritize tasks in real-time. This system is exploring and demonstrating to us the potential of large language models, such as GPT and how it can autonomously perform tasks. Apr 17, 2023 At its core, LangChain is a framework built around LLMs. We can use it for chatbots, Generative Question-Answering (GQA), summarization, and much more. The core idea of the library is that we can “chain” together different components to create more advanced use cases around LLMs. >>> USER INPUT: explain in great detail the difference between the langchain framework and baby agi >>> RESPONSE: > Finished chain. > Finished chain. ' LangChain is a framework for developing applications powered by language models. It provides a standard interface for chains, lots of integrations with other tools, and end-to-end chains for common applications. On the other hand, Baby AGI is an AI system that is exploring and demonstrating the potential of large language models, such as GPT, and how it can autonomously perform tasks. Baby AGI has the ability to complete tasks, generate new tasks based on previous results, and prioritize tasks in real-time. 'llm = OpenAI()llm(query) '\n\nThe Langchain framework and Baby AGI are both artificial intelligence (AI) frameworks that are used to create intelligent agents. The Langchain framework is a supervised learning system that is based on the concept of “language chains”. It uses a set of rules to map natural language inputs to specific outputs. It is a general-purpose AI framework and can be used to build applications such as natural language processing (NLP), chatbots, and more.\n\nBaby AGI, on the other hand, is an unsupervised learning system that uses neural networks and reinforcement learning to learn from its environment. It is used to create intelligent agents that can adapt to changing environments. It is a more advanced AI system and can be used to build more complex applications such as game playing, robotic vision, and more.\n\nThe main difference between the two is that the Langchain framework uses supervised learning while Baby AGI uses unsupervised learning. The Langchain framework is a general-purpose AI framework that can be used for various applications, while Baby AGI is a more advanced AI system that can be used to create more complex applications.'flare.run(""how are the origin stories of langchain and bitcoin similar or different?"") > Entering new FlareChain chain... Current Response: Prompt after formatting: Respond to the user message using any relevant context. If context is provided, you should ground your answer in that context. Once you're done responding return FINISHED. >>> CONTEXT: >>> USER INPUT: how are the origin stories of langchain and bitcoin similar or different? >>> RESPONSE: > Entering new QuestionGeneratorChain chain... Prompt after formatting: Given a user input and an existing partial response as context, ask a question to which the answer is the given term/entity/phrase: >>> USER INPUT: how are the origin stories of langchain and bitcoin similar or different? >>> EXISTING PARTIAL RESPONSE: Langchain and Bitcoin have very different origin stories. Bitcoin was created by the mysterious Satoshi Nakamoto in 2008 as a decentralized digital currency. Langchain, on the other hand, was created in 2020 by a team of developers as a platform for creating and managing decentralized language learning applications. FINISHED The question to which the answer is the term/entity/phrase "" very different origin"" is: Prompt after formatting: Given a user input and an existing partial response as context, ask a question to which the answer is the given term/entity/phrase: >>> USER INPUT: how are the origin stories of langchain and bitcoin similar or different? >>> EXISTING PARTIAL RESPONSE: Langchain and Bitcoin have very different origin stories. Bitcoin was created by the mysterious Satoshi Nakamoto in 2008 as a decentralized digital currency. Langchain, on the other hand, was created in 2020 by a team of developers as a platform for creating and managing decentralized language learning applications. FINISHED The question to which the answer is the term/entity/phrase "" 2020 by a"" is: Prompt after formatting: Given a user input and an existing partial response as context, ask a question to which the answer is the given term/entity/phrase: >>> USER INPUT: how are the origin stories of langchain and bitcoin similar or different? >>> EXISTING PARTIAL RESPONSE: Langchain and Bitcoin have very different origin stories. Bitcoin was created by the mysterious Satoshi Nakamoto in 2008 as a decentralized digital currency. Langchain, on the other hand, was created in 2020 by a team of developers as a platform for creating and managing decentralized language learning applications. FINISHED The question to which the answer is the term/entity/phrase "" developers as a platform for creating and managing decentralized language learning applications."" is: > Finished chain. Generated Questions: ['How would you describe the origin stories of Langchain and Bitcoin in terms of their similarities or differences?', 'When was Langchain created and by whom?', 'What was the purpose of creating Langchain?'] > Entering new _OpenAIResponseChain chain... Prompt after formatting: Respond to the user message using any relevant context. If context is provided, you should ground your answer in that context. Once you're done responding return FINISHED. >>> CONTEXT: Bitcoin and Ethereum have many similarities but different long-term visions and limitations. Ethereum changed from proof of work to proof of ... Bitcoin will be around for many years and examining its white paper origins is a great exercise in understanding why. Satoshi Nakamoto's blueprint describes ... Bitcoin is a new currency that was created in 2009 by an unknown person using the alias Satoshi Nakamoto. Transactions are made with no middle men – meaning, no ... Missing: Langchain | Must include:Langchain. By comparison, Bitcoin transaction speeds are tremendously lower. ... learn about its history and its role in the emergence of the Bitcoin ... LangChain is a powerful framework that simplifies the process of ... tasks like document retrieval, clustering, and similarity comparisons. Key terms: Bitcoin System, Blockchain Technology, ... Furthermore, the research paper will discuss and compare the five payment. Blockchain first appeared in Nakamoto's Bitcoin white paper that describes a new decentralized cryptocurrency [1]. Bitcoin takes the blockchain technology ... Missing: stories | Must include:stories. A score of 0 means there were not enough data for this term. Google trends was accessed on 5 November 2018 with searches for bitcoin, euro, gold ... Contracts, transactions, and records of them provide critical structure in our economic system, but they haven't kept up with the world's digital ... Missing: Langchain | Must include:Langchain. Of course, traders try to make a profit on their portfolio in this way.The difference between investing and trading is the regularity with which ... After all these giant leaps forward in the LLM space, OpenAI released ChatGPT — thrusting LLMs into the spotlight. LangChain appeared around the same time. Its creator, Harrison Chase, made the first commit in late October 2022. Leaving a short couple of months of development before getting caught in the LLM wave. At its core, LangChain is a framework built around LLMs. We can use it for chatbots, Generative Question-Answering (GQA), summarization, and much more. The core idea of the library is that we can “chain” together different components to create more advanced use cases around LLMs. >>> USER INPUT: how are the origin stories of langchain and bitcoin similar or different? >>> RESPONSE: > Finished chain. > Finished chain. ' The origin stories of LangChain and Bitcoin are quite different. Bitcoin" was created in 2009 by an unknown person using the alias Satoshi Nakamoto. LangChain was created in late October 2022 by Harrison Chase. Bitcoin is a decentralized cryptocurrency," while LangChain is a framework built around LLMs. 'PreviousPerform context-aware text splittingNextImprove document indexing with HyDEImportsRetrieverFLARE Chain""", 97,https://python.langchain.com/docs/use_cases/question_answering/how_to/hyde,"Question AnsweringHow toImprove document indexing with HyDEOn this pageImprove document indexing with HyDEThis notebook goes over how to use Hypothetical Document Embeddings (HyDE), as described in this paper. At a high level, HyDE is an embedding technique that takes queries, generates a hypothetical answer, and then embeds that generated document and uses that as the final example. In order to use HyDE, we therefore need to provide a base embedding model, as well as an LLMChain that can be used to generate those documents. By default, the HyDE class comes with some default prompts to use (see the paper for more details on them), but we can also create our own.from langchain.llms import OpenAIfrom langchain.embeddings import OpenAIEmbeddingsfrom langchain.chains import LLMChain, HypotheticalDocumentEmbedderfrom langchain.prompts import PromptTemplatebase_embeddings = OpenAIEmbeddings()llm = OpenAI()# Load with `web_search` promptembeddings = HypotheticalDocumentEmbedder.from_llm(llm, base_embeddings, ""web_search"")# Now we can use it as any embedding class!result = embeddings.embed_query(""Where is the Taj Mahal?"")Multiple generations​We can also generate multiple documents and then combine the embeddings for those. By default, we combine those by taking the average. We can do this by changing the LLM we use to generate documents to return multiple things.multi_llm = OpenAI(n=4, best_of=4)embeddings = HypotheticalDocumentEmbedder.from_llm( multi_llm, base_embeddings, ""web_search"")result = embeddings.embed_query(""Where is the Taj Mahal?"")Using our own prompts​Besides using preconfigured prompts, we can also easily construct our own prompts and use those in the LLMChain that is generating the documents. This can be useful if we know the domain our queries will be in, as we can condition the prompt to generate text more similar to that.In the example below, let's condition it to generate text about a state of the union address (because we will use that in the next example).prompt_template = """"""Please answer the user's question about the most recent state of the union addressQuestion: {question}Answer:""""""prompt = PromptTemplate(input_variables=[""question""], template=prompt_template)llm_chain = LLMChain(llm=llm, prompt=prompt)embeddings = HypotheticalDocumentEmbedder( llm_chain=llm_chain, base_embeddings=base_embeddings)result = embeddings.embed_query( ""What did the president say about Ketanji Brown Jackson"")Using HyDE​Now that we have HyDE, we can use it as we would any other embedding class! Here is using it to find similar passages in the state of the union example.from langchain.text_splitter import CharacterTextSplitterfrom langchain.vectorstores import Chromawith open(""../../state_of_the_union.txt"") as f: state_of_the_union = f.read()text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)texts = text_splitter.split_text(state_of_the_union)docsearch = Chroma.from_texts(texts, embeddings)query = ""What did the president say about Ketanji Brown Jackson""docs = docsearch.similarity_search(query) Running Chroma using direct local API. Using DuckDB in-memory for database. Data will be transient.print(docs[0].page_content) In state after state, new laws have been passed, not only to suppress the vote, but to subvert entire elections. We cannot let this happen. Tonight. I call on the Senate to: Pass the Freedom to Vote Act. Pass the John Lewis Voting Rights Act. And while you’re at it, pass the Disclose Act so Americans can know who is funding our elections. Tonight, I’d like to honor someone who has dedicated his life to serve this country: Justice Stephen Breyer—an Army veteran, Constitutional scholar, and retiring Justice of the United States Supreme Court. Justice Breyer, thank you for your service. One of the most serious constitutional responsibilities a President has is nominating someone to serve on the United States Supreme Court. And I did that 4 days ago, when I nominated Circuit Court of Appeals Judge Ketanji Brown Jackson. One of our nation’s top legal minds, who will continue Justice Breyer’s legacy of excellence.PreviousRetrieve as you generate with FLARENextUse local LLMsMultiple generationsUsing our own promptsUsing HyDE" 98,https://python.langchain.com/docs/use_cases/question_answering/how_to/local_retrieval_qa,"Question AnsweringHow toUse local LLMsOn this pageUse local LLMsThe popularity of projects like PrivateGPT, llama.cpp, and GPT4All underscore the importance of running LLMs locally.LangChain has integrations with many open source LLMs that can be run locally.See here for setup instructions for these LLMs. For example, here we show how to run GPT4All or LLaMA2 locally (e.g., on your laptop) using local embeddings and a local LLM.Document Loading​First, install packages needed for local embeddings and vector storage.pip install gpt4all chromadb langchainhubLoad and split an example docucment.We'll use a blog post on agents as an example.from langchain.document_loaders import WebBaseLoaderloader = WebBaseLoader(""https://lilianweng.github.io/posts/2023-06-23-agent/"")data = loader.load()from langchain.text_splitter import RecursiveCharacterTextSplittertext_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=0)all_splits = text_splitter.split_documents(data)Next, the below steps will download the GPT4All embeddings locally (if you don't already have them).from langchain.vectorstores import Chromafrom langchain.embeddings import GPT4AllEmbeddingsvectorstore = Chroma.from_documents(documents=all_splits, embedding=GPT4AllEmbeddings()) Found model file at /Users/rlm/.cache/gpt4all/ggml-all-MiniLM-L6-v2-f16.bin objc[49534]: Class GGMLMetalClass is implemented in both /Users/rlm/miniforge3/envs/llama2/lib/python3.9/site-packages/gpt4all/llmodel_DO_NOT_MODIFY/build/libreplit-mainline-metal.dylib (0x131614208) and /Users/rlm/miniforge3/envs/llama2/lib/python3.9/site-packages/gpt4all/llmodel_DO_NOT_MODIFY/build/libllamamodel-mainline-metal.dylib (0x131988208). One of the two will be used. Which one is undefined.Test similarity search is working with our local embeddings.question = ""What are the approaches to Task Decomposition?""docs = vectorstore.similarity_search(question)len(docs) 4docs[0] Document(page_content='Task decomposition can be done (1) by LLM with simple prompting like ""Steps for XYZ.\\n1."", ""What are the subgoals for achieving XYZ?"", (2) by using task-specific instructions; e.g. ""Write a story outline."" for writing a novel, or (3) with human inputs.', metadata={'description': 'Building agents with LLM (large language model) as its core controller is a cool concept. Several proof-of-concepts demos, such as AutoGPT, GPT-Engineer and BabyAGI, serve as inspiring examples. The potentiality of LLM extends beyond generating well-written copies, stories, essays and programs; it can be framed as a powerful general problem solver.\nAgent System Overview In a LLM-powered autonomous agent system, LLM functions as the agent’s brain, complemented by several key components:', 'language': 'en', 'source': 'https://lilianweng.github.io/posts/2023-06-23-agent/', 'title': ""LLM Powered Autonomous Agents | Lil'Log""})Model​LLaMA2​Note: new versions of llama-cpp-python use GGUF model files (see here).If you have an existing GGML model, see here for instructions for conversion for GGUF. And / or, you can download a GGUF converted model (e.g., here).Finally, as noted in detail here install llama-cpp-pythonpip install llama-cpp-pythonTo enable use of GPU on Apple Silicon, follow the steps here to use the Python binding with Metal support.In particular, ensure that conda is using the correct virtual enviorment that you created (miniforge3).E.g., for me:conda activate /Users/rlm/miniforge3/envs/llamaWith this confirmed:CMAKE_ARGS=""-DLLAMA_METAL=on"" FORCE_CMAKE=1 /Users/rlm/miniforge3/envs/llama/bin/pip install -U llama-cpp-python --no-cache-dirfrom langchain.llms import LlamaCppfrom langchain.callbacks.manager import CallbackManagerfrom langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandlerSetting model parameters as noted in the llama.cpp docs.n_gpu_layers = 1 # Metal set to 1 is enough.n_batch = 512 # Should be between 1 and n_ctx, consider the amount of RAM of your Apple Silicon Chip.callback_manager = CallbackManager([StreamingStdOutCallbackHandler()])# Make sure the model path is correct for your system!llm = LlamaCpp( model_path=""/Users/rlm/Desktop/Code/llama.cpp/models/llama-2-13b-chat.ggufv3.q4_0.bin"", n_gpu_layers=n_gpu_layers, n_batch=n_batch, n_ctx=2048, f16_kv=True, # MUST set to True, otherwise you will run into problem after a couple of calls callback_manager=callback_manager, verbose=True,)Note that these indicate that Metal was enabled properly:ggml_metal_init: allocatingggml_metal_init: using MPSllm(""Simulate a rap battle between Stephen Colbert and John Oliver"") Llama.generate: prefix-match hit by jonathan Here's the hypothetical rap battle: [Stephen Colbert]: Yo, this is Stephen Colbert, known for my comedy show. I'm here to put some sense in your mind, like an enema do-go. Your opponent? A man of laughter and witty quips, John Oliver! Now let's see who gets the most laughs while taking shots at each other [John Oliver]: Yo, this is John Oliver, known for my own comedy show. I'm here to take your mind on an adventure through wit and humor. But first, allow me to you to our contestant: Stephen Colbert! His show has been around since the '90s, but it's time to see who can out-rap whom [Stephen Colbert]: You claim to be a witty man, John Oliver, with your British charm and clever remarks. But my knows that I'm America's funnyman! Who's the one taking you? Nobody! [John Oliver]: Hey Stephen Colbert, don't get too cocky. You may llama_print_timings: load time = 4481.74 ms llama_print_timings: sample time = 183.05 ms / 256 runs ( 0.72 ms per token, 1398.53 tokens per second) llama_print_timings: prompt eval time = 456.05 ms / 13 tokens ( 35.08 ms per token, 28.51 tokens per second) llama_print_timings: eval time = 7375.20 ms / 255 runs ( 28.92 ms per token, 34.58 tokens per second) llama_print_timings: total time = 8388.92 ms ""by jonathan \n\nHere's the hypothetical rap battle:\n\n[Stephen Colbert]: Yo, this is Stephen Colbert, known for my comedy show. I'm here to put some sense in your mind, like an enema do-go. Your opponent? A man of laughter and witty quips, John Oliver! Now let's see who gets the most laughs while taking shots at each other\n\n[John Oliver]: Yo, this is John Oliver, known for my own comedy show. I'm here to take your mind on an adventure through wit and humor. But first, allow me to you to our contestant: Stephen Colbert! His show has been around since the '90s, but it's time to see who can out-rap whom\n\n[Stephen Colbert]: You claim to be a witty man, John Oliver, with your British charm and clever remarks. But my knows that I'm America's funnyman! Who's the one taking you? Nobody!\n\n[John Oliver]: Hey Stephen Colbert, don't get too cocky. You may""GPT4All​Similarly, we can use GPT4All.Download the GPT4All model binary.The Model Explorer on the GPT4All is a great way to choose and download a model.Then, specify the path that you downloaded to to.E.g., for me, the model lives here:/Users/rlm/Desktop/Code/gpt4all/models/nous-hermes-13b.ggmlv3.q4_0.binfrom langchain.llms import GPT4Allllm = GPT4All( model=""/Users/rlm/Desktop/Code/gpt4all/models/nous-hermes-13b.ggmlv3.q4_0.bin"", max_tokens=2048,)LLMChain​Run an LLMChain (see here) with either model by passing in the retrieved docs and a simple prompt.It formats the prompt template using the input key values provided and passes the formatted string to GPT4All, LLama-V2, or another specified LLM.In this case, the list of retrieved documents (docs) above are pass into {context}.from langchain.prompts import PromptTemplatefrom langchain.chains import LLMChain# Promptprompt = PromptTemplate.from_template( ""Summarize the main themes in these retrieved docs: {docs}"")# Chainllm_chain = LLMChain(llm=llm, prompt=prompt)# Runquestion = ""What are the approaches to Task Decomposition?""docs = vectorstore.similarity_search(question)result = llm_chain(docs)# Outputresult[""text""] Llama.generate: prefix-match hit Based on the retrieved documents, the main themes are: 1. Task decomposition: The ability to break down complex tasks into smaller subtasks, which can be handled by an LLM or other components of the agent system. 2. LLM as the core controller: The use of a large language model (LLM) as the primary controller of an autonomous agent system, complemented by other key components such as a knowledge graph and a planner. 3. Potentiality of LLM: The idea that LLMs have the potential to be used as powerful general problem solvers, not just for generating well-written copies but also for solving complex tasks and achieving human-like intelligence. 4. Challenges in long-term planning: The challenges in planning over a lengthy history and effectively exploring the solution space, which are important limitations of current LLM-based autonomous agent systems. llama_print_timings: load time = 1191.88 ms llama_print_timings: sample time = 134.47 ms / 193 runs ( 0.70 ms per token, 1435.25 tokens per second) llama_print_timings: prompt eval time = 39470.18 ms / 1055 tokens ( 37.41 ms per token, 26.73 tokens per second) llama_print_timings: eval time = 8090.85 ms / 192 runs ( 42.14 ms per token, 23.73 tokens per second) llama_print_timings: total time = 47943.12 ms '\nBased on the retrieved documents, the main themes are:\n1. Task decomposition: The ability to break down complex tasks into smaller subtasks, which can be handled by an LLM or other components of the agent system.\n2. LLM as the core controller: The use of a large language model (LLM) as the primary controller of an autonomous agent system, complemented by other key components such as a knowledge graph and a planner.\n3. Potentiality of LLM: The idea that LLMs have the potential to be used as powerful general problem solvers, not just for generating well-written copies but also for solving complex tasks and achieving human-like intelligence.\n4. Challenges in long-term planning: The challenges in planning over a lengthy history and effectively exploring the solution space, which are important limitations of current LLM-based autonomous agent systems.'QA Chain​We can use a QA chain to handle our question above.chain_type=""stuff"" (see here) means that all the docs will be added (stuffed) into a prompt.We can also use the LangChain Prompt Hub to store and fetch prompts that are model-specific.This will work with your LangSmith API key.Let's try with a default RAG prompt, here.pip install langchainhub# Prompt from langchain import hubrag_prompt = hub.pull(""rlm/rag-prompt"")from langchain.chains.question_answering import load_qa_chain# Chainchain = load_qa_chain(llm, chain_type=""stuff"", prompt=rag_prompt)# Runchain({""input_documents"": docs, ""question"": question}, return_only_outputs=True) Llama.generate: prefix-match hit Task can be done by down a task into smaller subtasks, using simple prompting like ""Steps for XYZ."" or task-specific like ""Write a story outline"" for writing a novel. llama_print_timings: load time = 11326.20 ms llama_print_timings: sample time = 33.03 ms / 47 runs ( 0.70 ms per token, 1422.86 tokens per second) llama_print_timings: prompt eval time = 1387.31 ms / 242 tokens ( 5.73 ms per token, 174.44 tokens per second) llama_print_timings: eval time = 1321.62 ms / 46 runs ( 28.73 ms per token, 34.81 tokens per second) llama_print_timings: total time = 2801.08 ms {'output_text': '\nTask can be done by down a task into smaller subtasks, using simple prompting like ""Steps for XYZ."" or task-specific like ""Write a story outline"" for writing a novel.'}Now, let's try with a prompt specifically for LLaMA, which includes special tokens.# Promptrag_prompt_llama = hub.pull(""rlm/rag-prompt-llama"")rag_prompt_llama ChatPromptTemplate(input_variables=['question', 'context'], output_parser=None, partial_variables={}, messages=[HumanMessagePromptTemplate(prompt=PromptTemplate(input_variables=['question', 'context'], output_parser=None, partial_variables={}, template=""[INST]<> You are an assistant for question-answering tasks. Use the following pieces of retrieved context to answer the question. If you don't know the answer, just say that you don't know. Use three sentences maximum and keep the answer concise.<> \nQuestion: {question} \nContext: {context} \nAnswer: [/INST]"", template_format='f-string', validate_template=True), additional_kwargs={})])# Chainchain = load_qa_chain(llm, chain_type=""stuff"", prompt=rag_prompt_llama)# Runchain({""input_documents"": docs, ""question"": question}, return_only_outputs=True) Llama.generate: prefix-match hit Sure, I'd be happy to help! Based on the context, here are some to task: 1. LLM with simple prompting: This using a large model (LLM) with simple prompts like ""Steps for XYZ"" or ""What are the subgoals for achieving XYZ?"" to decompose tasks into smaller steps. 2. Task-specific: Another is to use task-specific, such as ""Write a story outline"" for writing a novel, to guide the of tasks. 3. Human inputs:, human inputs can be used to supplement the process, in cases where the task a high degree of creativity or expertise. As fores in long-term and task, one major is that LLMs to adjust plans when faced with errors, making them less robust to humans who learn from trial and error. llama_print_timings: load time = 11326.20 ms llama_print_timings: sample time = 144.81 ms / 207 runs ( 0.70 ms per token, 1429.47 tokens per second) llama_print_timings: prompt eval time = 1506.13 ms / 258 tokens ( 5.84 ms per token, 171.30 tokens per second) llama_print_timings: eval time = 6231.92 ms / 206 runs ( 30.25 ms per token, 33.06 tokens per second) llama_print_timings: total time = 8158.41 ms {'output_text': ' Sure, I\'d be happy to help! Based on the context, here are some to task:\n\n1. LLM with simple prompting: This using a large model (LLM) with simple prompts like ""Steps for XYZ"" or ""What are the subgoals for achieving XYZ?"" to decompose tasks into smaller steps.\n2. Task-specific: Another is to use task-specific, such as ""Write a story outline"" for writing a novel, to guide the of tasks.\n3. Human inputs:, human inputs can be used to supplement the process, in cases where the task a high degree of creativity or expertise.\n\nAs fores in long-term and task, one major is that LLMs to adjust plans when faced with errors, making them less robust to humans who learn from trial and error.'}RetrievalQA​For an even simpler flow, use RetrievalQA.This will use a QA default prompt (shown here) and will retrieve from the vectorDB.But, you can still pass in a prompt, as before, if desired.from langchain.chains import RetrievalQAqa_chain = RetrievalQA.from_chain_type( llm, retriever=vectorstore.as_retriever(), chain_type_kwargs={""prompt"": rag_prompt_llama},)qa_chain({""query"": question}) Llama.generate: prefix-match hit Sure! Based on the context, here's my answer to your: There are several to task,: 1. LLM-based with simple prompting, such as ""Steps for XYZ"" or ""What are the subgoals for achieving XYZ?"" 2. Task-specific, like ""Write a story outline"" for writing a novel. 3. Human inputs to guide the process. These can be used to decompose complex tasks into smaller, more manageable subtasks, which can help improve the and effectiveness of task. However, long-term and task can being due to the need to plan over a lengthy history and explore the space., LLMs may to adjust plans when faced with errors, making them less robust to human learners who can learn from trial and error. llama_print_timings: load time = 11326.20 ms llama_print_timings: sample time = 139.20 ms / 200 runs ( 0.70 ms per token, 1436.76 tokens per second) llama_print_timings: prompt eval time = 1532.26 ms / 258 tokens ( 5.94 ms per token, 168.38 tokens per second) llama_print_timings: eval time = 5977.62 ms / 199 runs ( 30.04 ms per token, 33.29 tokens per second) llama_print_timings: total time = 7916.21 ms {'query': 'What are the approaches to Task Decomposition?', 'result': ' Sure! Based on the context, here\'s my answer to your:\n\nThere are several to task,:\n\n1. LLM-based with simple prompting, such as ""Steps for XYZ"" or ""What are the subgoals for achieving XYZ?""\n2. Task-specific, like ""Write a story outline"" for writing a novel.\n3. Human inputs to guide the process.\n\nThese can be used to decompose complex tasks into smaller, more manageable subtasks, which can help improve the and effectiveness of task. However, long-term and task can being due to the need to plan over a lengthy history and explore the space., LLMs may to adjust plans when faced with errors, making them less robust to human learners who can learn from trial and error.'}PreviousImprove document indexing with HyDENextDynamically select from multiple retrieversDocument LoadingModelLLaMA2GPT4AllLLMChainQA ChainRetrievalQA" 99,https://python.langchain.com/docs/use_cases/question_answering/how_to/multi_retrieval_qa_router,"Question AnsweringHow toDynamically select from multiple retrieversDynamically select from multiple retrieversThis notebook demonstrates how to use the RouterChain paradigm to create a chain that dynamically selects which Retrieval system to use. Specifically we show how to use the MultiRetrievalQAChain to create a question-answering chain that selects the retrieval QA chain which is most relevant for a given question, and then answers the question using it.from langchain.chains.router import MultiRetrievalQAChainfrom langchain.llms import OpenAIfrom langchain.embeddings import OpenAIEmbeddingsfrom langchain.document_loaders import TextLoaderfrom langchain.vectorstores import FAISSsou_docs = TextLoader('../../state_of_the_union.txt').load_and_split()sou_retriever = FAISS.from_documents(sou_docs, OpenAIEmbeddings()).as_retriever()pg_docs = TextLoader('../../paul_graham_essay.txt').load_and_split()pg_retriever = FAISS.from_documents(pg_docs, OpenAIEmbeddings()).as_retriever()personal_texts = [ ""I love apple pie"", ""My favorite color is fuchsia"", ""My dream is to become a professional dancer"", ""I broke my arm when I was 12"", ""My parents are from Peru"",]personal_retriever = FAISS.from_texts(personal_texts, OpenAIEmbeddings()).as_retriever()retriever_infos = [ { ""name"": ""state of the union"", ""description"": ""Good for answering questions about the 2023 State of the Union address"", ""retriever"": sou_retriever }, { ""name"": ""pg essay"", ""description"": ""Good for answering questions about Paul Graham's essay on his career"", ""retriever"": pg_retriever }, { ""name"": ""personal"", ""description"": ""Good for answering questions about me"", ""retriever"": personal_retriever }]chain = MultiRetrievalQAChain.from_retrievers(OpenAI(), retriever_infos, verbose=True)print(chain.run(""What did the president say about the economy?"")) > Entering new MultiRetrievalQAChain chain... state of the union: {'query': 'What did the president say about the economy in the 2023 State of the Union address?'} > Finished chain. The president said that the economy was stronger than it had been a year prior, and that the American Rescue Plan helped create record job growth and fuel economic relief for millions of Americans. He also proposed a plan to fight inflation and lower costs for families, including cutting the cost of prescription drugs and energy, providing investments and tax credits for energy efficiency, and increasing access to child care and Pre-K.print(chain.run(""What is something Paul Graham regrets about his work?"")) > Entering new MultiRetrievalQAChain chain... pg essay: {'query': 'What is something Paul Graham regrets about his work?'} > Finished chain. Paul Graham regrets that he did not take a vacation after selling his company, instead of immediately starting to paint.print(chain.run(""What is my background?"")) > Entering new MultiRetrievalQAChain chain... personal: {'query': 'What is my background?'} > Finished chain. Your background is Peruvian.print(chain.run(""What year was the Internet created in?"")) > Entering new MultiRetrievalQAChain chain... None: {'query': 'What year was the Internet created in?'} > Finished chain. The Internet was created in 1969 through a project called ARPANET, which was funded by the United States Department of Defense. However, the World Wide Web, which is often confused with the Internet, was created in 1989 by British computer scientist Tim Berners-Lee.PreviousUse local LLMsNextMultiple Retrieval Sources" 100,https://python.langchain.com/docs/use_cases/question_answering/how_to/multiple_retrieval,"Question AnsweringHow toMultiple Retrieval SourcesOn this pageMultiple Retrieval SourcesOften times you may want to do retrieval over multiple sources. These can be different vectorstores (where one contains information about topic X and the other contains info about topic Y). They could also be completely different databases altogether!A key part is is doing as much of the retrieval in parrelel as possible. This will keep the latency as low as possible. Luckily, LangChain Expression Language supports parrellism out of the box.Let's take a look where we do retrieval over a SQL database and a vectorstore.from langchain.chat_models import ChatOpenAISet up SQL query​from langchain.utilities import SQLDatabasefrom langchain.chains import create_sql_query_chaindb = SQLDatabase.from_uri(""sqlite:///../../../../../notebooks/Chinook.db"")query_chain = create_sql_query_chain(ChatOpenAI(temperature=0), db)Set up vectorstore​from langchain.indexes import VectorstoreIndexCreatorfrom langchain.schema.document import Documentindex_creator = VectorstoreIndexCreator()index = index_creator.from_documents([Document(page_content=""Foo"")])retriever = index.vectorstore.as_retriever()Combine​from langchain.prompts import ChatPromptTemplatesystem_message = """"""Use the information from the below two sources to answer any questions.Source 1: a SQL database about employee data{source1}Source 2: a text database of random information{source2}""""""prompt = ChatPromptTemplate.from_messages([(""system"", system_message), (""human"", ""{question}"")])full_chain = { ""source1"": {""question"": lambda x: x[""question""]} | query_chain | db.run, ""source2"": (lambda x: x['question']) | retriever, ""question"": lambda x: x['question'],} | prompt | ChatOpenAI()response = full_chain.invoke({""question"":""How many Employees are there""})print(response) Number of requested results 4 is greater than number of elements in index 1, updating n_results = 1 content='There are 8 employees.' additional_kwargs={} example=FalsePreviousDynamically select from multiple retrieversNextCite sourcesSet up SQL querySet up vectorstoreCombine" 101,https://python.langchain.com/docs/use_cases/question_answering/how_to/qa_citations,"Question AnsweringHow toCite sourcesCite sourcesThis notebook shows how to use OpenAI functions ability to extract citations from text.from langchain.chains import create_citation_fuzzy_match_chainfrom langchain.chat_models import ChatOpenAI /Users/harrisonchase/.pyenv/versions/3.9.1/envs/langchain/lib/python3.9/site-packages/deeplake/util/check_latest_version.py:32: UserWarning: A newer version of deeplake (3.6.4) is available. It's recommended that you update to the latest version using `pip install -U deeplake`. warnings.warn(question = ""What did the author do during college?""context = """"""My name is Jason Liu, and I grew up in Toronto Canada but I was born in China.I went to an arts highschool but in university I studied Computational Mathematics and physics. As part of coop I worked at many companies including Stitchfix, Facebook.I also started the Data Science club at the University of Waterloo and I was the president of the club for 2 years.""""""llm = ChatOpenAI(temperature=0, model=""gpt-3.5-turbo-0613"")chain = create_citation_fuzzy_match_chain(llm)result = chain.run(question=question, context=context)print(result) question='What did the author do during college?' answer=[FactWithEvidence(fact='The author studied Computational Mathematics and physics in university.', substring_quote=['in university I studied Computational Mathematics and physics']), FactWithEvidence(fact='The author started the Data Science club at the University of Waterloo and was the president of the club for 2 years.', substring_quote=['started the Data Science club at the University of Waterloo', 'president of the club for 2 years'])]def highlight(text, span): return ( ""..."" + text[span[0] - 20 : span[0]] + ""*"" + ""\033[91m"" + text[span[0] : span[1]] + ""\033[0m"" + ""*"" + text[span[1] : span[1] + 20] + ""..."" )for fact in result.answer: print(""Statement:"", fact.fact) for span in fact.get_spans(context): print(""Citation:"", highlight(context, span)) print() Statement: The author studied Computational Mathematics and physics in university. Citation: ...arts highschool but *in university I studied Computational Mathematics and physics*. As part of coop I... Statement: The author started the Data Science club at the University of Waterloo and was the president of the club for 2 years. Citation: ...x, Facebook. I also *started the Data Science club at the University of Waterloo* and I was the presi... Citation: ...erloo and I was the *president of the club for 2 years*. ... PreviousMultiple Retrieval SourcesNextQA over in-memory documents" 102,https://python.langchain.com/docs/use_cases/question_answering/how_to/question_answering,"Question AnsweringHow toQA over in-memory documentsOn this pageQA over in-memory documentsHere we walk through how to use LangChain for question answering over a list of documents. Under the hood we'll be using our Document chains.Prepare Data​First we prepare the data. For this example we do similarity search over a vector database, but these documents could be fetched in any manner (the point of this notebook to highlight what to do AFTER you fetch the documents).from langchain.embeddings.openai import OpenAIEmbeddingsfrom langchain.text_splitter import CharacterTextSplitterfrom langchain.vectorstores import Chromafrom langchain.docstore.document import Documentfrom langchain.prompts import PromptTemplatefrom langchain.indexes.vectorstore import VectorstoreIndexCreatorwith open(""../../state_of_the_union.txt"") as f: state_of_the_union = f.read()text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)texts = text_splitter.split_text(state_of_the_union)embeddings = OpenAIEmbeddings()docsearch = Chroma.from_texts(texts, embeddings, metadatas=[{""source"": str(i)} for i in range(len(texts))]).as_retriever() Running Chroma using direct local API. Using DuckDB in-memory for database. Data will be transient.query = ""What did the president say about Justice Breyer""docs = docsearch.get_relevant_documents(query)from langchain.chains.question_answering import load_qa_chainfrom langchain.llms import OpenAIQuickstart​If you just want to get started as quickly as possible, this is the recommended way to do it:chain = load_qa_chain(OpenAI(temperature=0), chain_type=""stuff"")query = ""What did the president say about Justice Breyer""chain.run(input_documents=docs, question=query) ' The president said that Justice Breyer has dedicated his life to serve the country and thanked him for his service.'If you want more control and understanding over what is happening, please see the information below.The stuff Chain​This sections shows results of using the stuff Chain to do question answering.chain = load_qa_chain(OpenAI(temperature=0), chain_type=""stuff"")query = ""What did the president say about Justice Breyer""chain({""input_documents"": docs, ""question"": query}, return_only_outputs=True) {'output_text': ' The president said that Justice Breyer has dedicated his life to serve the country and thanked him for his service.'}Custom PromptsYou can also use your own prompts with this chain. In this example, we will respond in Italian.prompt_template = """"""Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer.{context}Question: {question}Answer in Italian:""""""PROMPT = PromptTemplate( template=prompt_template, input_variables=[""context"", ""question""])chain = load_qa_chain(OpenAI(temperature=0), chain_type=""stuff"", prompt=PROMPT)chain({""input_documents"": docs, ""question"": query}, return_only_outputs=True) {'output_text': ' Il presidente ha detto che Justice Breyer ha dedicato la sua vita a servire questo paese e ha ricevuto una vasta gamma di supporto.'}The map_reduce Chain​This sections shows results of using the map_reduce Chain to do question answering.chain = load_qa_chain(OpenAI(temperature=0), chain_type=""map_reduce"")query = ""What did the president say about Justice Breyer""chain({""input_documents"": docs, ""question"": query}, return_only_outputs=True) {'output_text': ' The president said that Justice Breyer is an Army veteran, Constitutional scholar, and retiring Justice of the United States Supreme Court, and thanked him for his service.'}Intermediate StepsWe can also return the intermediate steps for map_reduce chains, should we want to inspect them. This is done with the return_map_steps variable.chain = load_qa_chain(OpenAI(temperature=0), chain_type=""map_reduce"", return_map_steps=True)chain({""input_documents"": docs, ""question"": query}, return_only_outputs=True) {'intermediate_steps': [' ""Tonight, I’d like to honor someone who has dedicated his life to serve this country: Justice Stephen Breyer—an Army veteran, Constitutional scholar, and retiring Justice of the United States Supreme Court. Justice Breyer, thank you for your service.""', ' A former top litigator in private practice. A former federal public defender. And from a family of public school educators and police officers. A consensus builder. Since she’s been nominated, she’s received a broad range of support—from the Fraternal Order of Police to former judges appointed by Democrats and Republicans.', ' None', ' None'], 'output_text': ' The president said that Justice Breyer is an Army veteran, Constitutional scholar, and retiring Justice of the United States Supreme Court, and thanked him for his service.'}Custom PromptsYou can also use your own prompts with this chain. In this example, we will respond in Italian.question_prompt_template = """"""Use the following portion of a long document to see if any of the text is relevant to answer the question. Return any relevant text translated into italian.{context}Question: {question}Relevant text, if any, in Italian:""""""QUESTION_PROMPT = PromptTemplate( template=question_prompt_template, input_variables=[""context"", ""question""])combine_prompt_template = """"""Given the following extracted parts of a long document and a question, create a final answer italian. If you don't know the answer, just say that you don't know. Don't try to make up an answer.QUESTION: {question}========={summaries}=========Answer in Italian:""""""COMBINE_PROMPT = PromptTemplate( template=combine_prompt_template, input_variables=[""summaries"", ""question""])chain = load_qa_chain(OpenAI(temperature=0), chain_type=""map_reduce"", return_map_steps=True, question_prompt=QUESTION_PROMPT, combine_prompt=COMBINE_PROMPT)chain({""input_documents"": docs, ""question"": query}, return_only_outputs=True) {'intermediate_steps': [""\nStasera vorrei onorare qualcuno che ha dedicato la sua vita a servire questo paese: il giustizia Stephen Breyer - un veterano dell'esercito, uno studioso costituzionale e un giustizia in uscita della Corte Suprema degli Stati Uniti. Giustizia Breyer, grazie per il tuo servizio."", '\nNessun testo pertinente.', ' Non ha detto nulla riguardo a Justice Breyer.', "" Non c'è testo pertinente.""], 'output_text': ' Non ha detto nulla riguardo a Justice Breyer.'}Batch SizeWhen using the map_reduce chain, one thing to keep in mind is the batch size you are using during the map step. If this is too high, it could cause rate limiting errors. You can control this by setting the batch size on the LLM used. Note that this only applies for LLMs with this parameter. Below is an example of doing so:llm = OpenAI(batch_size=5, temperature=0)The refine Chain​This sections shows results of using the refine Chain to do question answering.chain = load_qa_chain(OpenAI(temperature=0), chain_type=""refine"")query = ""What did the president say about Justice Breyer""chain({""input_documents"": docs, ""question"": query}, return_only_outputs=True) {'output_text': '\n\nThe president said that he wanted to honor Justice Breyer for his dedication to serving the country, his legacy of excellence, and his commitment to advancing liberty and justice, as well as for his support of the Equality Act and his commitment to protecting the rights of LGBTQ+ Americans. He also praised Justice Breyer for his role in helping to pass the Bipartisan Infrastructure Law, which he said would be the most sweeping investment to rebuild America in history and would help the country compete for the jobs of the 21st Century.'}Intermediate StepsWe can also return the intermediate steps for refine chains, should we want to inspect them. This is done with the return_refine_steps variable.chain = load_qa_chain(OpenAI(temperature=0), chain_type=""refine"", return_refine_steps=True)chain({""input_documents"": docs, ""question"": query}, return_only_outputs=True) {'intermediate_steps': ['\nThe president said that he wanted to honor Justice Breyer for his dedication to serving the country and his legacy of excellence.', '\nThe president said that he wanted to honor Justice Breyer for his dedication to serving the country, his legacy of excellence, and his commitment to advancing liberty and justice.', '\n\nThe president said that he wanted to honor Justice Breyer for his dedication to serving the country, his legacy of excellence, and his commitment to advancing liberty and justice, as well as for his support of the Equality Act and his commitment to protecting the rights of LGBTQ+ Americans.', '\n\nThe president said that he wanted to honor Justice Breyer for his dedication to serving the country, his legacy of excellence, and his commitment to advancing liberty and justice, as well as for his support of the Equality Act and his commitment to protecting the rights of LGBTQ+ Americans. He also praised Justice Breyer for his role in helping to pass the Bipartisan Infrastructure Law, which is the most sweeping investment to rebuild America in history.'], 'output_text': '\n\nThe president said that he wanted to honor Justice Breyer for his dedication to serving the country, his legacy of excellence, and his commitment to advancing liberty and justice, as well as for his support of the Equality Act and his commitment to protecting the rights of LGBTQ+ Americans. He also praised Justice Breyer for his role in helping to pass the Bipartisan Infrastructure Law, which is the most sweeping investment to rebuild America in history.'}Custom PromptsYou can also use your own prompts with this chain. In this example, we will respond in Italian.refine_prompt_template = ( ""The original question is as follows: {question}\n"" ""We have provided an existing answer: {existing_answer}\n"" ""We have the opportunity to refine the existing answer"" ""(only if needed) with some more context below.\n"" ""------------\n"" ""{context_str}\n"" ""------------\n"" ""Given the new context, refine the original answer to better "" ""answer the question. "" ""If the context isn't useful, return the original answer. Reply in Italian."")refine_prompt = PromptTemplate( input_variables=[""question"", ""existing_answer"", ""context_str""], template=refine_prompt_template,)initial_qa_template = ( ""Context information is below. \n"" ""---------------------\n"" ""{context_str}"" ""\n---------------------\n"" ""Given the context information and not prior knowledge, "" ""answer the question: {question}\nYour answer should be in Italian.\n"")initial_qa_prompt = PromptTemplate( input_variables=[""context_str"", ""question""], template=initial_qa_template)chain = load_qa_chain(OpenAI(temperature=0), chain_type=""refine"", return_refine_steps=True, question_prompt=initial_qa_prompt, refine_prompt=refine_prompt)chain({""input_documents"": docs, ""question"": query}, return_only_outputs=True) {'intermediate_steps': ['\nIl presidente ha detto che Justice Breyer ha dedicato la sua vita al servizio di questo paese e ha reso omaggio al suo servizio.', ""\nIl presidente ha detto che Justice Breyer ha dedicato la sua vita al servizio di questo paese, ha reso omaggio al suo servizio e ha sostenuto la nomina di una top litigatrice in pratica privata, un ex difensore pubblico federale e una famiglia di insegnanti e agenti di polizia delle scuole pubbliche. Ha anche sottolineato l'importanza di avanzare la libertà e la giustizia attraverso la sicurezza delle frontiere e la risoluzione del sistema di immigrazione."", ""\nIl presidente ha detto che Justice Breyer ha dedicato la sua vita al servizio di questo paese, ha reso omaggio al suo servizio e ha sostenuto la nomina di una top litigatrice in pratica privata, un ex difensore pubblico federale e una famiglia di insegnanti e agenti di polizia delle scuole pubbliche. Ha anche sottolineato l'importanza di avanzare la libertà e la giustizia attraverso la sicurezza delle frontiere, la risoluzione del sistema di immigrazione, la protezione degli americani LGBTQ+ e l'approvazione dell'Equality Act. Ha inoltre sottolineato l'importanza di lavorare insieme per sconfiggere l'epidemia di oppiacei."", ""\n\nIl presidente ha detto che Justice Breyer ha dedicato la sua vita al servizio di questo paese, ha reso omaggio al suo servizio e ha sostenuto la nomina di una top litigatrice in pratica privata, un ex difensore pubblico federale e una famiglia di insegnanti e agenti di polizia delle scuole pubbliche. Ha anche sottolineato l'importanza di avanzare la libertà e la giustizia attraverso la sicurezza delle frontiere, la risoluzione del sistema di immigrazione, la protezione degli americani LGBTQ+ e l'approvazione dell'Equality Act. Ha inoltre sottolineato l'importanza di lavorare insieme per sconfiggere l'epidemia di oppiacei e per investire in America, educare gli americani, far crescere la forza lavoro e costruire l'economia dal""], 'output_text': ""\n\nIl presidente ha detto che Justice Breyer ha dedicato la sua vita al servizio di questo paese, ha reso omaggio al suo servizio e ha sostenuto la nomina di una top litigatrice in pratica privata, un ex difensore pubblico federale e una famiglia di insegnanti e agenti di polizia delle scuole pubbliche. Ha anche sottolineato l'importanza di avanzare la libertà e la giustizia attraverso la sicurezza delle frontiere, la risoluzione del sistema di immigrazione, la protezione degli americani LGBTQ+ e l'approvazione dell'Equality Act. Ha inoltre sottolineato l'importanza di lavorare insieme per sconfiggere l'epidemia di oppiacei e per investire in America, educare gli americani, far crescere la forza lavoro e costruire l'economia dal""}The map-rerank Chain​This sections shows results of using the map-rerank Chain to do question answering with sources.chain = load_qa_chain(OpenAI(temperature=0), chain_type=""map_rerank"", return_intermediate_steps=True)query = ""What did the president say about Justice Breyer""results = chain({""input_documents"": docs, ""question"": query}, return_only_outputs=True)results[""output_text""] ' The President thanked Justice Breyer for his service and honored him for dedicating his life to serve the country.'results[""intermediate_steps""] [{'answer': ' The President thanked Justice Breyer for his service and honored him for dedicating his life to serve the country.', 'score': '100'}, {'answer': ' This document does not answer the question', 'score': '0'}, {'answer': ' This document does not answer the question', 'score': '0'}, {'answer': ' This document does not answer the question', 'score': '0'}]Custom PromptsYou can also use your own prompts with this chain. In this example, we will respond in Italian.from langchain.output_parsers import RegexParseroutput_parser = RegexParser( regex=r""(.*?)\nScore: (.*)"", output_keys=[""answer"", ""score""],)prompt_template = """"""Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer.In addition to giving an answer, also return a score of how fully it answered the user's question. This should be in the following format:Question: [question here]Helpful Answer In Italian: [answer here]Score: [score between 0 and 100]Begin!Context:---------{context}---------Question: {question}Helpful Answer In Italian:""""""PROMPT = PromptTemplate( template=prompt_template, input_variables=[""context"", ""question""], output_parser=output_parser,)chain = load_qa_chain(OpenAI(temperature=0), chain_type=""map_rerank"", return_intermediate_steps=True, prompt=PROMPT)query = ""What did the president say about Justice Breyer""chain({""input_documents"": docs, ""question"": query}, return_only_outputs=True) {'intermediate_steps': [{'answer': ' Il presidente ha detto che Justice Breyer ha dedicato la sua vita a servire questo paese.', 'score': '100'}, {'answer': ' Il presidente non ha detto nulla sulla Giustizia Breyer.', 'score': '100'}, {'answer': ' Non so.', 'score': '0'}, {'answer': ' Non so.', 'score': '0'}], 'output_text': ' Il presidente ha detto che Justice Breyer ha dedicato la sua vita a servire questo paese.'}Document QA with sources​We can also perform document QA and return the sources that were used to answer the question. To do this we'll just need to make sure each document has a ""source"" key in the metadata, and we'll use the load_qa_with_sources helper to construct our chain:docsearch = Chroma.from_texts(texts, embeddings, metadatas=[{""source"": str(i)} for i in range(len(texts))])query = ""What did the president say about Justice Breyer""docs = docsearch.similarity_search(query)from langchain.chains.qa_with_sources import load_qa_with_sources_chainchain = load_qa_with_sources_chain(OpenAI(temperature=0), chain_type=""stuff"")query = ""What did the president say about Justice Breyer""chain({""input_documents"": docs, ""question"": query}, return_only_outputs=True) {'output_text': ' The president thanked Justice Breyer for his service.\nSOURCES: 30-pl'}PreviousCite sourcesNextRetrieve from vector stores directlyDocument QA with sources" 103,https://python.langchain.com/docs/use_cases/question_answering/how_to/vector_db_text_generation,"Question AnsweringHow toRetrieve from vector stores directlyOn this pageRetrieve from vector stores directlyThis notebook walks through how to use LangChain for text generation over a vector index. This is useful if we want to generate text that is able to draw from a large body of custom text, for example, generating blog posts that have an understanding of previous blog posts written, or product tutorials that can refer to product documentation.Prepare Data​First, we prepare the data. For this example, we fetch a documentation site that consists of markdown files hosted on Github and split them into small enough Documents.from langchain.llms import OpenAIfrom langchain.docstore.document import Documentimport requestsfrom langchain.embeddings.openai import OpenAIEmbeddingsfrom langchain.vectorstores import Chromafrom langchain.text_splitter import CharacterTextSplitterfrom langchain.prompts import PromptTemplateimport pathlibimport subprocessimport tempfiledef get_github_docs(repo_owner, repo_name): with tempfile.TemporaryDirectory() as d: subprocess.check_call( f""git clone --depth 1 https://github.com/{repo_owner}/{repo_name}.git ."", cwd=d, shell=True, ) git_sha = ( subprocess.check_output(""git rev-parse HEAD"", shell=True, cwd=d) .decode(""utf-8"") .strip() ) repo_path = pathlib.Path(d) markdown_files = list(repo_path.glob(""*/*.md"")) + list( repo_path.glob(""*/*.mdx"") ) for markdown_file in markdown_files: with open(markdown_file, ""r"") as f: relative_path = markdown_file.relative_to(repo_path) github_url = f""https://github.com/{repo_owner}/{repo_name}/blob/{git_sha}/{relative_path}"" yield Document(page_content=f.read(), metadata={""source"": github_url})sources = get_github_docs(""yirenlu92"", ""deno-manual-forked"")source_chunks = []splitter = CharacterTextSplitter(separator="" "", chunk_size=1024, chunk_overlap=0)for source in sources: for chunk in splitter.split_text(source.page_content): source_chunks.append(Document(page_content=chunk, metadata=source.metadata)) Cloning into '.'...Set Up Vector DB​Now that we have the documentation content in chunks, let's put all this information in a vector index for easy retrieval.search_index = Chroma.from_documents(source_chunks, OpenAIEmbeddings())Set Up LLM Chain with Custom Prompt​Next, let's set up a simple LLM chain but give it a custom prompt for blog post generation. Note that the custom prompt is parameterized and takes two inputs: context, which will be the documents fetched from the vector search, and topic, which is given by the user.from langchain.chains import LLMChainprompt_template = """"""Use the context below to write a 400 word blog post about the topic below: Context: {context} Topic: {topic} Blog post:""""""PROMPT = PromptTemplate(template=prompt_template, input_variables=[""context"", ""topic""])llm = OpenAI(temperature=0)chain = LLMChain(llm=llm, prompt=PROMPT)Generate Text​Finally, we write a function to apply our inputs to the chain. The function takes an input parameter topic. We find the documents in the vector index that correspond to that topic, and use them as additional context in our simple LLM chain.def generate_blog_post(topic): docs = search_index.similarity_search(topic, k=4) inputs = [{""context"": doc.page_content, ""topic"": topic} for doc in docs] print(chain.apply(inputs))generate_blog_post(""environment variables"") [{'text': '\n\nEnvironment variables are a great way to store and access sensitive information in your Deno applications. Deno offers built-in support for environment variables with `Deno.env`, and you can also use a `.env` file to store and access environment variables.\n\nUsing `Deno.env` is simple. It has getter and setter methods, so you can easily set and retrieve environment variables. For example, you can set the `FIREBASE_API_KEY` and `FIREBASE_AUTH_DOMAIN` environment variables like this:\n\n```ts\nDeno.env.set(""FIREBASE_API_KEY"", ""examplekey123"");\nDeno.env.set(""FIREBASE_AUTH_DOMAIN"", ""firebasedomain.com"");\n\nconsole.log(Deno.env.get(""FIREBASE_API_KEY"")); // examplekey123\nconsole.log(Deno.env.get(""FIREBASE_AUTH_DOMAIN"")); // firebasedomain.com\n```\n\nYou can also store environment variables in a `.env` file. This is a great'}, {'text': '\n\nEnvironment variables are a powerful tool for managing configuration settings in a program. They allow us to set values that can be used by the program, without having to hard-code them into the code. This makes it easier to change settings without having to modify the code.\n\nIn Deno, environment variables can be set in a few different ways. The most common way is to use the `VAR=value` syntax. This will set the environment variable `VAR` to the value `value`. This can be used to set any number of environment variables before running a command. For example, if we wanted to set the environment variable `VAR` to `hello` before running a Deno command, we could do so like this:\n\n```\nVAR=hello deno run main.ts\n```\n\nThis will set the environment variable `VAR` to `hello` before running the command. We can then access this variable in our code using the `Deno.env.get()` function. For example, if we ran the following command:\n\n```\nVAR=hello && deno eval ""console.log(\'Deno: \' + Deno.env.get(\'VAR'}, {'text': '\n\nEnvironment variables are a powerful tool for developers, allowing them to store and access data without having to hard-code it into their applications. In Deno, you can access environment variables using the `Deno.env.get()` function.\n\nFor example, if you wanted to access the `HOME` environment variable, you could do so like this:\n\n```js\n// env.js\nDeno.env.get(""HOME"");\n```\n\nWhen running this code, you\'ll need to grant the Deno process access to environment variables. This can be done by passing the `--allow-env` flag to the `deno run` command. You can also specify which environment variables you want to grant access to, like this:\n\n```shell\n# Allow access to only the HOME env var\ndeno run --allow-env=HOME env.js\n```\n\nIt\'s important to note that environment variables are case insensitive on Windows, so Deno also matches them case insensitively (on Windows only).\n\nAnother thing to be aware of when using environment variables is subprocess permissions. Subprocesses are powerful and can access system resources regardless of the permissions you granted to the Den'}, {'text': '\n\nEnvironment variables are an important part of any programming language, and Deno is no exception. Deno is a secure JavaScript and TypeScript runtime built on the V8 JavaScript engine, and it recently added support for environment variables. This feature was added in Deno version 1.6.0, and it is now available for use in Deno applications.\n\nEnvironment variables are used to store information that can be used by programs. They are typically used to store configuration information, such as the location of a database or the name of a user. In Deno, environment variables are stored in the `Deno.env` object. This object is similar to the `process.env` object in Node.js, and it allows you to access and set environment variables.\n\nThe `Deno.env` object is a read-only object, meaning that you cannot directly modify the environment variables. Instead, you must use the `Deno.env.set()` function to set environment variables. This function takes two arguments: the name of the environment variable and the value to set it to. For example, if you wanted to set the `FOO` environment variable to `bar`, you would use the following code:\n\n```'}]PreviousQA over in-memory documentsNextStructure answers with OpenAI functionsPrepare DataSet Up Vector DBSet Up LLM Chain with Custom PromptGenerate Text" 104,https://python.langchain.com/docs/use_cases/question_answering/integrations/openai_functions_retrieval_qa,"Question AnsweringIntegration-specificStructure answers with OpenAI functionsOn this pageStructure answers with OpenAI functionsOpenAI functions allows for structuring of response output. This is often useful in question answering when you want to not only get the final answer but also supporting evidence, citations, etc.In this notebook we show how to use an LLM chain which uses OpenAI functions as part of an overall retrieval pipeline.from langchain.chains import RetrievalQAfrom langchain.document_loaders import TextLoaderfrom langchain.embeddings.openai import OpenAIEmbeddingsfrom langchain.text_splitter import CharacterTextSplitterfrom langchain.vectorstores import Chromaloader = TextLoader(""../../state_of_the_union.txt"", encoding=""utf-8"")documents = loader.load()text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)texts = text_splitter.split_documents(documents)for i, text in enumerate(texts): text.metadata[""source""] = f""{i}-pl""embeddings = OpenAIEmbeddings()docsearch = Chroma.from_documents(texts, embeddings)from langchain.chat_models import ChatOpenAIfrom langchain.chains.combine_documents.stuff import StuffDocumentsChainfrom langchain.prompts import PromptTemplatefrom langchain.chains import create_qa_with_sources_chainllm = ChatOpenAI(temperature=0, model=""gpt-3.5-turbo-0613"")qa_chain = create_qa_with_sources_chain(llm)doc_prompt = PromptTemplate( template=""Content: {page_content}\nSource: {source}"", input_variables=[""page_content"", ""source""],)final_qa_chain = StuffDocumentsChain( llm_chain=qa_chain, document_variable_name=""context"", document_prompt=doc_prompt,)retrieval_qa = RetrievalQA( retriever=docsearch.as_retriever(), combine_documents_chain=final_qa_chain)query = ""What did the president say about russia""retrieval_qa.run(query) '{\n ""answer"": ""The President expressed strong condemnation of Russia\'s actions in Ukraine and announced measures to isolate Russia and provide support to Ukraine. He stated that Russia\'s invasion of Ukraine will have long-term consequences for Russia and emphasized the commitment to defend NATO countries. The President also mentioned taking robust action through sanctions and releasing oil reserves to mitigate gas prices. Overall, the President conveyed a message of solidarity with Ukraine and determination to protect American interests."",\n ""sources"": [""0-pl"", ""4-pl"", ""5-pl"", ""6-pl""]\n}'Using Pydantic​If we want to, we can set the chain to return in Pydantic. Note that if downstream chains consume the output of this chain - including memory - they will generally expect it to be in string format, so you should only use this chain when it is the final chain.qa_chain_pydantic = create_qa_with_sources_chain(llm, output_parser=""pydantic"")final_qa_chain_pydantic = StuffDocumentsChain( llm_chain=qa_chain_pydantic, document_variable_name=""context"", document_prompt=doc_prompt,)retrieval_qa_pydantic = RetrievalQA( retriever=docsearch.as_retriever(), combine_documents_chain=final_qa_chain_pydantic)retrieval_qa_pydantic.run(query) AnswerWithSources(answer=""The President expressed strong condemnation of Russia's actions in Ukraine and announced measures to isolate Russia and provide support to Ukraine. He stated that Russia's invasion of Ukraine will have long-term consequences for Russia and emphasized the commitment to defend NATO countries. The President also mentioned taking robust action through sanctions and releasing oil reserves to mitigate gas prices. Overall, the President conveyed a message of solidarity with Ukraine and determination to protect American interests."", sources=['0-pl', '4-pl', '5-pl', '6-pl'])Using in ConversationalRetrievalChain​We can also show what it's like to use this in the ConversationalRetrievalChain. Note that because this chain involves memory, we will NOT use the Pydantic return type.from langchain.chains import ConversationalRetrievalChainfrom langchain.memory import ConversationBufferMemoryfrom langchain.chains import LLMChainmemory = ConversationBufferMemory(memory_key=""chat_history"", return_messages=True)_template = """"""Given the following conversation and a follow up question, rephrase the follow up question to be a standalone question, in its original language.\Make sure to avoid using any unclear pronouns.Chat History:{chat_history}Follow Up Input: {question}Standalone question:""""""CONDENSE_QUESTION_PROMPT = PromptTemplate.from_template(_template)condense_question_chain = LLMChain( llm=llm, prompt=CONDENSE_QUESTION_PROMPT,)qa = ConversationalRetrievalChain( question_generator=condense_question_chain, retriever=docsearch.as_retriever(), memory=memory, combine_docs_chain=final_qa_chain,)query = ""What did the president say about Ketanji Brown Jackson""result = qa({""question"": query})result {'question': 'What did the president say about Ketanji Brown Jackson', 'chat_history': [HumanMessage(content='What did the president say about Ketanji Brown Jackson', additional_kwargs={}, example=False), AIMessage(content='{\n ""answer"": ""The President nominated Ketanji Brown Jackson as a Circuit Court of Appeals Judge and praised her as one of the nation\'s top legal minds who will continue Justice Breyer\'s legacy of excellence."",\n ""sources"": [""31-pl""]\n}', additional_kwargs={}, example=False)], 'answer': '{\n ""answer"": ""The President nominated Ketanji Brown Jackson as a Circuit Court of Appeals Judge and praised her as one of the nation\'s top legal minds who will continue Justice Breyer\'s legacy of excellence."",\n ""sources"": [""31-pl""]\n}'}query = ""what did he say about her predecessor?""result = qa({""question"": query})result {'question': 'what did he say about her predecessor?', 'chat_history': [HumanMessage(content='What did the president say about Ketanji Brown Jackson', additional_kwargs={}, example=False), AIMessage(content='{\n ""answer"": ""The President nominated Ketanji Brown Jackson as a Circuit Court of Appeals Judge and praised her as one of the nation\'s top legal minds who will continue Justice Breyer\'s legacy of excellence."",\n ""sources"": [""31-pl""]\n}', additional_kwargs={}, example=False), HumanMessage(content='what did he say about her predecessor?', additional_kwargs={}, example=False), AIMessage(content='{\n ""answer"": ""The President honored Justice Stephen Breyer for his service as an Army veteran, Constitutional scholar, and retiring Justice of the United States Supreme Court."",\n ""sources"": [""31-pl""]\n}', additional_kwargs={}, example=False)], 'answer': '{\n ""answer"": ""The President honored Justice Stephen Breyer for his service as an Army veteran, Constitutional scholar, and retiring Justice of the United States Supreme Court."",\n ""sources"": [""31-pl""]\n}'}Using your own output schema​We can change the outputs of our chain by passing in our own schema. The values and descriptions of this schema will inform the function we pass to the OpenAI API, meaning it won't just affect how we parse outputs but will also change the OpenAI output itself. For example we can add a countries_referenced parameter to our schema and describe what we want this parameter to mean, and that'll cause the OpenAI output to include a description of a speaker in the response.In addition to the previous example, we can also add a custom prompt to the chain. This will allow you to add additional context to the response, which can be useful for question answering.from typing import Listfrom pydantic import BaseModel, Fieldfrom langchain.chains.openai_functions import create_qa_with_structure_chainfrom langchain.prompts.chat import ChatPromptTemplate, HumanMessagePromptTemplatefrom langchain.schema import SystemMessage, HumanMessageclass CustomResponseSchema(BaseModel): """"""An answer to the question being asked, with sources."""""" answer: str = Field(..., description=""Answer to the question that was asked"") countries_referenced: List[str] = Field( ..., description=""All of the countries mentioned in the sources"" ) sources: List[str] = Field( ..., description=""List of sources used to answer the question"" )prompt_messages = [ SystemMessage( content=( ""You are a world class algorithm to answer "" ""questions in a specific format."" ) ), HumanMessage(content=""Answer question using the following context""), HumanMessagePromptTemplate.from_template(""{context}""), HumanMessagePromptTemplate.from_template(""Question: {question}""), HumanMessage( content=""Tips: Make sure to answer in the correct format. Return all of the countries mentioned in the sources in uppercase characters."" ),]chain_prompt = ChatPromptTemplate(messages=prompt_messages)qa_chain_pydantic = create_qa_with_structure_chain( llm, CustomResponseSchema, output_parser=""pydantic"", prompt=chain_prompt)final_qa_chain_pydantic = StuffDocumentsChain( llm_chain=qa_chain_pydantic, document_variable_name=""context"", document_prompt=doc_prompt,)retrieval_qa_pydantic = RetrievalQA( retriever=docsearch.as_retriever(), combine_documents_chain=final_qa_chain_pydantic)query = ""What did he say about russia""retrieval_qa_pydantic.run(query) CustomResponseSchema(answer=""He announced that American airspace will be closed off to all Russian flights, further isolating Russia and adding an additional squeeze on their economy. The Ruble has lost 30% of its value and the Russian stock market has lost 40% of its value. He also mentioned that Putin alone is to blame for Russia's reeling economy. The United States and its allies are providing support to Ukraine in their fight for freedom, including military, economic, and humanitarian assistance. The United States is giving more than $1 billion in direct assistance to Ukraine. He made it clear that American forces are not engaged and will not engage in conflict with Russian forces in Ukraine, but they are deployed to defend NATO allies in case Putin decides to keep moving west. He also mentioned that Putin's attack on Ukraine was premeditated and unprovoked, and that the West and NATO responded by building a coalition of freedom-loving nations to confront Putin. The free world is holding Putin accountable through powerful economic sanctions, cutting off Russia's largest banks from the international financial system, and preventing Russia's central bank from defending the Russian Ruble. The U.S. Department of Justice is also assembling a task force to go after the crimes of Russian oligarchs."", countries_referenced=['AMERICA', 'RUSSIA', 'UKRAINE'], sources=['4-pl', '5-pl', '2-pl', '3-pl'])PreviousRetrieve from vector stores directlyNextQA using Activeloop's DeepLakeUsing PydanticUsing in ConversationalRetrievalChainUsing your own output schema" 105,https://python.langchain.com/docs/use_cases/question_answering/integrations/semantic-search-over-chat,"Question AnsweringIntegration-specificQA using Activeloop's DeepLakeOn this pageQA using Activeloop's DeepLakeIn this tutorial, we are going to use Langchain + Activeloop's Deep Lake with GPT4 to semantically search and ask questions over a group chat.View a working demo here1. Install required packages​python3 -m pip install --upgrade langchain 'deeplake[enterprise]' openai tiktoken2. Add API keys​import osimport getpassfrom langchain.document_loaders import PyPDFLoader, TextLoaderfrom langchain.embeddings.openai import OpenAIEmbeddingsfrom langchain.text_splitter import ( RecursiveCharacterTextSplitter, CharacterTextSplitter,)from langchain.vectorstores import DeepLakefrom langchain.chains import ConversationalRetrievalChain, RetrievalQAfrom langchain.chat_models import ChatOpenAIfrom langchain.llms import OpenAIos.environ[""OPENAI_API_KEY""] = getpass.getpass(""OpenAI API Key:"")activeloop_token = getpass.getpass(""Activeloop Token:"")os.environ[""ACTIVELOOP_TOKEN""] = activeloop_tokenos.environ[""ACTIVELOOP_ORG""] = getpass.getpass(""Activeloop Org:"")org_id = os.environ[""ACTIVELOOP_ORG""]embeddings = OpenAIEmbeddings()dataset_path = ""hub://"" + org_id + ""/data""2. Create sample data​You can generate a sample group chat conversation using ChatGPT with this prompt:Generate a group chat conversation with three friends talking about their day, referencing real places and fictional names. Make it funny and as detailed as possible.I've already generated such a chat in messages.txt. We can keep it simple and use this for our example.3. Ingest chat embeddings​We load the messages in the text file, chunk and upload to ActiveLoop Vector store.with open(""messages.txt"") as f: state_of_the_union = f.read()text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)pages = text_splitter.split_text(state_of_the_union)text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100)texts = text_splitter.create_documents(pages)print(texts)dataset_path = ""hub://"" + org_id + ""/data""embeddings = OpenAIEmbeddings()db = DeepLake.from_documents( texts, embeddings, dataset_path=dataset_path, overwrite=True) [Document(page_content='Participants:\n\nJerry: Loves movies and is a bit of a klutz.\nSamantha: Enthusiastic about food and always trying new restaurants.\nBarry: A nature lover, but always manages to get lost.\nJerry: Hey, guys! You won\'t believe what happened to me at the Times Square AMC theater. I tripped over my own feet and spilled popcorn everywhere! 🍿💥\n\nSamantha: LOL, that\'s so you, Jerry! Was the floor buttery enough for you to ice skate on after that? 😂\n\nBarry: Sounds like a regular Tuesday for you, Jerry. Meanwhile, I tried to find that new hiking trail in Central Park. You know, the one that\'s supposed to be impossible to get lost on? Well, guess what...\n\nJerry: You found a hidden treasure?\n\nBarry: No, I got lost. AGAIN. 🧭🙄\n\nSamantha: Barry, you\'d get lost in your own backyard! But speaking of treasures, I found this new sushi place in Little Tokyo. ""Samantha\'s Sushi Symphony"" it\'s called. Coincidence? I think not!\n\nJerry: Maybe they named it after your ability to eat your body weight in sushi. 🍣', metadata={}), Document(page_content='Barry: How do you even FIND all these places, Samantha?\n\nSamantha: Simple, I don\'t rely on Barry\'s navigation skills. 😉 But seriously, the wasabi there was hotter than Jerry\'s love for Marvel movies!\n\nJerry: Hey, nothing wrong with a little superhero action. By the way, did you guys see the new ""Captain Crunch: Breakfast Avenger"" trailer?\n\nSamantha: Captain Crunch? Are you sure you didn\'t get that from one of your Saturday morning cereal binges?\n\nBarry: Yeah, and did he defeat his arch-enemy, General Mills? 😆\n\nJerry: Ha-ha, very funny. Anyway, that sushi place sounds awesome, Samantha. Next time, let\'s go together, and maybe Barry can guide us... if we want a city-wide tour first.\n\nBarry: As long as we\'re not hiking, I\'ll get us there... eventually. 😅\n\nSamantha: It\'s a date! But Jerry, you\'re banned from carrying any food items.\n\nJerry: Deal! Just promise me no wasabi challenges. I don\'t want to end up like the time I tried Sriracha ice cream.', metadata={}), Document(page_content=""Barry: Wait, what happened with Sriracha ice cream?\n\nJerry: Let's just say it was a hot situation. Literally. 🔥\n\nSamantha: 🤣 I still have the video!\n\nJerry: Samantha, if you value our friendship, that video will never see the light of day.\n\nSamantha: No promises, Jerry. No promises. 🤐😈\n\nBarry: I foresee a fun weekend ahead! 🎉"", metadata={})] Your Deep Lake dataset has been successfully created! \ Dataset(path='hub://adilkhan/data', tensors=['embedding', 'id', 'metadata', 'text']) tensor htype shape dtype compression ------- ------- ------- ------- ------- embedding embedding (3, 1536) float32 None id text (3, 1) str None metadata json (3, 1) str None text text (3, 1) str None Optional: You can also use Deep Lake's Managed Tensor Database as a hosting service and run queries there. In order to do so, it is necessary to specify the runtime parameter as {'tensor_db': True} during the creation of the vector store. This configuration enables the execution of queries on the Managed Tensor Database, rather than on the client side. It should be noted that this functionality is not applicable to datasets stored locally or in-memory. In the event that a vector store has already been created outside of the Managed Tensor Database, it is possible to transfer it to the Managed Tensor Database by following the prescribed steps.# with open(""messages.txt"") as f:# state_of_the_union = f.read()# text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)# pages = text_splitter.split_text(state_of_the_union)# text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100)# texts = text_splitter.create_documents(pages)# print(texts)# dataset_path = ""hub://"" + org + ""/data""# embeddings = OpenAIEmbeddings()# db = DeepLake.from_documents(# texts, embeddings, dataset_path=dataset_path, overwrite=True, runtime={""tensor_db"": True}# )4. Ask questions​Now we can ask a question and get an answer back with a semantic search:db = DeepLake(dataset_path=dataset_path, read_only=True, embedding=embeddings)retriever = db.as_retriever()retriever.search_kwargs[""distance_metric""] = ""cos""retriever.search_kwargs[""k""] = 4qa = RetrievalQA.from_chain_type( llm=OpenAI(), chain_type=""stuff"", retriever=retriever, return_source_documents=False)# What was the restaurant the group was talking about called?query = input(""Enter query:"")# The Hungry Lobsterans = qa({""query"": query})print(ans)PreviousStructure answers with OpenAI functionsNextSQL1. Install required packages2. Add API keys2. Create sample data3. Ingest chat embeddings4. Ask questions" 106,https://python.langchain.com/docs/use_cases/qa_structured/sql,"QA over structured dataSQLOn this pageSQLUse case​Enterprise data is often stored in SQL databases.LLMs make it possible to interact with SQL databases using natural langugae.LangChain offers SQL Chains and Agents to build and run SQL queries based on natural language prompts. These are compatible with any SQL dialect supported by SQLAlchemy (e.g., MySQL, PostgreSQL, Oracle SQL, Databricks, SQLite).They enable use cases such as:Generating queries that will be run based on natural language questionsCreating chatbots that can answer questions based on database dataBuilding custom dashboards based on insights a user wants to analyzeOverview​LangChain provides tools to interact with SQL Databases:Build SQL queries based on natural language user questionsQuery a SQL database using chains for query creation and executionInteract with a SQL database using agents for robust and flexible querying Quickstart​First, get required packages and set environment variables:pip install langchain langchain-experimental openai# Set env var OPENAI_API_KEY or load from a .env file# import dotenv# dotenv.load_dotenv()The below example will use a SQLite connection with Chinook database. Follow installation steps to create Chinook.db in the same directory as this notebook:Save this file to the directory as Chinook_Sqlite.sqlRun sqlite3 Chinook.dbRun .read Chinook_Sqlite.sqlTest SELECT * FROM Artist LIMIT 10;Now, Chinhook.db is in our directory.Let's create a SQLDatabaseChain to create and execute SQL queries.from langchain.utilities import SQLDatabasefrom langchain.llms import OpenAIfrom langchain_experimental.sql import SQLDatabaseChaindb = SQLDatabase.from_uri(""sqlite:///Chinook.db"")llm = OpenAI(temperature=0, verbose=True)db_chain = SQLDatabaseChain.from_llm(llm, db, verbose=True)db_chain.run(""How many employees are there?"") > Entering new SQLDatabaseChain chain... How many employees are there? SQLQuery:SELECT COUNT(*) FROM ""Employee""; SQLResult: [(8,)] Answer:There are 8 employees. > Finished chain. 'There are 8 employees.'Note that this both creates and executes the query. In the following sections, we will cover the 3 different use cases mentioned in the overview.Go deeper​You can load tabular data from other sources other than SQL Databases. For example:Loading a CSV fileLoading a Pandas DataFrame Here you can check full list of Document LoadersCase 1: Text-to-SQL query​from langchain.chat_models import ChatOpenAIfrom langchain.chains import create_sql_query_chainLet's create the chain that will build the SQL Query:chain = create_sql_query_chain(ChatOpenAI(temperature=0), db)response = chain.invoke({""question"":""How many employees are there""})print(response) SELECT COUNT(*) FROM EmployeeAfter building the SQL query based on a user question, we can execute the query:db.run(response) '[(8,)]'As we can see, the SQL Query Builder chain only created the query, and we handled the query execution separately.Go deeper​Looking under the hoodWe can look at the LangSmith trace to unpack this:Some papers have reported good performance when prompting with:A CREATE TABLE description for each table, which include column names, their types, etcFollowed by three example rows in a SELECT statementcreate_sql_query_chain adopts this the best practice (see more in this blog). ImprovementsThe query builder can be improved in several ways, such as (but not limited to):Customizing database description to your specific use caseHardcoding a few examples of questions and their corresponding SQL query in the promptUsing a vector database to include dynamic examples that are relevant to the specific user questionAll these examples involve customizing the chain's prompt. For example, we can include a few examples in our prompt like so:from langchain.prompts import PromptTemplateTEMPLATE = """"""Given an input question, first create a syntactically correct {dialect} query to run, then look at the results of the query and return the answer.Use the following format:Question: ""Question here""SQLQuery: ""SQL Query to run""SQLResult: ""Result of the SQLQuery""Answer: ""Final answer here""Only use the following tables:{table_info}.Some examples of SQL queries that corrsespond to questions are:{few_shot_examples}Question: {input}""""""CUSTOM_PROMPT = PromptTemplate( input_variables=[""input"", ""few_shot_examples"", ""table_info"", ""dialect""], template=TEMPLATE)We can also access this prompt in the LangChain prompt hub.This will work with your LangSmith API key.from langchain import hubCUSTOM_PROMPT = hub.pull(""rlm/text-to-sql"")Case 2: Text-to-SQL query and execution​We can use SQLDatabaseChain from langchain_experimental to create and run SQL queries.from langchain.llms import OpenAIfrom langchain_experimental.sql import SQLDatabaseChainllm = OpenAI(temperature=0, verbose=True)db_chain = SQLDatabaseChain.from_llm(llm, db, verbose=True)db_chain.run(""How many employees are there?"") > Entering new SQLDatabaseChain chain... How many employees are there? SQLQuery:SELECT COUNT(*) FROM ""Employee""; SQLResult: [(8,)] Answer:There are 8 employees. > Finished chain. 'There are 8 employees.'As we can see, we get the same result as the previous case.Here, the chain also handles the query execution and provides a final answer based on the user question and the query result.Be careful while using this approach as it is susceptible to SQL Injection:The chain is executing queries that are created by an LLM, and weren't validatede.g. records may be created, modified or deleted unintentionally_This is why we see the SQLDatabaseChain is inside langchain_experimental.Go deeper​Looking under the hoodWe can use the LangSmith trace to see what is happening under the hood:As discussed above, first we create the query:text: ' SELECT COUNT(*) FROM ""Employee"";'Then, it executes the query and passes the results to an LLM for synthesis.ImprovementsThe performance of the SQLDatabaseChain can be enhanced in several ways:Adding sample rowsSpecifying custom table informationUsing Query Checker self-correct invalid SQL using parameter use_query_checker=TrueCustomizing the LLM Prompt include specific instructions or relevant information, using parameter prompt=CUSTOM_PROMPTGet intermediate steps access the SQL statement as well as the final result using parameter return_intermediate_steps=TrueLimit the number of rows a query will return using parameter top_k=5You might find SQLDatabaseSequentialChain useful for cases in which the number of tables in the database is large.This Sequential Chain handles the process of:Determining which tables to use based on the user questionCalling the normal SQL database chain using only relevant tablesAdding Sample RowsProviding sample data can help the LLM construct correct queries when the data format is not obvious. For example, we can tell LLM that artists are saved with their full names by providing two rows from the Track table.db = SQLDatabase.from_uri( ""sqlite:///Chinook.db"", include_tables=['Track'], # we include only one table to save tokens in the prompt :) sample_rows_in_table_info=2)The sample rows are added to the prompt after each corresponding table's column information.We can use db.table_info and check which sample rows are included:print(db.table_info) CREATE TABLE ""Track"" ( ""TrackId"" INTEGER NOT NULL, ""Name"" NVARCHAR(200) NOT NULL, ""AlbumId"" INTEGER, ""MediaTypeId"" INTEGER NOT NULL, ""GenreId"" INTEGER, ""Composer"" NVARCHAR(220), ""Milliseconds"" INTEGER NOT NULL, ""Bytes"" INTEGER, ""UnitPrice"" NUMERIC(10, 2) NOT NULL, PRIMARY KEY (""TrackId""), FOREIGN KEY(""MediaTypeId"") REFERENCES ""MediaType"" (""MediaTypeId""), FOREIGN KEY(""GenreId"") REFERENCES ""Genre"" (""GenreId""), FOREIGN KEY(""AlbumId"") REFERENCES ""Album"" (""AlbumId"") ) /* 2 rows from Track table: TrackId Name AlbumId MediaTypeId GenreId Composer Milliseconds Bytes UnitPrice 1 For Those About To Rock (We Salute You) 1 1 1 Angus Young, Malcolm Young, Brian Johnson 343719 11170334 0.99 2 Balls to the Wall 2 2 1 None 342562 5510424 0.99 */Case 3: SQL agents​LangChain has an SQL Agent which provides a more flexible way of interacting with SQL Databases than the SQLDatabaseChain.The main advantages of using the SQL Agent are:It can answer questions based on the databases' schema as well as on the databases' content (like describing a specific table)It can recover from errors by running a generated query, catching the traceback and regenerating it correctlyTo initialize the agent, we use create_sql_agent function. This agent contains the SQLDatabaseToolkit which contains tools to: Create and execute queriesCheck query syntaxRetrieve table descriptions... and morefrom langchain.agents import create_sql_agentfrom langchain.agents.agent_toolkits import SQLDatabaseToolkit# from langchain.agents import AgentExecutorfrom langchain.agents.agent_types import AgentTypedb = SQLDatabase.from_uri(""sqlite:///Chinook.db"")llm = OpenAI(temperature=0, verbose=True)agent_executor = create_sql_agent( llm=OpenAI(temperature=0), toolkit=SQLDatabaseToolkit(db=db, llm=OpenAI(temperature=0)), verbose=True, agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION,)Agent task example #1 - Running queries​agent_executor.run( ""List the total sales per country. Which country's customers spent the most?"") > Entering new AgentExecutor chain... Action: sql_db_list_tables Action Input: Observation: Album, Artist, Customer, Employee, Genre, Invoice, InvoiceLine, MediaType, Playlist, PlaylistTrack, Track Thought: I should query the schema of the Invoice and Customer tables. Action: sql_db_schema Action Input: Invoice, Customer Observation: CREATE TABLE ""Customer"" ( ""CustomerId"" INTEGER NOT NULL, ""FirstName"" NVARCHAR(40) NOT NULL, ""LastName"" NVARCHAR(20) NOT NULL, ""Company"" NVARCHAR(80), ""Address"" NVARCHAR(70), ""City"" NVARCHAR(40), ""State"" NVARCHAR(40), ""Country"" NVARCHAR(40), ""PostalCode"" NVARCHAR(10), ""Phone"" NVARCHAR(24), ""Fax"" NVARCHAR(24), ""Email"" NVARCHAR(60) NOT NULL, ""SupportRepId"" INTEGER, PRIMARY KEY (""CustomerId""), FOREIGN KEY(""SupportRepId"") REFERENCES ""Employee"" (""EmployeeId"") ) /* 3 rows from Customer table: CustomerId FirstName LastName Company Address City State Country PostalCode Phone Fax Email SupportRepId 1 Luís Gonçalves Embraer - Empresa Brasileira de Aeronáutica S.A. Av. Brigadeiro Faria Lima, 2170 São José dos Campos SP Brazil 12227-000 +55 (12) 3923-5555 +55 (12) 3923-5566 luisg@embraer.com.br 3 2 Leonie Köhler None Theodor-Heuss-Straße 34 Stuttgart None Germany 70174 +49 0711 2842222 None leonekohler@surfeu.de 5 3 François Tremblay None 1498 rue Bélanger Montréal QC Canada H2G 1A7 +1 (514) 721-4711 None ftremblay@gmail.com 3 */ CREATE TABLE ""Invoice"" ( ""InvoiceId"" INTEGER NOT NULL, ""CustomerId"" INTEGER NOT NULL, ""InvoiceDate"" DATETIME NOT NULL, ""BillingAddress"" NVARCHAR(70), ""BillingCity"" NVARCHAR(40), ""BillingState"" NVARCHAR(40), ""BillingCountry"" NVARCHAR(40), ""BillingPostalCode"" NVARCHAR(10), ""Total"" NUMERIC(10, 2) NOT NULL, PRIMARY KEY (""InvoiceId""), FOREIGN KEY(""CustomerId"") REFERENCES ""Customer"" (""CustomerId"") ) /* 3 rows from Invoice table: InvoiceId CustomerId InvoiceDate BillingAddress BillingCity BillingState BillingCountry BillingPostalCode Total 1 2 2009-01-01 00:00:00 Theodor-Heuss-Straße 34 Stuttgart None Germany 70174 1.98 2 4 2009-01-02 00:00:00 Ullevålsveien 14 Oslo None Norway 0171 3.96 3 8 2009-01-03 00:00:00 Grétrystraat 63 Brussels None Belgium 1000 5.94 */ Thought: I should query the total sales per country. Action: sql_db_query Action Input: SELECT Country, SUM(Total) AS TotalSales FROM Invoice INNER JOIN Customer ON Invoice.CustomerId = Customer.CustomerId GROUP BY Country ORDER BY TotalSales DESC LIMIT 10 Observation: [('USA', 523.0600000000003), ('Canada', 303.9599999999999), ('France', 195.09999999999994), ('Brazil', 190.09999999999997), ('Germany', 156.48), ('United Kingdom', 112.85999999999999), ('Czech Republic', 90.24000000000001), ('Portugal', 77.23999999999998), ('India', 75.25999999999999), ('Chile', 46.62)] Thought: I now know the final answer Final Answer: The country with the highest total sales is the USA, with a total of $523.06. > Finished chain. 'The country with the highest total sales is the USA, with a total of $523.06.'Looking at the LangSmith trace, we can see:The agent is using a ReAct style promptFirst, it will look at the tables: Action: sql_db_list_tables using tool sql_db_list_tablesGiven the tables as an observation, it thinks and then determinates the next action:Observation: Album, Artist, Customer, Employee, Genre, Invoice, InvoiceLine, MediaType, Playlist, PlaylistTrack, TrackThought: I should query the schema of the Invoice and Customer tables.Action: sql_db_schemaAction Input: Invoice, CustomerIt then formulates the query using the schema from tool sql_db_schemaThought: I should query the total sales per country.Action: sql_db_queryAction Input: SELECT Country, SUM(Total) AS TotalSales FROM Invoice INNER JOIN Customer ON Invoice.CustomerId = Customer.CustomerId GROUP BY Country ORDER BY TotalSales DESC LIMIT 10It finally executes the generated query using tool sql_db_queryAgent task example #2 - Describing a Table​agent_executor.run(""Describe the playlisttrack table"") > Entering new AgentExecutor chain... Action: sql_db_list_tables Action Input: Observation: Album, Artist, Customer, Employee, Genre, Invoice, InvoiceLine, MediaType, Playlist, PlaylistTrack, Track Thought: The PlaylistTrack table is the most relevant to the question. Action: sql_db_schema Action Input: PlaylistTrack Observation: CREATE TABLE ""PlaylistTrack"" ( ""PlaylistId"" INTEGER NOT NULL, ""TrackId"" INTEGER NOT NULL, PRIMARY KEY (""PlaylistId"", ""TrackId""), FOREIGN KEY(""TrackId"") REFERENCES ""Track"" (""TrackId""), FOREIGN KEY(""PlaylistId"") REFERENCES ""Playlist"" (""PlaylistId"") ) /* 3 rows from PlaylistTrack table: PlaylistId TrackId 1 3402 1 3389 1 3390 */ Thought: I now know the final answer Final Answer: The PlaylistTrack table contains two columns, PlaylistId and TrackId, which are both integers and form a primary key. It also has two foreign keys, one to the Track table and one to the Playlist table. > Finished chain. 'The PlaylistTrack table contains two columns, PlaylistId and TrackId, which are both integers and form a primary key. It also has two foreign keys, one to the Track table and one to the Playlist table.'Extending the SQL Toolkit​Although the out-of-the-box SQL Toolkit contains the necessary tools to start working on a database, it is often the case that some extra tools may be useful for extending the agent's capabilities. This is particularly useful when trying to use domain specific knowledge in the solution, in order to improve its overall performance.Some examples include:Including dynamic few shot examplesFinding misspellings in proper nouns to use as column filtersWe can create separate tools which tackle these specific use cases and include them as a complement to the standard SQL Toolkit. Let's see how to include these two custom tools.Including dynamic few-shot examples​In order to include dynamic few-shot examples, we need a custom Retriever Tool that handles the vector database in order to retrieve the examples that are semantically similar to the user’s question.Let's start by creating a dictionary with some examples: # few_shots = {'List all artists.': 'SELECT * FROM artists;',# ""Find all albums for the artist 'AC/DC'."": ""SELECT * FROM albums WHERE ArtistId = (SELECT ArtistId FROM artists WHERE Name = 'AC/DC');"",# ""List all tracks in the 'Rock' genre."": ""SELECT * FROM tracks WHERE GenreId = (SELECT GenreId FROM genres WHERE Name = 'Rock');"",# 'Find the total duration of all tracks.': 'SELECT SUM(Milliseconds) FROM tracks;',# 'List all customers from Canada.': ""SELECT * FROM customers WHERE Country = 'Canada';"",# 'How many tracks are there in the album with ID 5?': 'SELECT COUNT(*) FROM tracks WHERE AlbumId = 5;',# 'Find the total number of invoices.': 'SELECT COUNT(*) FROM invoices;',# 'List all tracks that are longer than 5 minutes.': 'SELECT * FROM tracks WHERE Milliseconds > 300000;',# 'Who are the top 5 customers by total purchase?': 'SELECT CustomerId, SUM(Total) AS TotalPurchase FROM invoices GROUP BY CustomerId ORDER BY TotalPurchase DESC LIMIT 5;',# 'Which albums are from the year 2000?': ""SELECT * FROM albums WHERE strftime('%Y', ReleaseDate) = '2000';"",# 'How many employees are there': 'SELECT COUNT(*) FROM ""employee""'# }We can then create a retriever using the list of questions, assigning the target SQL query as metadata:from langchain.embeddings.openai import OpenAIEmbeddingsfrom langchain.vectorstores import FAISSfrom langchain.schema import Documentembeddings = OpenAIEmbeddings()few_shot_docs = [Document(page_content=question, metadata={'sql_query': few_shots[question]}) for question in few_shots.keys()]vector_db = FAISS.from_documents(few_shot_docs, embeddings)retriever = vector_db.as_retriever()Now we can create our own custom tool and append it as a new tool in the create_sql_agent function:from langchain.agents.agent_toolkits import create_retriever_tooltool_description = """"""This tool will help you understand similar examples to adapt them to the user question.Input to this tool should be the user question.""""""retriever_tool = create_retriever_tool( retriever, name='sql_get_similar_examples', description=tool_description )custom_tool_list = [retriever_tool]Now we can create the agent, adjusting the standard SQL Agent suffix to consider our use case. Although the most straightforward way to handle this would be to include it just in the tool description, this is often not enough and we need to specify it in the agent prompt using the suffix argument in the constructor.from langchain.agents import create_sql_agent, AgentTypefrom langchain.agents.agent_toolkits import SQLDatabaseToolkitfrom langchain.utilities import SQLDatabasefrom langchain.chat_models import ChatOpenAIdb = SQLDatabase.from_uri(""sqlite:///Chinook.db"")llm = ChatOpenAI(model_name='gpt-4',temperature=0)toolkit = SQLDatabaseToolkit(db=db, llm=llm)custom_suffix = """"""I should first get the similar examples I know.If the examples are enough to construct the query, I can build it.Otherwise, I can then look at the tables in the database to see what I can query.Then I should query the schema of the most relevant tables""""""agent = create_sql_agent(llm=llm, toolkit=toolkit, verbose=True, agent_type=AgentType.OPENAI_FUNCTIONS, extra_tools=custom_tool_list, suffix=custom_suffix )Let's try it out:agent.run(""How many employees do we have?"") > Entering new AgentExecutor chain... Invoking: `sql_get_similar_examples` with `How many employees do we have?` [Document(page_content='How many employees are there', metadata={'sql_query': 'SELECT COUNT(*) FROM ""employee""'}), Document(page_content='Find the total number of invoices.', metadata={'sql_query': 'SELECT COUNT(*) FROM invoices;'})] Invoking: `sql_db_query_checker` with `SELECT COUNT(*) FROM employee` responded: {content} SELECT COUNT(*) FROM employee Invoking: `sql_db_query` with `SELECT COUNT(*) FROM employee` [(8,)]We have 8 employees. > Finished chain. 'We have 8 employees.'As we can see, the agent first used the sql_get_similar_examples tool in order to retrieve similar examples. As the question was very similar to other few shot examples, the agent didn't need to use any other tool from the standard Toolkit, thus saving time and tokens.Finding and correcting misspellings for proper nouns​In order to filter columns that contain proper nouns such as addresses, song names or artists, we first need to double-check the spelling in order to filter the data correctly. We can achieve this by creating a vector store using all the distinct proper nouns that exist in the database. We can then have the agent query that vector store each time the user includes a proper noun in their question, to find the correct spelling for that word. In this way, the agent can make sure it understands which entity the user is referring to before building the target query.Let's follow a similar approach to the few shots, but without metadata: just embedding the proper nouns and then querying to get the most similar one to the misspelled user question.First we need the unique values for each entity we want, for which we define a function that parses the result into a list of elements:import astimport redef run_query_save_results(db, query): res = db.run(query) res = [el for sub in ast.literal_eval(res) for el in sub if el] res = [re.sub(r'\b\d+\b', '', string).strip() for string in res] return resartists = run_query_save_results(db, ""SELECT Name FROM Artist"")albums = run_query_save_results(db, ""SELECT Title FROM Album"")Now we can proceed with creating the custom retreiver tool and the final agent:from langchain.agents.agent_toolkits import create_retriever_toolfrom langchain.embeddings.openai import OpenAIEmbeddingsfrom langchain.vectorstores import FAISStexts = (artists + albums)embeddings = OpenAIEmbeddings()vector_db = FAISS.from_texts(texts, embeddings)retriever = vector_db.as_retriever()retriever_tool = create_retriever_tool( retriever, name='name_search', description='use to learn how a piece of data is actually written, can be from names, surnames addresses etc' )custom_tool_list = [retriever_tool]from langchain.agents import create_sql_agent, AgentTypefrom langchain.agents.agent_toolkits import SQLDatabaseToolkitfrom langchain.utilities import SQLDatabasefrom langchain.chat_models import ChatOpenAI# db = SQLDatabase.from_uri(""sqlite:///Chinook.db"")llm = ChatOpenAI(model_name='gpt-4', temperature=0)toolkit = SQLDatabaseToolkit(db=db, llm=llm)custom_suffix = """"""If a user asks for me to filter based on proper nouns, I should first check the spelling using the name_search tool.Otherwise, I can then look at the tables in the database to see what I can query.Then I should query the schema of the most relevant tables""""""agent = create_sql_agent(llm=llm, toolkit=toolkit, verbose=True, agent_type=AgentType.OPENAI_FUNCTIONS, extra_tools=custom_tool_list, suffix=custom_suffix )Let's try it out:agent.run(""How many albums does alis in pains have?"") > Entering new AgentExecutor chain... Invoking: `name_search` with `alis in pains` [Document(page_content='House of Pain', metadata={}), Document(page_content='Alice In Chains', metadata={}), Document(page_content='Aisha Duo', metadata={}), Document(page_content='House Of Pain', metadata={})] Invoking: `sql_db_list_tables` with `` responded: {content} Album, Artist, Customer, Employee, Genre, Invoice, InvoiceLine, MediaType, Playlist, PlaylistTrack, Track Invoking: `sql_db_schema` with `Album, Artist` responded: {content} CREATE TABLE ""Album"" ( ""AlbumId"" INTEGER NOT NULL, ""Title"" NVARCHAR(160) NOT NULL, ""ArtistId"" INTEGER NOT NULL, PRIMARY KEY (""AlbumId""), FOREIGN KEY(""ArtistId"") REFERENCES ""Artist"" (""ArtistId"") ) /* 3 rows from Album table: AlbumId Title ArtistId 1 For Those About To Rock We Salute You 1 2 Balls to the Wall 2 3 Restless and Wild 2 */ CREATE TABLE ""Artist"" ( ""ArtistId"" INTEGER NOT NULL, ""Name"" NVARCHAR(120), PRIMARY KEY (""ArtistId"") ) /* 3 rows from Artist table: ArtistId Name 1 AC/DC 2 Accept 3 Aerosmith */ Invoking: `sql_db_query_checker` with `SELECT COUNT(*) FROM Album JOIN Artist ON Album.ArtistId = Artist.ArtistId WHERE Artist.Name = 'Alice In Chains'` responded: {content} SELECT COUNT(*) FROM Album JOIN Artist ON Album.ArtistId = Artist.ArtistId WHERE Artist.Name = 'Alice In Chains' Invoking: `sql_db_query` with `SELECT COUNT(*) FROM Album JOIN Artist ON Album.ArtistId = Artist.ArtistId WHERE Artist.Name = 'Alice In Chains'` [(1,)]Alice In Chains has 1 album in the database. > Finished chain. 'Alice In Chains has 1 album in the database.'As we can see, the agent used the name_search tool in order to check how to correctly query the database for this specific artist.Go deeper​To learn more about the SQL Agent and how it works we refer to the SQL Agent Toolkit documentation.You can also check Agents for other document types:Pandas AgentCSV AgentElastic Search​Going beyond the above use-case, there are integrations with other databases.For example, we can interact with Elasticsearch analytics database. This chain builds search queries via the Elasticsearch DSL API (filters and aggregations).The Elasticsearch client must have permissions for index listing, mapping description and search queries.See here for instructions on how to run Elasticsearch locally.Make sure to install the Elasticsearch Python client before:pip install elasticsearchfrom elasticsearch import Elasticsearchfrom langchain.chat_models import ChatOpenAIfrom langchain.chains.elasticsearch_database import ElasticsearchDatabaseChain# Initialize Elasticsearch python client.# See https://elasticsearch-py.readthedocs.io/en/v8.8.2/api.html#elasticsearch.ElasticsearchELASTIC_SEARCH_SERVER = ""https://elastic:pass@localhost:9200""db = Elasticsearch(ELASTIC_SEARCH_SERVER)Uncomment the next cell to initially populate your db.# customers = [# {""firstname"": ""Jennifer"", ""lastname"": ""Walters""},# {""firstname"": ""Monica"",""lastname"":""Rambeau""},# {""firstname"": ""Carol"",""lastname"":""Danvers""},# {""firstname"": ""Wanda"",""lastname"":""Maximoff""},# {""firstname"": ""Jennifer"",""lastname"":""Takeda""},# ]# for i, customer in enumerate(customers):# db.create(index=""customers"", document=customer, id=i)llm = ChatOpenAI(model_name=""gpt-4"", temperature=0)chain = ElasticsearchDatabaseChain.from_llm(llm=llm, database=db, verbose=True)question = ""What are the first names of all the customers?""chain.run(question)We can customize the prompt.from langchain.chains.elasticsearch_database.prompts import DEFAULT_DSL_TEMPLATEfrom langchain.prompts.prompt import PromptTemplatePROMPT_TEMPLATE = """"""Given an input question, create a syntactically correct Elasticsearch query to run. Unless the user specifies in their question a specific number of examples they wish to obtain, always limit your query to at most {top_k} results. You can order the results by a relevant column to return the most interesting examples in the database.Unless told to do not query for all the columns from a specific index, only ask for a the few relevant columns given the question.Pay attention to use only the column names that you can see in the mapping description. Be careful to not query for columns that do not exist. Also, pay attention to which column is in which index. Return the query as valid json.Use the following format:Question: Question hereESQuery: Elasticsearch Query formatted as json""""""PROMPT = PromptTemplate.from_template( PROMPT_TEMPLATE,)chain = ElasticsearchDatabaseChain.from_llm(llm=llm, database=db, query_prompt=PROMPT)PreviousQA using Activeloop's DeepLakeNextDatabricksUse caseOverviewQuickstartGo deeperCase 1: Text-to-SQL queryGo deeperCase 2: Text-to-SQL query and executionGo deeperCase 3: SQL agentsAgent task example #1 - Running queriesAgent task example #2 - Describing a TableExtending the SQL ToolkitGo deeperElastic Search" 107,https://python.langchain.com/docs/use_cases/qa_structured/integrations/databricks,"QA over structured dataIntegration-specificDatabricksOn this pageDatabricksThis notebook covers how to connect to the Databricks runtimes and Databricks SQL using the SQLDatabase wrapper of LangChain. It is broken into 3 parts: installation and setup, connecting to Databricks, and examples.Installation and Setup​pip install databricks-sql-connectorConnecting to Databricks​You can connect to Databricks runtimes and Databricks SQL using the SQLDatabase.from_databricks() method.Syntax​SQLDatabase.from_databricks( catalog: str, schema: str, host: Optional[str] = None, api_token: Optional[str] = None, warehouse_id: Optional[str] = None, cluster_id: Optional[str] = None, engine_args: Optional[dict] = None, **kwargs: Any)Required Parameters​catalog: The catalog name in the Databricks database.schema: The schema name in the catalog.Optional Parameters​There following parameters are optional. When executing the method in a Databricks notebook, you don't need to provide them in most of the cases.host: The Databricks workspace hostname, excluding 'https://' part. Defaults to 'DATABRICKS_HOST' environment variable or current workspace if in a Databricks notebook.api_token: The Databricks personal access token for accessing the Databricks SQL warehouse or the cluster. Defaults to 'DATABRICKS_TOKEN' environment variable or a temporary one is generated if in a Databricks notebook.warehouse_id: The warehouse ID in the Databricks SQL.cluster_id: The cluster ID in the Databricks Runtime. If running in a Databricks notebook and both 'warehouse_id' and 'cluster_id' are None, it uses the ID of the cluster the notebook is attached to.engine_args: The arguments to be used when connecting Databricks.**kwargs: Additional keyword arguments for the SQLDatabase.from_uri method.Examples​# Connecting to Databricks with SQLDatabase wrapperfrom langchain.utilities import SQLDatabasedb = SQLDatabase.from_databricks(catalog=""samples"", schema=""nyctaxi"")# Creating a OpenAI Chat LLM wrapperfrom langchain.chat_models import ChatOpenAIllm = ChatOpenAI(temperature=0, model_name=""gpt-4"")SQL Chain example​This example demonstrates the use of the SQL Chain for answering a question over a Databricks database.from langchain.utilities import SQLDatabaseChaindb_chain = SQLDatabaseChain.from_llm(llm, db, verbose=True)db_chain.run( ""What is the average duration of taxi rides that start between midnight and 6am?"") > Entering new SQLDatabaseChain chain... What is the average duration of taxi rides that start between midnight and 6am? SQLQuery:SELECT AVG(UNIX_TIMESTAMP(tpep_dropoff_datetime) - UNIX_TIMESTAMP(tpep_pickup_datetime)) as avg_duration FROM trips WHERE HOUR(tpep_pickup_datetime) >= 0 AND HOUR(tpep_pickup_datetime) < 6 SQLResult: [(987.8122786304605,)] Answer:The average duration of taxi rides that start between midnight and 6am is 987.81 seconds. > Finished chain. 'The average duration of taxi rides that start between midnight and 6am is 987.81 seconds.'SQL Database Agent example​This example demonstrates the use of the SQL Database Agent for answering questions over a Databricks database.from langchain.agents import create_sql_agentfrom langchain.agents.agent_toolkits import SQLDatabaseToolkittoolkit = SQLDatabaseToolkit(db=db, llm=llm)agent = create_sql_agent(llm=llm, toolkit=toolkit, verbose=True)agent.run(""What is the longest trip distance and how long did it take?"") > Entering new AgentExecutor chain... Action: list_tables_sql_db Action Input: Observation: trips Thought:I should check the schema of the trips table to see if it has the necessary columns for trip distance and duration. Action: schema_sql_db Action Input: trips Observation: CREATE TABLE trips ( tpep_pickup_datetime TIMESTAMP, tpep_dropoff_datetime TIMESTAMP, trip_distance FLOAT, fare_amount FLOAT, pickup_zip INT, dropoff_zip INT ) USING DELTA /* 3 rows from trips table: tpep_pickup_datetime tpep_dropoff_datetime trip_distance fare_amount pickup_zip dropoff_zip 2016-02-14 16:52:13+00:00 2016-02-14 17:16:04+00:00 4.94 19.0 10282 10171 2016-02-04 18:44:19+00:00 2016-02-04 18:46:00+00:00 0.28 3.5 10110 10110 2016-02-17 17:13:57+00:00 2016-02-17 17:17:55+00:00 0.7 5.0 10103 10023 */ Thought:The trips table has the necessary columns for trip distance and duration. I will write a query to find the longest trip distance and its duration. Action: query_checker_sql_db Action Input: SELECT trip_distance, tpep_dropoff_datetime - tpep_pickup_datetime as duration FROM trips ORDER BY trip_distance DESC LIMIT 1 Observation: SELECT trip_distance, tpep_dropoff_datetime - tpep_pickup_datetime as duration FROM trips ORDER BY trip_distance DESC LIMIT 1 Thought:The query is correct. I will now execute it to find the longest trip distance and its duration. Action: query_sql_db Action Input: SELECT trip_distance, tpep_dropoff_datetime - tpep_pickup_datetime as duration FROM trips ORDER BY trip_distance DESC LIMIT 1 Observation: [(30.6, '0 00:43:31.000000000')] Thought:I now know the final answer. Final Answer: The longest trip distance is 30.6 miles and it took 43 minutes and 31 seconds. > Finished chain. 'The longest trip distance is 30.6 miles and it took 43 minutes and 31 seconds.'PreviousSQLNextElasticsearchInstallation and SetupConnecting to DatabricksSyntaxRequired ParametersOptional ParametersExamplesSQL Chain exampleSQL Database Agent example" 108,https://python.langchain.com/docs/use_cases/qa_structured/integrations/elasticsearch,"QA over structured dataIntegration-specificElasticsearchElasticsearchWe can use LLMs to interact with Elasticsearch analytics databases in natural language.This chain builds search queries via the Elasticsearch DSL API (filters and aggregations).The Elasticsearch client must have permissions for index listing, mapping description and search queries.See here for instructions on how to run Elasticsearch locally.pip install langchain langchain-experimental openai elasticsearch# Set env var OPENAI_API_KEY or load from a .env file# import dotenv# dotenv.load_dotenv()from elasticsearch import Elasticsearchfrom langchain.chat_models import ChatOpenAIfrom langchain.chains.elasticsearch_database import ElasticsearchDatabaseChain# Initialize Elasticsearch python client.# See https://elasticsearch-py.readthedocs.io/en/v8.8.2/api.html#elasticsearch.ElasticsearchELASTIC_SEARCH_SERVER = ""https://elastic:pass@localhost:9200""db = Elasticsearch(ELASTIC_SEARCH_SERVER)Uncomment the next cell to initially populate your db.# customers = [# {""firstname"": ""Jennifer"", ""lastname"": ""Walters""},# {""firstname"": ""Monica"",""lastname"":""Rambeau""},# {""firstname"": ""Carol"",""lastname"":""Danvers""},# {""firstname"": ""Wanda"",""lastname"":""Maximoff""},# {""firstname"": ""Jennifer"",""lastname"":""Takeda""},# ]# for i, customer in enumerate(customers):# db.create(index=""customers"", document=customer, id=i)llm = ChatOpenAI(model_name=""gpt-4"", temperature=0)chain = ElasticsearchDatabaseChain.from_llm(llm=llm, database=db, verbose=True)question = ""What are the first names of all the customers?""chain.run(question)We can customize the prompt.from langchain.chains.elasticsearch_database.prompts import DEFAULT_DSL_TEMPLATEfrom langchain.prompts.prompt import PromptTemplatePROMPT_TEMPLATE = """"""Given an input question, create a syntactically correct Elasticsearch query to run. Unless the user specifies in their question a specific number of examples they wish to obtain, always limit your query to at most {top_k} results. You can order the results by a relevant column to return the most interesting examples in the database.Unless told to do not query for all the columns from a specific index, only ask for a the few relevant columns given the question.Pay attention to use only the column names that you can see in the mapping description. Be careful to not query for columns that do not exist. Also, pay attention to which column is in which index. Return the query as valid json.Use the following format:Question: Question hereESQuery: Elasticsearch Query formatted as json""""""PROMPT = PromptTemplate.from_template( PROMPT_TEMPLATE,)chain = ElasticsearchDatabaseChain.from_llm(llm=llm, database=db, query_prompt=PROMPT)PreviousDatabricksNextVector SQL Retriever with MyScale" 109,https://python.langchain.com/docs/use_cases/qa_structured/integrations/myscale_vector_sql,"QA over structured dataIntegration-specificVector SQL Retriever with MyScaleOn this pageVector SQL Retriever with MyScaleMyScale is an integrated vector database. You can access your database in SQL and also from here, LangChain. MyScale can make a use of various data types and functions for filters. It will boost up your LLM app no matter if you are scaling up your data or expand your system to broader application.pip3 install clickhouse-sqlalchemy InstructorEmbedding sentence_transformers openai langchain-experimentalfrom os import environimport getpassfrom typing import Dict, Anyfrom langchain.llms import OpenAIfrom langchain.utilities import SQLDatabasefrom langchain.chains import LLMChainfrom langchain_experimental.sql.vector_sql import VectorSQLDatabaseChainfrom sqlalchemy import create_engine, Column, MetaDatafrom langchain.prompts import PromptTemplatefrom sqlalchemy import create_engineMYSCALE_HOST = ""msc-1decbcc9.us-east-1.aws.staging.myscale.cloud""MYSCALE_PORT = 443MYSCALE_USER = ""chatdata""MYSCALE_PASSWORD = ""myscale_rocks""OPENAI_API_KEY = getpass.getpass(""OpenAI API Key:"")engine = create_engine( f""clickhouse://{MYSCALE_USER}:{MYSCALE_PASSWORD}@{MYSCALE_HOST}:{MYSCALE_PORT}/default?protocol=https"")metadata = MetaData(bind=engine)environ[""OPENAI_API_KEY""] = OPENAI_API_KEYfrom langchain.embeddings import HuggingFaceInstructEmbeddingsfrom langchain_experimental.sql.vector_sql import VectorSQLOutputParseroutput_parser = VectorSQLOutputParser.from_embeddings( model=HuggingFaceInstructEmbeddings( model_name=""hkunlp/instructor-xl"", model_kwargs={""device"": ""cpu""} ))from langchain.llms import OpenAIfrom langchain.callbacks import StdOutCallbackHandlerfrom langchain.utilities.sql_database import SQLDatabasefrom langchain_experimental.sql.prompt import MYSCALE_PROMPTfrom langchain_experimental.sql.vector_sql import VectorSQLDatabaseChainchain = VectorSQLDatabaseChain( llm_chain=LLMChain( llm=OpenAI(openai_api_key=OPENAI_API_KEY, temperature=0), prompt=MYSCALE_PROMPT, ), top_k=10, return_direct=True, sql_cmd_parser=output_parser, database=SQLDatabase(engine, None, metadata),)import pandas as pdpd.DataFrame( chain.run( ""Please give me 10 papers to ask what is PageRank?"", callbacks=[StdOutCallbackHandler()], ))SQL Database as Retriever​from langchain.chat_models import ChatOpenAIfrom langchain.chains.qa_with_sources.retrieval import RetrievalQAWithSourcesChainfrom langchain_experimental.sql.vector_sql import VectorSQLDatabaseChainfrom langchain_experimental.retrievers.vector_sql_database \ import VectorSQLDatabaseChainRetrieverfrom langchain_experimental.sql.prompt import MYSCALE_PROMPTfrom langchain_experimental.sql.vector_sql import VectorSQLRetrieveAllOutputParseroutput_parser_retrieve_all = VectorSQLRetrieveAllOutputParser.from_embeddings( output_parser.model)chain = VectorSQLDatabaseChain.from_llm( llm=OpenAI(openai_api_key=OPENAI_API_KEY, temperature=0), prompt=MYSCALE_PROMPT, top_k=10, return_direct=True, db=SQLDatabase(engine, None, metadata), sql_cmd_parser=output_parser_retrieve_all, native_format=True,)# You need all those keys to get docsretriever = VectorSQLDatabaseChainRetriever(sql_db_chain=chain, page_content_key=""abstract"")document_with_metadata_prompt = PromptTemplate( input_variables=[""page_content"", ""id"", ""title"", ""authors"", ""pubdate"", ""categories""], template=""Content:\n\tTitle: {title}\n\tAbstract: {page_content}\n\tAuthors: {authors}\n\tDate of Publication: {pubdate}\n\tCategories: {categories}\nSOURCE: {id}"",)chain = RetrievalQAWithSourcesChain.from_chain_type( ChatOpenAI( model_name=""gpt-3.5-turbo-16k"", openai_api_key=OPENAI_API_KEY, temperature=0.6 ), retriever=retriever, chain_type=""stuff"", chain_type_kwargs={ ""document_prompt"": document_with_metadata_prompt, }, return_source_documents=True,)ans = chain(""Please give me 10 papers to ask what is PageRank?"", callbacks=[StdOutCallbackHandler()])print(ans[""answer""])PreviousElasticsearchNextSQL Database ChainSQL Database as Retriever" 110,https://python.langchain.com/docs/use_cases/qa_structured/integrations/sqlite,"QA over structured dataIntegration-specificSQL Database ChainSQL Database ChainThis example demonstrates the use of the SQLDatabaseChain for answering questions over a SQL database.Under the hood, LangChain uses SQLAlchemy to connect to SQL databases. The SQLDatabaseChain can therefore be used with any SQL dialect supported by SQLAlchemy, such as MS SQL, MySQL, MariaDB, PostgreSQL, Oracle SQL, Databricks and SQLite. Please refer to the SQLAlchemy documentation for more information about requirements for connecting to your database. For example, a connection to MySQL requires an appropriate connector such as PyMySQL. A URI for a MySQL connection might look like: mysql+pymysql://user:pass@some_mysql_db_address/db_name.This demonstration uses SQLite and the example Chinook database. To set it up, follow the instructions on https://database.guide/2-sample-databases-sqlite/, placing the .db file in a notebooks folder at the root of this repository.from langchain.llms import OpenAIfrom langchain.utilities import SQLDatabasefrom langchain_experimental.sql import SQLDatabaseChaindb = SQLDatabase.from_uri(""sqlite:///../../../../notebooks/Chinook.db"")llm = OpenAI(temperature=0, verbose=True)NOTE: For data-sensitive projects, you can specify return_direct=True in the SQLDatabaseChain initialization to directly return the output of the SQL query without any additional formatting. This prevents the LLM from seeing any contents within the database. Note, however, the LLM still has access to the database scheme (i.e. dialect, table and key names) by default.db_chain = SQLDatabaseChain.from_llm(llm, db, verbose=True)db_chain.run(""How many employees are there?"") > Entering new SQLDatabaseChain chain... How many employees are there? SQLQuery: /workspace/langchain/langchain/sql_database.py:191: SAWarning: Dialect sqlite+pysqlite does *not* support Decimal objects natively, and SQLAlchemy must convert from floating point - rounding errors and other issues may occur. Please consider storing Decimal numbers as strings or integers on this platform for lossless storage. sample_rows = connection.execute(command) SELECT COUNT(*) FROM ""Employee""; SQLResult: [(8,)] Answer:There are 8 employees. > Finished chain. 'There are 8 employees.'Use Query Checker​Sometimes the Language Model generates invalid SQL with small mistakes that can be self-corrected using the same technique used by the SQL Database Agent to try and fix the SQL using the LLM. You can simply specify this option when creating the chain:db_chain = SQLDatabaseChain.from_llm(llm, db, verbose=True, use_query_checker=True)db_chain.run(""How many albums by Aerosmith?"") > Entering new SQLDatabaseChain chain... How many albums by Aerosmith? SQLQuery:SELECT COUNT(*) FROM Album WHERE ArtistId = 3; SQLResult: [(1,)] Answer:There is 1 album by Aerosmith. > Finished chain. 'There is 1 album by Aerosmith.'Customize Prompt​You can also customize the prompt that is used. Here is an example prompting it to understand that foobar is the same as the Employee tablefrom langchain.prompts.prompt import PromptTemplate_DEFAULT_TEMPLATE = """"""Given an input question, first create a syntactically correct {dialect} query to run, then look at the results of the query and return the answer.Use the following format:Question: ""Question here""SQLQuery: ""SQL Query to run""SQLResult: ""Result of the SQLQuery""Answer: ""Final answer here""Only use the following tables:{table_info}If someone asks for the table foobar, they really mean the employee table.Question: {input}""""""PROMPT = PromptTemplate( input_variables=[""input"", ""table_info"", ""dialect""], template=_DEFAULT_TEMPLATE)db_chain = SQLDatabaseChain.from_llm(llm, db, prompt=PROMPT, verbose=True)db_chain.run(""How many employees are there in the foobar table?"") > Entering new SQLDatabaseChain chain... How many employees are there in the foobar table? SQLQuery:SELECT COUNT(*) FROM Employee; SQLResult: [(8,)] Answer:There are 8 employees in the foobar table. > Finished chain. 'There are 8 employees in the foobar table.'Return Intermediate Steps​You can also return the intermediate steps of the SQLDatabaseChain. This allows you to access the SQL statement that was generated, as well as the result of running that against the SQL Database.db_chain = SQLDatabaseChain.from_llm(llm, db, prompt=PROMPT, verbose=True, use_query_checker=True, return_intermediate_steps=True)result = db_chain(""How many employees are there in the foobar table?"")result[""intermediate_steps""] > Entering new SQLDatabaseChain chain... How many employees are there in the foobar table? SQLQuery:SELECT COUNT(*) FROM Employee; SQLResult: [(8,)] Answer:There are 8 employees in the foobar table. > Finished chain. [{'input': 'How many employees are there in the foobar table?\nSQLQuery:SELECT COUNT(*) FROM Employee;\nSQLResult: [(8,)]\nAnswer:', 'top_k': '5', 'dialect': 'sqlite', 'table_info': '\nCREATE TABLE ""Artist"" (\n\t""ArtistId"" INTEGER NOT NULL, \n\t""Name"" NVARCHAR(120), \n\tPRIMARY KEY (""ArtistId"")\n)\n\n/*\n3 rows from Artist table:\nArtistId\tName\n1\tAC/DC\n2\tAccept\n3\tAerosmith\n*/\n\n\nCREATE TABLE ""Employee"" (\n\t""EmployeeId"" INTEGER NOT NULL, \n\t""LastName"" NVARCHAR(20) NOT NULL, \n\t""FirstName"" NVARCHAR(20) NOT NULL, \n\t""Title"" NVARCHAR(30), \n\t""ReportsTo"" INTEGER, \n\t""BirthDate"" DATETIME, \n\t""HireDate"" DATETIME, \n\t""Address"" NVARCHAR(70), \n\t""City"" NVARCHAR(40), \n\t""State"" NVARCHAR(40), \n\t""Country"" NVARCHAR(40), \n\t""PostalCode"" NVARCHAR(10), \n\t""Phone"" NVARCHAR(24), \n\t""Fax"" NVARCHAR(24), \n\t""Email"" NVARCHAR(60), \n\tPRIMARY KEY (""EmployeeId""), \n\tFOREIGN KEY(""ReportsTo"") REFERENCES ""Employee"" (""EmployeeId"")\n)\n\n/*\n3 rows from Employee table:\nEmployeeId\tLastName\tFirstName\tTitle\tReportsTo\tBirthDate\tHireDate\tAddress\tCity\tState\tCountry\tPostalCode\tPhone\tFax\tEmail\n1\tAdams\tAndrew\tGeneral Manager\tNone\t1962-02-18 00:00:00\t2002-08-14 00:00:00\t11120 Jasper Ave NW\tEdmonton\tAB\tCanada\tT5K 2N1\t+1 (780) 428-9482\t+1 (780) 428-3457\tandrew@chinookcorp.com\n2\tEdwards\tNancy\tSales Manager\t1\t1958-12-08 00:00:00\t2002-05-01 00:00:00\t825 8 Ave SW\tCalgary\tAB\tCanada\tT2P 2T3\t+1 (403) 262-3443\t+1 (403) 262-3322\tnancy@chinookcorp.com\n3\tPeacock\tJane\tSales Support Agent\t2\t1973-08-29 00:00:00\t2002-04-01 00:00:00\t1111 6 Ave SW\tCalgary\tAB\tCanada\tT2P 5M5\t+1 (403) 262-3443\t+1 (403) 262-6712\tjane@chinookcorp.com\n*/\n\n\nCREATE TABLE ""Genre"" (\n\t""GenreId"" INTEGER NOT NULL, \n\t""Name"" NVARCHAR(120), \n\tPRIMARY KEY (""GenreId"")\n)\n\n/*\n3 rows from Genre table:\nGenreId\tName\n1\tRock\n2\tJazz\n3\tMetal\n*/\n\n\nCREATE TABLE ""MediaType"" (\n\t""MediaTypeId"" INTEGER NOT NULL, \n\t""Name"" NVARCHAR(120), \n\tPRIMARY KEY (""MediaTypeId"")\n)\n\n/*\n3 rows from MediaType table:\nMediaTypeId\tName\n1\tMPEG audio file\n2\tProtected AAC audio file\n3\tProtected MPEG-4 video file\n*/\n\n\nCREATE TABLE ""Playlist"" (\n\t""PlaylistId"" INTEGER NOT NULL, \n\t""Name"" NVARCHAR(120), \n\tPRIMARY KEY (""PlaylistId"")\n)\n\n/*\n3 rows from Playlist table:\nPlaylistId\tName\n1\tMusic\n2\tMovies\n3\tTV Shows\n*/\n\n\nCREATE TABLE ""Album"" (\n\t""AlbumId"" INTEGER NOT NULL, \n\t""Title"" NVARCHAR(160) NOT NULL, \n\t""ArtistId"" INTEGER NOT NULL, \n\tPRIMARY KEY (""AlbumId""), \n\tFOREIGN KEY(""ArtistId"") REFERENCES ""Artist"" (""ArtistId"")\n)\n\n/*\n3 rows from Album table:\nAlbumId\tTitle\tArtistId\n1\tFor Those About To Rock We Salute You\t1\n2\tBalls to the Wall\t2\n3\tRestless and Wild\t2\n*/\n\n\nCREATE TABLE ""Customer"" (\n\t""CustomerId"" INTEGER NOT NULL, \n\t""FirstName"" NVARCHAR(40) NOT NULL, \n\t""LastName"" NVARCHAR(20) NOT NULL, \n\t""Company"" NVARCHAR(80), \n\t""Address"" NVARCHAR(70), \n\t""City"" NVARCHAR(40), \n\t""State"" NVARCHAR(40), \n\t""Country"" NVARCHAR(40), \n\t""PostalCode"" NVARCHAR(10), \n\t""Phone"" NVARCHAR(24), \n\t""Fax"" NVARCHAR(24), \n\t""Email"" NVARCHAR(60) NOT NULL, \n\t""SupportRepId"" INTEGER, \n\tPRIMARY KEY (""CustomerId""), \n\tFOREIGN KEY(""SupportRepId"") REFERENCES ""Employee"" (""EmployeeId"")\n)\n\n/*\n3 rows from Customer table:\nCustomerId\tFirstName\tLastName\tCompany\tAddress\tCity\tState\tCountry\tPostalCode\tPhone\tFax\tEmail\tSupportRepId\n1\tLuís\tGonçalves\tEmbraer - Empresa Brasileira de Aeronáutica S.A.\tAv. Brigadeiro Faria Lima, 2170\tSão José dos Campos\tSP\tBrazil\t12227-000\t+55 (12) 3923-5555\t+55 (12) 3923-5566\tluisg@embraer.com.br\t3\n2\tLeonie\tKöhler\tNone\tTheodor-Heuss-Straße 34\tStuttgart\tNone\tGermany\t70174\t+49 0711 2842222\tNone\tleonekohler@surfeu.de\t5\n3\tFrançois\tTremblay\tNone\t1498 rue Bélanger\tMontréal\tQC\tCanada\tH2G 1A7\t+1 (514) 721-4711\tNone\tftremblay@gmail.com\t3\n*/\n\n\nCREATE TABLE ""Invoice"" (\n\t""InvoiceId"" INTEGER NOT NULL, \n\t""CustomerId"" INTEGER NOT NULL, \n\t""InvoiceDate"" DATETIME NOT NULL, \n\t""BillingAddress"" NVARCHAR(70), \n\t""BillingCity"" NVARCHAR(40), \n\t""BillingState"" NVARCHAR(40), \n\t""BillingCountry"" NVARCHAR(40), \n\t""BillingPostalCode"" NVARCHAR(10), \n\t""Total"" NUMERIC(10, 2) NOT NULL, \n\tPRIMARY KEY (""InvoiceId""), \n\tFOREIGN KEY(""CustomerId"") REFERENCES ""Customer"" (""CustomerId"")\n)\n\n/*\n3 rows from Invoice table:\nInvoiceId\tCustomerId\tInvoiceDate\tBillingAddress\tBillingCity\tBillingState\tBillingCountry\tBillingPostalCode\tTotal\n1\t2\t2009-01-01 00:00:00\tTheodor-Heuss-Straße 34\tStuttgart\tNone\tGermany\t70174\t1.98\n2\t4\t2009-01-02 00:00:00\tUllevålsveien 14\tOslo\tNone\tNorway\t0171\t3.96\n3\t8\t2009-01-03 00:00:00\tGrétrystraat 63\tBrussels\tNone\tBelgium\t1000\t5.94\n*/\n\n\nCREATE TABLE ""Track"" (\n\t""TrackId"" INTEGER NOT NULL, \n\t""Name"" NVARCHAR(200) NOT NULL, \n\t""AlbumId"" INTEGER, \n\t""MediaTypeId"" INTEGER NOT NULL, \n\t""GenreId"" INTEGER, \n\t""Composer"" NVARCHAR(220), \n\t""Milliseconds"" INTEGER NOT NULL, \n\t""Bytes"" INTEGER, \n\t""UnitPrice"" NUMERIC(10, 2) NOT NULL, \n\tPRIMARY KEY (""TrackId""), \n\tFOREIGN KEY(""MediaTypeId"") REFERENCES ""MediaType"" (""MediaTypeId""), \n\tFOREIGN KEY(""GenreId"") REFERENCES ""Genre"" (""GenreId""), \n\tFOREIGN KEY(""AlbumId"") REFERENCES ""Album"" (""AlbumId"")\n)\n\n/*\n3 rows from Track table:\nTrackId\tName\tAlbumId\tMediaTypeId\tGenreId\tComposer\tMilliseconds\tBytes\tUnitPrice\n1\tFor Those About To Rock (We Salute You)\t1\t1\t1\tAngus Young, Malcolm Young, Brian Johnson\t343719\t11170334\t0.99\n2\tBalls to the Wall\t2\t2\t1\tNone\t342562\t5510424\t0.99\n3\tFast As a Shark\t3\t2\t1\tF. Baltes, S. Kaufman, U. Dirkscneider & W. Hoffman\t230619\t3990994\t0.99\n*/\n\n\nCREATE TABLE ""InvoiceLine"" (\n\t""InvoiceLineId"" INTEGER NOT NULL, \n\t""InvoiceId"" INTEGER NOT NULL, \n\t""TrackId"" INTEGER NOT NULL, \n\t""UnitPrice"" NUMERIC(10, 2) NOT NULL, \n\t""Quantity"" INTEGER NOT NULL, \n\tPRIMARY KEY (""InvoiceLineId""), \n\tFOREIGN KEY(""TrackId"") REFERENCES ""Track"" (""TrackId""), \n\tFOREIGN KEY(""InvoiceId"") REFERENCES ""Invoice"" (""InvoiceId"")\n)\n\n/*\n3 rows from InvoiceLine table:\nInvoiceLineId\tInvoiceId\tTrackId\tUnitPrice\tQuantity\n1\t1\t2\t0.99\t1\n2\t1\t4\t0.99\t1\n3\t2\t6\t0.99\t1\n*/\n\n\nCREATE TABLE ""PlaylistTrack"" (\n\t""PlaylistId"" INTEGER NOT NULL, \n\t""TrackId"" INTEGER NOT NULL, \n\tPRIMARY KEY (""PlaylistId"", ""TrackId""), \n\tFOREIGN KEY(""TrackId"") REFERENCES ""Track"" (""TrackId""), \n\tFOREIGN KEY(""PlaylistId"") REFERENCES ""Playlist"" (""PlaylistId"")\n)\n\n/*\n3 rows from PlaylistTrack table:\nPlaylistId\tTrackId\n1\t3402\n1\t3389\n1\t3390\n*/', 'stop': ['\nSQLResult:']}, 'SELECT COUNT(*) FROM Employee;', {'query': 'SELECT COUNT(*) FROM Employee;', 'dialect': 'sqlite'}, 'SELECT COUNT(*) FROM Employee;', '[(8,)]']Adding Memory​How to add memory to a SQLDatabaseChain:from langchain.llms import OpenAIfrom langchain.utilities import SQLDatabasefrom langchain_experimental.sql import SQLDatabaseChainSet up the SQLDatabase and LLMdb = SQLDatabase.from_uri(""sqlite:///../../../../notebooks/Chinook.db"")llm = OpenAI(temperature=0, verbose=True)Set up the memoryfrom langchain.memory import ConversationBufferMemorymemory = ConversationBufferMemory()Now we need to add a place for memory in the prompt templatefrom langchain.prompts import PromptTemplatePROMPT_SUFFIX = """"""Only use the following tables:{table_info}Previous Conversation:{history}Question: {input}""""""_DEFAULT_TEMPLATE = """"""Given an input question, first create a syntactically correct {dialect} query to run, then look at the results of the query and return the answer. Unless the user specifies in his question a specific number of examples he wishes to obtain, always limit your query to at most {top_k} results. You can order the results by a relevant column to return the most interesting examples in the database.Never query for all the columns from a specific table, only ask for a the few relevant columns given the question.Pay attention to use only the column names that you can see in the schema description. Be careful to not query for columns that do not exist. Also, pay attention to which column is in which table.Use the following format:Question: Question hereSQLQuery: SQL Query to runSQLResult: Result of the SQLQueryAnswer: Final answer here""""""PROMPT = PromptTemplate.from_template( _DEFAULT_TEMPLATE + PROMPT_SUFFIX,)Now let's create and run out chaindb_chain = SQLDatabaseChain.from_llm(llm, db, prompt=PROMPT, verbose=True, memory=memory)db_chain.run(""name one employee"") > Entering new SQLDatabaseChain chain... name one employee SQLQuery:SELECT FirstName, LastName FROM Employee LIMIT 1 SQLResult: [('Andrew', 'Adams')] Answer:Andrew Adams > Finished chain. 'Andrew Adams'db_chain.run(""how many letters in their name?"") > Entering new SQLDatabaseChain chain... how many letters in their name? SQLQuery:SELECT LENGTH(FirstName) + LENGTH(LastName) AS 'NameLength' FROM Employee WHERE FirstName = 'Andrew' AND LastName = 'Adams' SQLResult: [(11,)] Answer:Andrew Adams has 11 letters in their name. > Finished chain. 'Andrew Adams has 11 letters in their name.'Choosing how to limit the number of rows returned​If you are querying for several rows of a table you can select the maximum number of results you want to get by using the 'top_k' parameter (default is 10). This is useful for avoiding query results that exceed the prompt max length or consume tokens unnecessarily.db_chain = SQLDatabaseChain.from_llm(llm, db, verbose=True, use_query_checker=True, top_k=3)db_chain.run(""What are some example tracks by composer Johann Sebastian Bach?"") > Entering new SQLDatabaseChain chain... What are some example tracks by composer Johann Sebastian Bach? SQLQuery:SELECT Name FROM Track WHERE Composer = 'Johann Sebastian Bach' LIMIT 3 SQLResult: [('Concerto for 2 Violins in D Minor, BWV 1043: I. Vivace',), ('Aria Mit 30 Veränderungen, BWV 988 ""Goldberg Variations"": Aria',), ('Suite for Solo Cello No. 1 in G Major, BWV 1007: I. Prélude',)] Answer:Examples of tracks by Johann Sebastian Bach are Concerto for 2 Violins in D Minor, BWV 1043: I. Vivace, Aria Mit 30 Veränderungen, BWV 988 ""Goldberg Variations"": Aria, and Suite for Solo Cello No. 1 in G Major, BWV 1007: I. Prélude. > Finished chain. 'Examples of tracks by Johann Sebastian Bach are Concerto for 2 Violins in D Minor, BWV 1043: I. Vivace, Aria Mit 30 Veränderungen, BWV 988 ""Goldberg Variations"": Aria, and Suite for Solo Cello No. 1 in G Major, BWV 1007: I. Prélude.'Adding example rows from each table​Sometimes, the format of the data is not obvious and it is optimal to include a sample of rows from the tables in the prompt to allow the LLM to understand the data before providing a final query. Here we will use this feature to let the LLM know that artists are saved with their full names by providing two rows from the Track table.db = SQLDatabase.from_uri( ""sqlite:///../../../../notebooks/Chinook.db"", include_tables=['Track'], # we include only one table to save tokens in the prompt :) sample_rows_in_table_info=2)The sample rows are added to the prompt after each corresponding table's column information:print(db.table_info) CREATE TABLE ""Track"" ( ""TrackId"" INTEGER NOT NULL, ""Name"" NVARCHAR(200) NOT NULL, ""AlbumId"" INTEGER, ""MediaTypeId"" INTEGER NOT NULL, ""GenreId"" INTEGER, ""Composer"" NVARCHAR(220), ""Milliseconds"" INTEGER NOT NULL, ""Bytes"" INTEGER, ""UnitPrice"" NUMERIC(10, 2) NOT NULL, PRIMARY KEY (""TrackId""), FOREIGN KEY(""MediaTypeId"") REFERENCES ""MediaType"" (""MediaTypeId""), FOREIGN KEY(""GenreId"") REFERENCES ""Genre"" (""GenreId""), FOREIGN KEY(""AlbumId"") REFERENCES ""Album"" (""AlbumId"") ) /* 2 rows from Track table: TrackId Name AlbumId MediaTypeId GenreId Composer Milliseconds Bytes UnitPrice 1 For Those About To Rock (We Salute You) 1 1 1 Angus Young, Malcolm Young, Brian Johnson 343719 11170334 0.99 2 Balls to the Wall 2 2 1 None 342562 5510424 0.99 */db_chain = SQLDatabaseChain.from_llm(llm, db, use_query_checker=True, verbose=True)db_chain.run(""What are some example tracks by Bach?"") > Entering new SQLDatabaseChain chain... What are some example tracks by Bach? SQLQuery:SELECT ""Name"", ""Composer"" FROM ""Track"" WHERE ""Composer"" LIKE '%Bach%' LIMIT 5 SQLResult: [('American Woman', 'B. Cummings/G. Peterson/M.J. Kale/R. Bachman'), ('Concerto for 2 Violins in D Minor, BWV 1043: I. Vivace', 'Johann Sebastian Bach'), ('Aria Mit 30 Veränderungen, BWV 988 ""Goldberg Variations"": Aria', 'Johann Sebastian Bach'), ('Suite for Solo Cello No. 1 in G Major, BWV 1007: I. Prélude', 'Johann Sebastian Bach'), ('Toccata and Fugue in D Minor, BWV 565: I. Toccata', 'Johann Sebastian Bach')] Answer:Tracks by Bach include 'American Woman', 'Concerto for 2 Violins in D Minor, BWV 1043: I. Vivace', 'Aria Mit 30 Veränderungen, BWV 988 ""Goldberg Variations"": Aria', 'Suite for Solo Cello No. 1 in G Major, BWV 1007: I. Prélude', and 'Toccata and Fugue in D Minor, BWV 565: I. Toccata'. > Finished chain. 'Tracks by Bach include \'American Woman\', \'Concerto for 2 Violins in D Minor, BWV 1043: I. Vivace\', \'Aria Mit 30 Veränderungen, BWV 988 ""Goldberg Variations"": Aria\', \'Suite for Solo Cello No. 1 in G Major, BWV 1007: I. Prélude\', and \'Toccata and Fugue in D Minor, BWV 565: I. Toccata\'.'Custom Table Info​In some cases, it can be useful to provide custom table information instead of using the automatically generated table definitions and the first sample_rows_in_table_info sample rows. For example, if you know that the first few rows of a table are uninformative, it could help to manually provide example rows that are more diverse or provide more information to the model. It is also possible to limit the columns that will be visible to the model if there are unnecessary columns. This information can be provided as a dictionary with table names as the keys and table information as the values. For example, let's provide a custom definition and sample rows for the Track table with only a few columns:custom_table_info = { ""Track"": """"""CREATE TABLE Track ( ""TrackId"" INTEGER NOT NULL, ""Name"" NVARCHAR(200) NOT NULL, ""Composer"" NVARCHAR(220), PRIMARY KEY (""TrackId""))/*3 rows from Track table:TrackId Name Composer1 For Those About To Rock (We Salute You) Angus Young, Malcolm Young, Brian Johnson2 Balls to the Wall None3 My favorite song ever The coolest composer of all time*/""""""}db = SQLDatabase.from_uri( ""sqlite:///../../../../notebooks/Chinook.db"", include_tables=['Track', 'Playlist'], sample_rows_in_table_info=2, custom_table_info=custom_table_info)print(db.table_info) CREATE TABLE ""Playlist"" ( ""PlaylistId"" INTEGER NOT NULL, ""Name"" NVARCHAR(120), PRIMARY KEY (""PlaylistId"") ) /* 2 rows from Playlist table: PlaylistId Name 1 Music 2 Movies */ CREATE TABLE Track ( ""TrackId"" INTEGER NOT NULL, ""Name"" NVARCHAR(200) NOT NULL, ""Composer"" NVARCHAR(220), PRIMARY KEY (""TrackId"") ) /* 3 rows from Track table: TrackId Name Composer 1 For Those About To Rock (We Salute You) Angus Young, Malcolm Young, Brian Johnson 2 Balls to the Wall None 3 My favorite song ever The coolest composer of all time */Note how our custom table definition and sample rows for Track overrides the sample_rows_in_table_info parameter. Tables that are not overridden by custom_table_info, in this example Playlist, will have their table info gathered automatically as usual.db_chain = SQLDatabaseChain.from_llm(llm, db, verbose=True)db_chain.run(""What are some example tracks by Bach?"") > Entering new SQLDatabaseChain chain... What are some example tracks by Bach? SQLQuery:SELECT ""Name"" FROM Track WHERE ""Composer"" LIKE '%Bach%' LIMIT 5; SQLResult: [('American Woman',), ('Concerto for 2 Violins in D Minor, BWV 1043: I. Vivace',), ('Aria Mit 30 Veränderungen, BWV 988 ""Goldberg Variations"": Aria',), ('Suite for Solo Cello No. 1 in G Major, BWV 1007: I. Prélude',), ('Toccata and Fugue in D Minor, BWV 565: I. Toccata',)] Answer:text='You are a SQLite expert. Given an input question, first create a syntactically correct SQLite query to run, then look at the results of the query and return the answer to the input question.\nUnless the user specifies in the question a specific number of examples to obtain, query for at most 5 results using the LIMIT clause as per SQLite. You can order the results to return the most informative data in the database.\nNever query for all columns from a table. You must query only the columns that are needed to answer the question. Wrap each column name in double quotes ("") to denote them as delimited identifiers.\nPay attention to use only the column names you can see in the tables below. Be careful to not query for columns that do not exist. Also, pay attention to which column is in which table.\n\nUse the following format:\n\nQuestion: ""Question here""\nSQLQuery: ""SQL Query to run""\nSQLResult: ""Result of the SQLQuery""\nAnswer: ""Final answer here""\n\nOnly use the following tables:\n\nCREATE TABLE ""Playlist"" (\n\t""PlaylistId"" INTEGER NOT NULL, \n\t""Name"" NVARCHAR(120), \n\tPRIMARY KEY (""PlaylistId"")\n)\n\n/*\n2 rows from Playlist table:\nPlaylistId\tName\n1\tMusic\n2\tMovies\n*/\n\nCREATE TABLE Track (\n\t""TrackId"" INTEGER NOT NULL, \n\t""Name"" NVARCHAR(200) NOT NULL,\n\t""Composer"" NVARCHAR(220),\n\tPRIMARY KEY (""TrackId"")\n)\n/*\n3 rows from Track table:\nTrackId\tName\tComposer\n1\tFor Those About To Rock (We Salute You)\tAngus Young, Malcolm Young, Brian Johnson\n2\tBalls to the Wall\tNone\n3\tMy favorite song ever\tThe coolest composer of all time\n*/\n\nQuestion: What are some example tracks by Bach?\nSQLQuery:SELECT ""Name"" FROM Track WHERE ""Composer"" LIKE \'%Bach%\' LIMIT 5;\nSQLResult: [(\'American Woman\',), (\'Concerto for 2 Violins in D Minor, BWV 1043: I. Vivace\',), (\'Aria Mit 30 Veränderungen, BWV 988 ""Goldberg Variations"": Aria\',), (\'Suite for Solo Cello No. 1 in G Major, BWV 1007: I. Prélude\',), (\'Toccata and Fugue in D Minor, BWV 565: I. Toccata\',)]\nAnswer:' You are a SQLite expert. Given an input question, first create a syntactically correct SQLite query to run, then look at the results of the query and return the answer to the input question. Unless the user specifies in the question a specific number of examples to obtain, query for at most 5 results using the LIMIT clause as per SQLite. You can order the results to return the most informative data in the database. Never query for all columns from a table. You must query only the columns that are needed to answer the question. Wrap each column name in double quotes ("") to denote them as delimited identifiers. Pay attention to use only the column names you can see in the tables below. Be careful to not query for columns that do not exist. Also, pay attention to which column is in which table. Use the following format: Question: ""Question here"" SQLQuery: ""SQL Query to run"" SQLResult: ""Result of the SQLQuery"" Answer: ""Final answer here"" Only use the following tables: CREATE TABLE ""Playlist"" ( ""PlaylistId"" INTEGER NOT NULL, ""Name"" NVARCHAR(120), PRIMARY KEY (""PlaylistId"") ) /* 2 rows from Playlist table: PlaylistId Name 1 Music 2 Movies */ CREATE TABLE Track ( ""TrackId"" INTEGER NOT NULL, ""Name"" NVARCHAR(200) NOT NULL, ""Composer"" NVARCHAR(220), PRIMARY KEY (""TrackId"") ) /* 3 rows from Track table: TrackId Name Composer 1 For Those About To Rock (We Salute You) Angus Young, Malcolm Young, Brian Johnson 2 Balls to the Wall None 3 My favorite song ever The coolest composer of all time */ Question: What are some example tracks by Bach? SQLQuery:SELECT ""Name"" FROM Track WHERE ""Composer"" LIKE '%Bach%' LIMIT 5; SQLResult: [('American Woman',), ('Concerto for 2 Violins in D Minor, BWV 1043: I. Vivace',), ('Aria Mit 30 Veränderungen, BWV 988 ""Goldberg Variations"": Aria',), ('Suite for Solo Cello No. 1 in G Major, BWV 1007: I. Prélude',), ('Toccata and Fugue in D Minor, BWV 565: I. Toccata',)] Answer: {'input': 'What are some example tracks by Bach?\nSQLQuery:SELECT ""Name"" FROM Track WHERE ""Composer"" LIKE \'%Bach%\' LIMIT 5;\nSQLResult: [(\'American Woman\',), (\'Concerto for 2 Violins in D Minor, BWV 1043: I. Vivace\',), (\'Aria Mit 30 Veränderungen, BWV 988 ""Goldberg Variations"": Aria\',), (\'Suite for Solo Cello No. 1 in G Major, BWV 1007: I. Prélude\',), (\'Toccata and Fugue in D Minor, BWV 565: I. Toccata\',)]\nAnswer:', 'top_k': '5', 'dialect': 'sqlite', 'table_info': '\nCREATE TABLE ""Playlist"" (\n\t""PlaylistId"" INTEGER NOT NULL, \n\t""Name"" NVARCHAR(120), \n\tPRIMARY KEY (""PlaylistId"")\n)\n\n/*\n2 rows from Playlist table:\nPlaylistId\tName\n1\tMusic\n2\tMovies\n*/\n\nCREATE TABLE Track (\n\t""TrackId"" INTEGER NOT NULL, \n\t""Name"" NVARCHAR(200) NOT NULL,\n\t""Composer"" NVARCHAR(220),\n\tPRIMARY KEY (""TrackId"")\n)\n/*\n3 rows from Track table:\nTrackId\tName\tComposer\n1\tFor Those About To Rock (We Salute You)\tAngus Young, Malcolm Young, Brian Johnson\n2\tBalls to the Wall\tNone\n3\tMy favorite song ever\tThe coolest composer of all time\n*/', 'stop': ['\nSQLResult:']} Examples of tracks by Bach include ""American Woman"", ""Concerto for 2 Violins in D Minor, BWV 1043: I. Vivace"", ""Aria Mit 30 Veränderungen, BWV 988 'Goldberg Variations': Aria"", ""Suite for Solo Cello No. 1 in G Major, BWV 1007: I. Prélude"", and ""Toccata and Fugue in D Minor, BWV 565: I. Toccata"". > Finished chain. 'Examples of tracks by Bach include ""American Woman"", ""Concerto for 2 Violins in D Minor, BWV 1043: I. Vivace"", ""Aria Mit 30 Veränderungen, BWV 988 \'Goldberg Variations\': Aria"", ""Suite for Solo Cello No. 1 in G Major, BWV 1007: I. Prélude"", and ""Toccata and Fugue in D Minor, BWV 565: I. Toccata"".'SQL Views​In some case, the table schema can be hidden behind a JSON or JSONB column. Adding row samples into the prompt might help won't always describe the data perfectly. For this reason, a custom SQL views can help.CREATE VIEW accounts_v AS select id, firstname, lastname, email, created_at, updated_at, cast(stats->>'total_post' as int) as total_post, cast(stats->>'total_comments' as int) as total_comments, cast(stats->>'ltv' as int) as ltv FROM accounts;Then limit the tables visible from SQLDatabase to the created view.db = SQLDatabase.from_uri( ""sqlite:///../../../../notebooks/Chinook.db"", include_tables=['accounts_v']) # we include only the viewSQLDatabaseSequentialChain​Chain for querying SQL database that is a sequential chain.The chain is as follows:1. Based on the query, determine which tables to use.2. Based on those tables, call the normal SQL database chain.This is useful in cases where the number of tables in the database is large.from langchain_experimental.sql import SQLDatabaseSequentialChaindb = SQLDatabase.from_uri(""sqlite:///../../../../notebooks/Chinook.db"")chain = SQLDatabaseSequentialChain.from_llm(llm, db, verbose=True)chain.run(""How many employees are also customers?"") > Entering new SQLDatabaseSequentialChain chain... Table names to use: ['Employee', 'Customer'] > Entering new SQLDatabaseChain chain... How many employees are also customers? SQLQuery:SELECT COUNT(*) FROM Employee e INNER JOIN Customer c ON e.EmployeeId = c.SupportRepId; SQLResult: [(59,)] Answer:59 employees are also customers. > Finished chain. > Finished chain. '59 employees are also customers.'Using Local Language Models​Sometimes you may not have the luxury of using OpenAI or other service-hosted large language model. You can, ofcourse, try to use the SQLDatabaseChain with a local model, but will quickly realize that most models you can run locally even with a large GPU struggle to generate the right output.import loggingimport torchfrom transformers import AutoTokenizer, GPT2TokenizerFast, pipeline, AutoModelForSeq2SeqLM, AutoModelForCausalLMfrom langchain.llms import HuggingFacePipeline# Note: This model requires a large GPU, e.g. an 80GB A100. See documentation for other ways to run private non-OpenAI models.model_id = ""google/flan-ul2""model = AutoModelForSeq2SeqLM.from_pretrained(model_id, temperature=0)device_id = -1 # default to no-GPU, but use GPU and half precision mode if availableif torch.cuda.is_available(): device_id = 0 try: model = model.half() except RuntimeError as exc: logging.warn(f""Could not run model in half precision mode: {str(exc)}"")tokenizer = AutoTokenizer.from_pretrained(model_id)pipe = pipeline(task=""text2text-generation"", model=model, tokenizer=tokenizer, max_length=1024, device=device_id)local_llm = HuggingFacePipeline(pipeline=pipe) /workspace/langchain/.venv/lib/python3.9/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html from .autonotebook import tqdm as notebook_tqdm Loading checkpoint shards: 100%|██████████| 8/8 [00:32<00:00, 4.11s/it]from langchain.utilities import SQLDatabasefrom langchain_experimental.sql import SQLDatabaseChaindb = SQLDatabase.from_uri(""sqlite:///../../../../notebooks/Chinook.db"", include_tables=['Customer'])local_chain = SQLDatabaseChain.from_llm(local_llm, db, verbose=True, return_intermediate_steps=True, use_query_checker=True)This model should work for very simple SQL queries, as long as you use the query checker as specified above, e.g.:local_chain(""How many customers are there?"") > Entering new SQLDatabaseChain chain... How many customers are there? SQLQuery: /workspace/langchain/.venv/lib/python3.9/site-packages/transformers/pipelines/base.py:1070: UserWarning: You seem to be using the pipelines sequentially on GPU. In order to maximize efficiency please use a dataset warnings.warn( /workspace/langchain/.venv/lib/python3.9/site-packages/transformers/pipelines/base.py:1070: UserWarning: You seem to be using the pipelines sequentially on GPU. In order to maximize efficiency please use a dataset warnings.warn( SELECT count(*) FROM Customer SQLResult: [(59,)] Answer: /workspace/langchain/.venv/lib/python3.9/site-packages/transformers/pipelines/base.py:1070: UserWarning: You seem to be using the pipelines sequentially on GPU. In order to maximize efficiency please use a dataset warnings.warn( [59] > Finished chain. {'query': 'How many customers are there?', 'result': '[59]', 'intermediate_steps': [{'input': 'How many customers are there?\nSQLQuery:SELECT count(*) FROM Customer\nSQLResult: [(59,)]\nAnswer:', 'top_k': '5', 'dialect': 'sqlite', 'table_info': '\nCREATE TABLE ""Customer"" (\n\t""CustomerId"" INTEGER NOT NULL, \n\t""FirstName"" NVARCHAR(40) NOT NULL, \n\t""LastName"" NVARCHAR(20) NOT NULL, \n\t""Company"" NVARCHAR(80), \n\t""Address"" NVARCHAR(70), \n\t""City"" NVARCHAR(40), \n\t""State"" NVARCHAR(40), \n\t""Country"" NVARCHAR(40), \n\t""PostalCode"" NVARCHAR(10), \n\t""Phone"" NVARCHAR(24), \n\t""Fax"" NVARCHAR(24), \n\t""Email"" NVARCHAR(60) NOT NULL, \n\t""SupportRepId"" INTEGER, \n\tPRIMARY KEY (""CustomerId""), \n\tFOREIGN KEY(""SupportRepId"") REFERENCES ""Employee"" (""EmployeeId"")\n)\n\n/*\n3 rows from Customer table:\nCustomerId\tFirstName\tLastName\tCompany\tAddress\tCity\tState\tCountry\tPostalCode\tPhone\tFax\tEmail\tSupportRepId\n1\tLuís\tGonçalves\tEmbraer - Empresa Brasileira" 111,https://python.langchain.com/docs/use_cases/apis,"Interacting with APIsOn this pageInteracting with APIsUse case​Suppose you want an LLM to interact with external APIs.This can be very useful for retrieving context for the LLM to utilize.And, more generally, it allows us to interact with APIs using natural language! Overview​There are two primary ways to interface LLMs with external APIs:Functions: For example, OpenAI functions is one popular means of doing this.LLM-generated interface: Use an LLM with access to API documentation to create an interface.Quickstart​Many APIs are already compatible with OpenAI function calling.For example, Klarna has a YAML file that describes its API and allows OpenAI to interact with it:https://www.klarna.com/us/shopping/public/openai/v0/api-docs/Other options include:Speak for translationXKCD for comicsWe can supply the specification to get_openapi_chain directly in order to query the API with OpenAI functions:pip install langchain openai # Set env var OPENAI_API_KEY or load from a .env file:# import dotenv# dotenv.load_dotenv()from langchain.chains.openai_functions.openapi import get_openapi_chainchain = get_openapi_chain(""https://www.klarna.com/us/shopping/public/openai/v0/api-docs/"")chain(""What are some options for a men's large blue button down shirt"") Attempting to load an OpenAPI 3.0.1 spec. This may result in degraded performance. Convert your OpenAPI spec to 3.1.* spec for better support. {'query': ""What are some options for a men's large blue button down shirt"", 'response': {'products': [{'name': 'Cubavera Four Pocket Guayabera Shirt', 'url': 'https://www.klarna.com/us/shopping/pl/cl10001/3202055522/Clothing/Cubavera-Four-Pocket-Guayabera-Shirt/?utm_source=openai&ref-site=openai_plugin', 'price': '$13.50', 'attributes': ['Material:Polyester,Cotton', 'Target Group:Man', 'Color:Red,White,Blue,Black', 'Properties:Pockets', 'Pattern:Solid Color', 'Size (Small-Large):S,XL,L,M,XXL']}, {'name': 'Polo Ralph Lauren Plaid Short Sleeve Button-down Oxford Shirt', 'url': 'https://www.klarna.com/us/shopping/pl/cl10001/3207163438/Clothing/Polo-Ralph-Lauren-Plaid-Short-Sleeve-Button-down-Oxford-Shirt/?utm_source=openai&ref-site=openai_plugin', 'price': '$52.20', 'attributes': ['Material:Cotton', 'Target Group:Man', 'Color:Red,Blue,Multicolor', 'Size (Small-Large):S,XL,L,M,XXL']}, {'name': 'Brixton Bowery Flannel Shirt', 'url': 'https://www.klarna.com/us/shopping/pl/cl10001/3202331096/Clothing/Brixton-Bowery-Flannel-Shirt/?utm_source=openai&ref-site=openai_plugin', 'price': '$27.48', 'attributes': ['Material:Cotton', 'Target Group:Man', 'Color:Gray,Blue,Black,Orange', 'Properties:Pockets', 'Pattern:Checkered', 'Size (Small-Large):XL,3XL,4XL,5XL,L,M,XXL']}, {'name': 'Vineyard Vines Gingham On-The-Go brrr Classic Fit Shirt Crystal', 'url': 'https://www.klarna.com/us/shopping/pl/cl10001/3201938510/Clothing/Vineyard-Vines-Gingham-On-The-Go-brrr-Classic-Fit-Shirt-Crystal/?utm_source=openai&ref-site=openai_plugin', 'price': '$80.64', 'attributes': ['Material:Cotton', 'Target Group:Man', 'Color:Blue', 'Size (Small-Large):XL,XS,L,M']}, {'name': ""Carhartt Men's Loose Fit Midweight Short Sleeve Plaid Shirt"", 'url': 'https://www.klarna.com/us/shopping/pl/cl10001/3201826024/Clothing/Carhartt-Men-s-Loose-Fit-Midweight-Short-Sleeve-Plaid-Shirt/?utm_source=openai&ref-site=openai_plugin', 'price': '$17.99', 'attributes': ['Material:Cotton', 'Target Group:Man', 'Color:Red,Brown,Blue,Green', 'Properties:Pockets', 'Pattern:Checkered', 'Size (Small-Large):S,XL,L,M']}]}}Functions​We can unpack what is happening when we use the functions to call external APIs.Let's look at the LangSmith trace:See here that we call the OpenAI LLM with the provided API spec:https://www.klarna.com/us/shopping/public/openai/v0/api-docs/The prompt then tells the LLM to use the API spec with input question:Use the provided APIs to respond to this user query:What are some options for a men's large blue button down shirtThe LLM returns the parameters for the function call productsUsingGET, which is specified in the provided API spec:function_call: name: productsUsingGET arguments: |- { ""params"": { ""countryCode"": ""US"", ""q"": ""men's large blue button down shirt"", ""size"": 5, ""min_price"": 0, ""max_price"": 100 } }This Dict above split and the API is called here.API Chain​We can also build our own interface to external APIs using the APIChain and provided API documentation.from langchain.llms import OpenAIfrom langchain.chains import APIChainfrom langchain.chains.api import open_meteo_docsllm = OpenAI(temperature=0)chain = APIChain.from_llm_and_api_docs(llm, open_meteo_docs.OPEN_METEO_DOCS, verbose=True)chain.run('What is the weather like right now in Munich, Germany in degrees Fahrenheit?') > Entering new APIChain chain... https://api.open-meteo.com/v1/forecast?latitude=48.1351&longitude=11.5820&hourly=temperature_2m&temperature_unit=fahrenheit¤t_weather=true {""latitude"":48.14,""longitude"":11.58,""generationtime_ms"":1.0769367218017578,""utc_offset_seconds"":0,""timezone"":""GMT"",""timezone_abbreviation"":""GMT"",""elevation"":521.0,""current_weather"":{""temperature"":52.9,""windspeed"":12.6,""winddirection"":239.0,""weathercode"":3,""is_day"":0,""time"":""2023-08-07T22:00""},""hourly_units"":{""time"":""iso8601"",""temperature_2m"":""°F""},""hourly"":{""time"":[""2023-08-07T00:00"",""2023-08-07T01:00"",""2023-08-07T02:00"",""2023-08-07T03:00"",""2023-08-07T04:00"",""2023-08-07T05:00"",""2023-08-07T06:00"",""2023-08-07T07:00"",""2023-08-07T08:00"",""2023-08-07T09:00"",""2023-08-07T10:00"",""2023-08-07T11:00"",""2023-08-07T12:00"",""2023-08-07T13:00"",""2023-08-07T14:00"",""2023-08-07T15:00"",""2023-08-07T16:00"",""2023-08-07T17:00"",""2023-08-07T18:00"",""2023-08-07T19:00"",""2023-08-07T20:00"",""2023-08-07T21:00"",""2023-08-07T22:00"",""2023-08-07T23:00"",""2023-08-08T00:00"",""2023-08-08T01:00"",""2023-08-08T02:00"",""2023-08-08T03:00"",""2023-08-08T04:00"",""2023-08-08T05:00"",""2023-08-08T06:00"",""2023-08-08T07:00"",""2023-08-08T08:00"",""2023-08-08T09:00"",""2023-08-08T10:00"",""2023-08-08T11:00"",""2023-08-08T12:00"",""2023-08-08T13:00"",""2023-08-08T14:00"",""2023-08-08T15:00"",""2023-08-08T16:00"",""2023-08-08T17:00"",""2023-08-08T18:00"",""2023-08-08T19:00"",""2023-08-08T20:00"",""2023-08-08T21:00"",""2023-08-08T22:00"",""2023-08-08T23:00"",""2023-08-09T00:00"",""2023-08-09T01:00"",""2023-08-09T02:00"",""2023-08-09T03:00"",""2023-08-09T04:00"",""2023-08-09T05:00"",""2023-08-09T06:00"",""2023-08-09T07:00"",""2023-08-09T08:00"",""2023-08-09T09:00"",""2023-08-09T10:00"",""2023-08-09T11:00"",""2023-08-09T12:00"",""2023-08-09T13:00"",""2023-08-09T14:00"",""2023-08-09T15:00"",""2023-08-09T16:00"",""2023-08-09T17:00"",""2023-08-09T18:00"",""2023-08-09T19:00"",""2023-08-09T20:00"",""2023-08-09T21:00"",""2023-08-09T22:00"",""2023-08-09T23:00"",""2023-08-10T00:00"",""2023-08-10T01:00"",""2023-08-10T02:00"",""2023-08-10T03:00"",""2023-08-10T04:00"",""2023-08-10T05:00"",""2023-08-10T06:00"",""2023-08-10T07:00"",""2023-08-10T08:00"",""2023-08-10T09:00"",""2023-08-10T10:00"",""2023-08-10T11:00"",""2023-08-10T12:00"",""2023-08-10T13:00"",""2023-08-10T14:00"",""2023-08-10T15:00"",""2023-08-10T16:00"",""2023-08-10T17:00"",""2023-08-10T18:00"",""2023-08-10T19:00"",""2023-08-10T20:00"",""2023-08-10T21:00"",""2023-08-10T22:00"",""2023-08-10T23:00"",""2023-08-11T00:00"",""2023-08-11T01:00"",""2023-08-11T02:00"",""2023-08-11T03:00"",""2023-08-11T04:00"",""2023-08-11T05:00"",""2023-08-11T06:00"",""2023-08-11T07:00"",""2023-08-11T08:00"",""2023-08-11T09:00"",""2023-08-11T10:00"",""2023-08-11T11:00"",""2023-08-11T12:00"",""2023-08-11T13:00"",""2023-08-11T14:00"",""2023-08-11T15:00"",""2023-08-11T16:00"",""2023-08-11T17:00"",""2023-08-11T18:00"",""2023-08-11T19:00"",""2023-08-11T20:00"",""2023-08-11T21:00"",""2023-08-11T22:00"",""2023-08-11T23:00"",""2023-08-12T00:00"",""2023-08-12T01:00"",""2023-08-12T02:00"",""2023-08-12T03:00"",""2023-08-12T04:00"",""2023-08-12T05:00"",""2023-08-12T06:00"",""2023-08-12T07:00"",""2023-08-12T08:00"",""2023-08-12T09:00"",""2023-08-12T10:00"",""2023-08-12T11:00"",""2023-08-12T12:00"",""2023-08-12T13:00"",""2023-08-12T14:00"",""2023-08-12T15:00"",""2023-08-12T16:00"",""2023-08-12T17:00"",""2023-08-12T18:00"",""2023-08-12T19:00"",""2023-08-12T20:00"",""2023-08-12T21:00"",""2023-08-12T22:00"",""2023-08-12T23:00"",""2023-08-13T00:00"",""2023-08-13T01:00"",""2023-08-13T02:00"",""2023-08-13T03:00"",""2023-08-13T04:00"",""2023-08-13T05:00"",""2023-08-13T06:00"",""2023-08-13T07:00"",""2023-08-13T08:00"",""2023-08-13T09:00"",""2023-08-13T10:00"",""2023-08-13T11:00"",""2023-08-13T12:00"",""2023-08-13T13:00"",""2023-08-13T14:00"",""2023-08-13T15:00"",""2023-08-13T16:00"",""2023-08-13T17:00"",""2023-08-13T18:00"",""2023-08-13T19:00"",""2023-08-13T20:00"",""2023-08-13T21:00"",""2023-08-13T22:00"",""2023-08-13T23:00""],""temperature_2m"":[53.0,51.2,50.9,50.4,50.7,51.3,51.7,52.9,54.3,56.1,57.4,59.3,59.1,60.7,59.7,58.8,58.8,57.8,56.6,55.3,53.9,52.7,52.9,53.2,52.0,51.8,51.3,50.7,50.8,51.5,53.9,57.7,61.2,63.2,64.7,66.6,67.5,67.0,68.7,68.7,67.9,66.2,64.4,61.4,59.8,58.9,57.9,56.3,55.7,55.3,55.5,55.4,55.7,56.5,57.6,58.8,59.7,59.1,58.9,60.6,59.9,59.8,59.9,61.7,63.2,63.6,62.3,58.9,57.3,57.1,57.0,56.5,56.2,56.0,55.3,54.7,54.4,55.2,57.8,60.7,63.0,65.3,66.9,68.2,70.1,72.1,72.6,71.4,69.7,68.6,66.2,63.6,61.8,60.6,59.6,58.9,58.0,57.1,56.3,56.2,56.7,57.9,59.9,63.7,68.4,72.4,75.0,76.8,78.0,78.7,78.9,78.4,76.9,74.8,72.5,70.1,67.6,65.6,64.4,63.9,63.4,62.7,62.2,62.1,62.5,63.4,65.1,68.0,71.7,74.8,76.8,78.2,79.1,79.6,79.7,79.2,77.6,75.3,73.7,68.6,66.8,65.3,64.2,63.4,62.6,61.7,60.9,60.6,60.9,61.6,63.2,65.9,69.3,72.2,74.4,76.2,77.6,78.8,79.6,79.6,78.4,76.4,74.3,72.3,70.4,68.7,67.6,66.8]}} > Finished chain. ' The current temperature in Munich, Germany is 52.9°F.'Note that we supply information about the API:open_meteo_docs.OPEN_METEO_DOCS[0:500] 'BASE URL: https://api.open-meteo.com/\n\nAPI Documentation\nThe API endpoint /v1/forecast accepts a geographical coordinate, a list of weather variables and responds with a JSON hourly weather forecast for 7 days. Time always starts at 0:00 today and contains 168 hours. All URL parameters are listed below:\n\nParameter\tFormat\tRequired\tDefault\tDescription\nlatitude, longitude\tFloating point\tYes\t\tGeographical WGS84 coordinate of the location\nhourly\tString array\tNo\t\tA list of weather variables which shou'Under the hood, we do two things:api_request_chain: Generate an API URL based on the input question and the api_docsapi_answer_chain: generate a final answer based on the API responseWe can look at the LangSmith trace to inspect this:The api_request_chain produces the API url from our question and the API documentation:Here we make the API request with the API url.The api_answer_chain takes the response from the API and provides us with a natural language response:Going deeper​Test with other APIsimport osos.environ['TMDB_BEARER_TOKEN'] = """"from langchain.chains.api import tmdb_docsheaders = {""Authorization"": f""Bearer {os.environ['TMDB_BEARER_TOKEN']}""}chain = APIChain.from_llm_and_api_docs(llm, tmdb_docs.TMDB_DOCS, headers=headers, verbose=True)chain.run(""Search for 'Avatar'"")import osfrom langchain.llms import OpenAIfrom langchain.chains.api import podcast_docsfrom langchain.chains import APIChain listen_api_key = 'xxx' # Get api key here: https://www.listennotes.com/api/pricing/llm = OpenAI(temperature=0)headers = {""X-ListenAPI-Key"": listen_api_key}chain = APIChain.from_llm_and_api_docs(llm, podcast_docs.PODCAST_DOCS, headers=headers, verbose=True)chain.run(""Search for 'silicon valley bank' podcast episodes, audio length is more than 30 minutes, return only 1 results"")Web requestsURL requests are such a common use-case that we have the LLMRequestsChain, which makes an HTTP GET request. from langchain.llms import OpenAIfrom langchain.prompts import PromptTemplatefrom langchain.chains import LLMRequestsChain, LLMChaintemplate = """"""Between >>> and <<< are the raw search result text from google.Extract the answer to the question '{query}' or say ""not found"" if the information is not contained.Use the formatExtracted:>>> {requests_result} << Entering new LLMChain chain... Prompt after formatting: System: You are a nice chatbot having a conversation with a human. Human: hi > Finished chain. {'question': 'hi', 'chat_history': [HumanMessage(content='hi', additional_kwargs={}, example=False), AIMessage(content='Hello! How can I assist you today?', additional_kwargs={}, example=False)], 'text': 'Hello! How can I assist you today?'}conversation({""question"": ""Translate this sentence from English to French: I love programming.""}) > Entering new LLMChain chain... Prompt after formatting: System: You are a nice chatbot having a conversation with a human. Human: hi AI: Hello! How can I assist you today? Human: Translate this sentence from English to French: I love programming. > Finished chain. {'question': 'Translate this sentence from English to French: I love programming.', 'chat_history': [HumanMessage(content='hi', additional_kwargs={}, example=False), AIMessage(content='Hello! How can I assist you today?', additional_kwargs={}, example=False), HumanMessage(content='Translate this sentence from English to French: I love programming.', additional_kwargs={}, example=False), AIMessage(content='Sure! The translation of ""I love programming"" from English to French is ""J\'adore programmer.""', additional_kwargs={}, example=False)], 'text': 'Sure! The translation of ""I love programming"" from English to French is ""J\'adore programmer.""'}conversation({""question"": ""Now translate the sentence to German.""}) > Entering new LLMChain chain... Prompt after formatting: System: You are a nice chatbot having a conversation with a human. Human: hi AI: Hello! How can I assist you today? Human: Translate this sentence from English to French: I love programming. AI: Sure! The translation of ""I love programming"" from English to French is ""J'adore programmer."" Human: Now translate the sentence to German. > Finished chain. {'question': 'Now translate the sentence to German.', 'chat_history': [HumanMessage(content='hi', additional_kwargs={}, example=False), AIMessage(content='Hello! How can I assist you today?', additional_kwargs={}, example=False), HumanMessage(content='Translate this sentence from English to French: I love programming.', additional_kwargs={}, example=False), AIMessage(content='Sure! The translation of ""I love programming"" from English to French is ""J\'adore programmer.""', additional_kwargs={}, example=False), HumanMessage(content='Now translate the sentence to German.', additional_kwargs={}, example=False), AIMessage(content='Certainly! The translation of ""I love programming"" from English to German is ""Ich liebe das Programmieren.""', additional_kwargs={}, example=False)], 'text': 'Certainly! The translation of ""I love programming"" from English to German is ""Ich liebe das Programmieren.""'}We can see the chat history preserved in the prompt using the LangSmith trace.Chat Retrieval​Now, suppose we want to chat with documents or some other source of knowledge.This is popular use case, combining chat with document retrieval.It allows us to chat with specific information that the model was not trained on.pip install tiktoken chromadbLoad a blog post.from langchain.document_loaders import WebBaseLoaderloader = WebBaseLoader(""https://lilianweng.github.io/posts/2023-06-23-agent/"")data = loader.load()Split and store this in a vector.from langchain.text_splitter import RecursiveCharacterTextSplittertext_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=0)all_splits = text_splitter.split_documents(data)from langchain.embeddings import OpenAIEmbeddingsfrom langchain.vectorstores import Chromavectorstore = Chroma.from_documents(documents=all_splits, embedding=OpenAIEmbeddings())Create our memory, as before, but's let's use ConversationSummaryMemory.memory = ConversationSummaryMemory(llm=llm,memory_key=""chat_history"",return_messages=True)from langchain.chat_models import ChatOpenAIfrom langchain.chains import ConversationalRetrievalChainllm = ChatOpenAI()retriever = vectorstore.as_retriever()qa = ConversationalRetrievalChain.from_llm(llm, retriever=retriever, memory=memory)qa(""How do agents use Task decomposition?"") {'question': 'How do agents use Task decomposition?', 'chat_history': [SystemMessage(content='', additional_kwargs={})], 'answer': 'Agents can use task decomposition in several ways:\n\n1. Simple prompting: Agents can use Language Model based prompting to break down tasks into subgoals. For example, by providing prompts like ""Steps for XYZ"" or ""What are the subgoals for achieving XYZ?"", the agent can generate a sequence of smaller steps that lead to the completion of the overall task.\n\n2. Task-specific instructions: Agents can be given task-specific instructions to guide their planning process. For example, if the task is to write a novel, the agent can be instructed to ""Write a story outline."" This provides a high-level structure for the task and helps in breaking it down into smaller components.\n\n3. Human inputs: Agents can also take inputs from humans to decompose tasks. This can be done through direct communication or by leveraging human expertise. Humans can provide guidance and insights to help the agent break down complex tasks into manageable subgoals.\n\nOverall, task decomposition allows agents to break down large tasks into smaller, more manageable subgoals, enabling them to plan and execute complex tasks efficiently.'}qa(""What are the various ways to implemet memory to support it?"") {'question': 'What are the various ways to implemet memory to support it?', 'chat_history': [SystemMessage(content='The human asks how agents use task decomposition. The AI explains that agents can use task decomposition in several ways, including simple prompting, task-specific instructions, and human inputs. Task decomposition allows agents to break down large tasks into smaller, more manageable subgoals, enabling them to plan and execute complex tasks efficiently.', additional_kwargs={})], 'answer': 'There are several ways to implement memory to support task decomposition:\n\n1. Long-Term Memory Management: This involves storing and organizing information in a long-term memory system. The agent can retrieve past experiences, knowledge, and learned strategies to guide the task decomposition process.\n\n2. Internet Access: The agent can use internet access to search for relevant information and gather resources to aid in task decomposition. This allows the agent to access a vast amount of information and utilize it in the decomposition process.\n\n3. GPT-3.5 Powered Agents: The agent can delegate simple tasks to GPT-3.5 powered agents. These agents can perform specific tasks or provide assistance in task decomposition, allowing the main agent to focus on higher-level planning and decision-making.\n\n4. File Output: The agent can store the results of task decomposition in files or documents. This allows for easy retrieval and reference during the execution of the task.\n\nThese memory resources help the agent in organizing and managing information, making informed decisions, and effectively decomposing complex tasks into smaller, manageable subgoals.'}Again, we can use the LangSmith trace to explore the prompt structure.Going deeper​Agents, such as the conversational retrieval agent, can be used for retrieval when necessary while also holding a conversation.PreviousInteracting with APIsNextCode understandingUse caseOverviewQuickstartMemoryConversationChat RetrievalGoing deeper" 113,https://python.langchain.com/docs/use_cases/code_understanding,"Code understandingOn this pageCode understandingUse case​Source code analysis is one of the most popular LLM applications (e.g., GitHub Co-Pilot, Code Interpreter, Codium, and Codeium) for use-cases such as:Q&A over the code base to understand how it worksUsing LLMs for suggesting refactors or improvementsUsing LLMs for documenting the codeOverview​The pipeline for QA over code follows the steps we do for document question answering, with some differences:In particular, we can employ a splitting strategy that does a few things:Keeps each top-level function and class in the code is loaded into separate documents. Puts remaining into a separate document.Retains metadata about where each split comes fromQuickstart​pip install openai tiktoken chromadb langchain# Set env var OPENAI_API_KEY or load from a .env file# import dotenv# dotenv.load_dotenv()We'lll follow the structure of this notebook and employ context aware code splitting.Loading​We will upload all python project files using the langchain.document_loaders.TextLoader.The following script iterates over the files in the LangChain repository and loads every .py file (a.k.a. documents):# from git import Repofrom langchain.text_splitter import Languagefrom langchain.document_loaders.generic import GenericLoaderfrom langchain.document_loaders.parsers import LanguageParser# Clonerepo_path = ""/Users/rlm/Desktop/test_repo""# repo = Repo.clone_from(""https://github.com/langchain-ai/langchain"", to_path=repo_path)We load the py code using LanguageParser, which will:Keep top-level functions and classes together (into a single document)Put remaining code into a separate documentRetains metadata about where each split comes from# Loadloader = GenericLoader.from_filesystem( repo_path+""/libs/langchain/langchain"", glob=""**/*"", suffixes=["".py""], parser=LanguageParser(language=Language.PYTHON, parser_threshold=500))documents = loader.load()len(documents) 1293Splitting​Split the Document into chunks for embedding and vector storage.We can use RecursiveCharacterTextSplitter w/ language specified.from langchain.text_splitter import RecursiveCharacterTextSplitterpython_splitter = RecursiveCharacterTextSplitter.from_language(language=Language.PYTHON, chunk_size=2000, chunk_overlap=200)texts = python_splitter.split_documents(documents)len(texts) 3748RetrievalQA​We need to store the documents in a way we can semantically search for their content. The most common approach is to embed the contents of each document then store the embedding and document in a vector store. When setting up the vectorstore retriever:We test max marginal relevance for retrievalAnd 8 documents returnedGo deeper​Browse the > 40 vectorstores integrations here.See further documentation on vectorstores here.Browse the > 30 text embedding integrations here.See further documentation on embedding models here.from langchain.vectorstores import Chromafrom langchain.embeddings.openai import OpenAIEmbeddingsdb = Chroma.from_documents(texts, OpenAIEmbeddings(disallowed_special=()))retriever = db.as_retriever( search_type=""mmr"", # Also test ""similarity"" search_kwargs={""k"": 8},)Chat​Test chat, just as we do for chatbots.Go deeper​Browse the > 55 LLM and chat model integrations here.See further documentation on LLMs and chat models here.Use local LLMS: The popularity of PrivateGPT and GPT4All underscore the importance of running LLMs locally.from langchain.chat_models import ChatOpenAIfrom langchain.memory import ConversationSummaryMemoryfrom langchain.chains import ConversationalRetrievalChainllm = ChatOpenAI(model_name=""gpt-4"") memory = ConversationSummaryMemory(llm=llm,memory_key=""chat_history"",return_messages=True)qa = ConversationalRetrievalChain.from_llm(llm, retriever=retriever, memory=memory)question = ""How can I initialize a ReAct agent?""result = qa(question)result['answer'] 'To initialize a ReAct agent, you need to follow these steps:\n\n1. Initialize a language model `llm` of type `BaseLanguageModel`.\n\n2. Initialize a document store `docstore` of type `Docstore`.\n\n3. Create a `DocstoreExplorer` with the initialized `docstore`. The `DocstoreExplorer` is used to search for and look up terms in the document store.\n\n4. Create an array of `Tool` objects. The `Tool` objects represent the actions that the agent can perform. In the case of `ReActDocstoreAgent`, the tools must be ""Search"" and ""Lookup"" with their corresponding functions from the `DocstoreExplorer`.\n\n5. Initialize the `ReActDocstoreAgent` using the `from_llm_and_tools` method with the `llm` (language model) and `tools` as parameters.\n\n6. Initialize the `ReActChain` (which is the `AgentExecutor`) using the `ReActDocstoreAgent` and `tools` as parameters.\n\nHere is an example of how to do this:\n\n```python\nfrom langchain.chains import ReActChain, OpenAI\nfrom langchain.docstore.base import Docstore\nfrom langchain.docstore.document import Document\nfrom langchain.tools.base import BaseTool\n\n# Initialize the LLM and a docstore\nllm = OpenAI()\ndocstore = Docstore()\n\ndocstore_explorer = DocstoreExplorer(docstore)\ntools = [\n Tool(\n name=""Search"",\n func=docstore_explorer.search,\n description=""Search for a term in the docstore."",\n ),\n Tool(\n name=""Lookup"",\n func=docstore_explorer.lookup,\n description=""Lookup a term in the docstore."",\n ),\n]\nagent = ReActDocstoreAgent.from_llm_and_tools(llm, tools)\nreact = ReActChain(agent=agent, tools=tools)\n```\n\nKeep in mind that this is a simplified example and you might need to adapt it to your specific needs.'questions = [ ""What is the class hierarchy?"", ""What classes are derived from the Chain class?"", ""What one improvement do you propose in code in relation to the class herarchy for the Chain class?"",]for question in questions: result = qa(question) print(f""-> **Question**: {question} \n"") print(f""**Answer**: {result['answer']} \n"") -> **Question**: What is the class hierarchy? **Answer**: The class hierarchy in object-oriented programming is the structure that forms when classes are derived from other classes. The derived class is a subclass of the base class also known as the superclass. This hierarchy is formed based on the concept of inheritance in object-oriented programming where a subclass inherits the properties and functionalities of the superclass. In the given context, we have the following examples of class hierarchies: 1. `BaseCallbackHandler --> CallbackHandler` means `BaseCallbackHandler` is a base class and `CallbackHandler` (like `AimCallbackHandler`, `ArgillaCallbackHandler` etc.) are derived classes that inherit from `BaseCallbackHandler`. 2. `BaseLoader --> Loader` means `BaseLoader` is a base class and `Loader` (like `TextLoader`, `UnstructuredFileLoader` etc.) are derived classes that inherit from `BaseLoader`. 3. `ToolMetaclass --> BaseTool --> Tool` means `ToolMetaclass` is a base class, `BaseTool` is a derived class that inherits from `ToolMetaclass`, and `Tool` (like `AIPluginTool`, `BaseGraphQLTool` etc.) are further derived classes that inherit from `BaseTool`. -> **Question**: What classes are derived from the Chain class? **Answer**: The classes that are derived from the Chain class are: 1. LLMSummarizationCheckerChain 2. MapReduceChain 3. OpenAIModerationChain 4. NatBotChain 5. QAGenerationChain 6. QAWithSourcesChain 7. RetrievalQAWithSourcesChain 8. VectorDBQAWithSourcesChain 9. RetrievalQA 10. VectorDBQA 11. LLMRouterChain 12. MultiPromptChain 13. MultiRetrievalQAChain 14. MultiRouteChain 15. RouterChain 16. SequentialChain 17. SimpleSequentialChain 18. TransformChain 19. BaseConversationalRetrievalChain 20. ConstitutionalChain -> **Question**: What one improvement do you propose in code in relation to the class herarchy for the Chain class? **Answer**: As an AI model, I don't have personal opinions. However, one suggestion could be to improve the documentation of the Chain class hierarchy. The current comments and docstrings provide some details but it could be helpful to include more explicit explanations about the hierarchy, roles of each subclass, and their relationships with one another. Also, incorporating UML diagrams or other visuals could help developers better understand the structure and interactions of the classes. The can look at the LangSmith trace to see what is happening under the hood:In particular, the code well structured and kept together in the retrival outputThe retrieved code and chat history are passed to the LLM for answer distillationOpen source LLMs​We can use Code LLaMA via LLamaCPP or Ollama integration.Note: be sure to upgrade llama-cpp-python in order to use the new gguf file format.CMAKE_ARGS=""-DLLAMA_METAL=on"" FORCE_CMAKE=1 /Users/rlm/miniforge3/envs/llama2/bin/pip install -U llama-cpp-python --no-cache-dirCheck out the latest code-llama models here.from langchain.llms import LlamaCppfrom langchain.prompts import PromptTemplatefrom langchain.chains import LLMChainfrom langchain.callbacks.manager import CallbackManagerfrom langchain.memory import ConversationSummaryMemoryfrom langchain.chains import ConversationalRetrievalChain from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandlercallback_manager = CallbackManager([StreamingStdOutCallbackHandler()])llm = LlamaCpp( model_path=""/Users/rlm/Desktop/Code/llama/code-llama/codellama-13b-instruct.Q4_K_M.gguf"", n_ctx=5000, n_gpu_layers=1, n_batch=512, f16_kv=True, # MUST set to True, otherwise you will run into problem after a couple of calls callback_manager=callback_manager, verbose=True,) llama_model_loader: loaded meta data with 17 key-value pairs and 363 tensors from /Users/rlm/Desktop/Code/llama/code-llama/codellama-13b-instruct.Q4_K_M.gguf (version GGUF V1 (latest)) llama_model_loader: - tensor 0: token_embd.weight q4_0 [ 5120, 32016, 1, 1 ] llama_model_loader: - tensor 1: output_norm.weight f32 [ 5120, 1, 1, 1 ] llama_model_loader: - tensor 2: output.weight f16 [ 5120, 32016, 1, 1 ] llama_model_loader: - tensor 3: blk.0.attn_q.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 4: blk.0.attn_k.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 5: blk.0.attn_v.weight q6_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 6: blk.0.attn_output.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 7: blk.0.ffn_gate.weight q4_K [ 5120, 13824, 1, 1 ] llama_model_loader: - tensor 8: blk.0.ffn_down.weight q6_K [ 13824, 5120, 1, 1 ] llama_model_loader: - tensor 9: blk.0.ffn_up.weight q4_K [ 5120, 13824, 1, 1 ] llama_model_loader: - tensor 10: blk.0.attn_norm.weight f32 [ 5120, 1, 1, 1 ] llama_model_loader: - tensor 11: blk.0.ffn_norm.weight f32 [ 5120, 1, 1, 1 ] llama_model_loader: - tensor 12: blk.1.attn_q.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 13: blk.1.attn_k.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 14: blk.1.attn_v.weight q6_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 15: blk.1.attn_output.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 16: blk.1.ffn_gate.weight q4_K [ 5120, 13824, 1, 1 ] llama_model_loader: - tensor 17: blk.1.ffn_down.weight q6_K [ 13824, 5120, 1, 1 ] llama_model_loader: - tensor 18: blk.1.ffn_up.weight q4_K [ 5120, 13824, 1, 1 ] llama_model_loader: - tensor 19: blk.1.attn_norm.weight f32 [ 5120, 1, 1, 1 ] llama_model_loader: - tensor 20: blk.1.ffn_norm.weight f32 [ 5120, 1, 1, 1 ] llama_model_loader: - tensor 21: blk.2.attn_q.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 22: blk.2.attn_k.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 23: blk.2.attn_v.weight q6_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 24: blk.2.attn_output.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 25: blk.2.ffn_gate.weight q4_K [ 5120, 13824, 1, 1 ] llama_model_loader: - tensor 26: blk.2.ffn_down.weight q6_K [ 13824, 5120, 1, 1 ] llama_model_loader: - tensor 27: blk.2.ffn_up.weight q4_K [ 5120, 13824, 1, 1 ] llama_model_loader: - tensor 28: blk.2.attn_norm.weight f32 [ 5120, 1, 1, 1 ] llama_model_loader: - tensor 29: blk.2.ffn_norm.weight f32 [ 5120, 1, 1, 1 ] llama_model_loader: - tensor 30: blk.3.attn_q.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 31: blk.3.attn_k.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 32: blk.3.attn_v.weight q6_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 33: blk.3.attn_output.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 34: blk.3.ffn_gate.weight q4_K [ 5120, 13824, 1, 1 ] llama_model_loader: - tensor 35: blk.3.ffn_down.weight q6_K [ 13824, 5120, 1, 1 ] llama_model_loader: - tensor 36: blk.3.ffn_up.weight q4_K [ 5120, 13824, 1, 1 ] llama_model_loader: - tensor 37: blk.3.attn_norm.weight f32 [ 5120, 1, 1, 1 ] llama_model_loader: - tensor 38: blk.3.ffn_norm.weight f32 [ 5120, 1, 1, 1 ] llama_model_loader: - tensor 39: blk.4.attn_q.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 40: blk.4.attn_k.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 41: blk.4.attn_v.weight q6_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 42: blk.4.attn_output.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 43: blk.4.ffn_gate.weight q4_K [ 5120, 13824, 1, 1 ] llama_model_loader: - tensor 44: blk.4.ffn_down.weight q6_K [ 13824, 5120, 1, 1 ] llama_model_loader: - tensor 45: blk.4.ffn_up.weight q4_K [ 5120, 13824, 1, 1 ] llama_model_loader: - tensor 46: blk.4.attn_norm.weight f32 [ 5120, 1, 1, 1 ] llama_model_loader: - tensor 47: blk.4.ffn_norm.weight f32 [ 5120, 1, 1, 1 ] llama_model_loader: - tensor 48: blk.5.attn_q.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 49: blk.5.attn_k.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 50: blk.5.attn_v.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 51: blk.5.attn_output.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 52: blk.5.ffn_gate.weight q4_K [ 5120, 13824, 1, 1 ] llama_model_loader: - tensor 53: blk.5.ffn_down.weight q4_K [ 13824, 5120, 1, 1 ] llama_model_loader: - tensor 54: blk.5.ffn_up.weight q4_K [ 5120, 13824, 1, 1 ] llama_model_loader: - tensor 55: blk.5.attn_norm.weight f32 [ 5120, 1, 1, 1 ] llama_model_loader: - tensor 56: blk.5.ffn_norm.weight f32 [ 5120, 1, 1, 1 ] llama_model_loader: - tensor 57: blk.6.attn_q.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 58: blk.6.attn_k.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 59: blk.6.attn_v.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 60: blk.6.attn_output.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 61: blk.6.ffn_gate.weight q4_K [ 5120, 13824, 1, 1 ] llama_model_loader: - tensor 62: blk.6.ffn_down.weight q4_K [ 13824, 5120, 1, 1 ] llama_model_loader: - tensor 63: blk.6.ffn_up.weight q4_K [ 5120, 13824, 1, 1 ] llama_model_loader: - tensor 64: blk.6.attn_norm.weight f32 [ 5120, 1, 1, 1 ] llama_model_loader: - tensor 65: blk.6.ffn_norm.weight f32 [ 5120, 1, 1, 1 ] llama_model_loader: - tensor 66: blk.7.attn_q.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 67: blk.7.attn_k.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 68: blk.7.attn_v.weight q6_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 69: blk.7.attn_output.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 70: blk.7.ffn_gate.weight q4_K [ 5120, 13824, 1, 1 ] llama_model_loader: - tensor 71: blk.7.ffn_down.weight q6_K [ 13824, 5120, 1, 1 ] llama_model_loader: - tensor 72: blk.7.ffn_up.weight q4_K [ 5120, 13824, 1, 1 ] llama_model_loader: - tensor 73: blk.7.attn_norm.weight f32 [ 5120, 1, 1, 1 ] llama_model_loader: - tensor 74: blk.7.ffn_norm.weight f32 [ 5120, 1, 1, 1 ] llama_model_loader: - tensor 75: blk.8.attn_q.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 76: blk.8.attn_k.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 77: blk.8.attn_v.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 78: blk.8.attn_output.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 79: blk.8.ffn_gate.weight q4_K [ 5120, 13824, 1, 1 ] llama_model_loader: - tensor 80: blk.8.ffn_down.weight q4_K [ 13824, 5120, 1, 1 ] llama_model_loader: - tensor 81: blk.8.ffn_up.weight q4_K [ 5120, 13824, 1, 1 ] llama_model_loader: - tensor 82: blk.8.attn_norm.weight f32 [ 5120, 1, 1, 1 ] llama_model_loader: - tensor 83: blk.8.ffn_norm.weight f32 [ 5120, 1, 1, 1 ] llama_model_loader: - tensor 84: blk.9.attn_q.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 85: blk.9.attn_k.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 86: blk.9.attn_v.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 87: blk.9.attn_output.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 88: blk.9.ffn_gate.weight q4_K [ 5120, 13824, 1, 1 ] llama_model_loader: - tensor 89: blk.9.ffn_down.weight q4_K [ 13824, 5120, 1, 1 ] llama_model_loader: - tensor 90: blk.9.ffn_up.weight q4_K [ 5120, 13824, 1, 1 ] llama_model_loader: - tensor 91: blk.9.attn_norm.weight f32 [ 5120, 1, 1, 1 ] llama_model_loader: - tensor 92: blk.9.ffn_norm.weight f32 [ 5120, 1, 1, 1 ] llama_model_loader: - tensor 93: blk.10.attn_q.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 94: blk.10.attn_k.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 95: blk.10.attn_v.weight q6_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 96: blk.10.attn_output.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 97: blk.10.ffn_gate.weight q4_K [ 5120, 13824, 1, 1 ] llama_model_loader: - tensor 98: blk.10.ffn_down.weight q6_K [ 13824, 5120, 1, 1 ] llama_model_loader: - tensor 99: blk.10.ffn_up.weight q4_K [ 5120, 13824, 1, 1 ] llama_model_loader: - tensor 100: blk.10.attn_norm.weight f32 [ 5120, 1, 1, 1 ] llama_model_loader: - tensor 101: blk.10.ffn_norm.weight f32 [ 5120, 1, 1, 1 ] llama_model_loader: - tensor 102: blk.11.attn_q.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 103: blk.11.attn_k.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 104: blk.11.attn_v.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 105: blk.11.attn_output.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 106: blk.11.ffn_gate.weight q4_K [ 5120, 13824, 1, 1 ] llama_model_loader: - tensor 107: blk.11.ffn_down.weight q4_K [ 13824, 5120, 1, 1 ] llama_model_loader: - tensor 108: blk.11.ffn_up.weight q4_K [ 5120, 13824, 1, 1 ] llama_model_loader: - tensor 109: blk.11.attn_norm.weight f32 [ 5120, 1, 1, 1 ] llama_model_loader: - tensor 110: blk.11.ffn_norm.weight f32 [ 5120, 1, 1, 1 ] llama_model_loader: - tensor 111: blk.12.attn_q.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 112: blk.12.attn_k.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 113: blk.12.attn_v.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 114: blk.12.attn_output.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 115: blk.12.ffn_gate.weight q4_K [ 5120, 13824, 1, 1 ] llama_model_loader: - tensor 116: blk.12.ffn_down.weight q4_K [ 13824, 5120, 1, 1 ] llama_model_loader: - tensor 117: blk.12.ffn_up.weight q4_K [ 5120, 13824, 1, 1 ] llama_model_loader: - tensor 118: blk.12.attn_norm.weight f32 [ 5120, 1, 1, 1 ] llama_model_loader: - tensor 119: blk.12.ffn_norm.weight f32 [ 5120, 1, 1, 1 ] llama_model_loader: - tensor 120: blk.13.attn_q.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 121: blk.13.attn_k.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 122: blk.13.attn_v.weight q6_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 123: blk.13.attn_output.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 124: blk.13.ffn_gate.weight q4_K [ 5120, 13824, 1, 1 ] llama_model_loader: - tensor 125: blk.13.ffn_down.weight q6_K [ 13824, 5120, 1, 1 ] llama_model_loader: - tensor 126: blk.13.ffn_up.weight q4_K [ 5120, 13824, 1, 1 ] llama_model_loader: - tensor 127: blk.13.attn_norm.weight f32 [ 5120, 1, 1, 1 ] llama_model_loader: - tensor 128: blk.13.ffn_norm.weight f32 [ 5120, 1, 1, 1 ] llama_model_loader: - tensor 129: blk.14.attn_q.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 130: blk.14.attn_k.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 131: blk.14.attn_v.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 132: blk.14.attn_output.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 133: blk.14.ffn_gate.weight q4_K [ 5120, 13824, 1, 1 ] llama_model_loader: - tensor 134: blk.14.ffn_down.weight q4_K [ 13824, 5120, 1, 1 ] llama_model_loader: - tensor 135: blk.14.ffn_up.weight q4_K [ 5120, 13824, 1, 1 ] llama_model_loader: - tensor 136: blk.14.attn_norm.weight f32 [ 5120, 1, 1, 1 ] llama_model_loader: - tensor 137: blk.14.ffn_norm.weight f32 [ 5120, 1, 1, 1 ] llama_model_loader: - tensor 138: blk.15.attn_q.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 139: blk.15.attn_k.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 140: blk.15.attn_v.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 141: blk.15.attn_output.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 142: blk.15.ffn_gate.weight q4_K [ 5120, 13824, 1, 1 ] llama_model_loader: - tensor 143: blk.15.ffn_down.weight q4_K [ 13824, 5120, 1, 1 ] llama_model_loader: - tensor 144: blk.15.ffn_up.weight q4_K [ 5120, 13824, 1, 1 ] llama_model_loader: - tensor 145: blk.15.attn_norm.weight f32 [ 5120, 1, 1, 1 ] llama_model_loader: - tensor 146: blk.15.ffn_norm.weight f32 [ 5120, 1, 1, 1 ] llama_model_loader: - tensor 147: blk.16.attn_q.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 148: blk.16.attn_k.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 149: blk.16.attn_v.weight q6_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 150: blk.16.attn_output.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 151: blk.16.ffn_gate.weight q4_K [ 5120, 13824, 1, 1 ] llama_model_loader: - tensor 152: blk.16.ffn_down.weight q6_K [ 13824, 5120, 1, 1 ] llama_model_loader: - tensor 153: blk.16.ffn_up.weight q4_K [ 5120, 13824, 1, 1 ] llama_model_loader: - tensor 154: blk.16.attn_norm.weight f32 [ 5120, 1, 1, 1 ] llama_model_loader: - tensor 155: blk.16.ffn_norm.weight f32 [ 5120, 1, 1, 1 ] llama_model_loader: - tensor 156: blk.17.attn_q.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 157: blk.17.attn_k.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 158: blk.17.attn_v.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 159: blk.17.attn_output.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 160: blk.17.ffn_gate.weight q4_K [ 5120, 13824, 1, 1 ] llama_model_loader: - tensor 161: blk.17.ffn_down.weight q4_K [ 13824, 5120, 1, 1 ] llama_model_loader: - tensor 162: blk.17.ffn_up.weight q4_K [ 5120, 13824, 1, 1 ] llama_model_loader: - tensor 163: blk.17.attn_norm.weight f32 [ 5120, 1, 1, 1 ] llama_model_loader: - tensor 164: blk.17.ffn_norm.weight f32 [ 5120, 1, 1, 1 ] llama_model_loader: - tensor 165: blk.18.attn_q.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 166: blk.18.attn_k.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 167: blk.18.attn_v.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 168: blk.18.attn_output.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 169: blk.18.ffn_gate.weight q4_K [ 5120, 13824, 1, 1 ] llama_model_loader: - tensor 170: blk.18.ffn_down.weight q4_K [ 13824, 5120, 1, 1 ] llama_model_loader: - tensor 171: blk.18.ffn_up.weight q4_K [ 5120, 13824, 1, 1 ] llama_model_loader: - tensor 172: blk.18.attn_norm.weight f32 [ 5120, 1, 1, 1 ] llama_model_loader: - tensor 173: blk.18.ffn_norm.weight f32 [ 5120, 1, 1, 1 ] llama_model_loader: - tensor 174: blk.19.attn_q.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 175: blk.19.attn_k.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 176: blk.19.attn_v.weight q6_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 177: blk.19.attn_output.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 178: blk.19.ffn_gate.weight q4_K [ 5120, 13824, 1, 1 ] llama_model_loader: - tensor 179: blk.19.ffn_down.weight q6_K [ 13824, 5120, 1, 1 ] llama_model_loader: - tensor 180: blk.19.ffn_up.weight q4_K [ 5120, 13824, 1, 1 ] llama_model_loader: - tensor 181: blk.19.attn_norm.weight f32 [ 5120, 1, 1, 1 ] llama_model_loader: - tensor 182: blk.19.ffn_norm.weight f32 [ 5120, 1, 1, 1 ] llama_model_loader: - tensor 183: blk.20.attn_q.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 184: blk.20.attn_k.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 185: blk.20.attn_v.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 186: blk.20.attn_output.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 187: blk.20.ffn_gate.weight q4_K [ 5120, 13824, 1, 1 ] llama_model_loader: - tensor 188: blk.20.ffn_down.weight q4_K [ 13824, 5120, 1, 1 ] llama_model_loader: - tensor 189: blk.20.ffn_up.weight q4_K [ 5120, 13824, 1, 1 ] llama_model_loader: - tensor 190: blk.20.attn_norm.weight f32 [ 5120, 1, 1, 1 ] llama_model_loader: - tensor 191: blk.20.ffn_norm.weight f32 [ 5120, 1, 1, 1 ] llama_model_loader: - tensor 192: blk.21.attn_q.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 193: blk.21.attn_k.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 194: blk.21.attn_v.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 195: blk.21.attn_output.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 196: blk.21.ffn_gate.weight q4_K [ 5120, 13824, 1, 1 ] llama_model_loader: - tensor 197: blk.21.ffn_down.weight q4_K [ 13824, 5120, 1, 1 ] llama_model_loader: - tensor 198: blk.21.ffn_up.weight q4_K [ 5120, 13824, 1, 1 ] llama_model_loader: - tensor 199: blk.21.attn_norm.weight f32 [ 5120, 1, 1, 1 ] llama_model_loader: - tensor 200: blk.21.ffn_norm.weight f32 [ 5120, 1, 1, 1 ] llama_model_loader: - tensor 201: blk.22.attn_q.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 202: blk.22.attn_k.weight q4_K [ 5120, 5120, 1, 1 ] llama_model_loader: - tensor 203: blk.22.attn_v.weight q6_" 114,https://python.langchain.com/docs/use_cases/extraction,"ExtractionOn this pageExtractionUse case​Getting structured output from raw LLM generations is hard.For example, suppose you need the model output formatted with a specific schema for:Extracting a structured row to insert into a database Extracting API parametersExtracting different parts of a user query (e.g., for semantic vs keyword search)Overview​There are two primary approaches for this:Functions: Some LLMs can call functions to extract arbitrary entities from LLM responses.Parsing: Output parsers are classes that structure LLM responses. Only some LLMs support functions (e.g., OpenAI), and they are more general than parsers. Parsers extract precisely what is enumerated in a provided schema (e.g., specific attributes of a person).Functions can infer things beyond of a provided schema (e.g., attributes about a person that you did not ask for).Quickstart​OpenAI functions are one way to get started with extraction.Define a schema that specifies the properties we want to extract from the LLM output.Then, we can use create_extraction_chain to extract our desired schema using an OpenAI function call.pip install langchain openai # Set env var OPENAI_API_KEY or load from a .env file:# import dotenv# dotenv.load_dotenv()from langchain.chat_models import ChatOpenAIfrom langchain.chains import create_extraction_chain# Schemaschema = { ""properties"": { ""name"": {""type"": ""string""}, ""height"": {""type"": ""integer""}, ""hair_color"": {""type"": ""string""}, }, ""required"": [""name"", ""height""],}# Input inp = """"""Alex is 5 feet tall. Claudia is 1 feet taller Alex and jumps higher than him. Claudia is a brunette and Alex is blonde.""""""# Run chainllm = ChatOpenAI(temperature=0, model=""gpt-3.5-turbo"")chain = create_extraction_chain(schema, llm)chain.run(inp) [{'name': 'Alex', 'height': 5, 'hair_color': 'blonde'}, {'name': 'Claudia', 'height': 6, 'hair_color': 'brunette'}]Option 1: OpenAI functions​Looking under the hood​Let's dig into what is happening when we call create_extraction_chain.The LangSmith trace shows that we call the function information_extraction on the input string, inp.This information_extraction function is defined here and returns a dict.We can see the dict in the model output: { ""info"": [ { ""name"": ""Alex"", ""height"": 5, ""hair_color"": ""blonde"" }, { ""name"": ""Claudia"", ""height"": 6, ""hair_color"": ""brunette"" } ] }The create_extraction_chain then parses the raw LLM output for us using JsonKeyOutputFunctionsParser.This results in the list of JSON objects returned by the chain above:[{'name': 'Alex', 'height': 5, 'hair_color': 'blonde'}, {'name': 'Claudia', 'height': 6, 'hair_color': 'brunette'}]Multiple entity types​We can extend this further.Let's say we want to differentiate between dogs and people.We can add person_ and dog_ prefixes for each propertyschema = { ""properties"": { ""person_name"": {""type"": ""string""}, ""person_height"": {""type"": ""integer""}, ""person_hair_color"": {""type"": ""string""}, ""dog_name"": {""type"": ""string""}, ""dog_breed"": {""type"": ""string""}, }, ""required"": [""person_name"", ""person_height""],}chain = create_extraction_chain(schema, llm)inp = """"""Alex is 5 feet tall. Claudia is 1 feet taller Alex and jumps higher than him. Claudia is a brunette and Alex is blonde.Alex's dog Frosty is a labrador and likes to play hide and seek.""""""chain.run(inp) [{'person_name': 'Alex', 'person_height': 5, 'person_hair_color': 'blonde', 'dog_name': 'Frosty', 'dog_breed': 'labrador'}, {'person_name': 'Claudia', 'person_height': 6, 'person_hair_color': 'brunette'}]Unrelated entities​If we use required: [], we allow the model to return only person attributes or only dog attributes for a single entity (person or dog).schema = { ""properties"": { ""person_name"": {""type"": ""string""}, ""person_height"": {""type"": ""integer""}, ""person_hair_color"": {""type"": ""string""}, ""dog_name"": {""type"": ""string""}, ""dog_breed"": {""type"": ""string""}, }, ""required"": [],}chain = create_extraction_chain(schema, llm)inp = """"""Alex is 5 feet tall. Claudia is 1 feet taller Alex and jumps higher than him. Claudia is a brunette and Alex is blonde.Willow is a German Shepherd that likes to play with other dogs and can always be found playing with Milo, a border collie that lives close by.""""""chain.run(inp) [{'person_name': 'Alex', 'person_height': 5, 'person_hair_color': 'blonde'}, {'person_name': 'Claudia', 'person_height': 6, 'person_hair_color': 'brunette'}, {'dog_name': 'Willow', 'dog_breed': 'German Shepherd'}, {'dog_name': 'Milo', 'dog_breed': 'border collie'}]Extra information​The power of functions (relative to using parsers alone) lies in the ability to perform sematic extraction.In particular, we can ask for things that are not explictly enumerated in the schema.Suppose we want unspecified additional information about dogs. We can use add a placeholder for unstructured extraction, dog_extra_info.schema = { ""properties"": { ""person_name"": {""type"": ""string""}, ""person_height"": {""type"": ""integer""}, ""person_hair_color"": {""type"": ""string""}, ""dog_name"": {""type"": ""string""}, ""dog_breed"": {""type"": ""string""}, ""dog_extra_info"": {""type"": ""string""}, },}chain = create_extraction_chain(schema, llm)chain.run(inp) [{'person_name': 'Alex', 'person_height': 5, 'person_hair_color': 'blonde'}, {'person_name': 'Claudia', 'person_height': 6, 'person_hair_color': 'brunette'}, {'dog_name': 'Willow', 'dog_breed': 'German Shepherd', 'dog_extra_info': 'likes to play with other dogs'}, {'dog_name': 'Milo', 'dog_breed': 'border collie', 'dog_extra_info': 'lives close by'}]This gives us additional information about the dogs.Pydantic​Pydantic is a data validation and settings management library for Python. It allows you to create data classes with attributes that are automatically validated when you instantiate an object.Lets define a class with attributes annotated with types.from typing import Optional, Listfrom pydantic import BaseModel, Fieldfrom langchain.chains import create_extraction_chain_pydantic# Pydantic data classclass Properties(BaseModel): person_name: str person_height: int person_hair_color: str dog_breed: Optional[str] dog_name: Optional[str] # Extractionchain = create_extraction_chain_pydantic(pydantic_schema=Properties, llm=llm)# Run inp = """"""Alex is 5 feet tall. Claudia is 1 feet taller Alex and jumps higher than him. Claudia is a brunette and Alex is blonde.""""""chain.run(inp) [Properties(person_name='Alex', person_height=5, person_hair_color='blonde', dog_breed=None, dog_name=None), Properties(person_name='Claudia', person_height=6, person_hair_color='brunette', dog_breed=None, dog_name=None)]As we can see from the trace, we use the function information_extraction, as above, with the Pydantic schema. Option 2: Parsing​Output parsers are classes that help structure language model responses. As shown above, they are used to parse the output of the OpenAI function calls in create_extraction_chain.But, they can be used independent of functions.Pydantic​Just as a above, let's parse a generation based on a Pydantic data class.from typing import Sequence, Optionalfrom langchain.prompts import ( PromptTemplate, ChatPromptTemplate, HumanMessagePromptTemplate,)from langchain.llms import OpenAIfrom pydantic import BaseModel, Field, validatorfrom langchain.output_parsers import PydanticOutputParserclass Person(BaseModel): person_name: str person_height: int person_hair_color: str dog_breed: Optional[str] dog_name: Optional[str]class People(BaseModel): """"""Identifying information about all people in a text."""""" people: Sequence[Person] # Run query = """"""Alex is 5 feet tall. Claudia is 1 feet taller Alex and jumps higher than him. Claudia is a brunette and Alex is blonde.""""""# Set up a parser + inject instructions into the prompt template.parser = PydanticOutputParser(pydantic_object=People)# Promptprompt = PromptTemplate( template=""Answer the user query.\n{format_instructions}\n{query}\n"", input_variables=[""query""], partial_variables={""format_instructions"": parser.get_format_instructions()},)# Run_input = prompt.format_prompt(query=query)model = OpenAI(temperature=0)output = model(_input.to_string())parser.parse(output) People(people=[Person(person_name='Alex', person_height=5, person_hair_color='blonde', dog_breed=None, dog_name=None), Person(person_name='Claudia', person_height=6, person_hair_color='brunette', dog_breed=None, dog_name=None)])We can see from the LangSmith trace that we get the same output as above.We can see that we provide a two-shot prompt in order to instruct the LLM to output in our desired format.And, we need to do a bit more work:Define a class that holds multiple instances of PersonExplicty parse the output of the LLM to the Pydantic classWe can see this for other cases, too.from langchain.prompts import ( PromptTemplate, ChatPromptTemplate, HumanMessagePromptTemplate,)from langchain.llms import OpenAIfrom pydantic import BaseModel, Field, validatorfrom langchain.output_parsers import PydanticOutputParser# Define your desired data structure.class Joke(BaseModel): setup: str = Field(description=""question to set up a joke"") punchline: str = Field(description=""answer to resolve the joke"") # You can add custom validation logic easily with Pydantic. @validator(""setup"") def question_ends_with_question_mark(cls, field): if field[-1] != ""?"": raise ValueError(""Badly formed question!"") return field# And a query intented to prompt a language model to populate the data structure.joke_query = ""Tell me a joke.""# Set up a parser + inject instructions into the prompt template.parser = PydanticOutputParser(pydantic_object=Joke)# Promptprompt = PromptTemplate( template=""Answer the user query.\n{format_instructions}\n{query}\n"", input_variables=[""query""], partial_variables={""format_instructions"": parser.get_format_instructions()},)# Run_input = prompt.format_prompt(query=joke_query)model = OpenAI(temperature=0)output = model(_input.to_string())parser.parse(output) Joke(setup='Why did the chicken cross the road?', punchline='To get to the other side!')As we can see, we get an output of the Joke class, which respects our originally desired schema: 'setup' and 'punchline'.We can look at the LangSmith trace to see exactly what is going on under the hood.Going deeper​The output parser documentation includes various parser examples for specific types (e.g., lists, datetimne, enum, etc). JSONFormer offers another way for structured decoding of a subset of the JSON Schema.Kor is another library for extraction where schema and examples can be provided to the LLM.PreviousCode understandingNextSummarizationUse caseOverviewQuickstartOption 1: OpenAI functionsLooking under the hoodMultiple entity typesUnrelated entitiesExtra informationPydanticOption 2: ParsingPydanticGoing deeper" 115,https://python.langchain.com/docs/use_cases/summarization,"SummarizationOn this pageSummarizationUse case​Suppose you have a set of documents (PDFs, Notion pages, customer questions, etc.) and you want to summarize the content. LLMs are a great tool for this given their proficiency in understanding and synthesizing text.In this walkthrough we'll go over how to perform document summarization using LLMs.Overview​A central question for building a summarizer is how to pass your documents into the LLM's context window. Two common approaches for this are:Stuff: Simply ""stuff"" all your documents into a single prompt. This is the simplest approach (see here for more on the StuffDocumentsChains, which is used for this method).Map-reduce: Summarize each document on it's own in a ""map"" step and then ""reduce"" the summaries into a final summary (see here for more on the MapReduceDocumentsChain, which is used for this method).Quickstart​To give you a sneak preview, either pipeline can be wrapped in a single object: load_summarize_chain. Suppose we want to summarize a blog post. We can create this in a few lines of code.First set environment variables and install packages:pip install openai tiktoken chromadb langchain# Set env var OPENAI_API_KEY or load from a .env file# import dotenv# dotenv.load_dotenv()We can use chain_type=""stuff"", especially if using larger context window models such as:16k token OpenAI gpt-3.5-turbo-16k 100k token Anthropic Claude-2We can also supply chain_type=""map_reduce"" or chain_type=""refine"" (read more here).from langchain.chat_models import ChatOpenAIfrom langchain.document_loaders import WebBaseLoaderfrom langchain.chains.summarize import load_summarize_chainloader = WebBaseLoader(""https://lilianweng.github.io/posts/2023-06-23-agent/"")docs = loader.load()llm = ChatOpenAI(temperature=0, model_name=""gpt-3.5-turbo-16k"")chain = load_summarize_chain(llm, chain_type=""stuff"")chain.run(docs) 'The article discusses the concept of building autonomous agents powered by large language models (LLMs). It explores the components of such agents, including planning, memory, and tool use. The article provides case studies and proof-of-concept examples of LLM-powered agents in various domains. It also highlights the challenges and limitations of using LLMs in agent systems.'Option 1. Stuff​When we use load_summarize_chain with chain_type=""stuff"", we will use the StuffDocumentsChain.The chain will take a list of documents, inserts them all into a prompt, and passes that prompt to an LLM:from langchain.chains.llm import LLMChainfrom langchain.prompts import PromptTemplatefrom langchain.chains.combine_documents.stuff import StuffDocumentsChain# Define promptprompt_template = """"""Write a concise summary of the following:""{text}""CONCISE SUMMARY:""""""prompt = PromptTemplate.from_template(prompt_template)# Define LLM chainllm = ChatOpenAI(temperature=0, model_name=""gpt-3.5-turbo-16k"")llm_chain = LLMChain(llm=llm, prompt=prompt)# Define StuffDocumentsChainstuff_chain = StuffDocumentsChain( llm_chain=llm_chain, document_variable_name=""text"")docs = loader.load()print(stuff_chain.run(docs)) The article discusses the concept of building autonomous agents powered by large language models (LLMs). It explores the components of such agents, including planning, memory, and tool use. The article provides case studies and examples of proof-of-concept demos, highlighting the challenges and limitations of LLM-powered agents. It also includes references to related research papers and provides a citation for the article.Great! We can see that we reproduce the earlier result using the load_summarize_chain.Go deeper​You can easily customize the prompt. You can easily try different LLMs, (e.g., Claude) via the llm parameter.Option 2. Map-Reduce​Let's unpack the map reduce approach. For this, we'll first map each document to an individual summary using an LLMChain. Then we'll use a ReduceDocumentsChain to combine those summaries into a single global summary.First, we specfy the LLMChain to use for mapping each document to an individual summary:from langchain.chains.mapreduce import MapReduceChainfrom langchain.text_splitter import CharacterTextSplitterfrom langchain.chains import ReduceDocumentsChain, MapReduceDocumentsChainllm = ChatOpenAI(temperature=0)# Mapmap_template = """"""The following is a set of documents{docs}Based on this list of docs, please identify the main themes Helpful Answer:""""""map_prompt = PromptTemplate.from_template(map_template)map_chain = LLMChain(llm=llm, prompt=map_prompt)We can also use the Prompt Hub to store and fetch prompts.This will work with your LangSmith API key.For example, see the map prompt here.from langchain import hubmap_prompt = hub.pull(""rlm/map-prompt"")map_chain = LLMChain(llm=llm, prompt=map_prompt)The ReduceDocumentsChain handles taking the document mapping results and reducing them into a single output. It wraps a generic CombineDocumentsChain (like StuffDocumentsChain) but adds the ability to collapse documents before passing it to the CombineDocumentsChain if their cumulative size exceeds token_max. In this example, we can actually re-use our chain for combining our docs to also collapse our docs.So if the cumulative number of tokens in our mapped documents exceeds 4000 tokens, then we'll recursively pass in the documents in batches of < 4000 tokens to our StuffDocumentsChain to create batched summaries. And once those batched summaries are cumulatively less than 4000 tokens, we'll pass them all one last time to the StuffDocumentsChain to create the final summary.# Reducereduce_template = """"""The following is set of summaries:{doc_summaries}Take these and distill it into a final, consolidated summary of the main themes. Helpful Answer:""""""reduce_prompt = PromptTemplate.from_template(reduce_template)# Note we can also get this from the prompt hub, as noted abovereduce_prompt = hub.pull(""rlm/map-prompt"")# Run chainreduce_chain = LLMChain(llm=llm, prompt=reduce_prompt)# Takes a list of documents, combines them into a single string, and passes this to an LLMChaincombine_documents_chain = StuffDocumentsChain( llm_chain=reduce_chain, document_variable_name=""doc_summaries"")# Combines and iteravely reduces the mapped documentsreduce_documents_chain = ReduceDocumentsChain( # This is final chain that is called. combine_documents_chain=combine_documents_chain, # If documents exceed context for `StuffDocumentsChain` collapse_documents_chain=combine_documents_chain, # The maximum number of tokens to group documents into. token_max=4000,)Combining our map and reduce chains into one:# Combining documents by mapping a chain over them, then combining resultsmap_reduce_chain = MapReduceDocumentsChain( # Map chain llm_chain=map_chain, # Reduce chain reduce_documents_chain=reduce_documents_chain, # The variable name in the llm_chain to put the documents in document_variable_name=""docs"", # Return the results of the map steps in the output return_intermediate_steps=False,)text_splitter = CharacterTextSplitter.from_tiktoken_encoder( chunk_size=1000, chunk_overlap=0)split_docs = text_splitter.split_documents(docs) Created a chunk of size 1003, which is longer than the specified 1000print(map_reduce_chain.run(split_docs)) The main themes identified in the provided set of documents are: 1. LLM-powered autonomous agent systems: The documents discuss the concept of building autonomous agents with large language models (LLMs) as the core controller. They explore the potential of LLMs beyond content generation and present them as powerful problem solvers. 2. Components of the agent system: The documents outline the key components of LLM-powered agent systems, including planning, memory, and tool use. Each component is described in detail, highlighting its role in enhancing the agent's capabilities. 3. Planning and task decomposition: The planning component focuses on task decomposition and self-reflection. The agent breaks down complex tasks into smaller subgoals and learns from past actions to improve future results. 4. Memory and learning: The memory component includes short-term memory for in-context learning and long-term memory for retaining and recalling information over extended periods. The use of external vector stores for fast retrieval is also mentioned. 5. Tool use and external APIs: The agent learns to utilize external APIs for accessing additional information, code execution, and proprietary sources. This enhances the agent's knowledge and problem-solving abilities. 6. Case studies and proof-of-concept examples: The documents provide case studies and examples to demonstrate the application of LLM-powered agents in scientific discovery, generative simulations, and other domains. These examples serve as proof-of-concept for the effectiveness of the agent system. 7. Challenges and limitations: The documents mention challenges associated with building LLM-powered autonomous agents, such as the limitations of finite context length, difficulties in long-term planning, and reliability issues with natural language interfaces. 8. Citation and references: The documents include a citation and reference section for acknowledging the sources and inspirations for the concepts discussed. Overall, the main themes revolve around the development and capabilities of LLM-powered autonomous agent systems, including their components, planning and task decomposition, memory and learning mechanisms, tool use and external APIs, case studies and proof-of-concept examples, challenges and limitations, and the importance of proper citation and references.Go deeper​Customization As shown above, you can customize the LLMs and prompts for map and reduce stages.Real-world use-caseSee this blog post case-study on analyzing user interactions (questions about LangChain documentation)! The blog post and associated repo also introduce clustering as a means of summarization.This opens up a third path beyond the stuff or map-reduce approaches that is worth considering.Option 3. Refine​Refine is similar to map-reduce:The refine documents chain constructs a response by looping over the input documents and iteratively updating its answer. For each document, it passes all non-document inputs, the current document, and the latest intermediate answer to an LLM chain to get a new answer.This can be easily run with the chain_type=""refine"" specified.chain = load_summarize_chain(llm, chain_type=""refine"")chain.run(split_docs) 'The GPT-Engineer project aims to create a repository of code for specific tasks specified in natural language. It involves breaking down tasks into smaller components and seeking clarification from the user when needed. The project emphasizes the importance of implementing every detail of the architecture as code and provides guidelines for file organization, code structure, and dependencies. However, there are challenges in long-term planning and task decomposition, as well as the reliability of the natural language interface. The system has limited communication bandwidth and struggles to adjust plans when faced with unexpected errors. The reliability of model outputs is questionable, as formatting errors and rebellious behavior can occur. The conversation also includes instructions for writing the code, including laying out the core classes, functions, and methods, and providing the code in a markdown code block format. The user is reminded to ensure that the code is fully functional and follows best practices for file naming, imports, and types. The project is powered by LLM (Large Language Models) and incorporates prompting techniques from various research papers.'It's also possible to supply a prompt and return intermediate steps.prompt_template = """"""Write a concise summary of the following:{text}CONCISE SUMMARY:""""""prompt = PromptTemplate.from_template(prompt_template)refine_template = ( ""Your job is to produce a final summary\n"" ""We have provided an existing summary up to a certain point: {existing_answer}\n"" ""We have the opportunity to refine the existing summary"" ""(only if needed) with some more context below.\n"" ""------------\n"" ""{text}\n"" ""------------\n"" ""Given the new context, refine the original summary in Italian"" ""If the context isn't useful, return the original summary."")refine_prompt = PromptTemplate.from_template(refine_template)chain = load_summarize_chain( llm=llm, chain_type=""refine"", question_prompt=prompt, refine_prompt=refine_prompt, return_intermediate_steps=True, input_key=""input_documents"", output_key=""output_text"",)result = chain({""input_documents"": split_docs}, return_only_outputs=True)print(result[""output_text""]) L'articolo discute il concetto di costruire agenti autonomi utilizzando LLM (large language model) come controller principale. Esplora i diversi componenti di un sistema di agenti alimentato da LLM, inclusa la pianificazione, la memoria e l'uso di strumenti. Dimostrazioni di concetto come AutoGPT mostrano la possibilità di creare agenti autonomi con LLM come controller principale. Approcci come Chain of Thought, Tree of Thoughts, LLM+P, ReAct e Reflexion consentono agli agenti autonomi di pianificare, riflettere su se stessi e migliorare iterativamente. Tuttavia, ci sono sfide legate alla lunghezza del contesto, alla pianificazione a lungo termine e alla decomposizione delle attività. Inoltre, l'affidabilità dell'interfaccia di linguaggio naturale tra LLM e componenti esterni come la memoria e gli strumenti è incerta. Nonostante ciò, l'uso di LLM come router per indirizzare le richieste ai moduli esperti più adatti è stato proposto come architettura neuro-simbolica per agenti autonomi nel sistema MRKL. L'articolo fa riferimento a diverse pubblicazioni che approfondiscono l'argomento, tra cui Chain of Thought, Tree of Thoughts, LLM+P, ReAct, Reflexion, e MRKL Systems.print(""\n\n"".join(result[""intermediate_steps""][:3])) This article discusses the concept of building autonomous agents using LLM (large language model) as the core controller. The article explores the different components of an LLM-powered agent system, including planning, memory, and tool use. It also provides examples of proof-of-concept demos and highlights the potential of LLM as a general problem solver. Questo articolo discute del concetto di costruire agenti autonomi utilizzando LLM (large language model) come controller principale. L'articolo esplora i diversi componenti di un sistema di agenti alimentato da LLM, inclusa la pianificazione, la memoria e l'uso degli strumenti. Vengono anche forniti esempi di dimostrazioni di proof-of-concept e si evidenzia il potenziale di LLM come risolutore generale di problemi. Inoltre, vengono presentati approcci come Chain of Thought, Tree of Thoughts, LLM+P, ReAct e Reflexion che consentono agli agenti autonomi di pianificare, riflettere su se stessi e migliorare iterativamente. Questo articolo discute del concetto di costruire agenti autonomi utilizzando LLM (large language model) come controller principale. L'articolo esplora i diversi componenti di un sistema di agenti alimentato da LLM, inclusa la pianificazione, la memoria e l'uso degli strumenti. Vengono anche forniti esempi di dimostrazioni di proof-of-concept e si evidenzia il potenziale di LLM come risolutore generale di problemi. Inoltre, vengono presentati approcci come Chain of Thought, Tree of Thoughts, LLM+P, ReAct e Reflexion che consentono agli agenti autonomi di pianificare, riflettere su se stessi e migliorare iterativamente. Il nuovo contesto riguarda l'approccio Chain of Hindsight (CoH) che permette al modello di migliorare autonomamente i propri output attraverso un processo di apprendimento supervisionato. Viene anche presentato l'approccio Algorithm Distillation (AD) che applica lo stesso concetto alle traiettorie di apprendimento per compiti di reinforcement learning.PreviousExtractionNextTaggingUse caseOverviewQuickstartOption 1. StuffGo deeperOption 2. Map-ReduceGo deeperOption 3. Refine" 116,https://python.langchain.com/docs/use_cases/tagging,"TaggingOn this pageTaggingUse case​Tagging means labeling a document with classes such as:sentimentlanguagestyle (formal, informal etc.)covered topicspolitical tendencyOverview​Tagging has a few components:function: Like extraction, tagging uses functions to specify how the model should tag a documentschema: defines how we want to tag the documentQuickstart​Let's see a very straightforward example of how we can use OpenAI functions for tagging in LangChain.pip install langchain openai # Set env var OPENAI_API_KEY or load from a .env file:# import dotenv# dotenv.load_dotenv()from langchain.chat_models import ChatOpenAIfrom langchain.prompts import ChatPromptTemplatefrom langchain.chains import create_tagging_chain, create_tagging_chain_pydanticWe specify a few properties with their expected type in our schema.# Schemaschema = { ""properties"": { ""sentiment"": {""type"": ""string""}, ""aggressiveness"": {""type"": ""integer""}, ""language"": {""type"": ""string""}, }}# LLMllm = ChatOpenAI(temperature=0, model=""gpt-3.5-turbo-0613"")chain = create_tagging_chain(schema, llm)inp = ""Estoy increiblemente contento de haberte conocido! Creo que seremos muy buenos amigos!""chain.run(inp) {'sentiment': 'positive', 'language': 'Spanish'}inp = ""Estoy muy enojado con vos! Te voy a dar tu merecido!""chain.run(inp) {'sentiment': 'enojado', 'aggressiveness': 1, 'language': 'es'}As we can see in the examples, it correctly interprets what we want.The results vary so that we get, for example, sentiments in different languages ('positive', 'enojado' etc.).We will see how to control these results in the next section.Finer control​Careful schema definition gives us more control over the model's output. Specifically, we can define:possible values for each propertydescription to make sure that the model understands the propertyrequired properties to be returnedHere is an example of how we can use _enum_, _description_, and _required_ to control for each of the previously mentioned aspects:schema = { ""properties"": { ""aggressiveness"": { ""type"": ""integer"", ""enum"": [1, 2, 3, 4, 5], ""description"": ""describes how aggressive the statement is, the higher the number the more aggressive"", }, ""language"": { ""type"": ""string"", ""enum"": [""spanish"", ""english"", ""french"", ""german"", ""italian""], }, }, ""required"": [""language"", ""sentiment"", ""aggressiveness""],}chain = create_tagging_chain(schema, llm)Now the answers are much better!inp = ""Estoy increiblemente contento de haberte conocido! Creo que seremos muy buenos amigos!""chain.run(inp) {'aggressiveness': 0, 'language': 'spanish'}inp = ""Estoy muy enojado con vos! Te voy a dar tu merecido!""chain.run(inp) {'aggressiveness': 5, 'language': 'spanish'}inp = ""Weather is ok here, I can go outside without much more than a coat""chain.run(inp) {'aggressiveness': 0, 'language': 'english'}The LangSmith trace lets us peek under the hood:As with extraction, we call the information_extraction function here on the input string.This OpenAI funtion extraction information based upon the provided schema.Pydantic​We can also use a Pydantic schema to specify the required properties and types. We can also send other arguments, such as enum or description, to each field.This lets us specify our schema in the same manner that we would a new class or function in Python with purely Pythonic types.from enum import Enumfrom pydantic import BaseModel, Fieldclass Tags(BaseModel): sentiment: str = Field(..., enum=[""happy"", ""neutral"", ""sad""]) aggressiveness: int = Field( ..., description=""describes how aggressive the statement is, the higher the number the more aggressive"", enum=[1, 2, 3, 4, 5], ) language: str = Field( ..., enum=[""spanish"", ""english"", ""french"", ""german"", ""italian""] )chain = create_tagging_chain_pydantic(Tags, llm)inp = ""Estoy muy enojado con vos! Te voy a dar tu merecido!""res = chain.run(inp)res Tags(sentiment='sad', aggressiveness=5, language='spanish')Going deeper​You can use the metadata tagger document transformer to extract metadata from a LangChain Document. This covers the same basic functionality as the tagging chain, only applied to a LangChain Document.PreviousSummarizationNextWeb scrapingUse caseOverviewQuickstartFiner controlPydanticGoing deeper" 117,https://python.langchain.com/docs/use_cases/web_scraping,"Web scrapingOn this pageWeb scrapingUse case​Web research is one of the killer LLM applications:Users have highlighted it as one of his top desired AI tools. OSS repos like gpt-researcher are growing in popularity. Overview​Gathering content from the web has a few components:Search: Query to url (e.g., using GoogleSearchAPIWrapper).Loading: Url to HTML (e.g., using AsyncHtmlLoader, AsyncChromiumLoader, etc).Transforming: HTML to formatted text (e.g., using HTML2Text or Beautiful Soup).Quickstart​pip install -q openai langchain playwright beautifulsoup4playwright install# Set env var OPENAI_API_KEY or load from a .env file:# import dotenv# dotenv.load_dotenv()Scraping HTML content using a headless instance of Chromium.The async nature of the scraping process is handled using Python's asyncio library.The actual interaction with the web pages is handled by Playwright.from langchain.document_loaders import AsyncChromiumLoaderfrom langchain.document_transformers import BeautifulSoupTransformer# Load HTMLloader = AsyncChromiumLoader([""https://www.wsj.com""])html = loader.load()Scrape text content tags such as

,

  • ,
  • : The list item tag. It is used within ordered (
      ) and unordered (