smartflow-sdk 0.2.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.
- smartflow_sdk-0.2.0/LICENSE +22 -0
- smartflow_sdk-0.2.0/PKG-INFO +345 -0
- smartflow_sdk-0.2.0/README.md +308 -0
- smartflow_sdk-0.2.0/pyproject.toml +91 -0
- smartflow_sdk-0.2.0/setup.cfg +4 -0
- smartflow_sdk-0.2.0/smartflow/__init__.py +129 -0
- smartflow_sdk-0.2.0/smartflow/client.py +1160 -0
- smartflow_sdk-0.2.0/smartflow/exceptions.py +120 -0
- smartflow_sdk-0.2.0/smartflow/sync.py +386 -0
- smartflow_sdk-0.2.0/smartflow/types.py +526 -0
- smartflow_sdk-0.2.0/smartflow_sdk.egg-info/PKG-INFO +345 -0
- smartflow_sdk-0.2.0/smartflow_sdk.egg-info/SOURCES.txt +14 -0
- smartflow_sdk-0.2.0/smartflow_sdk.egg-info/dependency_links.txt +1 -0
- smartflow_sdk-0.2.0/smartflow_sdk.egg-info/requires.txt +12 -0
- smartflow_sdk-0.2.0/smartflow_sdk.egg-info/top_level.txt +1 -0
- smartflow_sdk-0.2.0/tests/test_client.py +124 -0
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Langsmart, Inc.
|
|
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.
|
|
22
|
+
|
|
@@ -0,0 +1,345 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: smartflow-sdk
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: Developer SDK for Smartflow AI orchestration, caching, compliance, and governance
|
|
5
|
+
Author-email: "Langsmart, Inc." <scott@sragrouptx.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://smartflow.ai
|
|
8
|
+
Project-URL: Documentation, https://docs.smartflow.ai/sdk/python
|
|
9
|
+
Project-URL: Repository, https://github.com/langsmart/smartflow-sdk-python
|
|
10
|
+
Project-URL: Changelog, https://github.com/langsmart/smartflow-sdk-python/blob/main/CHANGELOG.md
|
|
11
|
+
Project-URL: Issues, https://github.com/langsmart/smartflow-sdk-python/issues
|
|
12
|
+
Keywords: ai,llm,openai,anthropic,claude,gpt,caching,compliance,governance,orchestration,proxy,agents,ml,enterprise
|
|
13
|
+
Classifier: Development Status :: 4 - Beta
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: Operating System :: OS Independent
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
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.9
|
|
24
|
+
Description-Content-Type: text/markdown
|
|
25
|
+
License-File: LICENSE
|
|
26
|
+
Requires-Dist: httpx>=0.25.0
|
|
27
|
+
Provides-Extra: dev
|
|
28
|
+
Requires-Dist: pytest>=7.0; extra == "dev"
|
|
29
|
+
Requires-Dist: pytest-asyncio>=0.21; extra == "dev"
|
|
30
|
+
Requires-Dist: pytest-cov>=4.0; extra == "dev"
|
|
31
|
+
Requires-Dist: black>=23.0; extra == "dev"
|
|
32
|
+
Requires-Dist: ruff>=0.1; extra == "dev"
|
|
33
|
+
Requires-Dist: mypy>=1.0; extra == "dev"
|
|
34
|
+
Provides-Extra: nest
|
|
35
|
+
Requires-Dist: nest-asyncio>=1.5; extra == "nest"
|
|
36
|
+
Dynamic: license-file
|
|
37
|
+
|
|
38
|
+
# Smartflow Python SDK
|
|
39
|
+
|
|
40
|
+
The official Python SDK for [Smartflow](https://smartflow.ai) - the enterprise AI orchestration, caching, compliance, and governance platform.
|
|
41
|
+
|
|
42
|
+
## Features
|
|
43
|
+
|
|
44
|
+
- **🚀 Simple API** - Chat, embeddings, and completions with one line of code
|
|
45
|
+
- **💰 60-80% Cost Savings** - 3-layer semantic caching (L1/L2/L3)
|
|
46
|
+
- **🛡️ ML-Powered Compliance** - Intelligent PII detection with adaptive learning
|
|
47
|
+
- **🔄 Automatic Failover** - Multi-provider routing with intelligent fallback
|
|
48
|
+
- **📊 Full Audit Trail** - VAS logs for every AI interaction
|
|
49
|
+
- **🤖 Agent Builder** - Create AI agents with built-in compliance
|
|
50
|
+
- **📈 Workflow Orchestration** - Chain AI operations with branching and error handling
|
|
51
|
+
|
|
52
|
+
## Installation
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
pip install smartflow-sdk
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## Quick Start
|
|
59
|
+
|
|
60
|
+
### Async Usage (Recommended)
|
|
61
|
+
|
|
62
|
+
```python
|
|
63
|
+
import asyncio
|
|
64
|
+
from smartflow import SmartflowClient
|
|
65
|
+
|
|
66
|
+
async def main():
|
|
67
|
+
async with SmartflowClient("http://your-smartflow:7775") as sf:
|
|
68
|
+
# Simple chat
|
|
69
|
+
response = await sf.chat("What is machine learning?")
|
|
70
|
+
print(response)
|
|
71
|
+
|
|
72
|
+
# Check cache stats
|
|
73
|
+
stats = await sf.get_cache_stats()
|
|
74
|
+
print(f"Cache hit rate: {stats.hit_rate:.1%}")
|
|
75
|
+
print(f"Tokens saved: {stats.tokens_saved:,}")
|
|
76
|
+
|
|
77
|
+
asyncio.run(main())
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### Sync Usage
|
|
81
|
+
|
|
82
|
+
```python
|
|
83
|
+
from smartflow import SyncSmartflowClient
|
|
84
|
+
|
|
85
|
+
sf = SyncSmartflowClient("http://your-smartflow:7775")
|
|
86
|
+
|
|
87
|
+
response = sf.chat("Explain quantum computing")
|
|
88
|
+
print(response)
|
|
89
|
+
|
|
90
|
+
sf.close()
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### OpenAI Drop-in Replacement
|
|
94
|
+
|
|
95
|
+
Just change the `base_url` - your existing OpenAI code works with Smartflow!
|
|
96
|
+
|
|
97
|
+
```python
|
|
98
|
+
from openai import OpenAI
|
|
99
|
+
|
|
100
|
+
# Point to Smartflow instead of OpenAI
|
|
101
|
+
client = OpenAI(
|
|
102
|
+
base_url="http://your-smartflow:7775/v1",
|
|
103
|
+
api_key="your-key" # Or use Smartflow's stored keys
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
response = client.chat.completions.create(
|
|
107
|
+
model="gpt-4o",
|
|
108
|
+
messages=[{"role": "user", "content": "Hello!"}]
|
|
109
|
+
)
|
|
110
|
+
print(response.choices[0].message.content)
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
## Intelligent Compliance (ML-Powered)
|
|
114
|
+
|
|
115
|
+
Smartflow's adaptive learning compliance engine provides:
|
|
116
|
+
|
|
117
|
+
- **Regex Pattern Matching** - SSN, credit cards, emails, phone numbers, etc.
|
|
118
|
+
- **ML Embedding Similarity** - Semantic violation detection
|
|
119
|
+
- **Behavioral Analysis** - User pattern tracking and anomaly detection
|
|
120
|
+
- **Organization Baselines** - Deviation detection from org norms
|
|
121
|
+
|
|
122
|
+
```python
|
|
123
|
+
async with SmartflowClient("http://your-smartflow:7775") as sf:
|
|
124
|
+
# Scan content for compliance issues
|
|
125
|
+
result = await sf.intelligent_scan(
|
|
126
|
+
content="My SSN is 123-45-6789 and my email is john@example.com",
|
|
127
|
+
user_id="user123",
|
|
128
|
+
org_id="acme_corp"
|
|
129
|
+
)
|
|
130
|
+
|
|
131
|
+
print(f"Risk Score: {result.risk_score:.2f}")
|
|
132
|
+
print(f"Risk Level: {result.risk_level}")
|
|
133
|
+
print(f"Action: {result.recommended_action}")
|
|
134
|
+
print(f"Explanation: {result.explanation}")
|
|
135
|
+
|
|
136
|
+
# Check regex violations
|
|
137
|
+
for violation in result.regex_violations:
|
|
138
|
+
print(f" - {violation['violation_type']}: {violation['severity']}")
|
|
139
|
+
|
|
140
|
+
# Submit feedback to improve detection
|
|
141
|
+
await sf.submit_compliance_feedback(
|
|
142
|
+
scan_id="scan_abc123",
|
|
143
|
+
is_false_positive=True,
|
|
144
|
+
notes="This was a test number"
|
|
145
|
+
)
|
|
146
|
+
|
|
147
|
+
# Get learning status
|
|
148
|
+
learning = await sf.get_learning_summary()
|
|
149
|
+
print(f"Users tracked: {learning.total_users}")
|
|
150
|
+
print(f"Learning complete: {learning.users_learning_complete}")
|
|
151
|
+
|
|
152
|
+
# Get ML stats
|
|
153
|
+
ml_stats = await sf.get_ml_stats()
|
|
154
|
+
print(f"Total patterns: {ml_stats.total_patterns}")
|
|
155
|
+
print(f"Learned patterns: {ml_stats.learned_patterns}")
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
## Building AI Agents
|
|
159
|
+
|
|
160
|
+
Create AI agents with built-in compliance scanning and conversation memory:
|
|
161
|
+
|
|
162
|
+
```python
|
|
163
|
+
from smartflow import SmartflowClient, SmartflowAgent
|
|
164
|
+
|
|
165
|
+
async with SmartflowClient("http://your-smartflow:7775") as sf:
|
|
166
|
+
agent = SmartflowAgent(
|
|
167
|
+
client=sf,
|
|
168
|
+
name="CustomerSupport",
|
|
169
|
+
model="gpt-4o",
|
|
170
|
+
system_prompt="""You are a helpful customer support agent for TechCorp.
|
|
171
|
+
Be professional, friendly, and always protect customer data.""",
|
|
172
|
+
compliance_policy="enterprise_standard",
|
|
173
|
+
enable_compliance_scan=True,
|
|
174
|
+
user_id="support_agent_1",
|
|
175
|
+
org_id="techcorp"
|
|
176
|
+
)
|
|
177
|
+
|
|
178
|
+
# Chat with automatic compliance scanning
|
|
179
|
+
response = await agent.chat("How do I reset my password?")
|
|
180
|
+
print(response)
|
|
181
|
+
|
|
182
|
+
# Conversation memory is maintained
|
|
183
|
+
response = await agent.chat("What about two-factor authentication?")
|
|
184
|
+
print(response)
|
|
185
|
+
|
|
186
|
+
# Get conversation history
|
|
187
|
+
history = agent.get_history()
|
|
188
|
+
print(f"Messages: {len(history)}")
|
|
189
|
+
|
|
190
|
+
# Clear and start fresh
|
|
191
|
+
agent.clear_history()
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
## Workflow Orchestration
|
|
195
|
+
|
|
196
|
+
Chain AI operations with branching, parallel execution, and error handling:
|
|
197
|
+
|
|
198
|
+
```python
|
|
199
|
+
from smartflow import SmartflowClient, SmartflowWorkflow
|
|
200
|
+
|
|
201
|
+
async with SmartflowClient("http://your-smartflow:7775") as sf:
|
|
202
|
+
workflow = SmartflowWorkflow(sf, name="TicketClassification")
|
|
203
|
+
|
|
204
|
+
# Step 1: Classify the ticket
|
|
205
|
+
workflow.add_step(
|
|
206
|
+
name="classify",
|
|
207
|
+
action="chat",
|
|
208
|
+
config={
|
|
209
|
+
"prompt": "Classify this support ticket into one of: billing, technical, account. Ticket: {input}",
|
|
210
|
+
"model": "gpt-4o-mini"
|
|
211
|
+
},
|
|
212
|
+
next_steps=["route"]
|
|
213
|
+
)
|
|
214
|
+
|
|
215
|
+
# Step 2: Route based on classification
|
|
216
|
+
workflow.add_step(
|
|
217
|
+
name="route",
|
|
218
|
+
action="condition",
|
|
219
|
+
config={
|
|
220
|
+
"field": "output",
|
|
221
|
+
"cases": {
|
|
222
|
+
"billing": "billing_response",
|
|
223
|
+
"technical": "technical_response",
|
|
224
|
+
"account": "account_response"
|
|
225
|
+
},
|
|
226
|
+
"default": "general_response"
|
|
227
|
+
}
|
|
228
|
+
)
|
|
229
|
+
|
|
230
|
+
# Execute the workflow
|
|
231
|
+
result = await workflow.execute({"input": "My payment failed yesterday"})
|
|
232
|
+
|
|
233
|
+
print(f"Success: {result.success}")
|
|
234
|
+
print(f"Output: {result.output}")
|
|
235
|
+
print(f"Steps executed: {result.steps_executed}")
|
|
236
|
+
print(f"Execution time: {result.execution_time_ms:.1f}ms")
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
## Monitoring & Analytics
|
|
240
|
+
|
|
241
|
+
```python
|
|
242
|
+
async with SmartflowClient("http://your-smartflow:7775") as sf:
|
|
243
|
+
# System health
|
|
244
|
+
health = await sf.health_comprehensive()
|
|
245
|
+
print(f"Status: {health.status}")
|
|
246
|
+
print(f"Uptime: {health.uptime_seconds / 3600:.1f} hours")
|
|
247
|
+
|
|
248
|
+
# Provider health
|
|
249
|
+
providers = await sf.get_provider_health()
|
|
250
|
+
for p in providers:
|
|
251
|
+
print(f"{p.provider}: {p.status} ({p.latency_ms:.0f}ms)")
|
|
252
|
+
|
|
253
|
+
# Cache statistics
|
|
254
|
+
cache = await sf.get_cache_stats()
|
|
255
|
+
print(f"Hit rate: {cache.hit_rate:.1%}")
|
|
256
|
+
print(f"L1 hits: {cache.l1_hits}")
|
|
257
|
+
print(f"L2 hits: {cache.l2_hits}")
|
|
258
|
+
print(f"Tokens saved: {cache.tokens_saved:,}")
|
|
259
|
+
|
|
260
|
+
# Audit logs
|
|
261
|
+
logs = await sf.get_logs(limit=10)
|
|
262
|
+
for log in logs:
|
|
263
|
+
print(f"{log.timestamp}: {log.provider}/{log.model} - {log.tokens_used} tokens")
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
## Configuration
|
|
267
|
+
|
|
268
|
+
### Client Options
|
|
269
|
+
|
|
270
|
+
```python
|
|
271
|
+
sf = SmartflowClient(
|
|
272
|
+
base_url="http://smartflow:7775", # Proxy URL
|
|
273
|
+
api_key="your-api-key", # Optional API key
|
|
274
|
+
timeout=30.0, # Request timeout
|
|
275
|
+
management_port=7778, # Management API port
|
|
276
|
+
compliance_port=7777, # Compliance API port
|
|
277
|
+
bridge_port=3500, # Hybrid bridge port
|
|
278
|
+
)
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
### Environment Variables
|
|
282
|
+
|
|
283
|
+
```bash
|
|
284
|
+
export SMARTFLOW_URL="http://your-smartflow:7775"
|
|
285
|
+
export SMARTFLOW_API_KEY="your-key"
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
## API Reference
|
|
289
|
+
|
|
290
|
+
### SmartflowClient Methods
|
|
291
|
+
|
|
292
|
+
| Method | Description |
|
|
293
|
+
|--------|-------------|
|
|
294
|
+
| `chat()` | Simple chat with AI |
|
|
295
|
+
| `chat_completions()` | OpenAI-compatible completions |
|
|
296
|
+
| `embeddings()` | Generate text embeddings |
|
|
297
|
+
| `claude_message()` | Anthropic Claude API |
|
|
298
|
+
| `intelligent_scan()` | ML-powered compliance scan |
|
|
299
|
+
| `check_compliance()` | Basic compliance check |
|
|
300
|
+
| `get_cache_stats()` | Cache hit rates and savings |
|
|
301
|
+
| `health()` | Quick health check |
|
|
302
|
+
| `health_comprehensive()` | Full system health |
|
|
303
|
+
| `get_logs()` | VAS audit logs |
|
|
304
|
+
| `get_provider_health()` | Provider status |
|
|
305
|
+
|
|
306
|
+
### SmartflowAgent Methods
|
|
307
|
+
|
|
308
|
+
| Method | Description |
|
|
309
|
+
|--------|-------------|
|
|
310
|
+
| `chat()` | Chat with compliance scanning |
|
|
311
|
+
| `clear_history()` | Reset conversation |
|
|
312
|
+
| `get_history()` | Get conversation history |
|
|
313
|
+
|
|
314
|
+
### SmartflowWorkflow Methods
|
|
315
|
+
|
|
316
|
+
| Method | Description |
|
|
317
|
+
|--------|-------------|
|
|
318
|
+
| `add_step()` | Add a workflow step |
|
|
319
|
+
| `set_entry()` | Set entry point |
|
|
320
|
+
| `execute()` | Run the workflow |
|
|
321
|
+
|
|
322
|
+
## Examples
|
|
323
|
+
|
|
324
|
+
See the [examples/](./examples/) directory for more:
|
|
325
|
+
|
|
326
|
+
- `simple_chat.py` - Basic chat usage
|
|
327
|
+
- `compliance_check.py` - PII detection and redaction
|
|
328
|
+
- `system_monitoring.py` - Health and analytics
|
|
329
|
+
- `openai_drop_in.py` - OpenAI compatibility
|
|
330
|
+
- `agent_example.py` - Building AI agents
|
|
331
|
+
- `workflow_example.py` - Workflow orchestration
|
|
332
|
+
|
|
333
|
+
## License
|
|
334
|
+
|
|
335
|
+
MIT License - see [LICENSE](./LICENSE) for details.
|
|
336
|
+
|
|
337
|
+
## Support
|
|
338
|
+
|
|
339
|
+
- Documentation: https://docs.smartflow.ai/sdk/python
|
|
340
|
+
- Issues: https://github.com/langsmart/smartflow-sdk-python/issues
|
|
341
|
+
- Email: support@smartflow.ai
|
|
342
|
+
|
|
343
|
+
---
|
|
344
|
+
|
|
345
|
+
Built with ❤️ by [Langsmart, Inc.](https://smartflow.ai)
|
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
# Smartflow Python SDK
|
|
2
|
+
|
|
3
|
+
The official Python SDK for [Smartflow](https://smartflow.ai) - the enterprise AI orchestration, caching, compliance, and governance platform.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **🚀 Simple API** - Chat, embeddings, and completions with one line of code
|
|
8
|
+
- **💰 60-80% Cost Savings** - 3-layer semantic caching (L1/L2/L3)
|
|
9
|
+
- **🛡️ ML-Powered Compliance** - Intelligent PII detection with adaptive learning
|
|
10
|
+
- **🔄 Automatic Failover** - Multi-provider routing with intelligent fallback
|
|
11
|
+
- **📊 Full Audit Trail** - VAS logs for every AI interaction
|
|
12
|
+
- **🤖 Agent Builder** - Create AI agents with built-in compliance
|
|
13
|
+
- **📈 Workflow Orchestration** - Chain AI operations with branching and error handling
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
pip install smartflow-sdk
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Quick Start
|
|
22
|
+
|
|
23
|
+
### Async Usage (Recommended)
|
|
24
|
+
|
|
25
|
+
```python
|
|
26
|
+
import asyncio
|
|
27
|
+
from smartflow import SmartflowClient
|
|
28
|
+
|
|
29
|
+
async def main():
|
|
30
|
+
async with SmartflowClient("http://your-smartflow:7775") as sf:
|
|
31
|
+
# Simple chat
|
|
32
|
+
response = await sf.chat("What is machine learning?")
|
|
33
|
+
print(response)
|
|
34
|
+
|
|
35
|
+
# Check cache stats
|
|
36
|
+
stats = await sf.get_cache_stats()
|
|
37
|
+
print(f"Cache hit rate: {stats.hit_rate:.1%}")
|
|
38
|
+
print(f"Tokens saved: {stats.tokens_saved:,}")
|
|
39
|
+
|
|
40
|
+
asyncio.run(main())
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### Sync Usage
|
|
44
|
+
|
|
45
|
+
```python
|
|
46
|
+
from smartflow import SyncSmartflowClient
|
|
47
|
+
|
|
48
|
+
sf = SyncSmartflowClient("http://your-smartflow:7775")
|
|
49
|
+
|
|
50
|
+
response = sf.chat("Explain quantum computing")
|
|
51
|
+
print(response)
|
|
52
|
+
|
|
53
|
+
sf.close()
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### OpenAI Drop-in Replacement
|
|
57
|
+
|
|
58
|
+
Just change the `base_url` - your existing OpenAI code works with Smartflow!
|
|
59
|
+
|
|
60
|
+
```python
|
|
61
|
+
from openai import OpenAI
|
|
62
|
+
|
|
63
|
+
# Point to Smartflow instead of OpenAI
|
|
64
|
+
client = OpenAI(
|
|
65
|
+
base_url="http://your-smartflow:7775/v1",
|
|
66
|
+
api_key="your-key" # Or use Smartflow's stored keys
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
response = client.chat.completions.create(
|
|
70
|
+
model="gpt-4o",
|
|
71
|
+
messages=[{"role": "user", "content": "Hello!"}]
|
|
72
|
+
)
|
|
73
|
+
print(response.choices[0].message.content)
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## Intelligent Compliance (ML-Powered)
|
|
77
|
+
|
|
78
|
+
Smartflow's adaptive learning compliance engine provides:
|
|
79
|
+
|
|
80
|
+
- **Regex Pattern Matching** - SSN, credit cards, emails, phone numbers, etc.
|
|
81
|
+
- **ML Embedding Similarity** - Semantic violation detection
|
|
82
|
+
- **Behavioral Analysis** - User pattern tracking and anomaly detection
|
|
83
|
+
- **Organization Baselines** - Deviation detection from org norms
|
|
84
|
+
|
|
85
|
+
```python
|
|
86
|
+
async with SmartflowClient("http://your-smartflow:7775") as sf:
|
|
87
|
+
# Scan content for compliance issues
|
|
88
|
+
result = await sf.intelligent_scan(
|
|
89
|
+
content="My SSN is 123-45-6789 and my email is john@example.com",
|
|
90
|
+
user_id="user123",
|
|
91
|
+
org_id="acme_corp"
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
print(f"Risk Score: {result.risk_score:.2f}")
|
|
95
|
+
print(f"Risk Level: {result.risk_level}")
|
|
96
|
+
print(f"Action: {result.recommended_action}")
|
|
97
|
+
print(f"Explanation: {result.explanation}")
|
|
98
|
+
|
|
99
|
+
# Check regex violations
|
|
100
|
+
for violation in result.regex_violations:
|
|
101
|
+
print(f" - {violation['violation_type']}: {violation['severity']}")
|
|
102
|
+
|
|
103
|
+
# Submit feedback to improve detection
|
|
104
|
+
await sf.submit_compliance_feedback(
|
|
105
|
+
scan_id="scan_abc123",
|
|
106
|
+
is_false_positive=True,
|
|
107
|
+
notes="This was a test number"
|
|
108
|
+
)
|
|
109
|
+
|
|
110
|
+
# Get learning status
|
|
111
|
+
learning = await sf.get_learning_summary()
|
|
112
|
+
print(f"Users tracked: {learning.total_users}")
|
|
113
|
+
print(f"Learning complete: {learning.users_learning_complete}")
|
|
114
|
+
|
|
115
|
+
# Get ML stats
|
|
116
|
+
ml_stats = await sf.get_ml_stats()
|
|
117
|
+
print(f"Total patterns: {ml_stats.total_patterns}")
|
|
118
|
+
print(f"Learned patterns: {ml_stats.learned_patterns}")
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
## Building AI Agents
|
|
122
|
+
|
|
123
|
+
Create AI agents with built-in compliance scanning and conversation memory:
|
|
124
|
+
|
|
125
|
+
```python
|
|
126
|
+
from smartflow import SmartflowClient, SmartflowAgent
|
|
127
|
+
|
|
128
|
+
async with SmartflowClient("http://your-smartflow:7775") as sf:
|
|
129
|
+
agent = SmartflowAgent(
|
|
130
|
+
client=sf,
|
|
131
|
+
name="CustomerSupport",
|
|
132
|
+
model="gpt-4o",
|
|
133
|
+
system_prompt="""You are a helpful customer support agent for TechCorp.
|
|
134
|
+
Be professional, friendly, and always protect customer data.""",
|
|
135
|
+
compliance_policy="enterprise_standard",
|
|
136
|
+
enable_compliance_scan=True,
|
|
137
|
+
user_id="support_agent_1",
|
|
138
|
+
org_id="techcorp"
|
|
139
|
+
)
|
|
140
|
+
|
|
141
|
+
# Chat with automatic compliance scanning
|
|
142
|
+
response = await agent.chat("How do I reset my password?")
|
|
143
|
+
print(response)
|
|
144
|
+
|
|
145
|
+
# Conversation memory is maintained
|
|
146
|
+
response = await agent.chat("What about two-factor authentication?")
|
|
147
|
+
print(response)
|
|
148
|
+
|
|
149
|
+
# Get conversation history
|
|
150
|
+
history = agent.get_history()
|
|
151
|
+
print(f"Messages: {len(history)}")
|
|
152
|
+
|
|
153
|
+
# Clear and start fresh
|
|
154
|
+
agent.clear_history()
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
## Workflow Orchestration
|
|
158
|
+
|
|
159
|
+
Chain AI operations with branching, parallel execution, and error handling:
|
|
160
|
+
|
|
161
|
+
```python
|
|
162
|
+
from smartflow import SmartflowClient, SmartflowWorkflow
|
|
163
|
+
|
|
164
|
+
async with SmartflowClient("http://your-smartflow:7775") as sf:
|
|
165
|
+
workflow = SmartflowWorkflow(sf, name="TicketClassification")
|
|
166
|
+
|
|
167
|
+
# Step 1: Classify the ticket
|
|
168
|
+
workflow.add_step(
|
|
169
|
+
name="classify",
|
|
170
|
+
action="chat",
|
|
171
|
+
config={
|
|
172
|
+
"prompt": "Classify this support ticket into one of: billing, technical, account. Ticket: {input}",
|
|
173
|
+
"model": "gpt-4o-mini"
|
|
174
|
+
},
|
|
175
|
+
next_steps=["route"]
|
|
176
|
+
)
|
|
177
|
+
|
|
178
|
+
# Step 2: Route based on classification
|
|
179
|
+
workflow.add_step(
|
|
180
|
+
name="route",
|
|
181
|
+
action="condition",
|
|
182
|
+
config={
|
|
183
|
+
"field": "output",
|
|
184
|
+
"cases": {
|
|
185
|
+
"billing": "billing_response",
|
|
186
|
+
"technical": "technical_response",
|
|
187
|
+
"account": "account_response"
|
|
188
|
+
},
|
|
189
|
+
"default": "general_response"
|
|
190
|
+
}
|
|
191
|
+
)
|
|
192
|
+
|
|
193
|
+
# Execute the workflow
|
|
194
|
+
result = await workflow.execute({"input": "My payment failed yesterday"})
|
|
195
|
+
|
|
196
|
+
print(f"Success: {result.success}")
|
|
197
|
+
print(f"Output: {result.output}")
|
|
198
|
+
print(f"Steps executed: {result.steps_executed}")
|
|
199
|
+
print(f"Execution time: {result.execution_time_ms:.1f}ms")
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
## Monitoring & Analytics
|
|
203
|
+
|
|
204
|
+
```python
|
|
205
|
+
async with SmartflowClient("http://your-smartflow:7775") as sf:
|
|
206
|
+
# System health
|
|
207
|
+
health = await sf.health_comprehensive()
|
|
208
|
+
print(f"Status: {health.status}")
|
|
209
|
+
print(f"Uptime: {health.uptime_seconds / 3600:.1f} hours")
|
|
210
|
+
|
|
211
|
+
# Provider health
|
|
212
|
+
providers = await sf.get_provider_health()
|
|
213
|
+
for p in providers:
|
|
214
|
+
print(f"{p.provider}: {p.status} ({p.latency_ms:.0f}ms)")
|
|
215
|
+
|
|
216
|
+
# Cache statistics
|
|
217
|
+
cache = await sf.get_cache_stats()
|
|
218
|
+
print(f"Hit rate: {cache.hit_rate:.1%}")
|
|
219
|
+
print(f"L1 hits: {cache.l1_hits}")
|
|
220
|
+
print(f"L2 hits: {cache.l2_hits}")
|
|
221
|
+
print(f"Tokens saved: {cache.tokens_saved:,}")
|
|
222
|
+
|
|
223
|
+
# Audit logs
|
|
224
|
+
logs = await sf.get_logs(limit=10)
|
|
225
|
+
for log in logs:
|
|
226
|
+
print(f"{log.timestamp}: {log.provider}/{log.model} - {log.tokens_used} tokens")
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
## Configuration
|
|
230
|
+
|
|
231
|
+
### Client Options
|
|
232
|
+
|
|
233
|
+
```python
|
|
234
|
+
sf = SmartflowClient(
|
|
235
|
+
base_url="http://smartflow:7775", # Proxy URL
|
|
236
|
+
api_key="your-api-key", # Optional API key
|
|
237
|
+
timeout=30.0, # Request timeout
|
|
238
|
+
management_port=7778, # Management API port
|
|
239
|
+
compliance_port=7777, # Compliance API port
|
|
240
|
+
bridge_port=3500, # Hybrid bridge port
|
|
241
|
+
)
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
### Environment Variables
|
|
245
|
+
|
|
246
|
+
```bash
|
|
247
|
+
export SMARTFLOW_URL="http://your-smartflow:7775"
|
|
248
|
+
export SMARTFLOW_API_KEY="your-key"
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
## API Reference
|
|
252
|
+
|
|
253
|
+
### SmartflowClient Methods
|
|
254
|
+
|
|
255
|
+
| Method | Description |
|
|
256
|
+
|--------|-------------|
|
|
257
|
+
| `chat()` | Simple chat with AI |
|
|
258
|
+
| `chat_completions()` | OpenAI-compatible completions |
|
|
259
|
+
| `embeddings()` | Generate text embeddings |
|
|
260
|
+
| `claude_message()` | Anthropic Claude API |
|
|
261
|
+
| `intelligent_scan()` | ML-powered compliance scan |
|
|
262
|
+
| `check_compliance()` | Basic compliance check |
|
|
263
|
+
| `get_cache_stats()` | Cache hit rates and savings |
|
|
264
|
+
| `health()` | Quick health check |
|
|
265
|
+
| `health_comprehensive()` | Full system health |
|
|
266
|
+
| `get_logs()` | VAS audit logs |
|
|
267
|
+
| `get_provider_health()` | Provider status |
|
|
268
|
+
|
|
269
|
+
### SmartflowAgent Methods
|
|
270
|
+
|
|
271
|
+
| Method | Description |
|
|
272
|
+
|--------|-------------|
|
|
273
|
+
| `chat()` | Chat with compliance scanning |
|
|
274
|
+
| `clear_history()` | Reset conversation |
|
|
275
|
+
| `get_history()` | Get conversation history |
|
|
276
|
+
|
|
277
|
+
### SmartflowWorkflow Methods
|
|
278
|
+
|
|
279
|
+
| Method | Description |
|
|
280
|
+
|--------|-------------|
|
|
281
|
+
| `add_step()` | Add a workflow step |
|
|
282
|
+
| `set_entry()` | Set entry point |
|
|
283
|
+
| `execute()` | Run the workflow |
|
|
284
|
+
|
|
285
|
+
## Examples
|
|
286
|
+
|
|
287
|
+
See the [examples/](./examples/) directory for more:
|
|
288
|
+
|
|
289
|
+
- `simple_chat.py` - Basic chat usage
|
|
290
|
+
- `compliance_check.py` - PII detection and redaction
|
|
291
|
+
- `system_monitoring.py` - Health and analytics
|
|
292
|
+
- `openai_drop_in.py` - OpenAI compatibility
|
|
293
|
+
- `agent_example.py` - Building AI agents
|
|
294
|
+
- `workflow_example.py` - Workflow orchestration
|
|
295
|
+
|
|
296
|
+
## License
|
|
297
|
+
|
|
298
|
+
MIT License - see [LICENSE](./LICENSE) for details.
|
|
299
|
+
|
|
300
|
+
## Support
|
|
301
|
+
|
|
302
|
+
- Documentation: https://docs.smartflow.ai/sdk/python
|
|
303
|
+
- Issues: https://github.com/langsmart/smartflow-sdk-python/issues
|
|
304
|
+
- Email: support@smartflow.ai
|
|
305
|
+
|
|
306
|
+
---
|
|
307
|
+
|
|
308
|
+
Built with ❤️ by [Langsmart, Inc.](https://smartflow.ai)
|