@observyze/sdk 0.1.0 → 0.1.2

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.
@@ -1,210 +0,0 @@
1
- /**
2
- * Example: Auto-Instrumentation with OpenAI and Anthropic
3
- *
4
- * This example demonstrates how to use Observyze's auto-instrumentation
5
- * to automatically capture traces from OpenAI and Anthropic API calls.
6
- */
7
-
8
- import { ObservyzeClient } from '../src'
9
-
10
- // Mock OpenAI and Anthropic clients for demonstration
11
- // In a real application, you would import these from their respective packages:
12
- // import OpenAI from 'openai'
13
- // import Anthropic from '@anthropic-ai/sdk'
14
-
15
- async function main() {
16
- // Initialize Observyze SDK
17
- const nw = new ObservyzeClient({
18
- apiKey: process.env.Observyze_API_KEY || 'demo-key',
19
- organizationId: 'demo-org',
20
- projectId: 'demo-project',
21
- endpoint: 'http://localhost:3001',
22
- debug: true,
23
- dryRun: true // Set to false in production
24
- })
25
-
26
- console.log('🚀 Observyze SDK initialized\n')
27
-
28
- // Example 1: OpenAI Auto-Instrumentation
29
- console.log('📝 Example 1: OpenAI Auto-Instrumentation')
30
- console.log('─'.repeat(50))
31
-
32
- // Create a mock OpenAI client
33
- const mockOpenAI = {
34
- chat: {
35
- completions: {
36
- create: async (params: any) => {
37
- console.log(' → Calling OpenAI API...')
38
- // Simulate API call
39
- await new Promise(resolve => setTimeout(resolve, 100))
40
- return {
41
- id: 'chatcmpl-123',
42
- model: params.model,
43
- choices: [
44
- {
45
- message: {
46
- role: 'assistant',
47
- content: 'Hello! I am an AI assistant. How can I help you today?'
48
- },
49
- finish_reason: 'stop'
50
- }
51
- ],
52
- usage: {
53
- prompt_tokens: 15,
54
- completion_tokens: 25,
55
- total_tokens: 40
56
- }
57
- }
58
- }
59
- }
60
- }
61
- }
62
-
63
- // Wrap the OpenAI client
64
- nw.wrap(mockOpenAI)
65
- console.log(' ✓ OpenAI client wrapped')
66
-
67
- // Make a call - it will be automatically traced!
68
- const openaiResponse = await mockOpenAI.chat.completions.create({
69
- model: 'gpt-4',
70
- messages: [
71
- { role: 'user', content: 'Hello, how are you?' }
72
- ],
73
- temperature: 0.7
74
- })
75
-
76
- console.log(' ✓ Response received:', openaiResponse.choices[0].message.content)
77
- console.log(' ✓ Trace automatically captured!\n')
78
-
79
- // Example 2: Anthropic Auto-Instrumentation
80
- console.log('📝 Example 2: Anthropic Auto-Instrumentation')
81
- console.log('─'.repeat(50))
82
-
83
- // Create a mock Anthropic client
84
- const mockAnthropic = {
85
- messages: {
86
- create: async (params: any) => {
87
- console.log(' → Calling Anthropic API...')
88
- // Simulate API call
89
- await new Promise(resolve => setTimeout(resolve, 100))
90
- return {
91
- id: 'msg_123',
92
- type: 'message',
93
- role: 'assistant',
94
- content: [
95
- {
96
- type: 'text',
97
- text: 'Hello! I am Claude, an AI assistant created by Anthropic.'
98
- }
99
- ],
100
- model: params.model,
101
- stop_reason: 'end_turn',
102
- usage: {
103
- input_tokens: 20,
104
- output_tokens: 30
105
- }
106
- }
107
- }
108
- }
109
- }
110
-
111
- // Wrap the Anthropic client
112
- nw.wrap(mockAnthropic)
113
- console.log(' ✓ Anthropic client wrapped')
114
-
115
- // Make a call - it will be automatically traced!
116
- const anthropicResponse = await mockAnthropic.messages.create({
117
- model: 'claude-3-opus-20240229',
118
- max_tokens: 1024,
119
- messages: [
120
- { role: 'user', content: 'Hello, Claude!' }
121
- ]
122
- })
123
-
124
- console.log(' ✓ Response received:', anthropicResponse.content[0].text)
125
- console.log(' ✓ Trace automatically captured!\n')
126
-
127
- // Example 3: Streaming Response
128
- console.log('📝 Example 3: Streaming Response (OpenAI)')
129
- console.log('─'.repeat(50))
130
-
131
- // Create a mock streaming OpenAI client
132
- const mockStreamingOpenAI = {
133
- chat: {
134
- completions: {
135
- create: async (params: any) => {
136
- console.log(' → Starting streaming call...')
137
- // Return an async generator that simulates streaming
138
- return {
139
- async *[Symbol.asyncIterator]() {
140
- const chunks = ['Hello', ' there', '!', ' How', ' can', ' I', ' help', '?']
141
- for (const chunk of chunks) {
142
- await new Promise(resolve => setTimeout(resolve, 50))
143
- yield {
144
- id: 'chatcmpl-stream-123',
145
- model: params.model,
146
- choices: [
147
- {
148
- delta: { content: chunk },
149
- finish_reason: null
150
- }
151
- ]
152
- }
153
- }
154
- // Final chunk
155
- yield {
156
- id: 'chatcmpl-stream-123',
157
- model: params.model,
158
- choices: [
159
- {
160
- delta: {},
161
- finish_reason: 'stop'
162
- }
163
- ]
164
- }
165
- }
166
- }
167
- }
168
- }
169
- }
170
- }
171
-
172
- // Wrap the streaming client
173
- nw.wrap(mockStreamingOpenAI)
174
- console.log(' ✓ Streaming client wrapped')
175
-
176
- // Make a streaming call
177
- const stream = await mockStreamingOpenAI.chat.completions.create({
178
- model: 'gpt-4',
179
- messages: [{ role: 'user', content: 'Hello!' }],
180
- stream: true
181
- })
182
-
183
- console.log(' → Streaming response: ', { newline: false })
184
- for await (const chunk of stream) {
185
- if (chunk.choices[0]?.delta?.content) {
186
- process.stdout.write(chunk.choices[0].delta.content)
187
- }
188
- }
189
- console.log('\n ✓ Stream completed and trace captured!\n')
190
-
191
- // Show buffer status
192
- console.log('📊 SDK Status')
193
- console.log('─'.repeat(50))
194
- console.log(` Buffered traces: ${nw.bufferSize}`)
195
- console.log(' ✓ All traces will be automatically flushed\n')
196
-
197
- // Flush and shutdown
198
- console.log('🔄 Flushing traces...')
199
- await nw.flush()
200
- console.log(' ✓ Traces flushed')
201
-
202
- console.log('👋 Shutting down SDK...')
203
- await nw.shutdown()
204
- console.log(' ✓ SDK shutdown complete\n')
205
-
206
- console.log('✨ Demo complete! In production, traces would be sent to Observyze.')
207
- }
208
-
209
- // Run the example
210
- main().catch(console.error)