langchain demo

环境要求:

python3.10以上,3.13以下。我用的是3.11

可以用anoconda创建环境,conda create -n py311 python=3.11

环境安装完成后:

选择anaconda powershell prompt

执行:

conda activate py311

pip install -U langchain

pip install -U langchain-openai

pip install -U langchain-anthropic

在系统环境变量中添加:

变量名:OPENAI_API_KEY

变量值:填你的阿里百炼的

打开pycharm 或者vscode

创建python文件

python 复制代码
from langchain.agents import create_agent
from langchain_openai import ChatOpenAI
import json
from langchain_core.messages import message_to_dict

def get_weather(city: str) -> str:
    """Get weather for a given city."""
    return f"It's always sunny in {city}!"

llm = ChatOpenAI(model="qwen-turbo", base_url="https://dashscope.aliyuncs.com/compatible-mode/v1")

agent = create_agent(
    model=llm,
    tools=[get_weather],
    system_prompt="You are a helpful assistant",
)

result = agent.invoke(
    {"messages": [{"role": "user", "content": "What's the weather in San Francisco?"}]}
)
payload = {
    "messages": [message_to_dict(message) for message in result["messages"]]
}
print(json.dumps(payload, ensure_ascii=False, indent=2))

执行python demo2.py

相关推荐
云和数据.ChenGuang11 小时前
LLM大模型对接本地Ollama部署模型
开发语言·人工智能·python·langchain
云和数据.ChenGuang12 小时前
光哥说Python 循环导入(Circular Import)深度解析:成因、排查与解决方案
开发语言·前端·人工智能·python·langchain
染指111013 小时前
152.Agent-LangGraph-Command的参数
人工智能·python·langchain·agents
小肝一下14 小时前
1.大模型介绍
python·langchain·大模型·ollama·langgrap
BD_Marathon17 小时前
【RAG开发】LangChain调用大语言模型
人工智能·语言模型·langchain
BD_Marathon21 小时前
【RAG开发】ChatPromptTemplate的使用
langchain
LMT流氓兔1 天前
第五单元——第4课:给工具参数加校验
java·前端·算法·ai·langchain
Mr_star_galaxy1 天前
【Agent】使用 LangSmith 跟踪 LLM 应用
langchain
BD_Marathon1 天前
【RAG开发】RAG介绍
langchain
似水流年QC2 天前
LangChain 入门与实战
服务器·数据库·langchain