Skip to content

Langchain

Last updated View as MarkdownAgent setup

LangChain ↗︎ is the most popular framework for building AI applications powered by large language models (LLMs).

LangChain publishes multiple Python packages. The following are provided by the Workers runtime:

Get Started

Clone the cloudflare/python-workers-examples repository and run the LangChain example:

git clone https://github.com/cloudflare/python-workers-examples
cd python-workers-examples/langchain
uv run pywrangler dev

Example code

from workers import WorkerEntrypoint, Response
from langchain_core.prompts import PromptTemplate
from langchain_openai import OpenAI

class Default(WorkerEntrypoint):
    async def fetch(self, request):
        prompt = PromptTemplate.from_template("Complete the following sentence: I am a {profession} and ")
        llm = OpenAI(api_key=self.env.API_KEY)
        chain = prompt | llm

        res = await chain.ainvoke({"profession": "electrician"})
        return Response(res.split(".")[0].strip())

Was this helpful?