LangChain is a framework for developing applications powered by language models.
This framework consists of several parts.
- LangChain Libraries: The Python and JavaScript libraries. Contains interfaces and integrations for a myriad of components, a basic run time for combining these components into chains and agents, and off-the-shelf implementations of chains and agents.
- LangChain Templates: A collection of easily deployable reference architectures for a wide variety of tasks. (Python only)
- LangServe: A library for deploying LangChain chains as a REST API. (Python only)
- LangSmith: A developer platform that lets you debug, test, evaluate, and monitor chains built on any LLM framework and seamlessly integrates with LangChain. Create an API key To create an API key head to the setting pages. Then click Create API Key.
Create directory and navigate to it
pnpm add -D typescript tsx && tsc --initInstall the framework packages
pnpm add dotenv langchain langsmith @langchain/openaiCreate a .env file in the root of your project and add the following
LANGCHAIN_TRACING_V2=true
LANGCHAIN_API_KEY=<your-api-key>
OPENAI_API_KEY=<your-openai-api-key>or
export LANGCHAIN_TRACING_V2=true
export LANGCHAIN_API_KEY=<your-api-key>
export LANGCHAIN_CALLBACKS_BACKGROUND=true;Set the following scripts in your package.json
{
"scripts": {
"dev": "tsx watch ./src/main.ts",
"build": "tsx build ./src/main.ts",
"start": "node ./dist/main.js",
"dry": "tsx ./src/main.ts"
},
}Log your first trace We provide multiple ways to log traces to LangSmith. Below, we'll highlight how to use traceable. See more on the Integrations page.
Python TypeScript import { OpenAI } from "openai"; import { traceable } from "langsmith/traceable"; import { wrapOpenAI } from "langsmith/wrappers";
// Auto-trace LLM calls in-context const client = wrapOpenAI(new OpenAI()); // Auto-trace this function const pipeline = traceable(async (user_input) => { const result = await client.chat.completions.create({ messages: [{ role: "user", content: user_input }], model: "gpt-3.5-turbo", }); return result.choices[0].message.content; });
await pipeline("Hello, world!") // Out: Hello there! How can I assist you today?