langwatch 0.2.0 → 0.3.0-prerelease.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.editorconfig +16 -0
- package/LICENSE +7 -0
- package/README.md +268 -1
- package/copy-types.sh +19 -8
- package/examples/langchain/.env.example +2 -0
- package/examples/langchain/README.md +42 -0
- package/examples/langchain/package-lock.json +2930 -0
- package/examples/langchain/package.json +27 -0
- package/examples/langchain/src/cli-markdown.d.ts +137 -0
- package/examples/langchain/src/index.ts +109 -0
- package/examples/langchain/tsconfig.json +25 -0
- package/examples/langgraph/.env.example +2 -0
- package/examples/langgraph/README.md +42 -0
- package/examples/langgraph/package-lock.json +3031 -0
- package/examples/langgraph/package.json +28 -0
- package/examples/langgraph/src/cli-markdown.d.ts +137 -0
- package/examples/langgraph/src/index.ts +196 -0
- package/examples/langgraph/tsconfig.json +25 -0
- package/examples/mastra/.env.example +2 -0
- package/examples/mastra/README.md +57 -0
- package/examples/mastra/package-lock.json +5296 -0
- package/examples/mastra/package.json +32 -0
- package/examples/mastra/src/cli-markdown.d.ts +137 -0
- package/examples/mastra/src/index.ts +120 -0
- package/examples/mastra/src/mastra/agents/weather-agent.ts +30 -0
- package/examples/mastra/src/mastra/index.ts +21 -0
- package/examples/mastra/src/mastra/tools/weather-tool.ts +102 -0
- package/examples/mastra/tsconfig.json +25 -0
- package/examples/vercel-ai/.env.example +2 -0
- package/examples/vercel-ai/README.md +38 -0
- package/examples/vercel-ai/package-lock.json +2571 -0
- package/examples/vercel-ai/package.json +27 -0
- package/examples/vercel-ai/src/cli-markdown.d.ts +137 -0
- package/examples/vercel-ai/src/index.ts +110 -0
- package/examples/vercel-ai/src/instrumentation.ts +9 -0
- package/examples/vercel-ai/tsconfig.json +25 -0
- package/package.json +78 -34
- package/src/__tests__/client-browser.test.ts +92 -0
- package/src/__tests__/client-node.test.ts +76 -0
- package/src/__tests__/client.test.ts +71 -0
- package/src/__tests__/integration/client-browser.test.ts +46 -0
- package/src/__tests__/integration/client-node.test.ts +46 -0
- package/src/client-browser.ts +70 -0
- package/src/client-node.ts +82 -0
- package/src/client-shared.ts +72 -0
- package/src/client.ts +119 -0
- package/src/evaluation/__tests__/record-evaluation.test.ts +112 -0
- package/src/evaluation/__tests__/run-evaluation.test.ts +171 -0
- package/src/evaluation/index.ts +2 -0
- package/src/evaluation/record-evaluation.ts +101 -0
- package/src/evaluation/run-evaluation.ts +133 -0
- package/src/evaluation/tracer.ts +3 -0
- package/src/evaluation/types.ts +23 -0
- package/src/index.ts +10 -593
- package/src/internal/api/__tests__/errors.test.ts +98 -0
- package/src/internal/api/client.ts +30 -0
- package/src/internal/api/errors.ts +32 -0
- package/src/internal/generated/types/.gitkeep +0 -0
- package/src/observability/__tests__/integration/base.test.ts +74 -0
- package/src/observability/__tests__/integration/browser-setup-ordering.test.ts +60 -0
- package/src/observability/__tests__/integration/complex-nested-spans.test.ts +29 -0
- package/src/observability/__tests__/integration/error-handling.test.ts +24 -0
- package/src/observability/__tests__/integration/langwatch-disabled-otel.test.ts +24 -0
- package/src/observability/__tests__/integration/langwatch-first-then-vercel.test.ts +24 -0
- package/src/observability/__tests__/integration/multiple-setup-attempts.test.ts +27 -0
- package/src/observability/__tests__/integration/otel-ordering.test.ts +27 -0
- package/src/observability/__tests__/integration/vercel-configurations.test.ts +20 -0
- package/src/observability/__tests__/integration/vercel-first-then-langwatch.test.ts +27 -0
- package/src/observability/__tests__/span.test.ts +214 -0
- package/src/observability/__tests__/trace.test.ts +180 -0
- package/src/observability/exporters/index.ts +1 -0
- package/src/observability/exporters/langwatch-exporter.ts +53 -0
- package/src/observability/index.ts +4 -0
- package/src/observability/instrumentation/langchain/__tests__/integration/langchain-chatbot.test.ts +112 -0
- package/src/observability/instrumentation/langchain/__tests__/langchain.test.ts +284 -0
- package/src/observability/instrumentation/langchain/index.ts +624 -0
- package/src/observability/processors/__tests__/filterable-batch-span-exporter.test.ts +98 -0
- package/src/observability/processors/filterable-batch-span-processor.ts +99 -0
- package/src/observability/processors/index.ts +1 -0
- package/src/observability/semconv/attributes.ts +185 -0
- package/src/observability/semconv/events.ts +42 -0
- package/src/observability/semconv/index.ts +16 -0
- package/src/observability/semconv/values.ts +159 -0
- package/src/observability/span.ts +728 -0
- package/src/observability/trace.ts +301 -0
- package/src/prompt/__tests__/prompt.test.ts +139 -0
- package/src/prompt/get-prompt-version.ts +49 -0
- package/src/prompt/get-prompt.ts +44 -0
- package/src/prompt/index.ts +3 -0
- package/src/prompt/prompt.ts +133 -0
- package/src/prompt/service.ts +221 -0
- package/src/prompt/tracer.ts +3 -0
- package/src/prompt/types.ts +0 -0
- package/ts-to-zod.config.js +11 -0
- package/tsconfig.json +3 -9
- package/tsup.config.ts +11 -1
- package/vitest.config.ts +1 -0
- package/dist/chunk-LKD2K67J.mjs +0 -717
- package/dist/chunk-LKD2K67J.mjs.map +0 -1
- package/dist/index.d.mts +0 -1030
- package/dist/index.d.ts +0 -1030
- package/dist/index.js +0 -27310
- package/dist/index.js.map +0 -1
- package/dist/index.mjs +0 -963
- package/dist/index.mjs.map +0 -1
- package/dist/utils-Cv-rUjJ1.d.mts +0 -313
- package/dist/utils-Cv-rUjJ1.d.ts +0 -313
- package/dist/utils.d.mts +0 -2
- package/dist/utils.d.ts +0 -2
- package/dist/utils.js +0 -709
- package/dist/utils.js.map +0 -1
- package/dist/utils.mjs +0 -11
- package/dist/utils.mjs.map +0 -1
- package/example/.env.example +0 -12
- package/example/.eslintrc.json +0 -26
- package/example/LICENSE +0 -13
- package/example/README.md +0 -12
- package/example/app/(chat)/chat/[id]/page.tsx +0 -60
- package/example/app/(chat)/layout.tsx +0 -14
- package/example/app/(chat)/page.tsx +0 -27
- package/example/app/actions.ts +0 -156
- package/example/app/globals.css +0 -76
- package/example/app/guardrails/page.tsx +0 -26
- package/example/app/langchain/page.tsx +0 -27
- package/example/app/langchain-rag/page.tsx +0 -28
- package/example/app/late-update/page.tsx +0 -27
- package/example/app/layout.tsx +0 -64
- package/example/app/login/actions.ts +0 -71
- package/example/app/login/page.tsx +0 -18
- package/example/app/manual/page.tsx +0 -27
- package/example/app/new/page.tsx +0 -5
- package/example/app/opengraph-image.png +0 -0
- package/example/app/share/[id]/page.tsx +0 -58
- package/example/app/signup/actions.ts +0 -111
- package/example/app/signup/page.tsx +0 -18
- package/example/app/twitter-image.png +0 -0
- package/example/auth.config.ts +0 -42
- package/example/auth.ts +0 -45
- package/example/components/button-scroll-to-bottom.tsx +0 -36
- package/example/components/chat-history.tsx +0 -49
- package/example/components/chat-list.tsx +0 -52
- package/example/components/chat-message-actions.tsx +0 -40
- package/example/components/chat-message.tsx +0 -80
- package/example/components/chat-panel.tsx +0 -139
- package/example/components/chat-share-dialog.tsx +0 -95
- package/example/components/chat.tsx +0 -84
- package/example/components/clear-history.tsx +0 -75
- package/example/components/empty-screen.tsx +0 -38
- package/example/components/external-link.tsx +0 -29
- package/example/components/footer.tsx +0 -19
- package/example/components/header.tsx +0 -114
- package/example/components/login-button.tsx +0 -42
- package/example/components/login-form.tsx +0 -97
- package/example/components/markdown.tsx +0 -9
- package/example/components/prompt-form.tsx +0 -115
- package/example/components/providers.tsx +0 -17
- package/example/components/sidebar-actions.tsx +0 -125
- package/example/components/sidebar-desktop.tsx +0 -19
- package/example/components/sidebar-footer.tsx +0 -16
- package/example/components/sidebar-item.tsx +0 -124
- package/example/components/sidebar-items.tsx +0 -42
- package/example/components/sidebar-list.tsx +0 -38
- package/example/components/sidebar-mobile.tsx +0 -31
- package/example/components/sidebar-toggle.tsx +0 -24
- package/example/components/sidebar.tsx +0 -21
- package/example/components/signup-form.tsx +0 -95
- package/example/components/stocks/events-skeleton.tsx +0 -31
- package/example/components/stocks/events.tsx +0 -30
- package/example/components/stocks/index.tsx +0 -36
- package/example/components/stocks/message.tsx +0 -134
- package/example/components/stocks/spinner.tsx +0 -16
- package/example/components/stocks/stock-purchase.tsx +0 -146
- package/example/components/stocks/stock-skeleton.tsx +0 -22
- package/example/components/stocks/stock.tsx +0 -210
- package/example/components/stocks/stocks-skeleton.tsx +0 -9
- package/example/components/stocks/stocks.tsx +0 -67
- package/example/components/tailwind-indicator.tsx +0 -14
- package/example/components/theme-toggle.tsx +0 -31
- package/example/components/ui/alert-dialog.tsx +0 -141
- package/example/components/ui/badge.tsx +0 -36
- package/example/components/ui/button.tsx +0 -57
- package/example/components/ui/codeblock.tsx +0 -148
- package/example/components/ui/dialog.tsx +0 -122
- package/example/components/ui/dropdown-menu.tsx +0 -205
- package/example/components/ui/icons.tsx +0 -507
- package/example/components/ui/input.tsx +0 -25
- package/example/components/ui/label.tsx +0 -26
- package/example/components/ui/select.tsx +0 -164
- package/example/components/ui/separator.tsx +0 -31
- package/example/components/ui/sheet.tsx +0 -140
- package/example/components/ui/sonner.tsx +0 -31
- package/example/components/ui/switch.tsx +0 -29
- package/example/components/ui/textarea.tsx +0 -24
- package/example/components/ui/tooltip.tsx +0 -30
- package/example/components/user-menu.tsx +0 -53
- package/example/components.json +0 -17
- package/example/instrumentation.ts +0 -11
- package/example/lib/chat/guardrails.tsx +0 -181
- package/example/lib/chat/langchain-rag.tsx +0 -191
- package/example/lib/chat/langchain.tsx +0 -112
- package/example/lib/chat/late-update.tsx +0 -208
- package/example/lib/chat/manual.tsx +0 -605
- package/example/lib/chat/vercel-ai.tsx +0 -576
- package/example/lib/hooks/use-copy-to-clipboard.tsx +0 -33
- package/example/lib/hooks/use-enter-submit.tsx +0 -23
- package/example/lib/hooks/use-local-storage.ts +0 -24
- package/example/lib/hooks/use-scroll-anchor.tsx +0 -86
- package/example/lib/hooks/use-sidebar.tsx +0 -60
- package/example/lib/hooks/use-streamable-text.ts +0 -25
- package/example/lib/types.ts +0 -41
- package/example/lib/utils.ts +0 -89
- package/example/middleware.ts +0 -8
- package/example/next-env.d.ts +0 -5
- package/example/next.config.js +0 -16
- package/example/package-lock.json +0 -10917
- package/example/package.json +0 -84
- package/example/pnpm-lock.yaml +0 -5712
- package/example/postcss.config.js +0 -6
- package/example/prettier.config.cjs +0 -34
- package/example/public/apple-touch-icon.png +0 -0
- package/example/public/favicon-16x16.png +0 -0
- package/example/public/favicon.ico +0 -0
- package/example/public/next.svg +0 -1
- package/example/public/thirteen.svg +0 -1
- package/example/public/vercel.svg +0 -1
- package/example/tailwind.config.ts +0 -81
- package/example/tsconfig.json +0 -35
- package/src/LangWatchExporter.ts +0 -96
- package/src/evaluations.ts +0 -219
- package/src/index.test.ts +0 -402
- package/src/langchain.ts +0 -557
- package/src/typeUtils.ts +0 -89
- package/src/types.ts +0 -82
- package/src/utils.ts +0 -205
- /package/src/{server/types → internal/generated/openapi}/.gitkeep +0 -0
package/.editorconfig
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
root = true
|
|
2
|
+
|
|
3
|
+
[*]
|
|
4
|
+
end_of_line = lf
|
|
5
|
+
insert_final_newline = true
|
|
6
|
+
trim_trailing_whitespace = true
|
|
7
|
+
charset = utf-8
|
|
8
|
+
indent_style = space
|
|
9
|
+
indent_size = 4
|
|
10
|
+
|
|
11
|
+
[*.ts]
|
|
12
|
+
indent_size = 2
|
|
13
|
+
indent_style = space
|
|
14
|
+
|
|
15
|
+
[*.md]
|
|
16
|
+
trim_trailing_whitespace = true
|
package/LICENSE
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
Copyright 2025 LangWatch
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,270 @@
|
|
|
1
1
|
# LangWatch TypeScript SDK
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
<p align="center">
|
|
4
|
+
<img src="https://app.langwatch.ai/logo.svg" alt="LangWatch Logo" width="120"/>
|
|
5
|
+
</p>
|
|
6
|
+
|
|
7
|
+
<p align="center">
|
|
8
|
+
<b>Observability, Prompt Management, and Evaluation for JS LLM/GenAI Apps</b><br/>
|
|
9
|
+
<a href="https://langwatch.ai">langwatch.ai</a> | <a href="https://github.com/langwatch/langwatch">GitHub</a>
|
|
10
|
+
</p>
|
|
11
|
+
|
|
12
|
+
<p align="center">
|
|
13
|
+
<a href="https://www.npmjs.com/package/langwatch"><img src="https://img.shields.io/npm/v/langwatch.svg?style=flat-square" alt="npm version"></a>
|
|
14
|
+
<a href="https://opensource.org/licenses/Apache-2.0"><img src="https://img.shields.io/badge/license-Apache--2.0-blue.svg?style=flat-square" alt="License"></a>
|
|
15
|
+
</p>
|
|
16
|
+
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
LangWatch helps you monitor, debug, and optimize your LLM/GenAI applications. This TypeScript SDK provides:
|
|
20
|
+
|
|
21
|
+
- **OpenTelemetry-based tracing** for LLM, RAG, tool, and workflow spans
|
|
22
|
+
- **Prompt management** with versioning and variable interpolation
|
|
23
|
+
- **Automated and custom evaluation** of model outputs
|
|
24
|
+
|
|
25
|
+
---
|
|
26
|
+
|
|
27
|
+
## Features
|
|
28
|
+
|
|
29
|
+
- 📊 **Observability**: Trace LLM, RAG, tool, and workflow operations with rich context
|
|
30
|
+
- 📝 **Prompt Management**: Fetch, version, and interpolate prompts with variables
|
|
31
|
+
- 🧪 **Evaluation**: Run and record evaluations, with results linked to traces
|
|
32
|
+
- 🔌 **OpenTelemetry**: Integrates with your existing observability stack
|
|
33
|
+
- 🦾 **TypeScript-first**: Full type safety and modern API design
|
|
34
|
+
|
|
35
|
+
---
|
|
36
|
+
|
|
37
|
+
## Installation
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
npm install langwatch
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
---
|
|
44
|
+
|
|
45
|
+
## 🚀 Getting Started
|
|
46
|
+
|
|
47
|
+
Here's the fastest way to get LangWatch working in your app:
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
import { setupLangWatch } from "langwatch/node";
|
|
51
|
+
import { getLangWatchTracer } from "langwatch";
|
|
52
|
+
|
|
53
|
+
// 1. Initialize LangWatch (Node.js example)
|
|
54
|
+
await setupLangWatch({ apiKey: "YOUR_API_KEY" }); // By default, this will read the LANGWATCH_API_KEY environment variable
|
|
55
|
+
|
|
56
|
+
// 2. Create a tracer and span
|
|
57
|
+
const tracer = getLangWatchTracer("my-app");
|
|
58
|
+
const span = tracer.startSpan("my-operation");
|
|
59
|
+
span.setInput("User prompt");
|
|
60
|
+
span.setOutput("Model response");
|
|
61
|
+
span.end();
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
> **Tip:** For use in the browser, use `import { setupLangWatch } from "langwatch/browser"` instead.
|
|
65
|
+
|
|
66
|
+
---
|
|
67
|
+
|
|
68
|
+
## Quick Start: Core Features
|
|
69
|
+
|
|
70
|
+
### 1. Tracing & Observability
|
|
71
|
+
|
|
72
|
+
- **Get a tracer:**
|
|
73
|
+
```ts
|
|
74
|
+
const tracer = getLangWatchTracer("my-app");
|
|
75
|
+
```
|
|
76
|
+
- **Start a span and record input/output:**
|
|
77
|
+
```ts
|
|
78
|
+
const span = tracer.startSpan("call-llm");
|
|
79
|
+
span.setType("llm");
|
|
80
|
+
span.setInput("User prompt"); // Main way to record input
|
|
81
|
+
span.setOutput("Model response"); // Main way to record output
|
|
82
|
+
span.end();
|
|
83
|
+
```
|
|
84
|
+
> **Note:** `setInput` and `setOutput` are the primary methods to record input/output. Use `setInputString`/`setOutputString` for plain text, or pass any serializable value.
|
|
85
|
+
|
|
86
|
+
- **Use withActiveSpan for automatic error handling:**
|
|
87
|
+
```ts
|
|
88
|
+
await tracer.withActiveSpan("my-operation", async (span) => {
|
|
89
|
+
span.setType("llm");
|
|
90
|
+
span.setInput("User prompt");
|
|
91
|
+
// ... your code ...
|
|
92
|
+
span.setOutput("Model response");
|
|
93
|
+
});
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
- **Record an evaluation directly on a span:**
|
|
97
|
+
```ts
|
|
98
|
+
span.recordEvaluation({ name: "My Eval", passed: true, score: 1.0 });
|
|
99
|
+
```
|
|
100
|
+
> **Note:** This associates evaluation results with a specific span (operation or model call).
|
|
101
|
+
|
|
102
|
+
- **(Optional) Add GenAI message events:**
|
|
103
|
+
```ts
|
|
104
|
+
span.addGenAISystemMessageEvent({ content: "You are a helpful assistant." });
|
|
105
|
+
span.addGenAIUserMessageEvent({ content: "Hello!" });
|
|
106
|
+
span.addGenAIAssistantMessageEvent({ content: "Hi! How can I help you?" });
|
|
107
|
+
span.addGenAIToolMessageEvent({ content: "Tool result", id: "tool-1" });
|
|
108
|
+
span.addGenAIChoiceEvent({ finish_reason: "stop", index: 0, message: { content: "Response" } });
|
|
109
|
+
```
|
|
110
|
+
> **Advanced:** The `addGenAI...` methods are optional and mainly for advanced/manual instrumentation. Most users do not need these unless you want fine-grained message event logs.
|
|
111
|
+
|
|
112
|
+
- **RAG context, metrics, and model information:**
|
|
113
|
+
```ts
|
|
114
|
+
span.setRAGContexts([{ document_id: "doc1", chunk_id: "c1", content: "..." }]);
|
|
115
|
+
span.setMetrics({ promptTokens: 10, completionTokens: 20, cost: 0.002 });
|
|
116
|
+
span.setRequestModel("gpt-4");
|
|
117
|
+
span.setResponseModel("gpt-4");
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### 2. Prompt Management
|
|
121
|
+
|
|
122
|
+
- **Fetch and format a prompt:**
|
|
123
|
+
```ts
|
|
124
|
+
import { getPrompt } from "langwatch/prompt";
|
|
125
|
+
const prompt = await getPrompt("prompt-id");
|
|
126
|
+
const compiledPrompt = await getPrompt("prompt-id", { user: "Alice" });
|
|
127
|
+
```
|
|
128
|
+
- **Fetch a specific prompt version:**
|
|
129
|
+
```ts
|
|
130
|
+
import { getPromptVersion } from "langwatch/prompt";
|
|
131
|
+
const compiledPrompt = await getPromptVersion("prompt-id", "version-id", {
|
|
132
|
+
user: "Alice",
|
|
133
|
+
});
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
> **Note:** The prompt APIs (`getPrompt`, `getPromptVersion`) automatically create spans and add useful tracing information.
|
|
137
|
+
|
|
138
|
+
### 3. Evaluation
|
|
139
|
+
|
|
140
|
+
- **Run an evaluation:**
|
|
141
|
+
```ts
|
|
142
|
+
import { runEvaluation } from "langwatch/evaluation";
|
|
143
|
+
const result = await runEvaluation({
|
|
144
|
+
slug: "helpfulness",
|
|
145
|
+
data: { input: "Hi", output: "Hello!" }
|
|
146
|
+
});
|
|
147
|
+
```
|
|
148
|
+
- **Record a custom evaluation:**
|
|
149
|
+
```ts
|
|
150
|
+
import { recordEvaluation } from "langwatch/evaluation";
|
|
151
|
+
recordEvaluation({
|
|
152
|
+
name: "Manual Eval",
|
|
153
|
+
passed: true,
|
|
154
|
+
score: 0.9,
|
|
155
|
+
details: "Looks good!"
|
|
156
|
+
});
|
|
157
|
+
```
|
|
158
|
+
> **Note:** The evaluation APIs (`runEvaluation`, `recordEvaluation`) also create spans and add tracing/evaluation info automatically.
|
|
159
|
+
|
|
160
|
+
### 4. LangChain Integration
|
|
161
|
+
|
|
162
|
+
- **Use with LangChain:**
|
|
163
|
+
```ts
|
|
164
|
+
import { LangWatchCallbackHandler } from "langwatch/observability/instrumentation/langchain";
|
|
165
|
+
|
|
166
|
+
const chatModel = new ChatOpenAI({
|
|
167
|
+
callbacks: [new LangWatchCallbackHandler()],
|
|
168
|
+
});
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
---
|
|
172
|
+
|
|
173
|
+
## API Reference
|
|
174
|
+
|
|
175
|
+
### Setup
|
|
176
|
+
- `setupLangWatch(options?)` → Initialize LangWatch (from `langwatch/node` or `langwatch/browser`)
|
|
177
|
+
|
|
178
|
+
### Observability
|
|
179
|
+
- `getLangWatchTracer(name, version?)` → `LangWatchTracer`
|
|
180
|
+
- `LangWatchTracer` methods: `.startSpan()`, `.startActiveSpan()`, `.withActiveSpan()`
|
|
181
|
+
- `LangWatchSpan` methods:
|
|
182
|
+
- `.setType()`, `.setInput()`, `.setOutput()`, `.setInputString()`, `.setOutputString()`
|
|
183
|
+
- `.recordEvaluation()`, `.setRequestModel()`, `.setResponseModel()`
|
|
184
|
+
- `.setRAGContexts()`, `.setRAGContext()`, `.setMetrics()`, `.setSelectedPrompt()`
|
|
185
|
+
- `.addGenAISystemMessageEvent()`, `.addGenAIUserMessageEvent()`, `.addGenAIAssistantMessageEvent()`, `.addGenAIToolMessageEvent()`, `.addGenAIChoiceEvent()`
|
|
186
|
+
|
|
187
|
+
### Prompt
|
|
188
|
+
- `getPrompt(promptId, variables?)` → fetches and formats a prompt (creates a span automatically)
|
|
189
|
+
- `getPromptVersion(promptId, versionId, variables?)` → fetches specific prompt version
|
|
190
|
+
|
|
191
|
+
### Evaluation
|
|
192
|
+
- `runEvaluation(details)` → runs an evaluation and returns result (creates a span automatically)
|
|
193
|
+
- `recordEvaluation(details, attributes?)` → records a custom evaluation span (creates a span automatically)
|
|
194
|
+
|
|
195
|
+
### LangChain Integration
|
|
196
|
+
- `LangWatchCallbackHandler` → LangChain callback handler for automatic instrumentation
|
|
197
|
+
|
|
198
|
+
### Exporters & Processors
|
|
199
|
+
- `LangWatchExporter` → Custom OpenTelemetry exporter
|
|
200
|
+
- `FilterableBatchSpanProcessor` → Span processor with filtering capabilities
|
|
201
|
+
|
|
202
|
+
---
|
|
203
|
+
|
|
204
|
+
## Types
|
|
205
|
+
|
|
206
|
+
### Core Types
|
|
207
|
+
- `LangWatchSpan` → Extended OpenTelemetry span with LangWatch methods
|
|
208
|
+
- `LangWatchTracer` → Extended OpenTelemetry tracer with LangWatch methods
|
|
209
|
+
- `SpanType` → Union of supported span types (`"llm"`, `"chain"`, `"tool"`, `"agent"`, etc.)
|
|
210
|
+
|
|
211
|
+
### Evaluation Types
|
|
212
|
+
- `EvaluationDetails` → Configuration for running evaluations
|
|
213
|
+
- `SingleEvaluationResult` → Result from evaluation runs
|
|
214
|
+
- `RecordedEvaluationDetails` → Configuration for recording custom evaluations
|
|
215
|
+
|
|
216
|
+
### Prompt Types
|
|
217
|
+
- `Prompt` → Prompt object with compilation capabilities
|
|
218
|
+
- `CompiledPrompt` → Compiled prompt with variables interpolated
|
|
219
|
+
- `TemplateVariables` → Variables for prompt compilation
|
|
220
|
+
|
|
221
|
+
### RAG & Metrics Types
|
|
222
|
+
- `LangWatchSpanRAGContext` → RAG context structure
|
|
223
|
+
- `LangWatchSpanMetrics` → Metrics structure (tokens, cost)
|
|
224
|
+
- `LangWatchSpanGenAI*EventBody` → GenAI message event structures
|
|
225
|
+
|
|
226
|
+
---
|
|
227
|
+
|
|
228
|
+
## Advanced
|
|
229
|
+
|
|
230
|
+
### Custom OpenTelemetry Integration
|
|
231
|
+
```ts
|
|
232
|
+
import { FilterableBatchSpanProcessor, LangWatchExporter } from "langwatch";
|
|
233
|
+
|
|
234
|
+
const processor = new FilterableBatchSpanProcessor(
|
|
235
|
+
new LangWatchExporter(apiKey, endpoint),
|
|
236
|
+
excludeRules
|
|
237
|
+
);
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
### Span Processing Rules
|
|
241
|
+
```ts
|
|
242
|
+
const excludeRules: SpanProcessingExcludeRule[] = [
|
|
243
|
+
{ attribute: "http.url", value: "/health" },
|
|
244
|
+
{ attribute: "span.type", value: "health" }
|
|
245
|
+
];
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
### Manual Instrumentation
|
|
249
|
+
```ts
|
|
250
|
+
import { semconv } from "langwatch/observability";
|
|
251
|
+
|
|
252
|
+
span.setAttributes({
|
|
253
|
+
[semconv.ATTR_LANGWATCH_THREAD_ID]: threadId,
|
|
254
|
+
});
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
---
|
|
258
|
+
|
|
259
|
+
## Community & Support
|
|
260
|
+
|
|
261
|
+
- [LangWatch Website](https://langwatch.ai)
|
|
262
|
+
- [Documentation](https://docs.langwatch.ai)
|
|
263
|
+
- [GitHub Issues](https://github.com/langwatch/langwatch/issues)
|
|
264
|
+
- [Discord Community](https://discord.gg/langwatch)
|
|
265
|
+
|
|
266
|
+
---
|
|
267
|
+
|
|
268
|
+
## License
|
|
269
|
+
|
|
270
|
+
This project is licensed under the MIT License - see the [LICENSE](./LICENSE) file for details.
|
package/copy-types.sh
CHANGED
|
@@ -1,17 +1,28 @@
|
|
|
1
|
+
#!/bin/bash
|
|
2
|
+
|
|
1
3
|
set -e
|
|
2
4
|
|
|
3
|
-
cp ../langwatch/src/server/tracer/types.ts src/
|
|
4
|
-
ts-to-zod src/
|
|
5
|
+
cp ../langwatch/src/server/tracer/types.ts src/internal/generated/types/tracer.ts
|
|
6
|
+
ts-to-zod src/internal/generated/types/tracer.ts src/internal/generated/types/tracer.generated.ts
|
|
5
7
|
|
|
6
|
-
cp ../langwatch/src/server/evaluations/types.ts src/
|
|
8
|
+
cp ../langwatch/src/server/evaluations/types.ts src/internal/generated/types/evaluations.ts
|
|
7
9
|
if [[ "$OSTYPE" == "darwin"* ]]; then
|
|
8
|
-
sed -i '' 's/\.\.\/tracer\/types\.generated/.\/tracer.generated/g' src/
|
|
10
|
+
sed -i '' 's/\.\.\/tracer\/types\.generated/.\/tracer.generated/g' src/internal/generated/types/evaluations.ts
|
|
9
11
|
else
|
|
10
|
-
sed -i 's/\.\.\/tracer\/types\.generated/.\/tracer.generated/g' src/
|
|
12
|
+
sed -i 's/\.\.\/tracer\/types\.generated/.\/tracer.generated/g' src/internal/generated/types/evaluations.ts
|
|
11
13
|
fi
|
|
12
|
-
ts-to-zod src/
|
|
14
|
+
ts-to-zod src/internal/generated/types/evaluations.ts src/internal/generated/types/evaluations.generated.ts
|
|
13
15
|
|
|
14
|
-
cd src/
|
|
16
|
+
cd src/internal/generated/types/
|
|
15
17
|
curl -L "https://raw.githubusercontent.com/langwatch/langevals/main/ts-integration/evaluators.generated.ts?$(date +%s)" -o evaluators.generated.ts
|
|
16
18
|
cd -
|
|
17
|
-
ts-to-zod src/
|
|
19
|
+
ts-to-zod src/internal/generated/types/evaluators.generated.ts src/internal/generated/types/evaluators.zod.generated.ts
|
|
20
|
+
|
|
21
|
+
# Fix z.record(z.never()) to z.record(z.string(), z.never())
|
|
22
|
+
if [[ "$OSTYPE" == "darwin"* ]]; then
|
|
23
|
+
sed -i '' 's/z\.record(z\.never())/z.record(z.string(), z.never())/g' src/internal/generated/types/evaluators.zod.generated.ts
|
|
24
|
+
sed -i '' 's/z\.record(z\.any())/z.record(z.string(), z.any())/g' src/internal/generated/types/tracer.generated.ts
|
|
25
|
+
else
|
|
26
|
+
sed -i 's/z\.record(z\.never())/z.record(z.string(), z.never())/g' src/internal/generated/types/evaluators.zod.generated.ts
|
|
27
|
+
sed -i 's/z\.record(z\.any())/z.record(z.string(), z.any())/g' src/internal/generated/types/tracer.generated.ts
|
|
28
|
+
fi
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# LangChain Example
|
|
2
|
+
|
|
3
|
+
This directory contains examples of AI apps built with the LangWatch SDK and LangChain.
|
|
4
|
+
|
|
5
|
+
## Example
|
|
6
|
+
|
|
7
|
+
### Basic Chatbot (`src/index.ts`)
|
|
8
|
+
|
|
9
|
+
A simple chatbot that handles basic conversation flow using LangChain:
|
|
10
|
+
|
|
11
|
+
- **Features:**
|
|
12
|
+
- Basic conversation loop
|
|
13
|
+
- User input handling
|
|
14
|
+
- AI response generation using LangChain
|
|
15
|
+
- Conversation history management
|
|
16
|
+
- Error handling
|
|
17
|
+
- Exit commands (`quit`, `exit`)
|
|
18
|
+
|
|
19
|
+
- **Usage:**
|
|
20
|
+
```bash
|
|
21
|
+
npm run start
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Troubleshooting
|
|
25
|
+
|
|
26
|
+
### Common Issues:
|
|
27
|
+
|
|
28
|
+
1. **"Cannot find module 'langwatch/node'"**
|
|
29
|
+
- Make sure you've built the LangWatch SDK
|
|
30
|
+
- Check that the package is properly built
|
|
31
|
+
|
|
32
|
+
2. **OpenAI API errors**
|
|
33
|
+
- Verify your API key is set correctly
|
|
34
|
+
- Check your OpenAI account has sufficient credits
|
|
35
|
+
|
|
36
|
+
3. **TypeScript compilation errors**
|
|
37
|
+
- Run `npm run start` to check for type errors
|
|
38
|
+
- Ensure all dependencies are installed
|
|
39
|
+
|
|
40
|
+
4. **LangChain import errors**
|
|
41
|
+
- Make sure all LangChain dependencies are installed
|
|
42
|
+
- Check that the LangChain version is compatible
|