Skip to main contentü¶úÔ∏èüîó LangChainDocsUse casesIntegrationsAPICommunityChat our docsLangSmithJS/TS DocsSearchCTRLKGet startedIntroductionInstallationQuickstartLangChain Expression LanguageInterfaceHow toCookbookLangChain Expression Language (LCEL)Why use LCEL?ModulesModel I/‚ÄãORetrievalChainsHow toAsync APIDifferent call methodsCustom chainDebugging chainsLoading from LangChainHubAdding memory (state)Using OpenAI functionsSerializationFoundationalDocumentsMemoryAgentsCallbacksModulesSecurityGuidesMoreModulesChainsHow toAsync APIAsync APILangChain provides async support for Chains by leveraging the asyncio library.Async methods are currently supported in LLMChain (through arun, apredict, acall) and LLMMathChain (through arun and acall), ChatVectorDBChain, and QA chains. Async support for other chains is on the roadmap.import asyncioimport timefrom langchain.llms import OpenAIfrom langchain.prompts import PromptTemplatefrom langchain.chains import LLMChaindef generate_serially():    llm = OpenAI(temperature=0.9)    prompt = PromptTemplate(        input_variables=["product"],        template="What is a good name for a company that makes {product}?",    )    chain = LLMChain(llm=llm, prompt=prompt)    for _ in range(5):        resp = chain.run(product="toothpaste")        print(resp)async def async_generate(chain):    resp = await chain.arun(product="toothpaste")    print(resp)async def generate_concurrently():    llm = OpenAI(temperature=0.9)    prompt = PromptTemplate(        input_variables=["product"],        template="What is a good name for a company that makes {product}?",    )    chain = LLMChain(llm=llm, prompt=prompt)    tasks = [async_generate(chain) for _ in range(5)]    await asyncio.gather(*tasks)s = time.perf_counter()# If running this outside of Jupyter, use asyncio.run(generate_concurrently())await generate_concurrently()elapsed = time.perf_counter() - sprint("\033[1m" + f"Concurrent executed in {elapsed:0.2f} seconds." + "\033[0m")s =
LangChain provides async support for Chains by leveraging the asyncio library.
LangChain provides async support for Chains by leveraging the asyncio library. ->: Skip to main contentü¶úÔ∏èüîó LangChainDocsUse casesIntegrationsAPICommunityChat our docsLangSmithJS/TS DocsSearchCTRLKGet startedIntroductionInstallationQuickstartLangChain Expression LanguageInterfaceHow toCookbookLangChain Expression Language (LCEL)Why use LCEL?ModulesModel I/‚ÄãORetrievalChainsHow toAsync APIDifferent call methodsCustom chainDebugging chainsLoading from LangChainHubAdding memory (state)Using OpenAI functionsSerializationFoundationalDocumentsMemoryAgentsCallbacksModulesSecurityGuidesMoreModulesChainsHow toAsync APIAsync APILangChain provides async support for Chains by leveraging the asyncio library.Async methods are currently supported in LLMChain (through arun, apredict, acall) and LLMMathChain (through arun and acall), ChatVectorDBChain, and QA chains. Async support for other chains is on the roadmap.import asyncioimport timefrom langchain.llms import OpenAIfrom langchain.prompts import PromptTemplatefrom langchain.chains import LLMChaindef generate_serially():    llm = OpenAI(temperature=0.9)    prompt = PromptTemplate(        input_variables=["product"],        template="What is a good name for a company that makes {product}?",    )    chain = LLMChain(llm=llm, prompt=prompt)    for _ in range(5):        resp = chain.run(product="toothpaste")        print(resp)async def async_generate(chain):    resp = await chain.arun(product="toothpaste")    print(resp)async def generate_concurrently():    llm = OpenAI(temperature=0.9)    prompt = PromptTemplate(        input_variables=["product"],        template="What is a good name for a company that makes {product}?",    )    chain = LLMChain(llm=llm, prompt=prompt)    tasks = [async_generate(chain) for _ in range(5)]    await asyncio.gather(*tasks)s = time.perf_counter()# If running this outside of Jupyter, use asyncio.run(generate_concurrently())await generate_concurrently()elapsed = time.perf_counter() - sprint("\033[1m" + f"Concurrent executed in {elapsed:0.2f} seconds." + "\033[0m")s =