这页是框架集成文档。原始 HTTP 端点说明请看
Messages。
安装
pip install langchain-anthropic
代码示例
- Python
- Node.js
import os
from langchain_anthropic import ChatAnthropic
llm = ChatAnthropic(
model='claude-sonnet-4-5-20250929',
anthropic_api_key=os.getenv('PIPELLM_API_KEY'),
anthropic_api_url='https://api.pipellm.ai'
)
response = llm.invoke([
('user', 'Why is the sky blue?')
])
print(response.content)
import { ChatAnthropic } from '@langchain/anthropic';
const llm = new ChatAnthropic({
model: 'claude-sonnet-4-5-20250929',
anthropicApiKey: process.env.PIPELLM_API_KEY,
anthropicApiUrl: 'https://api.pipellm.ai'
});
const response = await llm.invoke([
{ role: 'user', content: 'Why is the sky blue?' }
]);
console.log(response.content);
流式响应
import os
from langchain_anthropic import ChatAnthropic
llm = ChatAnthropic(
model='claude-sonnet-4-5-20250929',
anthropic_api_key=os.getenv('PIPELLM_API_KEY'),
anthropic_api_url='https://api.pipellm.ai',
streaming=True
)
for chunk in llm.stream('Tell me a story'):
print(chunk.content, end='', flush=True)
函数调用
import os
from langchain_anthropic import ChatAnthropic
from langchain_core.tools import tool
@tool
def get_weather(location: str) -> str:
"""获取指定地点的天气"""
return f"{location} 的天气:22°C,晴天"
llm = ChatAnthropic(
model='claude-sonnet-4-5-20250929',
anthropic_api_key=os.getenv('PIPELLM_API_KEY'),
anthropic_api_url='https://api.pipellm.ai'
)
llm_with_tools = llm.bind_tools([get_weather])
response = llm_with_tools.invoke("东京现在天气怎么样?")
print(response.tool_calls)
相关文档
Anthropic 概述
查看 Header、模型和原生路由
Messages
查看
POST /v1/messages 的端点说明开发工具总览
查看 PipeLLM 上的更多工具与框架集成