Managing prompt size | 🦜🔗 Langchain

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


Managing prompt size | 🦜🔗 Langchain

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


Managing prompt size | 🦜🔗 Langchain

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


Managing prompt size | 🦜🔗 Langchain

the National Wildlife Federation as one of the country's Great Waters and has been called the "Lifeblood of the Northeast" by American Rivers. Its watershed drains an area of 13,539 square miles (35,070 km2) and provides drinking water for 17 million people, including half of New York City via the Delaware Aqueduct.     The Delaware River has two branches that rise in the Catskill Mountains of New York: the West Branch at Mount Jefferson in Jefferson, Schoharie County, and the East Branch at Grand Gorge, Delaware County. The branches merge to form the main Delaware River at Hancock, New York. Flowing south, the river remains relatively undeveloped, with 152 miles (245 km) protected as the Upper, Middle, and Lower Delaware National Scenic Rivers. At Trenton, New Jersey, the Delaware becomes tidal, navigable, and significantly more industrial. This section forms the backbone of the Delaware Valley metropolitan area, serving the port cities of Philadelphia, Camden, New Jersey, and Wilmington, Delaware. The river flows into Delaware Bay at Liston Point, 48 miles (77 km) upstream of the bay's outlet to the Atlantic Ocean between Cape May and Cape Henlopen.     Before the arrival of European settlers, the river was the homeland of the Lenape native people. They called the river Lenapewihittuk, or Lenape River, and Kithanne, meaning the largest river in this part of the country.In 1609, the river was visited by a Dutch East India Company expedition led by Henry Hudson. Hudson, an English navigator, was hired to find a western route to Cathay (China), but his encounters set the stage for Dutch colonization of North America in the 17th century. Early Dutch and Swedish settlements were established along the lower section of the river and Delaware Bay. Both colonial powers called the river the South River (Zuidrivier), compared to the Hudson River, which was known as the North River. After the English expelled the Dutch and took control of the New Netherland colony in 1664, the river was renamed Delaware after Sir Thomas West, 3rd Baron De La Warr, an English nobleman and the Virginia colony's first royal governor who defended the colony during the First Anglo-Powhatan War.          Page: Lenape     Summary: The Lenape (English: , , ; Lenape languages: [lenaːpe]), also called the Lenni Lenape and Delaware people, are an indigenous people of the Northeastern Woodlands, who live in the United States and Canada.The Lenape's historical territory included present-day northeastern Delaware, all of New Jersey, the eastern Pennsylvania regions of the Lehigh Valley and Northeastern Pennsylvania, and New York Bay, western Long Island, and the lower Hudson Valley in New York state. Today they are based in Oklahoma, Wisconsin, and Ontario.     During the last decades of the 18th century, European settlers and the effects of the American Revolutionary War displaced most Lenape from their homelands and pushed them north and west. In the 1860s, under the Indian removal policy, the U.S. federal government relocated most Lenape remaining in the Eastern United States to the Indian Territory and surrounding regions. Lenape people currently belong to the Delaware Nation and Delaware Tribe of Indians in Oklahoma, the Stockbridge–Munsee Community in Wisconsin, and the Munsee-Delaware Nation, Moravian of the Thames First Nation, and Delaware of Six Nations in Ontario.          Page: University of Delaware     Summary: The University of Delaware (colloquially known as UD or Delaware) is a privately governed, state-assisted land-grant research university located in Newark, Delaware. UD is the largest university in Delaware. It offers three associate's programs, 148 bachelor's programs, 121 master's programs (with 13 joint degrees), and 55 doctoral programs across its eight colleges. The main campus is in Newark, with satellite campuses in Dover, Wilmington, Lewes, and Georgetown. It is considered a large institution with approximately 18,200 undergraduate and 4,200 graduate students. It is a privately governed university which receives public funding for being a land-grant, sea-grant, and space-grant state-supported research institution.UD is classified among "R1: Doctoral Universities – Very high research activity". According to the National Science Foundation, UD spent $186 million on research and development in 2018, ranking it 119th in the nation.  It is recognized with the Community Engagement Classification by the Carnegie Foundation for the Advancement of Teaching.UD students, alumni, and sports teams are known as the "Fightin' Blue Hens", more commonly shortened to "Blue Hens", and the school colors are Delaware blue and gold. UD sponsors 21 men's and women's NCAA Division-I sports teams and have competed in the Colonial Athletic Association (CAA) since 2001.     Invoking: `Wikipedia` with `Delaware Blue Hen`               Page: Delaware Blue Hen     Summary: The Delaware Blue Hen or Blue Hen of Delaware is a blue strain of American gamecock. Under the name Blue Hen Chicken it is the official bird of the State of Delaware. It is the emblem or mascot of several institutions in the state, among them the sports teams of the University of Delaware.          Page: Delaware Fightin' Blue Hens football     Summary: The Delaware Fightin' Blue Hens football team represents the University of Delaware (UD) in National Collegiate Athletic Association (NCAA) Division I Football Championship Subdivision (FCS) college football as a member of CAA Football, the technically separate football arm of UD's full-time home of the Coastal Athletic Association. The team is currently led by head coach Ryan Carty  and plays on Tubby Raymond Field at 22,000-seat Delaware Stadium located in Newark, Delaware. The Fightin' Blue Hens have won six national titles in their 117-year history – 1946 (AP College Division), 1963 (UPI College Division), 1971 (AP/UPI College Division), 1972 (AP/UPI College Division), 1979 (Division II), and 2003 (Divisio


Managing prompt size | 🦜🔗 Langchain

n I-AA). They returned to the FCS National Championship game in 2007 and 2010.     The program has produced NFL quarterbacks Rich Gannon, Joe Flacco, Jeff Komlo, Pat Devlin and Scott Brunner.     The Blue Hens are recognized as a perennial power in FCS football and Delaware was the only FCS program to average more than 20,000 fans per regular-season home game for each season from 1999 to 2010.          Page: Delaware Fightin' Blue Hens     Summary: The Delaware Fightin' Blue Hens are the athletic teams of the University of Delaware of Newark, Delaware, in the United States. The Blue Hens compete in the Football Championship Subdivision (FCS) of Division I of the National Collegiate Athletic Association (NCAA) as members of the Coastal Athletic Association.          Page: Delaware Fightin' Blue Hens men's basketball     Summary: The Delaware Fightin' Blue Hens men's basketball team is the basketball team that represents University of Delaware in Newark, Delaware. The school's team currently competes in the National Collegiate Athletic Association (NCAA) at the Division I level as a member of the Colonial Athletic Association since 2001. Home games are played at the Acierno Arena at the Bob Carpenter Center.     The Blue Hens are coached by Martin Ingelsby who has been the head coach since 2016.          Page: University of Delaware     Summary: The University of Delaware (colloquially known as UD or Delaware) is a privately governed, state-assisted land-grant research university located in Newark, Delaware. UD is the largest university in Delaware. It offers three associate's programs, 148 bachelor's programs, 121 master's programs (with 13 joint degrees), and 55 doctoral programs across its eight colleges. The main campus is in Newark, with satellite campuses in Dover, Wilmington, Lewes, and Georgetown. It is considered a large institution with approximately 18,200 undergraduate and 4,200 graduate students. It is a privately governed university which receives public funding for being a land-grant, sea-grant, and space-grant state-supported research institution.UD is classified among "R1: Doctoral Universities – Very high research activity". According to the National Science Foundation, UD spent $186 million on research and development in 2018, ranking it 119th in the nation.  It is recognized with the Community Engagement Classification by the Carnegie Foundation for the Advancement of Teaching.UD students, alumni, and sports teams are known as the "Fightin' Blue Hens", more commonly shortened to "Blue Hens", and the school colors are Delaware blue and gold. UD sponsors 21 men's and women's NCAA Division-I sports teams and have competed in the Colonial Athletic Association (CAA) since 2001.The current US president is Joe Biden. His home state is Delaware. The state bird of Delaware is the Delaware Blue Hen. Its scientific name is Gallus gallus domesticus.          > Finished chain.          {'input': "Who is the current US president? What's their home state? What's their home state's bird? What's that bird's scientific name?",      'output': 'The current US president is Joe Biden. His home state is Delaware. The state bird of Delaware is the Delaware Blue Hen. Its scientific name is Gallus gallus domesticus.'}  [LANGSMITH TRACE](HTTPS://SMITH.LANGCHAIN.COM/PUBLIC/3B27D47F-E4DF-4AFB-81B1-0F88B80CA97E/R)


RAG | 🦜🔗 Langchain

Main:  Skip to main content  🦜🔗 LangChain  Search CTRLK LangChain Expression LanguageCookbookRAG On this page RAG Let's look at adding in a retrieval step to a prompt and LLM, which adds up to a "retrieval-augmented generation" chain  pip install langchain openai faiss-cpu tiktoken  from operator import itemgetter  from langchain.chat_models import ChatOpenAI from langchain.embeddings import OpenAIEmbeddings from langchain.prompts import ChatPromptTemplate from langchain.schema.output_parser import StrOutputParser from langchain.schema.runnable import RunnableLambda, RunnablePassthrough from langchain.vectorstores import FAISS  vectorstore = 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_history  from langchain.schema import format_document from langchain.schema.runnable import RunnableParallel  from 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 List, Tuple   def _format_chat_history(chat_history: List[Tuple[str, str]]) -> str:     # chat history is of format:     # [     #   (human_message_str, ai_message_str),     #   ...     # ]     # see below for an example of how it's invoked     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 = RunnableParallel(     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 itemgetter  from langchain.memory import ConversationBufferMemory  memory = 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 object loaded_memory = RunnablePassthrough.assign(     chat_history=RunnableLambda(memory.load_memory_variables) | itemgetter("history"), ) # Now we calculate the standalone question standalone_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 documents retrieved_documents = {     "docs": itemgetter("standalone_question") | retriever,     "question": lambda x: x["standalone_question"], } # Now we construct the inputs for the final prompt final_inputs = {     "context": lambda x: _combine_documents(x["docs"]),     "question": itemgetter("question"), } # And finally, we do the part that returns the answers answer = {     "answer": final_inputs | ANSWER_PROMPT | ChatOpenAI(),     "docs": itemgetter("docs"), } # And now we put it all together! final_chain = loaded_memory | standalone_question | retrieved_documents | answer  inputs = {"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


RAG | 🦜🔗 Langchain

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 yourself memory.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)]}  Previous Prompt + LLM Next Multiple chains Community Discord Twitter GitHub Python JS/TS More Homepage Blog Copyright © 2023 LangChain, Inc.


Querying a SQL DB | 🦜🔗 Langchain

Main:  We can replicate our SQLDatabaseChain with Runnables.  from langchain.prompts import ChatPromptTemplate  template = """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 SQLDatabase  We'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/](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 ChatOpenAI from langchain.schema.output_parser import StrOutputParser from langchain.schema.runnable import RunnablePassthrough  model = 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)


Using tools | 🦜🔗 Langchain

Main:  #Using tools You can use any Tools with Runnables easily.  pip install duckduckgo-search  from langchain.chat_models import ChatOpenAI from langchain.prompts import ChatPromptTemplate from langchain.schema.output_parser import StrOutputParser from langchain.tools import DuckDuckGoSearchRun  search = 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() | search  chain.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 ...'


Get started | 🦜🔗 Langchain

Main:  On this page LCEL makes it easy to build complex chains from basic components, and supports out of the box functionality such as streaming, parallelism, and logging.  ##Basic example: prompt + model + output parser[](#basic-example-prompt--model--output-parser) The most basic and common use case is chaining a prompt template and a model together. To see how this works, let's create a chain that takes a topic and generates a joke:  from langchain.chat_models import ChatOpenAI from langchain.prompts import ChatPromptTemplate from langchain.schema.output_parser import StrOutputParser  prompt = ChatPromptTemplate.from_template("tell me a short joke about {topic}") model = ChatOpenAI() output_parser = StrOutputParser()  chain = prompt | model | output_parser  chain.invoke({"topic": "ice cream"})      "Why did the ice cream go to therapy?\n\nBecause it had too many toppings and couldn't find its cone-fidence!"  Notice this line of this code, where we piece together then different components into a single chain using LCEL:  chain = prompt | model | output_parser  The | symbol is similar to a [unix pipe operator](https://en.wikipedia.org/wiki/Pipeline_(Unix)), which chains together the different components feeds the output from one component as input into the next component.  In this chain the user input is passed to the prompt template, then the prompt template output is passed to the model, then the model output is passed to the output parser. Let's take a look at each component individually to really understand what's going on.  ###1. Prompt[](#1-prompt) prompt is a BasePromptTemplate, which means it takes in a dictionary of template variables and produces a PromptValue. A PromptValue is a wrapper around a completed prompt that can be passed to either an LLM (which takes a string as input) or ChatModel (which takes a sequence of messages as input). It can work with either language model type because it defines logic both for producing BaseMessages and for producing a string.  prompt_value = prompt.invoke({"topic": "ice cream"}) prompt_value      ChatPromptValue(messages=[HumanMessage(content='tell me a short joke about ice cream')])  prompt_value.to_messages()      [HumanMessage(content='tell me a short joke about ice cream')]  prompt_value.to_string()      'Human: tell me a short joke about ice cream'  ###2. Model[](#2-model) The PromptValue is then passed to model. In this case our model is a ChatModel, meaning it will output a BaseMessage.  message = model.invoke(prompt_value) message      AIMessage(content="Why did the ice cream go to therapy? \n\nBecause it had too many toppings and couldn't find its cone-fidence!")  If our model was an LLM, it would output a string.  from langchain.llms import OpenAI  llm = OpenAI(model="gpt-3.5-turbo-instruct") llm.invoke(prompt_value)      '\n\nRobot: Why did the ice cream go to therapy? Because it had a rocky road.'  ###3. Output parser[](#3-output-parser) And lastly we pass our model output to the output_parser, which is a BaseOutputParser meaning it takes either a string or a BaseMessage as input. The StrOutputParser specifically simple converts any input into a string.  output_parser.invoke(message)      "Why did the ice cream go to therapy? \n\nBecause it had too many toppings and couldn't find its cone-fidence!"  ###4. Entire Pipeline[](#4-entire-pipeline) To follow the steps along:  - We pass in user input on the desired topic as {"topic": "ice cream"} - The prompt component takes the user input, which is then used to construct a PromptValue after using the topic to construct the prompt. - The model component takes the generated prompt, and passes into the OpenAI LLM model for evaluation. The generated output from the model is a ChatMessage object. - Finally, the output_parser component takes in a ChatMessage, and transforms this into a Python string, which is returned from the invoke method. INFO Note that if you’re curious about the output of any components, you can always test out a smaller version of the chain such as prompt or prompt | model to see the intermediate results:  input = {"topic": "ice cream"}  prompt.invoke(input) # > ChatPromptValue(messages=[HumanMessage(content='tell me a short joke about ice cream')])  (prompt | model).invoke(input) # > AIMessage(content="Why did the ice cream go to therapy?\nBecause it had too many toppings and couldn't cone-trol itself!")  ##RAG Search Example[](#rag-search-example) For our next example, we want to run a retrieval-augmented generation chain to add some context when responding to questions.  # Requires: # pip install langchain docarray  from langchain.chat_models import ChatOpenAI from langchain.embeddings import OpenAIEmbeddings from langchain.prompts import ChatPromptTemplate from langchain.schema.output_parser import StrOutputParser from langchain.schema.runnable import RunnableParallel, RunnablePassthrough from langchain.vectorstores import DocArrayInMemorySearch  vectorstore = DocArrayInMemorySearch.from_texts(     ["harrison worked at kensho", "bears like to eat honey"],     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() output_parser = StrOutputParser()  setup_and_retrieval = RunnableParallel(     {"context": retriever, "question": RunnablePassthrough()} ) chain = setup_and_retrieval | prompt | model | output_parser  chain.invoke("where did harrison work?")  In this case, the composed chain is:  chain = setup_and_retrieval | prompt | model | output_parser  To explain this, we first can see that the prompt template above takes in context and question as values to be substituted in the prompt. Before building the prompt template, we want to retrieve relevant documents to the search and include them as part of the context.  As a preliminary step, we’ve setup the retrieve


Get started | 🦜🔗 Langchain

r using an in memory store, which can retrieve documents based on a query. This is a runnable component as well that can be chained together with other components, but you can also try to run it separately:  retriever.invoke("where did harrison work?")  We then use the RunnableParallel to prepare the expected inputs into the prompt by using the entries for the retrieved documents as well as the original user question, using the retriever for document search, and RunnablePassthrough to pass the user’s question:  setup_and_retrieval = RunnableParallel(     {"context": retriever, "question": RunnablePassthrough()} )  To review, the complete chain is:  setup_and_retrieval = RunnableParallel(     {"context": retriever, "question": RunnablePassthrough()} ) chain = setup_and_retrieval | prompt | model | output_parser  With the flow being:  - The first steps create a RunnableParallel object with two entries. The first entry, context will include the document results fetched by the retriever. The second entry, question will contain the user’s original question. To pass on the question, we use RunnablePassthrough to copy this entry. - Feed the dictionary from the step above to the prompt component. It then takes the user input which is question as well as the retrieved document which is context to construct a prompt and output a PromptValue. - The model component takes the generated prompt, and passes into the OpenAI LLM model for evaluation. The generated output from the model is a ChatMessage object. - Finally, the output_parser component takes in a ChatMessage, and transforms this into a Python string, which is returned from the invoke method. ##Next steps[](#next-steps) We recommend reading our [Why use LCEL](/docs/expression_language/why) section next to see a side-by-side comparison of the code needed to produce common functionality with and without LCEL.


How to | 🦜🔗 Langchain

Main:  #How to [ ##📄 Bind runtime args Sometimes 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.  ](/docs/expression_language/how_to/binding) [ ##📄 Configure chain internals at runtime Oftentimes you may want to experiment with, or even expose to the end user, multiple different ways of doing things.  ](/docs/expression_language/how_to/configure) [ ##📄 Add fallbacks There 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.  ](/docs/expression_language/how_to/fallbacks) [ ##📄 Run custom functions You can use arbitrary functions in the pipeline  ](/docs/expression_language/how_to/functions) [ ##📄 Stream custom generator functions You can use generator functions (ie. functions that use the yield keyword, and behave like iterators) in a LCEL pipeline.  ](/docs/expression_language/how_to/generators) [ ##📄 Parallelize steps RunnableParallel (aka. RunnableMap) makes it easy to execute multiple Runnables in parallel, and to return the output of these Runnables as a map.  ](/docs/expression_language/how_to/map) [ ##📄 Add message history (memory) The RunnableWithMessageHistory let's us add message history to certain types of chains.  ](/docs/expression_language/how_to/message_history) [ ##📄 Dynamically route logic based on input This notebook covers how to do routing in the LangChain Expression Language.  ](/docs/expression_language/how_to/routing)


Bind runtime args | 🦜🔗 Langchain

Main:  On this page #Bind runtime args Sometimes 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 ChatOpenAI from langchain.prompts import ChatPromptTemplate from langchain.schema import StrOutputParser from langchain.schema.runnable import RunnablePassthrough  prompt = 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[](#attaching-openai-functions) One particularly useful application of binding is to attach OpenAI functions to a compatible OpenAI model:  function = {     "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 correctly prompt = 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=[function] ) 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)  ##Attaching OpenAI tools[](#attaching-openai-tools) tools = [     {         "type": "function",         "function": {             "name": "get_current_weather",             "description": "Get the current weather in a given location",             "parameters": {                 "type": "object",                 "properties": {                     "location": {                         "type": "string",                         "description": "The city and state, e.g. San Francisco, CA",                     },                     "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},                 },                 "required": ["location"],             },         },     } ]  model = ChatOpenAI(model="gpt-3.5-turbo-1106").bind(tools=tools) model.invoke("What's the weather in SF, NYC and LA?")      AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_zHN0ZHwrxM7nZDdqTp6dkPko', 'function': {'arguments': '{"location": "San Francisco, CA", "unit": "celsius"}', 'name': 'get_current_weather'}, 'type': 'function'}, {'id': 'call_aqdMm9HBSlFW9c9rqxTa7eQv', 'function': {'arguments': '{"location": "New York, NY", "unit": "celsius"}', 'name': 'get_current_weather'}, 'type': 'function'}, {'id': 'call_cx8E567zcLzYV2WSWVgO63f1', 'function': {'arguments': '{"location": "Los Angeles, CA", "unit": "celsius"}', 'name': 'get_current_weather'}, 'type': 'function'}]})


Configure chain internals at runtime | 🦜🔗 Langchain

Main:  On this page #Configure chain internals at runtime Oftentimes you may want to experiment with, or even expose to the end user, multiple different ways of doing things. In order to make this experience as easy as possible, we have defined two methods.  First, a configurable_fields method. This lets you configure particular fields of a runnable.  Second, a configurable_alternatives method. With this method, you can list out alternatives for any particular runnable that can be set during runtime.  ##Configuration Fields[](#configuration-fields) ###With LLMs[](#with-llms) With LLMs we can configure things like temperature  from langchain.chat_models import ChatOpenAI from langchain.prompts import PromptTemplate  model = ChatOpenAI(temperature=0).configurable_fields(     temperature=ConfigurableField(         id="llm_temperature",         name="LLM Temperature",         description="The temperature of the LLM",     ) )  model.invoke("pick a random number")      AIMessage(content='7')  model.with_config(configurable={"llm_temperature": 0.9}).invoke("pick a random number")      AIMessage(content='34')  We can also do this when its used as part of a chain  prompt = PromptTemplate.from_template("Pick a random number above {x}") chain = prompt | model  chain.invoke({"x": 0})      AIMessage(content='57')  chain.with_config(configurable={"llm_temperature": 0.9}).invoke({"x": 0})      AIMessage(content='6')  ###With HubRunnables[](#with-hubrunnables) This is useful to allow for switching of prompts  from langchain.runnables.hub import HubRunnable  prompt = HubRunnable("rlm/rag-prompt").configurable_fields(     owner_repo_commit=ConfigurableField(         id="hub_commit",         name="Hub Commit",         description="The Hub commit to pull from",     ) )  prompt.invoke({"question": "foo", "context": "bar"})      ChatPromptValue(messages=[HumanMessage(content="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: foo \nContext: bar \nAnswer:")])  prompt.with_config(configurable={"hub_commit": "rlm/rag-prompt-llama"}).invoke(     {"question": "foo", "context": "bar"} )      ChatPromptValue(messages=[HumanMessage(content="[INST]<<SYS>> 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.<</SYS>> \nQuestion: foo \nContext: bar \nAnswer: [/INST]")])  ##Configurable Alternatives[](#configurable-alternatives) ###With LLMs[](#with-llms-1) Let's take a look at doing this with LLMs  from langchain.chat_models import ChatAnthropic, ChatOpenAI from langchain.prompts import PromptTemplate from langchain.schema.runnable import ConfigurableField  llm = ChatAnthropic(temperature=0).configurable_alternatives(     # This gives this field an id     # When configuring the end runnable, we can then use this id to configure this field     ConfigurableField(id="llm"),     # This sets a default_key.     # If we specify this key, the default LLM (ChatAnthropic initialized above) will be used     default_key="anthropic",     # This adds a new option, with name `openai` that is equal to `ChatOpenAI()`     openai=ChatOpenAI(),     # This adds a new option, with name `gpt4` that is equal to `ChatOpenAI(model="gpt-4")`     gpt4=ChatOpenAI(model="gpt-4"),     # You can add more configuration options here ) prompt = PromptTemplate.from_template("Tell me a joke about {topic}") chain = prompt | llm  # By default it will call Anthropic chain.invoke({"topic": "bears"})      AIMessage(content=" Here's a silly joke about bears:\n\nWhat do you call a bear with no teeth?\nA gummy bear!")  # We can use `.with_config(configurable={"llm": "openai"})` to specify an llm to use chain.with_config(configurable={"llm": "openai"}).invoke({"topic": "bears"})      AIMessage(content="Sure, here's a bear joke for you:\n\nWhy don't bears wear shoes?\n\nBecause they already have bear feet!")  # If we use the `default_key` then it uses the default chain.with_config(configurable={"llm": "anthropic"}).invoke({"topic": "bears"})      AIMessage(content=" Here's a silly joke about bears:\n\nWhat do you call a bear with no teeth?\nA gummy bear!")  ###With Prompts[](#with-prompts) We can do a similar thing, but alternate between prompts  llm = ChatAnthropic(temperature=0) prompt = PromptTemplate.from_template(     "Tell me a joke about {topic}" ).configurable_alternatives(     # This gives this field an id     # When configuring the end runnable, we can then use this id to configure this field     ConfigurableField(id="prompt"),     # This sets a default_key.     # If we specify this key, the default LLM (ChatAnthropic initialized above) will be used     default_key="joke",     # This adds a new option, with name `poem`     poem=PromptTemplate.from_template("Write a short poem about {topic}"),     # You can add more configuration options here ) chain = prompt | llm  # By default it will write a joke chain.invoke({"topic": "bears"})      AIMessage(content=" Here's a silly joke about bears:\n\nWhat do you call a bear with no teeth?\nA gummy bear!")  # We can configure it write a poem chain.with_config(configurable={"prompt": "poem"}).invoke({"topic": "bears"})      AIMessage(content=' Here is a short poem about bears:\n\nThe bears awaken from their sleep\nAnd lumber out into the deep\nForests filled with trees so tall\nForaging for food before nightfall \nTheir furry coats and claws so sharp\nSniffing for berries and fish to nab\nLumbering about without a care\nThe mighty grizzly and black bear\nProud creatures, wild and free\nRuling their domain majestically\nWandering the woods they call their own\nBefore returning to their dens alone')  ###With Prompts and LLMs[](#with-promp


ts-and-llms) We can also have multiple things configurable! Here's an example doing that with both prompts and LLMs.  llm = ChatAnthropic(temperature=0).configurable_alternatives(     # This gives this field an id     # When configuring the end runnable, we can then use this id to configure this field     ConfigurableField(id="llm"),     # This sets a default_key.     # If we specify this key, the default LLM (ChatAnthropic initialized above) will be used     default_key="anthropic",     # This adds a new option, with name `openai` that is equal to `ChatOpenAI()`     openai=ChatOpenAI(),     # This adds a new option, with name `gpt4` that is equal to `ChatOpenAI(model="gpt-4")`     gpt4=ChatOpenAI(model="gpt-4"),     # You can add more configuration options here ) prompt = PromptTemplate.from_template(     "Tell me a joke about {topic}" ).configurable_alternatives(     # This gives this field an id     # When configuring the end runnable, we can then use this id to configure this field     ConfigurableField(id="prompt"),     # This sets a default_key.     # If we specify this key, the default LLM (ChatAnthropic initialized above) will be used     default_key="joke",     # This adds a new option, with name `poem`     poem=PromptTemplate.from_template("Write a short poem about {topic}"),     # You can add more configuration options here ) chain = prompt | llm  # We can configure it write a poem with OpenAI chain.with_config(configurable={"prompt": "poem", "llm": "openai"}).invoke(     {"topic": "bears"} )      AIMessage(content="In the forest, where tall trees sway,\nA creature roams, both fierce and gray.\nWith mighty paws and piercing eyes,\nThe bear, a symbol of strength, defies.\n\nThrough snow-kissed mountains, it does roam,\nA guardian of its woodland home.\nWith fur so thick, a shield of might,\nIt braves the coldest winter night.\n\nA gentle giant, yet wild and free,\nThe bear commands respect, you see.\nWith every step, it leaves a trace,\nOf untamed power and ancient grace.\n\nFrom honeyed feast to salmon's leap,\nIt takes its place, in nature's keep.\nA symbol of untamed delight,\nThe bear, a wonder, day and night.\n\nSo let us honor this noble beast,\nIn forests where its soul finds peace.\nFor in its presence, we come to know,\nThe untamed spirit that in us also flows.")  # We can always just configure only one if we want chain.with_config(configurable={"llm": "openai"}).invoke({"topic": "bears"})      AIMessage(content="Sure, here's a bear joke for you:\n\nWhy don't bears wear shoes?\n\nBecause they have bear feet!")  ###Saving configurations[](#saving-configurations) We can also easily save configured chains as their own objects  openai_poem = chain.with_config(configurable={"llm": "openai"})  openai_poem.invoke({"topic": "bears"})      AIMessage(content="Why don't bears wear shoes?\n\nBecause they have bear feet!")


Add fallbacks | 🦜🔗 Langchain

Main:  On this page #Add fallbacks There 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[](#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 ChatAnthropic, ChatOpenAI  First, let's mock out what happens if we hit a RateLimitError from OpenAI  from unittest.mock import patch  from openai.error import RateLimitError  # Note that we set max_retries = 0 to avoid retrying on RateLimits, etc openai_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 error with 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 Anthropic with patch("openai.ChatCompletion.create", side_effect=RateLimitError()):     try:         print(llm.invoke("Why did 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=False  We can use our "LLM with Fallbacks" as we would a normal LLM.  from langchain.prompts import ChatPromptTemplate  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"),     ] ) chain = prompt | llm with 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=False  ###Specifying errors to handle[](#specifying-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 | llm with patch("openai.ChatCompletion.create", side_effect=RateLimitError()):     try:         print(chain.invoke({"animal": "kangaroo"}))     except:         print("Hit error")      Hit error  ##Fallbacks for Sequences[](#fallbacks-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 type from langchain.schema.output_parser import StrOutputParser  chat_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 error chat_model = ChatOpenAI(model_name="gpt-fake") bad_chain = chat_prompt | chat_model | StrOutputParser()  # Now lets create a chain with the normal OpenAI model from langchain.llms import OpenAI from langchain.prompts import PromptTemplate  prompt_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 two chain = 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.'


Run custom functions | 🦜🔗 Langchain

Main:  On this page #Run custom functions You can use arbitrary functions in the pipeline  Note 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 operator import itemgetter  from langchain.chat_models import ChatOpenAI from langchain.prompts import ChatPromptTemplate from langchain.schema.runnable import RunnableLambda   def 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 | model  chain = (     {         "a": itemgetter("foo") | RunnableLambda(length_function),         "b": {"text1": itemgetter("foo"), "text2": itemgetter("bar")}         | RunnableLambda(multiple_length_function),     }     | prompt     | model )  chain.invoke({"foo": "bar", "bar": "gah"})      AIMessage(content='3 + 9 equals 12.', additional_kwargs={}, example=False)  ##Accepting a Runnable Config[](#accepting-a-runnable-config) Runnable lambdas can optionally accept a [RunnableConfig](https://api.python.langchain.com/en/latest/runnables/langchain_core.runnables.config.RunnableConfig.html#langchain_core.runnables.config.RunnableConfig), which they can use to pass callbacks, tags, and other configuration information to nested runs.  from langchain.schema.output_parser import StrOutputParser from langchain.schema.runnable import RunnableConfig  import json   def 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_callback  with 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.00010200000000000001


Stream custom generator functions | 🦜🔗 Langchain

Main:  #Stream custom generator functions You can use generator functions (ie. functions that use the yield keyword, and behave like iterators) in a LCEL pipeline.  The signature of these generators should be Iterator[Input] -> Iterator[Output]. Or for async generators: AsyncIterator[Input] -> AsyncIterator[Output].  These are useful for:  - implementing a custom output parser - modifying the output of a previous step, while preserving streaming capabilities Let's implement a custom output parser for comma-separated lists.  from typing import Iterator, List  from langchain.chat_models import ChatOpenAI from langchain.prompts.chat import ChatPromptTemplate from langchain.schema.output_parser import StrOutputParser  prompt = ChatPromptTemplate.from_template(     "Write a comma-separated list of 5 animals similar to: {animal}" ) model = ChatOpenAI(temperature=0.0)  str_chain = prompt | model | StrOutputParser()  for chunk in str_chain.stream({"animal": "bear"}):     print(chunk, end="", flush=True)      lion, tiger, wolf, gorilla, panda  str_chain.invoke({"animal": "bear"})      'lion, tiger, wolf, gorilla, panda'  # This is a custom parser that splits an iterator of llm tokens # into a list of strings separated by commas def split_into_list(input: Iterator[str]) -> Iterator[List[str]]:     # hold partial input until we get a comma     buffer = ""     for chunk in input:         # add current chunk to buffer         buffer += chunk         # while there are commas in the buffer         while "," in buffer:             # split buffer on comma             comma_index = buffer.index(",")             # yield everything before the comma             yield [buffer[:comma_index].strip()]             # save the rest for the next iteration             buffer = buffer[comma_index + 1 :]     # yield the last chunk     yield [buffer.strip()]  list_chain = str_chain | split_into_list  for chunk in list_chain.stream({"animal": "bear"}):     print(chunk, flush=True)      ['lion']     ['tiger']     ['wolf']     ['gorilla']     ['panda']  list_chain.invoke({"animal": "bear"})      ['lion', 'tiger', 'wolf', 'gorilla', 'panda']


Parallelize steps | 🦜🔗 Langchain

Main:  On this page #Parallelize steps RunnableParallel (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 ChatOpenAI from langchain.prompts import ChatPromptTemplate from langchain.schema.runnable import RunnableParallel  model = ChatOpenAI() joke_chain = ChatPromptTemplate.from_template("tell me a joke about {topic}") | model poem_chain = (     ChatPromptTemplate.from_template("write a 2-line poem about {topic}") | model )  map_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[](#manipulating-outputsinputs) 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 OpenAIEmbeddings from langchain.schema.output_parser import StrOutputParser from langchain.schema.runnable import RunnablePassthrough from langchain.vectorstores import FAISS  vectorstore = 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 RunnableParallel with another Runnable we don't even need to wrap our dictionary in the RunnableParallel class — the type conversion is handled for us.  ##Parallelism[](#parallelism) RunnableParallel 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)


Add message history (memory) | 🦜🔗 Langchain

Main:  On this page #Add message history (memory) The RunnableWithMessageHistory let's us add message history to certain types of chains.  Specifically, it can be used for any Runnable that takes as input one of  - a sequence of BaseMessage - a dict with a key that takes a sequence of BaseMessage - a dict with a key that takes the latest message(s) as a string or sequence of BaseMessage, and a separate key that takes historical messages And returns as output one of  - a string that can be treated as the contents of an AIMessage - a sequence of BaseMessage - a dict with a key that contains a sequence of BaseMessage Let's take a look at some examples to see how it works.  ##Setup[](#setup) We'll use Redis to store our chat message histories and Anthropic's claude-2 model so we'll need to install the following dependencies:  pip install -U langchain redis anthropic  Set your [Anthropic API key](https://console.anthropic.com/):  import getpass import os  os.environ["ANTHROPIC_API_KEY"] = getpass.getpass()  Start a local Redis Stack server if we don't have an existing Redis deployment to connect to:  docker run -d -p 6379:6379 -p 8001:8001 redis/redis-stack:latest  REDIS_URL = "redis://localhost:6379/0"  ###[LangSmith](/docs/langsmith)[](#langsmith) LangSmith is especially useful for something like message history injection, where it can be hard to otherwise understand what the inputs are to various parts of the chain.  Note that LangSmith is not needed, but it is helpful. If you do want to use LangSmith, after you sign up at the link above, make sure to uncoment the below and set your environment variables to start logging traces:  # os.environ["LANGCHAIN_TRACING_V2"] = "true" # os.environ["LANGCHAIN_API_KEY"] = getpass.getpass()  ##Example: Dict input, message output[](#example-dict-input-message-output) Let's create a simple chain that takes a dict as input and returns a BaseMessage.  In this case the "question" key in the input represents our input message, and the "history" key is where our historical messages will be injected.  from typing import Optional  from langchain.chat_models import ChatAnthropic from langchain.memory.chat_message_histories import RedisChatMessageHistory from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder from langchain.schema.chat_history import BaseChatMessageHistory from langchain.schema.runnable.history import RunnableWithMessageHistory  prompt = ChatPromptTemplate.from_messages(     [         ("system", "You're an assistant who's good at {ability}"),         MessagesPlaceholder(variable_name="history"),         ("human", "{question}"),     ] )  chain = prompt | ChatAnthropic(model="claude-2")  ###Adding message history[](#adding-message-history) To add message history to our original chain we wrap it in the RunnableWithMessageHistory class.  Crucially, we also need to define a method that takes a session_id string and based on it returns a BaseChatMessageHistory. Given the same input, this method should return an equivalent output.  In this case we'll also want to specify input_messages_key (the key to be treated as the latest input message) and history_messages_key (the key to add historical messages to).  chain_with_history = RunnableWithMessageHistory(     chain,     lambda session_id: RedisChatMessageHistory(session_id, url=REDIS_URL),     input_messages_key="question",     history_messages_key="history", )  ##Invoking with config[](#invoking-with-config) Whenever we call our chain with message history, we need to include a config that contains the session_id  config={"configurable": {"session_id": "<SESSION_ID>"}}  Given the same configuration, our chain should be pulling from the same chat message history.  chain_with_history.invoke(     {"ability": "math", "question": "What does cosine mean?"},     config={"configurable": {"session_id": "foobar"}}, )      AIMessage(content=' Cosine is one of the basic trigonometric functions in mathematics. It is defined as the ratio of the adjacent side to the hypotenuse in a right triangle.\n\nSome key properties and facts about cosine:\n\n- It is denoted by cos(θ), where θ is the angle in a right triangle. \n\n- The cosine of an acute angle is always positive. For angles greater than 90 degrees, cosine can be negative.\n\n- Cosine is one of the three main trig functions along with sine and tangent.\n\n- The cosine of 0 degrees is 1. As the angle increases towards 90 degrees, the cosine value decreases towards 0.\n\n- The range of values for cosine is -1 to 1.\n\n- The cosine function maps angles in a circle to the x-coordinate on the unit circle.\n\n- Cosine is used to find adjacent side lengths in right triangles, and has many other applications in mathematics, physics, engineering and more.\n\n- Key cosine identities include: cos(A+B) = cosAcosB − sinAsinB and cos(2A) = cos^2(A) − sin^2(A)\n\nSo in summary, cosine is a fundamental trig')  chain_with_history.invoke(     {"ability": "math", "question": "What's its inverse"},     config={"configurable": {"session_id": "foobar"}}, )      AIMessage(content=' The inverse of the cosine function is called the arccosine or inverse cosine, often denoted as cos-1(x) or arccos(x).\n\nThe key properties and facts about arccosine:\n\n- It is defined as the angle θ between 0 and π radians whose cosine is x. So arccos(x) = θ such that cos(θ) = x.\n\n- The range of arccosine is 0 to π radians (0 to 180 degrees).\n\n- The domain of arccosine is -1 to 1. \n\n- arccos(cos(θ)) = θ for values of θ from 0 to π radians.\n\n- arccos(x) is the angle in a right triangle whose adjacent side is x and hypotenuse is 1.\n\n- arccos(0) = 90 degrees. As x increases from 0 to 1, arccos(x) decreases from 90 to 0 degrees.\n\n- arccos(1) = 0 degrees. arccos(-1) = 180 degrees.\n\n- The graph of y = arccos(x) is part of the unit circle, restricted to x')  [LANGSMITH TRACE](HTTPS://SMITH.LANGCHAIN.COM/PUBLIC/863A003B-7CA8-4B24-BE9E-D63EC13C106E/R) Looking at the Langsmith trace fo


r the second call, we can see that when constructing the prompt, a "history" variable has been injected which is a list of two messages (our first input and first output).  ##Example: messages input, dict output[](#example-messages-input-dict-output) from langchain.schema.messages import HumanMessage from langchain.schema.runnable import RunnableParallel  chain = RunnableParallel({"output_message": ChatAnthropic(model="claude-2")}) chain_with_history = RunnableWithMessageHistory(     chain,     lambda session_id: RedisChatMessageHistory(session_id, url=REDIS_URL),     output_messages_key="output_message", )  chain_with_history.invoke(     [HumanMessage(content="What did Simone de Beauvoir believe about free will")],     config={"configurable": {"session_id": "baz"}}, )      {'output_message': AIMessage(content=' Here is a summary of Simone de Beauvoir\'s views on free will:\n\n- De Beauvoir was an existentialist philosopher and believed strongly in the concept of free will. She rejected the idea that human nature or instincts determine behavior.\n\n- Instead, de Beauvoir argued that human beings define their own essence or nature through their actions and choices. As she famously wrote, "One is not born, but rather becomes, a woman."\n\n- De Beauvoir believed that while individuals are situated in certain cultural contexts and social conditions, they still have agency and the ability to transcend these situations. Freedom comes from choosing one\'s attitude toward these constraints.\n\n- She emphasized the radical freedom and responsibility of the individual. We are "condemned to be free" because we cannot escape making choices and taking responsibility for our choices. \n\n- De Beauvoir felt that many people evade their freedom and responsibility by adopting rigid mindsets, ideologies, or conforming uncritically to social roles.\n\n- She advocated for the recognition of ambiguity in the human condition and warned against the quest for absolute rules that deny freedom and responsibility. Authentic living involves embracing ambiguity.\n\nIn summary, de Beauvoir promoted an existential ethics')}  chain_with_history.invoke(     [HumanMessage(content="How did this compare to Sartre")],     config={"configurable": {"session_id": "baz"}}, )      {'output_message': AIMessage(content=" There are many similarities between Simone de Beauvoir's views on free will and those of Jean-Paul Sartre, though some key differences emerge as well:\n\nSimilarities with Sartre:\n\n- Both were existentialist thinkers who rejected determinism and emphasized human freedom and responsibility.\n\n- They agreed that existence precedes essence - there is no predefined human nature that determines who we are.\n\n- Individuals must define themselves through their choices and actions. This leads to anxiety but also freedom.\n\n- The human condition is characterized by ambiguity and uncertainty, rather than fixed meanings/values.\n\n- Both felt that most people evade their freedom through self-deception, conformity, or adopting collective identities/values uncritically.\n\nDifferences from Sartre: \n\n- Sartre placed more emphasis on the burden and anguish of radical freedom. De Beauvoir focused more on its positive potential.\n\n- De Beauvoir critiqued Sartre's premise that human relations are necessarily conflictual. She saw more potential for mutual recognition.\n\n- Sartre saw the Other's gaze as a threat to freedom. De Beauvoir put more stress on how the Other's gaze can confirm")}  [LANGSMITH TRACE](HTTPS://SMITH.LANGCHAIN.COM/PUBLIC/F6C3E1D1-A49D-4955-A9FA-C6519DF74FA7/R) ##More examples[](#more-examples) We could also do any of the below:  from operator import itemgetter  # messages in, messages out RunnableWithMessageHistory(     ChatAnthropic(model="claude-2"),     lambda session_id: RedisChatMessageHistory(session_id, url=REDIS_URL), )  # dict with single key for all messages in, messages out RunnableWithMessageHistory(     itemgetter("input_messages") | ChatAnthropic(model="claude-2"),     lambda session_id: RedisChatMessageHistory(session_id, url=REDIS_URL),     input_messages_key="input_messages", )


Dynamically route logic based on input | 🦜🔗 Langchain

Main:  On this page #Dynamically route logic based on input This 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[](#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.chat_models import ChatAnthropic from langchain.prompts import PromptTemplate from langchain.schema.output_parser import StrOutputParser  First, 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> {question} </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 RunnableBranch  branch = 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"]} | branch  full_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[](#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_chain  from langchain.schema.runnable import RunnableLambda  full_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)


Interface | 🦜🔗 Langchain

Main:  On this page To make it as easy as possible to create custom chains, we've implemented a ["Runnable"](https://api.python.langchain.com/en/stable/runnables/langchain_core.runnables.base.Runnable.html#langchain_core.runnables.base.Runnable) protocol. The Runnable protocol is implemented for most components. This is a standard interface, which makes it easy to define custom chains as well as invoke them in a standard way. The standard interface includes:  - [stream](#stream): stream back chunks of the response - [invoke](#invoke): call the chain on an input - [batch](#batch): call the chain on a list of inputs These also have corresponding async methods:  - [astream](#async-stream): stream back chunks of the response async - [ainvoke](#async-invoke): call the chain on an input async - [abatch](#async-batch): call the chain on a list of inputs async - [astream_log](#async-stream-intermediate-steps): stream back intermediate steps as they happen, in addition to the final response The input type and output type varies by component:  Component	Input Type	Output Type Prompt	Dictionary	PromptValue ChatModel	Single string, list of chat messages or a PromptValue	ChatMessage LLM	Single string, list of chat messages or a PromptValue	String OutputParser	The output of an LLM or ChatModel	Depends on the parser Retriever	Single string	List of Documents Tool	Single string or dictionary, depending on the tool	Depends on the tool All runnables expose input and output schemas to inspect the inputs and outputs:  - [input_schema](#input-schema): an input Pydantic model auto-generated from the structure of the Runnable - [output_schema](#output-schema): an output Pydantic model auto-generated from the structure of the Runnable Let's take a look at these methods. To do so, we'll create a super simple PromptTemplate + ChatModel chain.  from langchain.chat_models import ChatOpenAI from langchain.prompts import ChatPromptTemplate  model = ChatOpenAI() prompt = ChatPromptTemplate.from_template("tell me a joke about {topic}") chain = prompt | model  ##Input Schema[](#input-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'}}}  prompt.input_schema.schema()      {'title': 'PromptInput',      'type': 'object',      'properties': {'topic': {'title': 'Topic', 'type': 'string'}}}  model.input_schema.schema()      {'title': 'ChatOpenAIInput',      'anyOf': [{'type': 'string'},       {'$ref': '#/definitions/StringPromptValue'},       {'$ref': '#/definitions/ChatPromptValueConcrete'},       {'type': 'array',        'items': {'anyOf': [{'$ref': '#/definitions/AIMessage'},          {'$ref': '#/definitions/HumanMessage'},          {'$ref': '#/definitions/ChatMessage'},          {'$ref': '#/definitions/SystemMessage'},          {'$ref': '#/definitions/FunctionMessage'}]}}],      'definitions': {'StringPromptValue': {'title': 'StringPromptValue',        'description': 'String prompt value.',        'type': 'object',        'properties': {'text': {'title': 'Text', 'type': 'string'},         'type': {'title': 'Type',          'default': 'StringPromptValue',          'enum': ['StringPromptValue'],          'type': 'string'}},        'required': ['text']},       'AIMessage': {'title': 'AIMessage',        'description': 'A Message 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'}},        'required': ['content']},       'HumanMessage': {'title': 'HumanMessage',        'description': 'A Message from a human.',        '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'}},        'required': ['content']},       'ChatMessage': {'title': 'ChatMessage',        'description': 'A Message that can be assigned an arbitrary speaker (i.e. role).',        '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'}},        'required': ['content', 'role']},       'SystemMessage': {'title': 'SystemMessage',        'description': 'A Message for priming AI behavior, usually passed in as the first of a sequence\nof input messages.',        'type': 'object',        'properties': {'content': {'title': 'Content', 'type': 'string'},         'additional_kwargs': {'title': 'Additional Kwargs', 'type': 'object'},         'type': {'title': 'Type',          'default': 'system',          'enum': ['system'],          'type': 'string'}},        'required': ['content']},       'FunctionMessage': {'title': 'FunctionMessage',        'description': 'A Message for passing the result of executing a function back to a model.',        'type': 'object',        'properties': {'content': {'title': 'Content', 'type': 'string'},         'additional_kwargs': {'title': 'Additional Kwargs', 'type': 'object'},         'type': {'title': 'Type',          'default': 'function',          'e