Skip to main content
다른 가이드에서 설명한 것처럼, 다음 환경 변수를 사용하여 트레이싱 활성화, API 엔드포인트, API 키 및 트레이싱 프로젝트를 구성할 수 있습니다:
  • LANGSMITH_TRACING
  • LANGSMITH_API_KEY
  • LANGSMITH_ENDPOINT
  • LANGSMITH_PROJECT
커스텀 구성으로 실행을 트레이싱해야 하거나, Cloudflare Workers와 같이 일반적인 환경 변수를 지원하지 않는 환경에서 작업하거나, 단순히 환경 변수에 의존하지 않으려는 경우, LangSmith를 사용하면 프로그래밍 방식으로 트레이싱을 구성할 수 있습니다.
trace 컨텍스트 매니저를 사용한 트레이싱의 세밀한 제어에 대한 여러 요청으로 인해, Python SDK 버전 0.1.95에서 with trace의 동작이 변경되어 LANGSMITH_TRACING 환경 변수를 준수하도록 했습니다. 자세한 내용은 릴리스 노트에서 확인할 수 있습니다. 환경 변수를 설정하지 않고 트레이싱을 비활성화/활성화하는 권장 방법은 아래 예제와 같이 with tracing_context 컨텍스트 매니저를 사용하는 것입니다.
  • Python: Python에서 이를 수행하는 권장 방법은 tracing_context 컨텍스트 매니저를 사용하는 것입니다. 이는 traceable로 주석이 달린 코드와 trace 컨텍스트 매니저 내의 코드 모두에서 작동합니다.
  • TypeScript: traceable 데코레이터에 클라이언트와 tracingEnabled 플래그를 모두 전달할 수 있습니다.
import openai
from langsmith import Client, tracing_context, traceable
from langsmith.wrappers import wrap_openai

langsmith_client = Client(
  api_key="YOUR_LANGSMITH_API_KEY",  # This can be retrieved from a secrets manager
  api_url="https://api.smith.langchain.com",  # Update appropriately for self-hosted installations or the EU region
  workspace_id="YOUR_WORKSPACE_ID", # Must be specified for API keys scoped to multiple workspaces
)

client = wrap_openai(openai.Client())

@traceable(run_type="tool", name="Retrieve Context")
def my_tool(question: str) -> str:
  return "During this morning's meeting, we solved all world conflict."

@traceable
def chat_pipeline(question: str):
  context = my_tool(question)
  messages = [
      { "role": "system", "content": "You are a helpful assistant. Please respond to the user's request only based on the given context." },
      { "role": "user", "content": f"Question: {question}\nContext: {context}"}
  ]
  chat_completion = client.chat.completions.create(
      model="gpt-4o-mini", messages=messages
  )
  return chat_completion.choices[0].message.content

# Can set to False to disable tracing here without changing code structure
with tracing_context(enabled=True):
  # Use langsmith_extra to pass in a custom client
  chat_pipeline("Can you summarize this morning's meetings?", langsmith_extra={"client": langsmith_client})
비디오 튜토리얼을 선호하는 경우, Introduction to LangSmith Course의 Alternative Ways to Trace 비디오를 확인하세요.
Connect these docs programmatically to Claude, VSCode, and more via MCP for real-time answers.
I