@macaw-ai/client 0.0.0-stage → 0.9.10
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.md +339 -0
- package/bin/install.js +278 -0
- package/package.json +37 -4
- package/payload/README.md +56 -0
- package/payload/secureAI/LICENSE +190 -0
- package/payload/secureAI/NOTICE +5 -0
- package/payload/secureAI/demos/policy-controls/1_uncontrolled.py +139 -0
- package/payload/secureAI/demos/policy-controls/2_with_macaw.py +139 -0
- package/payload/secureAI/demos/policy-controls/3_with_user_policies.py +162 -0
- package/payload/secureAI/demos/policy-controls/credentials.txt +12 -0
- package/payload/secureAI/demos/policy-controls/malicious_report.txt +21 -0
- package/payload/secureAI/demos/policy-controls/sales_report.txt +20 -0
- package/payload/secureAI/demos/tutorial-1-rbac/README.md +143 -0
- package/payload/secureAI/demos/tutorial-1-rbac/app.py +147 -0
- package/payload/secureAI/demos/tutorial-1-rbac/authprompts_demo.py +306 -0
- package/payload/secureAI/demos/tutorial-1-rbac/config/claims-config.yaml +152 -0
- package/payload/secureAI/demos/tutorial-1-rbac/demo_secureopenai_with_policies.py +150 -0
- package/payload/secureAI/demos/tutorial-1-rbac/policies/bu_Analytics.json +8 -0
- package/payload/secureAI/demos/tutorial-1-rbac/policies/company_FinTech_Corp.json +70 -0
- package/payload/secureAI/demos/tutorial-1-rbac/policies/llm-base.json +69 -0
- package/payload/secureAI/demos/tutorial-1-rbac/policies/team_Reporting.json +8 -0
- package/payload/secureAI/demos/tutorial-1-rbac/policies/user_alice.json +32 -0
- package/payload/secureAI/demos/tutorial-1-rbac/policies/user_bob.json +28 -0
- package/payload/secureAI/demos/tutorial-1-rbac/policies/user_carol.json +34 -0
- package/payload/secureAI/demos/tutorial-1-rbac/requirements.txt +6 -0
- package/payload/secureAI/demos/tutorial-1-rbac/setup/auth0_complete_setup.sh +392 -0
- package/payload/secureAI/demos/tutorial-1-rbac/setup/keycloak_complete_setup.sh +320 -0
- package/payload/secureAI/demos/tutorial-2-intent/Policies/alation-remote-proxy.json +155 -0
- package/payload/secureAI/demos/tutorial-2-intent/Policies/bu.json +22 -0
- package/payload/secureAI/demos/tutorial-2-intent/Policies/company.json +17 -0
- package/payload/secureAI/demos/tutorial-2-intent/Policies/user_alice.json +48 -0
- package/payload/secureAI/demos/tutorial-2-intent/Policies/user_bob.json +83 -0
- package/payload/secureAI/demos/tutorial-2-intent/README.md +213 -0
- package/payload/secureAI/demos/tutorial-2-intent/script.py +188 -0
- package/payload/secureAI/demos/tutorial-2-intent/setup/alation_verifier.py +177 -0
- package/payload/secureAI/demos/tutorial-2-intent/setup/get_alation_user_token.py +180 -0
- package/payload/secureAI/examples/README.md +244 -0
- package/payload/secureAI/examples/anthropic/anthropic_1a_dropin_simple.py +103 -0
- package/payload/secureAI/examples/anthropic/anthropic_1b_multiuser_bind.py +190 -0
- package/payload/secureAI/examples/anthropic/anthropic_1b_multiuser_bind_streaming.py +190 -0
- package/payload/secureAI/examples/anthropic/anthropic_1c_a2a_invoke.py +156 -0
- package/payload/secureAI/examples/attestations/1a_trade_alice.py +215 -0
- package/payload/secureAI/examples/attestations/1a_trade_bob.py +164 -0
- package/payload/secureAI/examples/attestations/1b_grant_admin.py +170 -0
- package/payload/secureAI/examples/attestations/1b_grant_researcher.py +231 -0
- package/payload/secureAI/examples/attestations/README.md +128 -0
- package/payload/secureAI/examples/langchain/langchain_1a_dropin_simple.py +199 -0
- package/payload/secureAI/examples/langchain/langchain_1b_multiuser.py +262 -0
- package/payload/secureAI/examples/langchain/langchain_1c_orchestration.py +332 -0
- package/payload/secureAI/examples/langchain/langchain_1d_llm_openai.py +130 -0
- package/payload/secureAI/examples/langchain/langchain_1e_llm_anthropic.py +130 -0
- package/payload/secureAI/examples/langchain/langchain_1f_memory.py +214 -0
- package/payload/secureAI/examples/litellm/litellm_1a_dropin_simple.py +131 -0
- package/payload/secureAI/examples/mcp/1a_simple_invocation.py +115 -0
- package/payload/secureAI/examples/mcp/1b_discovery_and_resources.py +142 -0
- package/payload/secureAI/examples/mcp/1c_logging_client.py +100 -0
- package/payload/secureAI/examples/mcp/1d_progress_client.py +100 -0
- package/payload/secureAI/examples/mcp/1e_sampling_client.py +150 -0
- package/payload/secureAI/examples/mcp/1e_sampling_server.py +90 -0
- package/payload/secureAI/examples/mcp/1f_elicitation_client.py +127 -0
- package/payload/secureAI/examples/mcp/1f_elicitation_server.py +73 -0
- package/payload/secureAI/examples/mcp/1g_roots_client.py +122 -0
- package/payload/secureAI/examples/mcp/1g_roots_server.py +85 -0
- package/payload/secureAI/examples/mcp/2a_proxy_snowflake.py +212 -0
- package/payload/secureAI/examples/mcp/2b_proxy_databricks.py +299 -0
- package/payload/secureAI/examples/mcp/2c_proxy_stdio_fetch.py +91 -0
- package/payload/secureAI/examples/mcp/2d_proxy_stdio_salesforce.py +119 -0
- package/payload/secureAI/examples/mcp/2e_proxy_multiuser.py +136 -0
- package/payload/secureAI/examples/mcp/2f_proxy_stdio_databricks.py +144 -0
- package/payload/secureAI/examples/mcp/3a_native_mcp_client.py +104 -0
- package/payload/secureAI/examples/mcp/3b_native_mcp_proxy.py +114 -0
- package/payload/secureAI/examples/mcp/3c_native_mcp_sampling.py +180 -0
- package/payload/secureAI/examples/mcp/3d_native_mcp_2026.py +135 -0
- package/payload/secureAI/examples/mcp/securemcp_calculator.py +248 -0
- package/payload/secureAI/examples/mcp/securemcp_server.py +75 -0
- package/payload/secureAI/examples/openai/openai_1a_dropin_simple.py +102 -0
- package/payload/secureAI/examples/openai/openai_1b_multiuser_bind.py +193 -0
- package/payload/secureAI/examples/openai/openai_1b_multiuser_bind_streaming.py +199 -0
- package/payload/secureAI/examples/openai/openai_1c_a2a_invoke.py +156 -0
- package/payload/secureAI/examples/openai/openai_1d_invocation_patterns.py +337 -0
- package/payload/secureAI/examples/phishing-resistance/1a_login_alice.py +180 -0
- package/payload/secureAI/examples/phishing-resistance/1b_trade_alice.py +241 -0
- package/payload/secureAI/examples/phishing-resistance/1b_trade_bob.py +164 -0
- package/payload/secureAI/examples/phishing-resistance/README.md +124 -0
- package/payload/secureAI/examples/phishing-resistance/get_token.py +114 -0
- package/payload/secureAI/examples/pydantic_ai/README.md +88 -0
- package/payload/secureAI/examples/pydantic_ai/policies/app_pydantic-agent.json +10 -0
- package/payload/secureAI/examples/pydantic_ai/pydanticai_1a_dropin_simple.py +90 -0
- package/payload/secureAI/examples/pydantic_ai/pydanticai_1b_model_routing.py +85 -0
- package/payload/secureAI/examples/pydantic_ai/pydanticai_1c_native_tools.py +83 -0
- package/payload/secureAI/examples/pydantic_ai/pydanticai_1d_multiuser_bind.py +104 -0
- package/payload/secureAI/examples/pydantic_ai/pydanticai_1e_mcp_compose.py +92 -0
- package/payload/secureAI/macaw_adapters/__init__.py +55 -0
- package/payload/secureAI/macaw_adapters/anthropic/REFERENCE.md +374 -0
- package/payload/secureAI/macaw_adapters/anthropic/__init__.py +22 -0
- package/payload/secureAI/macaw_adapters/anthropic/secure_anthropic.py +792 -0
- package/payload/secureAI/macaw_adapters/anthropic_ext_tools/__init__.py +25 -0
- package/payload/secureAI/macaw_adapters/anthropic_ext_tools/secure_anthropic.py +840 -0
- package/payload/secureAI/macaw_adapters/langchain/REFERENCE.md +234 -0
- package/payload/secureAI/macaw_adapters/langchain/__init__.py +100 -0
- package/payload/secureAI/macaw_adapters/langchain/_utils.py +83 -0
- package/payload/secureAI/macaw_adapters/langchain/agents.py +296 -0
- package/payload/secureAI/macaw_adapters/langchain/anthropic.py +456 -0
- package/payload/secureAI/macaw_adapters/langchain/callbacks.py +306 -0
- package/payload/secureAI/macaw_adapters/langchain/memory.py +352 -0
- package/payload/secureAI/macaw_adapters/langchain/openai.py +410 -0
- package/payload/secureAI/macaw_adapters/langchain/tools.py +174 -0
- package/payload/secureAI/macaw_adapters/litellm/README.md +216 -0
- package/payload/secureAI/macaw_adapters/litellm/__init__.py +143 -0
- package/payload/secureAI/macaw_adapters/litellm/secure_litellm.py +686 -0
- package/payload/secureAI/macaw_adapters/mcp/REFERENCE.md +460 -0
- package/payload/secureAI/macaw_adapters/mcp/__init__.py +65 -0
- package/payload/secureAI/macaw_adapters/mcp/_endpoint.py +193 -0
- package/payload/secureAI/macaw_adapters/mcp/_endpoint2.py +225 -0
- package/payload/secureAI/macaw_adapters/mcp/_serve.py +25 -0
- package/payload/secureAI/macaw_adapters/mcp/client.py +721 -0
- package/payload/secureAI/macaw_adapters/mcp/decorators.py +228 -0
- package/payload/secureAI/macaw_adapters/mcp/fastmcp_compat.py +353 -0
- package/payload/secureAI/macaw_adapters/mcp/mcp.py +785 -0
- package/payload/secureAI/macaw_adapters/mcp/proxy.py +626 -0
- package/payload/secureAI/macaw_adapters/mcp/server.py +317 -0
- package/payload/secureAI/macaw_adapters/mcp/subscriptions.py +450 -0
- package/payload/secureAI/macaw_adapters/openai/REFERENCE.md +368 -0
- package/payload/secureAI/macaw_adapters/openai/__init__.py +22 -0
- package/payload/secureAI/macaw_adapters/openai/secure_openai.py +833 -0
- package/payload/secureAI/macaw_adapters/openai_ext_tools/REFERENCE.md +368 -0
- package/payload/secureAI/macaw_adapters/openai_ext_tools/__init__.py +25 -0
- package/payload/secureAI/macaw_adapters/openai_ext_tools/secure_openai.py +876 -0
- package/payload/secureAI/macaw_adapters/pydantic_ai/__init__.py +36 -0
- package/payload/secureAI/macaw_adapters/pydantic_ai/_core.py +257 -0
- package/payload/secureAI/macaw_adapters/pydantic_ai/agent.py +218 -0
- package/payload/secureAI/macaw_adapters/pydantic_ai/model.py +99 -0
- package/payload/secureAI/macaw_adapters/pydantic_ai/toolset.py +86 -0
- package/payload/secureAI/test_harness_quick.py +296 -0
- package/wheels/macaw_adapters-0.9.10.1-py3-none-any.whl +0 -0
- package/wheels/macaw_client-0.9.10.1-cp312-cp312-macosx_11_0_arm64.whl +0 -0
- package/wheels/macaw_client-0.9.10.1-cp312-cp312-manylinux_2_17_x86_64.whl +0 -0
- package/wheels/macaw_client-0.9.10.1-cp314-cp314-macosx_11_0_arm64.whl +0 -0
- package/wheels/macaw_client-0.9.10.1-cp314-cp314-manylinux_2_17_x86_64.whl +0 -0
- package/wheels/macaw_client-0.9.10.1-cp314-cp314-win_amd64.whl +0 -0
- package/wheels/macaw_client-0.9.10.1-cp39-cp39-macosx_11_0_arm64.whl +0 -0
- package/README.md +0 -3
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
# SecureLiteLLM - MACAW-Protected LiteLLM Adapter
|
|
2
|
+
|
|
3
|
+
Drop-in replacement for [LiteLLM](https://github.com/BerriAI/litellm) with MACAW security.
|
|
4
|
+
Supports 100+ LLM providers through LiteLLM's unified interface.
|
|
5
|
+
|
|
6
|
+
## Installation
|
|
7
|
+
|
|
8
|
+
```bash
|
|
9
|
+
pip install macaw-adapters[litellm]
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
Or for development:
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
pip install -e ".[litellm]"
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Usage
|
|
19
|
+
|
|
20
|
+
### Drop-in Replacement
|
|
21
|
+
|
|
22
|
+
Just change your import - everything else stays the same:
|
|
23
|
+
|
|
24
|
+
```python
|
|
25
|
+
# Before
|
|
26
|
+
import litellm
|
|
27
|
+
|
|
28
|
+
# After
|
|
29
|
+
from macaw_adapters import litellm
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
### Examples
|
|
33
|
+
|
|
34
|
+
```python
|
|
35
|
+
from macaw_adapters import litellm
|
|
36
|
+
|
|
37
|
+
# Groq (Llama, Mixtral)
|
|
38
|
+
response = litellm.completion(
|
|
39
|
+
model="groq/llama3-70b-8192",
|
|
40
|
+
messages=[{"role": "user", "content": "Hello"}]
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
# Together AI (100+ models)
|
|
44
|
+
response = litellm.completion(
|
|
45
|
+
model="together_ai/meta-llama/Llama-3-70b",
|
|
46
|
+
messages=[{"role": "user", "content": "Hello"}]
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
# Mistral
|
|
50
|
+
response = litellm.completion(
|
|
51
|
+
model="mistral/mistral-large-latest",
|
|
52
|
+
messages=[{"role": "user", "content": "Hello"}]
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
# Local vLLM
|
|
56
|
+
response = litellm.completion(
|
|
57
|
+
model="openai/llama3",
|
|
58
|
+
messages=[{"role": "user", "content": "Hello"}],
|
|
59
|
+
api_base="http://localhost:8000/v1"
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
# Local Ollama
|
|
63
|
+
response = litellm.completion(
|
|
64
|
+
model="ollama/llama3",
|
|
65
|
+
messages=[{"role": "user", "content": "Hello"}],
|
|
66
|
+
api_base="http://localhost:11434"
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
# With explicit app name for MACAW registration
|
|
70
|
+
response = litellm.completion(
|
|
71
|
+
model="groq/llama3-70b",
|
|
72
|
+
messages=[{"role": "user", "content": "Hello"}],
|
|
73
|
+
app_name="my-production-service"
|
|
74
|
+
)
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### Class-Based Usage
|
|
78
|
+
|
|
79
|
+
For advanced control over the client lifecycle:
|
|
80
|
+
|
|
81
|
+
```python
|
|
82
|
+
from macaw_adapters.litellm import SecureLiteLLM
|
|
83
|
+
|
|
84
|
+
# Create client with custom configuration
|
|
85
|
+
client = SecureLiteLLM(
|
|
86
|
+
app_name="my-app",
|
|
87
|
+
intent_policy={"resources": ["llm:*"]}
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
# Use like regular litellm
|
|
91
|
+
response = client.completion(
|
|
92
|
+
model="groq/llama3-70b",
|
|
93
|
+
messages=[{"role": "user", "content": "Hello"}]
|
|
94
|
+
)
|
|
95
|
+
|
|
96
|
+
# Or OpenAI-style API
|
|
97
|
+
response = client.chat.completions.create(
|
|
98
|
+
model="groq/llama3-70b",
|
|
99
|
+
messages=[{"role": "user", "content": "Hello"}]
|
|
100
|
+
)
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
### Tool Registration
|
|
104
|
+
|
|
105
|
+
Register tools that the LLM can call:
|
|
106
|
+
|
|
107
|
+
```python
|
|
108
|
+
from macaw_adapters.litellm import SecureLiteLLM
|
|
109
|
+
|
|
110
|
+
client = SecureLiteLLM(app_name="my-app")
|
|
111
|
+
|
|
112
|
+
# Register a tool
|
|
113
|
+
def get_weather(city: str) -> dict:
|
|
114
|
+
return {"city": city, "temperature": 72, "condition": "sunny"}
|
|
115
|
+
|
|
116
|
+
client.register_tool("get_weather", get_weather)
|
|
117
|
+
|
|
118
|
+
# LLM can now call this tool (through MACAW policy enforcement)
|
|
119
|
+
response = client.completion(
|
|
120
|
+
model="groq/llama3-70b",
|
|
121
|
+
messages=[{"role": "user", "content": "What's the weather in NYC?"}],
|
|
122
|
+
tools=[{
|
|
123
|
+
"type": "function",
|
|
124
|
+
"function": {
|
|
125
|
+
"name": "get_weather",
|
|
126
|
+
"parameters": {"type": "object", "properties": {"city": {"type": "string"}}}
|
|
127
|
+
}
|
|
128
|
+
}]
|
|
129
|
+
)
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
## Supported Providers
|
|
133
|
+
|
|
134
|
+
SecureLiteLLM supports all providers that LiteLLM supports, including:
|
|
135
|
+
|
|
136
|
+
| Provider | Model Format | Example |
|
|
137
|
+
|----------|--------------|---------|
|
|
138
|
+
| OpenAI | `gpt-4`, `gpt-3.5-turbo` | `model="gpt-4"` |
|
|
139
|
+
| Groq | `groq/<model>` | `model="groq/llama3-70b-8192"` |
|
|
140
|
+
| Together AI | `together_ai/<model>` | `model="together_ai/meta-llama/Llama-3-70b"` |
|
|
141
|
+
| Mistral | `mistral/<model>` | `model="mistral/mistral-large-latest"` |
|
|
142
|
+
| Anthropic | `claude-3-opus`, etc. | `model="claude-3-opus-20240229"` |
|
|
143
|
+
| AWS Bedrock | `bedrock/<model>` | `model="bedrock/anthropic.claude-3"` |
|
|
144
|
+
| Google Vertex | `vertex_ai/<model>` | `model="vertex_ai/gemini-pro"` |
|
|
145
|
+
| Azure OpenAI | `azure/<deployment>` | `model="azure/my-deployment"` |
|
|
146
|
+
| Cohere | `cohere/<model>` | `model="cohere/command-r-plus"` |
|
|
147
|
+
| vLLM (local) | `openai/<model>` | `model="openai/llama3"` + `api_base` |
|
|
148
|
+
| Ollama (local) | `ollama/<model>` | `model="ollama/llama3"` |
|
|
149
|
+
|
|
150
|
+
See [LiteLLM Providers](https://docs.litellm.ai/docs/providers) for the full list.
|
|
151
|
+
|
|
152
|
+
## Key Features
|
|
153
|
+
|
|
154
|
+
- **Drop-in replacement**: Just change `import litellm` to `from macaw_adapters import litellm`
|
|
155
|
+
- **100+ providers**: All LiteLLM-supported providers work automatically
|
|
156
|
+
- **MACAW security**: Full policy enforcement, attestations, and audit logging
|
|
157
|
+
- **Default app_name**: `macaw-litellm` (can be overridden per-call or at client creation)
|
|
158
|
+
- **Tool isolation**: User-registered tools are isolated and policy-protected
|
|
159
|
+
|
|
160
|
+
## Policy IDs
|
|
161
|
+
|
|
162
|
+
When using SecureLiteLLM, the following MAPL resource patterns are used:
|
|
163
|
+
|
|
164
|
+
| Operation | Resource ID |
|
|
165
|
+
|-----------|-------------|
|
|
166
|
+
| Chat completion | `tool:{app_name}/generate` |
|
|
167
|
+
| Text completion | `tool:{app_name}/complete` |
|
|
168
|
+
| Embeddings | `tool:{app_name}/embed` |
|
|
169
|
+
| User tools | `tool:{app_name}/{tool_name}` |
|
|
170
|
+
|
|
171
|
+
Example policy for a LiteLLM-based service:
|
|
172
|
+
|
|
173
|
+
```json
|
|
174
|
+
{
|
|
175
|
+
"policy_id": "tool:my-app/generate",
|
|
176
|
+
"resources": ["tool:my-app/generate"],
|
|
177
|
+
"constraints": {
|
|
178
|
+
"parameters": {
|
|
179
|
+
"tool:my-app/generate": {
|
|
180
|
+
"model": ["groq/llama3-70b-8192", "gpt-4"]
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
## Environment Variables
|
|
188
|
+
|
|
189
|
+
LiteLLM uses provider-specific environment variables for API keys:
|
|
190
|
+
|
|
191
|
+
```bash
|
|
192
|
+
# Groq
|
|
193
|
+
export GROQ_API_KEY="your-key"
|
|
194
|
+
|
|
195
|
+
# Together AI
|
|
196
|
+
export TOGETHER_API_KEY="your-key"
|
|
197
|
+
|
|
198
|
+
# Mistral
|
|
199
|
+
export MISTRAL_API_KEY="your-key"
|
|
200
|
+
|
|
201
|
+
# OpenAI
|
|
202
|
+
export OPENAI_API_KEY="your-key"
|
|
203
|
+
|
|
204
|
+
# Or pass directly
|
|
205
|
+
response = litellm.completion(
|
|
206
|
+
model="groq/llama3-70b",
|
|
207
|
+
messages=[...],
|
|
208
|
+
api_key="your-key"
|
|
209
|
+
)
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
## More Information
|
|
213
|
+
|
|
214
|
+
- [MACAW Security](https://macawsecurity.ai)
|
|
215
|
+
- [MACAW Console](https://console.macawsecurity.ai)
|
|
216
|
+
- [LiteLLM Documentation](https://docs.litellm.ai)
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
"""
|
|
2
|
+
MACAW-secured LiteLLM adapter.
|
|
3
|
+
|
|
4
|
+
Drop-in replacement for litellm with MACAW security.
|
|
5
|
+
Supports 100+ LLM providers through LiteLLM's unified interface.
|
|
6
|
+
|
|
7
|
+
Usage:
|
|
8
|
+
# Drop-in replacement
|
|
9
|
+
from macaw_adapters import litellm
|
|
10
|
+
|
|
11
|
+
response = litellm.completion(
|
|
12
|
+
model="groq/llama3-70b-8192",
|
|
13
|
+
messages=[{"role": "user", "content": "Hello"}]
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
# With explicit app_name
|
|
17
|
+
response = litellm.completion(
|
|
18
|
+
model="together_ai/meta-llama/Llama-3-70b",
|
|
19
|
+
messages=[...],
|
|
20
|
+
app_name="my-service"
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
# With custom endpoint (vLLM, Ollama, etc.)
|
|
24
|
+
response = litellm.completion(
|
|
25
|
+
model="openai/llama3",
|
|
26
|
+
messages=[...],
|
|
27
|
+
api_base="http://localhost:8000/v1"
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
# Class-based usage for advanced control
|
|
31
|
+
from macaw_adapters.litellm import SecureLiteLLM
|
|
32
|
+
|
|
33
|
+
client = SecureLiteLLM(app_name="my-app", intent_policy={...})
|
|
34
|
+
response = client.completion(model="groq/llama3-70b", messages=[...])
|
|
35
|
+
|
|
36
|
+
For more information, visit: https://macawsecurity.ai
|
|
37
|
+
"""
|
|
38
|
+
|
|
39
|
+
# Re-export everything from litellm for drop-in compatibility
|
|
40
|
+
from litellm import *
|
|
41
|
+
|
|
42
|
+
# Import our secure wrapper
|
|
43
|
+
from .secure_litellm import SecureLiteLLM, BoundSecureLiteLLM
|
|
44
|
+
|
|
45
|
+
# Default client instance (lazy initialization)
|
|
46
|
+
_default_client = None
|
|
47
|
+
_default_app_name = None
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def _get_client(app_name=None, api_base=None, api_key=None, intent_policy=None):
|
|
51
|
+
"""Get or create the default SecureLiteLLM client."""
|
|
52
|
+
global _default_client, _default_app_name
|
|
53
|
+
|
|
54
|
+
# Use provided app_name or default
|
|
55
|
+
app = app_name or "macaw-litellm"
|
|
56
|
+
|
|
57
|
+
# Create new client if needed
|
|
58
|
+
if _default_client is None or (app_name and app_name != _default_app_name):
|
|
59
|
+
_default_client = SecureLiteLLM(
|
|
60
|
+
app_name=app,
|
|
61
|
+
api_base=api_base,
|
|
62
|
+
api_key=api_key,
|
|
63
|
+
intent_policy=intent_policy
|
|
64
|
+
)
|
|
65
|
+
_default_app_name = app
|
|
66
|
+
|
|
67
|
+
return _default_client
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def completion(model, messages, app_name=None, api_base=None, api_key=None,
|
|
71
|
+
intent_policy=None, **kwargs):
|
|
72
|
+
"""
|
|
73
|
+
Secured drop-in replacement for litellm.completion().
|
|
74
|
+
|
|
75
|
+
All LLM calls go through MACAW policy enforcement.
|
|
76
|
+
|
|
77
|
+
Args:
|
|
78
|
+
model: Model identifier (e.g., "groq/llama3-70b-8192", "gpt-4", etc.)
|
|
79
|
+
messages: List of message dicts
|
|
80
|
+
app_name: Optional MACAW app name (default: macaw-litellm)
|
|
81
|
+
api_base: Optional custom API endpoint
|
|
82
|
+
api_key: Optional API key (uses env vars by default)
|
|
83
|
+
intent_policy: Optional MACAW intent policy
|
|
84
|
+
**kwargs: Additional arguments passed to LiteLLM
|
|
85
|
+
|
|
86
|
+
Returns:
|
|
87
|
+
ModelResponse from LiteLLM
|
|
88
|
+
"""
|
|
89
|
+
client = _get_client(app_name, api_base, api_key, intent_policy)
|
|
90
|
+
return client.completion(model=model, messages=messages, **kwargs)
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
async def acompletion(model, messages, app_name=None, api_base=None, api_key=None,
|
|
94
|
+
intent_policy=None, **kwargs):
|
|
95
|
+
"""
|
|
96
|
+
Secured async drop-in replacement for litellm.acompletion().
|
|
97
|
+
|
|
98
|
+
Note: Currently wraps sync completion. Full async support coming soon.
|
|
99
|
+
"""
|
|
100
|
+
# TODO: Implement proper async support
|
|
101
|
+
return completion(model, messages, app_name, api_base, api_key, intent_policy, **kwargs)
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def embedding(model, input, app_name=None, api_base=None, api_key=None,
|
|
105
|
+
intent_policy=None, **kwargs):
|
|
106
|
+
"""
|
|
107
|
+
Secured drop-in replacement for litellm.embedding().
|
|
108
|
+
"""
|
|
109
|
+
client = _get_client(app_name, api_base, api_key, intent_policy)
|
|
110
|
+
return client.embeddings.create(model=model, input=input, **kwargs)
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
async def aembedding(model, input, app_name=None, api_base=None, api_key=None,
|
|
114
|
+
intent_policy=None, **kwargs):
|
|
115
|
+
"""
|
|
116
|
+
Secured async drop-in replacement for litellm.aembedding().
|
|
117
|
+
|
|
118
|
+
Note: Currently wraps sync embedding. Full async support coming soon.
|
|
119
|
+
"""
|
|
120
|
+
return embedding(model, input, app_name, api_base, api_key, intent_policy, **kwargs)
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def text_completion(model, prompt, app_name=None, api_base=None, api_key=None,
|
|
124
|
+
intent_policy=None, **kwargs):
|
|
125
|
+
"""
|
|
126
|
+
Secured drop-in replacement for litellm.text_completion().
|
|
127
|
+
"""
|
|
128
|
+
client = _get_client(app_name, api_base, api_key, intent_policy)
|
|
129
|
+
return client.completions.create(model=model, prompt=prompt, **kwargs)
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
# Export all
|
|
133
|
+
__all__ = [
|
|
134
|
+
# Secured classes
|
|
135
|
+
"SecureLiteLLM",
|
|
136
|
+
"BoundSecureLiteLLM",
|
|
137
|
+
# Secured drop-in functions
|
|
138
|
+
"completion",
|
|
139
|
+
"acompletion",
|
|
140
|
+
"embedding",
|
|
141
|
+
"aembedding",
|
|
142
|
+
"text_completion",
|
|
143
|
+
]
|