from langchain.agents import create_agentdef send_email(to: str, subject: str, body: str): """向收件人发送邮件。""" # ... 邮件发送逻辑 return f"Email sent to {to}"def search_web(query: str): """在网络上搜索信息。""" # ... 网页搜索逻辑 return f"Search results for: {query}"agent = create_agent( model="gpt-5.4", tools=[send_email, search_web], system_prompt="You are a helpful assistant that can send emails and search the web.")# 运行智能体 - 所有步骤将自动被追踪response = agent.invoke({ "messages": [{"role": "user", "content": "Search for the latest AI news and email a summary to john@example.com"}]})
You may opt to trace specific invocations or parts of your application using LangSmith’s tracing_context context manager:
import langsmith as ls# This WILL be tracedwith ls.tracing_context(enabled=True): agent.invoke({"messages": [{"role": "user", "content": "Send a test email to alice@example.com"}]})# This will NOT be traced (if LANGSMITH_TRACING is not set)agent.invoke({"messages": [{"role": "user", "content": "Send another email"}]})