> Discover all available pages from the documentation index: https://mastra.zisheng.pro/zh-HK/llms.txt # LangSmithExporter 將 Tracing 數據傳送至 LangSmith,以便進行可觀測性監察。 ## 建構函數 ```typescript new LangSmithExporter(config: LangSmithExporterConfig) ``` ## `LangSmithExporterConfig` ```typescript interface LangSmithExporterConfig extends ClientConfig, BaseExporterConfig { client?: Client projectName?: string } ``` 同時擴充 `ClientConfig`(來自 LangSmith SDK)和 `BaseExporterConfig`: - 來自 `BaseExporterConfig`:`logger?: IMastraLogger`、`logLevel?: LogLevel | 'debug' | 'info' | 'warn' | 'error'` - 來自 `ClientConfig`:`apiKey`、`apiUrl`、`callerOptions`、`hideInputs`、`hideOutputs` 等。 ## 方法 ### `exportTracingEvent` ```typescript async exportTracingEvent(event: TracingEvent): Promise ``` 將 tracing 事件匯出至 LangSmith。 ### flush ```typescript async flush(): Promise ``` 強制將所有待處理 span 清空並傳送至 LangSmith,而不關閉 exporter。這在 serverless 環境中很有用,可確保 runtime 終止前已匯出 span。 ### shutdown ```typescript async shutdown(): Promise ``` 結束所有作用中的 span,並清除 Trace map。 ## 使用方式 ```typescript import { LangSmithExporter } from '@mastra/langsmith' const exporter = new LangSmithExporter({ apiKey: process.env.LANGSMITH_API_KEY, projectName: 'my-project', // Optional: specify which project to send traces to apiUrl: 'https://api.smith.langchain.com', logLevel: 'info', }) ``` ## 環境變數 | 變數 | 說明 | | -------------------- | ------------------------------------ | | `LANGSMITH_API_KEY` | 你的 LangSmith API 金鑰 | | `LANGCHAIN_PROJECT` | Trace 的預設項目名稱(未指定 `projectName` 時使用) | | `LANGSMITH_BASE_URL` | 自行託管實例的 API URL | ## Span 類型對應 | Span 類型 | LangSmith 類型 | | -------------------- | ------------ | | `MODEL_GENERATION` | `llm` | | `MODEL_CHUNK` | `llm` | | `TOOL_CALL` | `tool` | | `MCP_TOOL_CALL` | `tool` | | `PROVIDER_TOOL_CALL` | `tool` | | 所有其他類型 | `chain` | ## `withLangsmithMetadata` 用於將 LangSmith 特定 metadata 加至 tracing 選項的輔助函式。 ```typescript function withLangsmithMetadata(metadata: LangSmithMetadataInput): TracingOptionsUpdater ``` ### `LangSmithMetadataInput` ### 使用方式 ```typescript import { buildTracingOptions } from '@mastra/observability' import { withLangsmithMetadata } from '@mastra/langsmith' // Route to a specific project const tracingOptions = buildTracingOptions(withLangsmithMetadata({ projectName: 'my-project' })) // Set multiple fields const tracingOptions = buildTracingOptions( withLangsmithMetadata({ projectName: 'my-project', sessionId: 'user-123', }), ) ```