@thinkhive/sdk 2.0.0
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/LICENSE +21 -0
- package/README.md +212 -0
- package/dist/index.d.ts +629 -0
- package/dist/index.js +639 -0
- package/package.json +54 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 ThinkHive
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
# ThinkHive SDK
|
|
2
|
+
|
|
3
|
+
The official JavaScript/TypeScript SDK for [ThinkHive](https://thinkhive.ai) - AI Agent Observability Platform.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Trace Analysis**: Analyze AI agent traces with detailed explainability
|
|
8
|
+
- **RAG Evaluation**: 8 quality metrics for RAG systems (groundedness, faithfulness, etc.)
|
|
9
|
+
- **Hallucination Detection**: 9 types of hallucination detection
|
|
10
|
+
- **Business Impact**: Industry-specific ROI calculations
|
|
11
|
+
- **Auto-Instrumentation**: Works with LangChain, OpenAI, Anthropic, and more
|
|
12
|
+
- **OpenTelemetry**: Built on OTLP for seamless integration
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install @thinkhive/sdk
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Quick Start
|
|
21
|
+
|
|
22
|
+
### Basic Usage
|
|
23
|
+
|
|
24
|
+
```typescript
|
|
25
|
+
import { ThinkHive } from '@thinkhive/sdk';
|
|
26
|
+
|
|
27
|
+
// Initialize client
|
|
28
|
+
const client = new ThinkHive({
|
|
29
|
+
apiKey: 'your_api_key',
|
|
30
|
+
baseUrl: 'https://api.thinkhive.ai'
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
// Send a trace
|
|
34
|
+
const result = await client.trace({
|
|
35
|
+
userMessage: 'What is the weather in San Francisco?',
|
|
36
|
+
agentResponse: 'The weather in San Francisco is currently 65°F and sunny.',
|
|
37
|
+
agentId: 'weather-agent'
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
console.log(`Trace ID: ${result.traceId}`);
|
|
41
|
+
if (result.analysis) {
|
|
42
|
+
console.log(`Outcome: ${result.analysis.outcome.verdict}`);
|
|
43
|
+
console.log(`Impact Score: ${result.analysis.businessImpact.impactScore}`);
|
|
44
|
+
}
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### With Business Context
|
|
48
|
+
|
|
49
|
+
```typescript
|
|
50
|
+
const result = await client.trace({
|
|
51
|
+
userMessage: 'I want to cancel my order #12345',
|
|
52
|
+
agentResponse: 'I understand you want to cancel order #12345...',
|
|
53
|
+
agentId: 'support-agent',
|
|
54
|
+
businessContext: {
|
|
55
|
+
customerId: 'cust_abc123',
|
|
56
|
+
transactionValue: 150.00,
|
|
57
|
+
priority: 'high',
|
|
58
|
+
industry: 'ecommerce'
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
// Access ROI metrics
|
|
63
|
+
if (result.analysis?.businessImpact?.roi) {
|
|
64
|
+
const roi = result.analysis.businessImpact.roi;
|
|
65
|
+
console.log(`Estimated Revenue Loss: $${roi.estimatedRevenueLoss}`);
|
|
66
|
+
console.log(`Churn Probability: ${roi.churnProbability}%`);
|
|
67
|
+
}
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### Explainer API
|
|
71
|
+
|
|
72
|
+
```typescript
|
|
73
|
+
// Full trace analysis with RAG evaluation
|
|
74
|
+
const analysis = await client.explainer.analyze({
|
|
75
|
+
userMessage: 'What is your return policy?',
|
|
76
|
+
agentResponse: 'Items can be returned within 30 days...',
|
|
77
|
+
retrievedContexts: ['Return Policy: 30 day returns...'],
|
|
78
|
+
outcome: 'success'
|
|
79
|
+
}, {
|
|
80
|
+
tier: 'full_llm',
|
|
81
|
+
includeRagEvaluation: true,
|
|
82
|
+
includeHallucinationDetection: true
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
console.log(`Summary: ${analysis.summary}`);
|
|
86
|
+
console.log(`Groundedness: ${analysis.ragEvaluation?.groundedness}`);
|
|
87
|
+
|
|
88
|
+
// Batch analysis
|
|
89
|
+
const batchResult = await client.explainer.analyzeBatch([
|
|
90
|
+
{ userMessage: '...', agentResponse: '...' },
|
|
91
|
+
{ userMessage: '...', agentResponse: '...' }
|
|
92
|
+
], { tier: 'fast_llm' });
|
|
93
|
+
|
|
94
|
+
// Semantic search
|
|
95
|
+
const searchResults = await client.explainer.search({
|
|
96
|
+
query: 'refund complaints',
|
|
97
|
+
filters: { outcome: 'failure' },
|
|
98
|
+
limit: 10
|
|
99
|
+
});
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### Quality Metrics
|
|
103
|
+
|
|
104
|
+
```typescript
|
|
105
|
+
// Get RAG scores
|
|
106
|
+
const ragScores = await client.quality.getRagScores('trace-123');
|
|
107
|
+
console.log(`Groundedness: ${ragScores.groundedness}`);
|
|
108
|
+
console.log(`Faithfulness: ${ragScores.faithfulness}`);
|
|
109
|
+
|
|
110
|
+
// Get hallucination report
|
|
111
|
+
const report = await client.quality.getHallucinationReport('trace-123');
|
|
112
|
+
if (report.hasHallucinations) {
|
|
113
|
+
for (const detection of report.detectedTypes) {
|
|
114
|
+
console.log(`- ${detection.type}: ${detection.description}`);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// Evaluate RAG for custom input
|
|
119
|
+
const evaluation = await client.quality.evaluateRag({
|
|
120
|
+
query: 'What is the return policy?',
|
|
121
|
+
response: 'Items can be returned within 30 days.',
|
|
122
|
+
contexts: [{ content: 'Return Policy: 30 day returns...' }]
|
|
123
|
+
});
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
### ROI Analytics
|
|
127
|
+
|
|
128
|
+
```typescript
|
|
129
|
+
// Get ROI summary
|
|
130
|
+
const summary = await client.analytics.getRoiSummary();
|
|
131
|
+
console.log(`Revenue Saved: $${summary.totalRevenueSaved}`);
|
|
132
|
+
|
|
133
|
+
// Get per-agent ROI
|
|
134
|
+
const agentRoi = await client.analytics.getRoiByAgent('support-agent');
|
|
135
|
+
console.log(`Success Rate: ${agentRoi.successRate}%`);
|
|
136
|
+
|
|
137
|
+
// Get correlation analysis
|
|
138
|
+
const correlations = await client.analytics.getCorrelations();
|
|
139
|
+
for (const corr of correlations.correlations) {
|
|
140
|
+
console.log(`${corr.type}: ${corr.actionableInsight}`);
|
|
141
|
+
}
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
### Providing Feedback
|
|
145
|
+
|
|
146
|
+
```typescript
|
|
147
|
+
// After receiving user feedback
|
|
148
|
+
await client.feedback({
|
|
149
|
+
traceId: result.traceId,
|
|
150
|
+
rating: 5,
|
|
151
|
+
wasHelpful: true,
|
|
152
|
+
comment: 'Very accurate response!'
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
// When response was incorrect
|
|
156
|
+
await client.feedback({
|
|
157
|
+
traceId: result.traceId,
|
|
158
|
+
rating: 2,
|
|
159
|
+
wasHelpful: false,
|
|
160
|
+
hadIssues: ['incorrect_info', 'too_long'],
|
|
161
|
+
correctedResponse: 'The correct answer is...'
|
|
162
|
+
});
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
### Auto-Instrumentation
|
|
166
|
+
|
|
167
|
+
```typescript
|
|
168
|
+
import { init, autoInstrument } from '@thinkhive/sdk';
|
|
169
|
+
|
|
170
|
+
// Initialize SDK
|
|
171
|
+
init({
|
|
172
|
+
apiKey: 'your_api_key',
|
|
173
|
+
serviceName: 'my-ai-agent',
|
|
174
|
+
autoInstrument: true,
|
|
175
|
+
frameworks: ['langchain', 'openai']
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
// Or manually instrument
|
|
179
|
+
autoInstrument(client, {
|
|
180
|
+
frameworks: ['langchain', 'openai'],
|
|
181
|
+
capturePrompts: true,
|
|
182
|
+
captureResponses: true,
|
|
183
|
+
businessContext: { industry: 'saas' }
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
// Now all LangChain and OpenAI calls are automatically traced!
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
## Analysis Tiers
|
|
190
|
+
|
|
191
|
+
| Tier | Description | Latency | Cost |
|
|
192
|
+
|------|-------------|---------|------|
|
|
193
|
+
| `rule_based` | Pattern matching, keyword extraction | ~50ms | Free |
|
|
194
|
+
| `fast_llm` | Quick LLM analysis (GPT-3.5) | ~500ms | Low |
|
|
195
|
+
| `full_llm` | Complete analysis (GPT-4o) | ~3s | Standard |
|
|
196
|
+
| `deep` | Multi-pass with validation | ~15s | Premium |
|
|
197
|
+
|
|
198
|
+
## Environment Variables
|
|
199
|
+
|
|
200
|
+
| Variable | Description |
|
|
201
|
+
|----------|-------------|
|
|
202
|
+
| `THINKHIVE_API_KEY` | Your ThinkHive API key |
|
|
203
|
+
| `THINKHIVE_ENDPOINT` | Custom API endpoint (optional) |
|
|
204
|
+
| `THINKHIVE_SERVICE_NAME` | Service name for traces (optional) |
|
|
205
|
+
|
|
206
|
+
## API Reference
|
|
207
|
+
|
|
208
|
+
See [API Documentation](https://docs.thinkhive.ai/sdk/javascript/reference) for complete type definitions.
|
|
209
|
+
|
|
210
|
+
## License
|
|
211
|
+
|
|
212
|
+
MIT License - see [LICENSE](LICENSE) for details.
|