|
3 | 3 | [](https://www.npmjs.com/package/@cozeloop/langchain) |
4 | 4 | [](https://opensource.org/licenses/MIT) |
5 | 5 |
|
6 | | -Integrate Langchain with Cozeloop via `@cozeloop/langchain`, supports: |
7 | | -* `CozeloopCallbackHandler`: report trace to Cozeloop |
| 6 | +Official LangChain integration for [CozeLoop](https://loop.coze.cn) - seamlessly report traces from your LangChain and LangGraph applications. |
8 | 7 |
|
9 | | -## Quick Start |
| 8 | +## Features |
10 | 9 |
|
11 | | -### 1. Installation |
| 10 | +- **CozeloopCallbackHandler**: Automatically capture and report traces to CozeLoop |
| 11 | +- Support for both LangChain and LangGraph |
| 12 | +- W3C trace context propagation for distributed tracing |
| 13 | + |
| 14 | +## Installation |
12 | 15 |
|
13 | 16 | ```sh |
14 | 17 | npm install @cozeloop/langchain |
15 | 18 | # or |
16 | 19 | pnpm install @cozeloop/langchain |
17 | 20 | ``` |
18 | 21 |
|
19 | | -### 2. Basic Usage |
20 | | - |
21 | | -1. Environment variables |
22 | | - |
23 | | -The following variables are optional, and will be used if values provided. |
| 22 | +## Configuration |
24 | 23 |
|
25 | | -|Variable|Comment|Example| |
26 | | -|----|----|------| |
27 | | -|COZELOOP_WORKSPACE_ID|Cozeloop workspace id, used to identify the workspace to which resource such as trace belongs|'7487806534651887643'| |
28 | | -|COZELOOP_API_TOKEN|Cozeloop api token, see [authentication-for-sdk](https://loop.coze.cn/open/docs/cozeloop/authentication-for-sdk) |'pat_xxxx'| |
29 | | -|COZELOOP_OTLP_ENDPOINT|Trace endpoint|'https://api.coze.cn/v1/loop/opentelemetry/v1/trace'| |
| 24 | +### Environment Variables |
30 | 25 |
|
| 26 | +The following environment variables can be used to configure the integration: |
31 | 27 |
|
32 | | -2. `CozeloopCallbackHandler` |
| 28 | +| Variable | Description | Example | |
| 29 | +|----------|-------------|---------| |
| 30 | +| `COZELOOP_WORKSPACE_ID` | Workspace ID for resource association | `'7487806534651887643'` | |
| 31 | +| `COZELOOP_API_TOKEN` | API token for authentication. See [Authentication Guide](https://loop.coze.cn/open/docs/cozeloop/authentication-for-sdk) | `'pat_xxxx'` | |
| 32 | +| `COZELOOP_OTLP_ENDPOINT` | Trace reporting endpoint | `'https://api.coze.cn/v1/loop/opentelemetry/v1/traces'` | |
33 | 33 |
|
34 | | -Callbacks can be used with LangChain and LangGraph. |
| 34 | +## Usage |
35 | 35 |
|
36 | | -Since the traces exporting is asynchronous, it can be interrupted by the termination of Node.js process. |
| 36 | +### CozeloopCallbackHandler |
37 | 37 |
|
38 | | -(1) When you run a file in command line, call `await callback.flush();` to ensure all traces exported. |
| 38 | +The callback handler integrates with LangChain and LangGraph to automatically capture traces. |
39 | 39 |
|
40 | | -(2) If you are running a server-side application, there is no need to flush, but it's recommended to call `await callback.shutdown();` to clean resource. |
| 40 | +> **Note**: Since trace exporting is asynchronous, proper cleanup is required: |
| 41 | +> |
| 42 | +> - **CLI scripts**: Call `await callback.flush()` before exit to ensure all traces are exported. |
| 43 | +> - **Server applications**: Call `await callback.shutdown()` during graceful shutdown to release resources. |
41 | 44 |
|
42 | | -* Initialize |
| 45 | +#### Initialization |
43 | 46 |
|
44 | 47 | ```typescript |
45 | 48 | import { CozeloopCallbackHandler } from '@cozeloop/langchain'; |
46 | 49 |
|
47 | | -// initialize callback |
48 | 50 | const callback = new CozeloopCallbackHandler({ |
49 | | - // Common callback params |
| 51 | + // Span exporter configuration |
| 52 | + spanExporter: { |
| 53 | + workspaceId: 'xxx', |
| 54 | + token: 'pat_xxx', |
| 55 | + traceEndpoint: 'https://api.coze.cn/v1/loop/opentelemetry/v1/traces', |
| 56 | + }, |
| 57 | + // Optional: Filter specific trace types |
50 | 58 | // ignoreAgent: false, |
51 | 59 | // ignoreChain: false, |
52 | 60 | // ignoreCustomEvent: false, |
53 | 61 | // ignoreLLM: false, |
54 | 62 | // ignoreRetriever: false, |
55 | 63 | // ignorePrompt: false, |
56 | | - /** Span exporter */ |
57 | | - spanExporter: { |
58 | | - workspaceId: 'xxx', |
59 | | - token: 'pat_xxx', |
60 | | - traceEndpoint: 'https://api.coze.cn/v1/loop/opentelemetry/v1/traces', |
61 | | - }, |
62 | | - /** Propagate with upstream services */ |
| 64 | + |
| 65 | + // Optional: Propagate trace context from upstream services |
63 | 66 | // propagationHeaders: { |
64 | | - // tracestate: '', |
65 | 67 | // traceparent: '00-b3691bfe8af1415029177821d4114cef-ddd0307891d51ce3-01', |
| 68 | + // tracestate: '', |
66 | 69 | // }, |
67 | 70 | }); |
68 | 71 |
|
69 | | -// use to propagate with downstream services |
70 | | -// callback.w3cPropagationHeaders |
| 72 | +// Access W3C propagation headers for downstream services |
| 73 | +// const headers = callback.w3cPropagationHeaders; |
71 | 74 | ``` |
72 | 75 |
|
73 | | -* With LangChain |
| 76 | +#### With LangChain |
| 77 | + |
74 | 78 | ```typescript |
75 | 79 | import { CozeloopCallbackHandler } from '@cozeloop/langchain'; |
76 | 80 | import { ChatPromptTemplate } from '@langchain/core/prompts'; |
77 | 81 |
|
78 | | -// Chain |
| 82 | +const callback = new CozeloopCallbackHandler({ /* options */ }); |
| 83 | + |
79 | 84 | const prompt = ChatPromptTemplate.fromTemplate('What is 1 + {number}?'); |
80 | 85 | const model = new CustomLLM({}); |
81 | 86 | const chain = prompt.pipe(model); |
82 | | -const callback = new CozeloopCallbackHandler({ /** options */}); |
83 | 87 |
|
84 | 88 | const resp = await chain.invoke( |
85 | 89 | { number: 1 }, |
86 | 90 | { |
87 | 91 | runName: 'SuperChain', |
88 | | - callbacks: [callback], // set callback |
| 92 | + callbacks: [callback], |
89 | 93 | }, |
90 | 94 | ); |
91 | 95 |
|
92 | | -// call `flush` to ensure trace exported in script run |
| 96 | +// Ensure traces are exported before script exits |
93 | 97 | await callback.flush(); |
94 | 98 | ``` |
95 | 99 |
|
96 | | -* With LangGraph |
| 100 | +#### With LangGraph |
| 101 | + |
97 | 102 | ```typescript |
98 | 103 | import { CozeloopCallbackHandler } from '@cozeloop/langchain'; |
99 | | - |
100 | 104 | import { createReactAgent } from '@langchain/langgraph/prebuilt'; |
101 | 105 |
|
| 106 | +const callback = new CozeloopCallbackHandler({ /* options */ }); |
| 107 | + |
102 | 108 | const agent = createReactAgent({ |
103 | 109 | llm: model, |
104 | 110 | tools: [tool1, tool2], |
105 | 111 | }); |
106 | 112 |
|
107 | | -const resp = await graphAgent.invoke( |
| 113 | +const resp = await agent.invoke( |
108 | 114 | { |
109 | | - messages: [ |
110 | | - { |
111 | | - role: 'user', |
112 | | - content: 'xxx', |
113 | | - }, |
114 | | - ], |
| 115 | + messages: [{ role: 'user', content: 'Hello!' }], |
115 | 116 | }, |
116 | | - { callbacks: [callback] }, // set callback |
| 117 | + { callbacks: [callback] }, |
117 | 118 | ); |
118 | 119 |
|
119 | | -// call `flush` to ensure trace exported in script run |
| 120 | +// Ensure traces are exported before script exits |
120 | 121 | await callback.flush(); |
121 | 122 | ``` |
0 commit comments