chatgpt-api/docs/marketplace/ts-sdks/langchain.mdx

125 wiersze
3.3 KiB
Plaintext
Czysty Zwykły widok Historia

2025-06-29 13:31:37 +00:00
---
title: LangChain
description: How to use Agentic tools with the LangChain TS SDK.
---
## Install
<CodeGroup>
```bash npm
npm install langchain @langchain/core @langchain/agents @agentic/langchain @agentic/platform-tool-client
```
```bash pnpm
pnpm add langchain @langchain/core @langchain/agents @agentic/langchain @agentic/platform-tool-client
```
```bash bun
bun add langchain @langchain/core @langchain/agents @agentic/langchain @agentic/platform-tool-client
```
```bash yarn
yarn add langchain @langchain/core @langchain/agents @agentic/langchain @agentic/platform-tool-client
```
</CodeGroup>
## Usage
This example uses the [`@agentic/search`](https://agentic.so/marketplace/projects/@agentic/search) tool.
```ts
import 'dotenv/config'
import { createLangChainTools } from '@agentic/langchain'
import { AgenticToolClient } from '@agentic/platform-tool-client'
import { ChatPromptTemplate } from '@langchain/core/prompts'
import { ChatOpenAI } from '@langchain/openai'
import { AgentExecutor, createToolCallingAgent } from 'langchain/agents'
async function main() {
const searchTool = await AgenticToolClient.fromIdentifier('@agentic/search')
const tools = createLangChainTools(searchTool)
const agent = createToolCallingAgent({
llm: new ChatOpenAI({ model: 'gpt-4o-mini', temperature: 0 }),
tools,
prompt: ChatPromptTemplate.fromMessages([
['system', 'You are a helpful assistant. Be as concise as possible.'],
['placeholder', '{chat_history}'],
['human', '{input}'],
['placeholder', '{agent_scratchpad}']
])
})
const agentExecutor = new AgentExecutor({
agent,
tools
// verbose: true
})
const result = await agentExecutor.invoke({
input: 'What is the weather in San Francisco?'
})
console.log(result.output)
}
await main()
```
<Expandable title="Additional dependencies">
This example also uses the [@langchain/openai](https://js.langchain.com/docs/integrations/platforms/openai) package, which adds OpenAI support to LangChain.
_Note that OpenAI is not necessary to use Agentic; this is just an example._
<CodeGroup>
```bash npm
npm install @langchain/openai dotenv
```
```bash pnpm
pnpm add @langchain/openai dotenv
```
```bash bun
bun add @langchain/openai dotenv
```
```bash yarn
yarn add @langchain/openai dotenv
```
</CodeGroup>
</Expandable>
## Running this example
You can view the full source for this example here: https://github.com/transitive-bullshit/agentic/tree/main/examples/ts-sdks/langchain
<Info>
You'll need an [OpenAI API key](https://platform.openai.com/docs/quickstart)
to run this example. Store it in a local `.env` file as `OPENAI_API_KEY`.
</Info>
<Info>
The
[`@agentic/search`](https://agentic.so/marketplace/projects/@agentic/search)
tool comes with a generous free tier, but once that runs out, you'll need to
sign up for a paid plan and add an `AGENTIC_API_KEY` to your `.env` file.
</Info>
```sh
git clone git@github.com:transitive-bullshit/agentic.git
cd agentic
pnpm install
pnpm build
echo 'OPENAI_API_KEY=your-key' >> .env
npx tsx examples/ts-sdks/langchain/bin/weather.ts
```
## Additional resources
- [`@agentic/langchain` source](https://github.com/transitive-bullshit/agentic/blob/main/stdlib/langchain/src/langchain.ts)
- [LangChain TS docs](https://js.langchain.com)