nexusos-sdk 1.0.0__tar.gz

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.
@@ -0,0 +1,411 @@
1
+ Metadata-Version: 2.4
2
+ Name: nexusos-sdk
3
+ Version: 1.0.0
4
+ Summary: Official Python SDK for NexusOS AI Agent Governance
5
+ Home-page: https://github.com/umair24171/nexusos-dashboard
6
+ Author: NexusOS
7
+ License: MIT
8
+ Project-URL: Homepage, https://nexusos-landing.vercel.app
9
+ Project-URL: Dashboard, https://nexusos-dashboard.vercel.app
10
+ Project-URL: Bug Reports, https://github.com/umair24171/nexusos-dashboard/issues
11
+ Keywords: nexusos,ai,agent,governance,monitoring,llm,logging,observability,openai,langchain
12
+ Classifier: Development Status :: 5 - Production/Stable
13
+ Classifier: Intended Audience :: Developers
14
+ Classifier: License :: OSI Approved :: MIT License
15
+ Classifier: Programming Language :: Python :: 3
16
+ Classifier: Programming Language :: Python :: 3.8
17
+ Classifier: Programming Language :: Python :: 3.9
18
+ Classifier: Programming Language :: Python :: 3.10
19
+ Classifier: Programming Language :: Python :: 3.11
20
+ Classifier: Programming Language :: Python :: 3.12
21
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
22
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
23
+ Requires-Python: >=3.8
24
+ Description-Content-Type: text/markdown
25
+ Requires-Dist: requests>=2.28.0
26
+ Dynamic: author
27
+ Dynamic: classifier
28
+ Dynamic: description
29
+ Dynamic: description-content-type
30
+ Dynamic: home-page
31
+ Dynamic: keywords
32
+ Dynamic: license
33
+ Dynamic: project-url
34
+ Dynamic: requires-dist
35
+ Dynamic: requires-python
36
+ Dynamic: summary
37
+
38
+ # NexusOS Python SDK
39
+
40
+ Official Python SDK for NexusOS AI Agent Governance. Monitor, log, and govern AI agents with ease.
41
+
42
+ ## Installation
43
+
44
+ ```bash
45
+ pip install nexusos-sdk
46
+ ```
47
+
48
+ ## Quick Start
49
+
50
+ ```python
51
+ from nexusos import NexusAgent
52
+
53
+ agent = NexusAgent(
54
+ api_key='nxs_live_xxxx',
55
+ agent_id='nexus_agt_xxxx'
56
+ )
57
+
58
+ # Log an LLM call
59
+ agent.log_llm_call(
60
+ model='gpt-4',
61
+ tokens_used=42,
62
+ duration_ms=1200,
63
+ prompt='What is the capital of France?',
64
+ response='The capital of France is Paris.'
65
+ )
66
+
67
+ # Send a heartbeat
68
+ agent.heartbeat()
69
+
70
+ # Cleanup
71
+ agent.shutdown()
72
+ ```
73
+
74
+ ## API Reference
75
+
76
+ ### Constructor
77
+
78
+ ```python
79
+ agent = NexusAgent(
80
+ api_key, # (required) API key for NexusOS
81
+ agent_id, # (required) Agent ID
82
+ base_url='https://nexusos-backend-7ue5.onrender.com', # (optional) Base API URL
83
+ batch_size=10, # (optional) Number of logs to batch before flushing
84
+ flush_interval=5 # (optional) Interval in seconds to flush logs
85
+ )
86
+ ```
87
+
88
+ ### Methods
89
+
90
+ #### `log(payload)`
91
+
92
+ Add a generic log event to the batch queue. Automatically flushes when batch reaches `batch_size`.
93
+
94
+ ```python
95
+ agent.log({
96
+ 'event': 'custom.event',
97
+ 'data': { 'key': 'value' }
98
+ })
99
+ ```
100
+
101
+ #### `log_llm_call(model, tokens_used, duration_ms, prompt=None, response=None)`
102
+
103
+ Log a large language model call.
104
+
105
+ ```python
106
+ agent.log_llm_call(
107
+ model='gpt-4',
108
+ tokens_used=350,
109
+ duration_ms=2500,
110
+ prompt='Summarize this article...',
111
+ response='The article discusses...'
112
+ )
113
+ ```
114
+
115
+ #### `log_http_request(url, method, status, duration_ms, success)`
116
+
117
+ Log an HTTP request.
118
+
119
+ ```python
120
+ agent.log_http_request(
121
+ url='https://api.example.com/data',
122
+ method='GET',
123
+ status=200,
124
+ duration_ms=450,
125
+ success=True
126
+ )
127
+ ```
128
+
129
+ #### `log_action(tool, input=None, output=None, duration_ms=None, success=True, error_message=None)`
130
+
131
+ Log a tool action execution.
132
+
133
+ ```python
134
+ agent.log_action(
135
+ tool='web_search',
136
+ input={'query': 'node.js best practices'},
137
+ output={'results': [...]},
138
+ duration_ms=800,
139
+ success=True
140
+ )
141
+ ```
142
+
143
+ #### `heartbeat()`
144
+
145
+ Send a heartbeat signal to indicate the agent is alive.
146
+
147
+ ```python
148
+ agent.heartbeat()
149
+ ```
150
+
151
+ #### `is_alive()`
152
+
153
+ Check if the agent is alive by fetching its stats from the API.
154
+
155
+ ```python
156
+ alive = agent.is_alive()
157
+ print(f'Agent is alive: {alive}')
158
+ ```
159
+
160
+ #### `start_heartbeat(interval=60)`
161
+
162
+ Start a background thread to send periodic heartbeats.
163
+
164
+ ```python
165
+ agent.start_heartbeat(interval=60)
166
+ ```
167
+
168
+ #### `trace(name)`
169
+
170
+ Context manager to trace the execution of a code block.
171
+
172
+ ```python
173
+ with agent.trace('process_data') as t:
174
+ result = process_data()
175
+ t.set_output(result)
176
+ ```
177
+
178
+ #### `monitor(tool=None)`
179
+
180
+ Decorator to automatically log function execution.
181
+
182
+ ```python
183
+ @agent.monitor(tool='web_search')
184
+ def search_web(query):
185
+ return results
186
+
187
+ # Works with async functions too
188
+ @agent.monitor(tool='fetch_data')
189
+ async def fetch_data(url):
190
+ return await get_data(url)
191
+ ```
192
+
193
+ #### `shutdown()`
194
+
195
+ Shutdown the agent: stop all threads and flush remaining logs.
196
+
197
+ ```python
198
+ agent.shutdown()
199
+ ```
200
+
201
+ #### Context Manager
202
+
203
+ Use the agent as a context manager for automatic cleanup:
204
+
205
+ ```python
206
+ with NexusAgent(api_key='nxs_live_xxxx', agent_id='nexus_agt_xxxx') as agent:
207
+ agent.log_llm_call(model='gpt-4', tokens_used=100, duration_ms=500)
208
+ # shutdown() is called automatically
209
+ ```
210
+
211
+ ## Sensitive Data Handling
212
+
213
+ The SDK automatically sanitizes logs to remove sensitive information. Keys containing the following terms (case-insensitive) are redacted:
214
+
215
+ - `password`
216
+ - `secret`
217
+ - `key`
218
+ - `token`
219
+ - `auth`
220
+
221
+ Example:
222
+
223
+ ```python
224
+ agent.log({
225
+ 'username': 'john_doe',
226
+ 'password': 'super_secret',
227
+ 'api_key': 'sk_live_xxx'
228
+ })
229
+
230
+ # Logged as:
231
+ # {
232
+ # 'username': 'john_doe',
233
+ # 'password': '[REDACTED]',
234
+ # 'api_key': '[REDACTED]'
235
+ # }
236
+ ```
237
+
238
+ ## Decorator Style
239
+
240
+ Log function execution with the `@monitor` decorator:
241
+
242
+ ```python
243
+ from nexusos import NexusAgent
244
+
245
+ agent = NexusAgent(
246
+ api_key='nxs_live_xxxx',
247
+ agent_id='nexus_agt_xxxx'
248
+ )
249
+
250
+ @agent.monitor(tool='calculate_sum')
251
+ def calculate_sum(numbers):
252
+ return sum(numbers)
253
+
254
+ result = calculate_sum([1, 2, 3])
255
+ # Automatically logs the function execution with timing and success/error
256
+ ```
257
+
258
+ ## Context Manager Style
259
+
260
+ Use the `trace()` context manager for more control:
261
+
262
+ ```python
263
+ with agent.trace('data_processing') as trace:
264
+ processed_data = process_data()
265
+ trace.set_output(processed_data)
266
+
267
+ # Automatically logs timing, duration, and output
268
+ ```
269
+
270
+ ## LangChain Integration
271
+
272
+ Integrate NexusOS with LangChain to automatically monitor LLM calls:
273
+
274
+ ```python
275
+ from nexusos import NexusAgent
276
+ from langchain.llms import OpenAI
277
+ from langchain.agents import initialize_agent, load_tools
278
+
279
+ agent = NexusAgent(
280
+ api_key='nxs_live_xxxx',
281
+ agent_id='nexus_agt_xxxx'
282
+ )
283
+
284
+ # Start periodic heartbeats
285
+ agent.start_heartbeat(interval=60)
286
+
287
+ llm = OpenAI(temperature=0)
288
+
289
+ # Wrap the LLM call
290
+ original_generate = llm.generate
291
+
292
+ def monitored_generate(prompts, *args, **kwargs):
293
+ import time
294
+ start = time.time()
295
+ try:
296
+ result = original_generate(prompts, *args, **kwargs)
297
+ duration_ms = int((time.time() - start) * 1000)
298
+ agent.log_llm_call(
299
+ model=llm.model_name,
300
+ tokens_used=result.llm_output.get('token_usage', {}).get('total_tokens', 0),
301
+ duration_ms=duration_ms,
302
+ prompt=str(prompts),
303
+ response=result.generations[0][0].text
304
+ )
305
+ return result
306
+ except Exception as e:
307
+ duration_ms = int((time.time() - start) * 1000)
308
+ agent.log_llm_call(
309
+ model=llm.model_name,
310
+ tokens_used=0,
311
+ duration_ms=duration_ms,
312
+ prompt=str(prompts),
313
+ response=f'Error: {str(e)}'
314
+ )
315
+ raise
316
+
317
+ llm.generate = monitored_generate
318
+
319
+ tools = load_tools(['serpapi'])
320
+ agent_executor = initialize_agent(tools, llm, agent='zero-shot-react-description')
321
+
322
+ result = agent_executor.run('What is the capital of France?')
323
+ agent.shutdown()
324
+ ```
325
+
326
+ ## OpenAI Integration
327
+
328
+ Monitor OpenAI API calls directly:
329
+
330
+ ```python
331
+ from nexusos import NexusAgent
332
+ import openai
333
+ import time
334
+
335
+ agent = NexusAgent(
336
+ api_key='nxs_live_xxxx',
337
+ agent_id='nexus_agt_xxxx'
338
+ )
339
+
340
+ def call_openai(prompt):
341
+ start = time.time()
342
+ try:
343
+ response = openai.ChatCompletion.create(
344
+ model='gpt-4',
345
+ messages=[{'role': 'user', 'content': prompt}]
346
+ )
347
+ duration_ms = int((time.time() - start) * 1000)
348
+ tokens_used = response['usage']['total_tokens']
349
+
350
+ agent.log_llm_call(
351
+ model='gpt-4',
352
+ tokens_used=tokens_used,
353
+ duration_ms=duration_ms,
354
+ prompt=prompt,
355
+ response=response['choices'][0]['message']['content']
356
+ )
357
+
358
+ return response
359
+ except Exception as e:
360
+ duration_ms = int((time.time() - start) * 1000)
361
+ agent.log_llm_call(
362
+ model='gpt-4',
363
+ tokens_used=0,
364
+ duration_ms=duration_ms,
365
+ prompt=prompt,
366
+ response=f'Error: {str(e)}'
367
+ )
368
+ raise
369
+
370
+ result = call_openai('What is the capital of France?')
371
+ agent.shutdown()
372
+ ```
373
+
374
+ ## Environment Variables
375
+
376
+ Configure the SDK using environment variables:
377
+
378
+ ```bash
379
+ export NEXUSOS_API_KEY=nxs_live_xxxx
380
+ export NEXUSOS_AGENT_ID=nexus_agt_xxxx
381
+ export NEXUSOS_BASE_URL=https://nexusos-backend-7ue5.onrender.com
382
+ ```
383
+
384
+ ```python
385
+ import os
386
+ from nexusos import NexusAgent
387
+
388
+ agent = NexusAgent(
389
+ api_key=os.getenv('NEXUSOS_API_KEY'),
390
+ agent_id=os.getenv('NEXUSOS_AGENT_ID'),
391
+ base_url=os.getenv('NEXUSOS_BASE_URL', 'https://nexusos-backend-7ue5.onrender.com')
392
+ )
393
+ ```
394
+
395
+ ## Async Support
396
+
397
+ The `@monitor` decorator and `trace()` context manager fully support async functions:
398
+
399
+ ```python
400
+ @agent.monitor(tool='async_fetch')
401
+ async def fetch_data(url):
402
+ async with aiohttp.ClientSession() as session:
403
+ async with session.get(url) as response:
404
+ return await response.json()
405
+
406
+ result = await fetch_data('https://api.example.com/data')
407
+ ```
408
+
409
+ ## License
410
+
411
+ MIT