cacheai 0.1.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.
- cacheai-0.1.0/PKG-INFO +317 -0
- cacheai-0.1.0/README.md +281 -0
- cacheai-0.1.0/pyproject.toml +76 -0
- cacheai-0.1.0/setup.cfg +4 -0
- cacheai-0.1.0/src/cacheai/__init__.py +48 -0
- cacheai-0.1.0/src/cacheai/cache_operation.py +391 -0
- cacheai-0.1.0/src/cacheai/client.py +264 -0
- cacheai-0.1.0/src/cacheai/exceptions.py +67 -0
- cacheai-0.1.0/src/cacheai/resources/__init__.py +5 -0
- cacheai-0.1.0/src/cacheai/resources/chat.py +114 -0
- cacheai-0.1.0/src/cacheai/types.py +120 -0
- cacheai-0.1.0/src/cacheai/version.py +4 -0
- cacheai-0.1.0/src/cacheai.egg-info/PKG-INFO +317 -0
- cacheai-0.1.0/src/cacheai.egg-info/SOURCES.txt +16 -0
- cacheai-0.1.0/src/cacheai.egg-info/dependency_links.txt +1 -0
- cacheai-0.1.0/src/cacheai.egg-info/requires.txt +12 -0
- cacheai-0.1.0/src/cacheai.egg-info/top_level.txt +1 -0
- cacheai-0.1.0/tests/test_client.py +71 -0
cacheai-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: cacheai
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Official Python API for CacheAI - Semantic Caching for Large Language Models
|
|
5
|
+
Author-email: "CacheAI Technologies, Inc." <info@cacheaitechnologies.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://www.cacheaitechnologies.com
|
|
8
|
+
Project-URL: Documentation, https://docs.cacheai.tech
|
|
9
|
+
Project-URL: Repository, https://github.com/cacheaitechnologies/cacheai
|
|
10
|
+
Project-URL: Changelog, https://github.com/cacheaitechnologies/cacheai/blob/main/CHANGELOG.md
|
|
11
|
+
Project-URL: Bug Tracker, https://github.com/cacheaitechnologies/cacheai/issues
|
|
12
|
+
Keywords: cacheai,llm,caching,openai,chatgpt,semantic-cache
|
|
13
|
+
Classifier: Development Status :: 4 - Beta
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
21
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
22
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
23
|
+
Requires-Python: >=3.10
|
|
24
|
+
Description-Content-Type: text/markdown
|
|
25
|
+
Requires-Dist: requests>=2.28.0
|
|
26
|
+
Requires-Dist: pydantic>=2.0.0
|
|
27
|
+
Requires-Dist: typing-extensions>=4.5.0
|
|
28
|
+
Requires-Dist: urllib3>=1.26.0
|
|
29
|
+
Provides-Extra: dev
|
|
30
|
+
Requires-Dist: pytest>=7.0.0; extra == "dev"
|
|
31
|
+
Requires-Dist: pytest-cov>=4.0.0; extra == "dev"
|
|
32
|
+
Requires-Dist: mypy>=1.0.0; extra == "dev"
|
|
33
|
+
Requires-Dist: black>=23.0.0; extra == "dev"
|
|
34
|
+
Requires-Dist: ruff>=0.1.0; extra == "dev"
|
|
35
|
+
Requires-Dist: types-requests>=2.28.0; extra == "dev"
|
|
36
|
+
|
|
37
|
+
# CacheAI Python API
|
|
38
|
+
|
|
39
|
+
Official Python API for CacheAI - Semantic Caching for Large Language Models
|
|
40
|
+
|
|
41
|
+
[](https://badge.fury.io/py/cacheai)
|
|
42
|
+
[](https://pypi.org/project/cacheai/)
|
|
43
|
+
[](https://opensource.org/licenses/MIT)
|
|
44
|
+
|
|
45
|
+
## Features
|
|
46
|
+
|
|
47
|
+
- **OpenAI Compatible**: Drop-in replacement for OpenAI Python SDK
|
|
48
|
+
- **Semantic Caching**: Automatic caching of similar queries using advanced semantic similarity
|
|
49
|
+
- **Multiple LLM Backends**: Support for OpenAI, Anthropic, Google AI, and more
|
|
50
|
+
- **Streaming Support**: Full support for streaming responses
|
|
51
|
+
- **Type-Safe**: Complete type hints for better IDE support
|
|
52
|
+
- **Easy Integration**: Minimal code changes required
|
|
53
|
+
|
|
54
|
+
## Installation
|
|
55
|
+
|
|
56
|
+
```bash
|
|
57
|
+
pip install cacheai
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Quick Start
|
|
61
|
+
|
|
62
|
+
### Basic Usage
|
|
63
|
+
|
|
64
|
+
```python
|
|
65
|
+
from cacheai import Client
|
|
66
|
+
|
|
67
|
+
# Initialize client
|
|
68
|
+
client = Client(
|
|
69
|
+
api_key="your-cacheai-api-key",
|
|
70
|
+
base_url="https://api.cacheai.tech/v1" # Optional, this is the default
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
# Create a chat completion
|
|
74
|
+
response = client.chat.completions.create(
|
|
75
|
+
model="gpt-3.5-turbo",
|
|
76
|
+
messages=[
|
|
77
|
+
{"role": "user", "content": "Hello, how are you?"}
|
|
78
|
+
]
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
print(response.choices[0].message.content)
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### With Environment Variables
|
|
85
|
+
|
|
86
|
+
```python
|
|
87
|
+
import os
|
|
88
|
+
from cacheai import Client
|
|
89
|
+
|
|
90
|
+
# Set environment variables
|
|
91
|
+
os.environ["CACHEAI_API_KEY"] = "your-cacheai-api-key"
|
|
92
|
+
os.environ["CACHEAI_BACKEND_PROVIDER"] = "openai"
|
|
93
|
+
os.environ["CACHEAI_BACKEND_API_KEY"] = "your-openai-api-key"
|
|
94
|
+
|
|
95
|
+
# Initialize client (reads from environment)
|
|
96
|
+
client = Client()
|
|
97
|
+
|
|
98
|
+
response = client.chat.completions.create(
|
|
99
|
+
model="gpt-3.5-turbo",
|
|
100
|
+
messages=[{"role": "user", "content": "What is Python?"}]
|
|
101
|
+
)
|
|
102
|
+
|
|
103
|
+
print(response.choices[0].message.content)
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### Streaming
|
|
107
|
+
|
|
108
|
+
```python
|
|
109
|
+
from cacheai import Client
|
|
110
|
+
|
|
111
|
+
client = Client(api_key="your-cacheai-api-key")
|
|
112
|
+
|
|
113
|
+
stream = client.chat.completions.create(
|
|
114
|
+
model="gpt-3.5-turbo",
|
|
115
|
+
messages=[{"role": "user", "content": "Tell me a story"}],
|
|
116
|
+
stream=True
|
|
117
|
+
)
|
|
118
|
+
|
|
119
|
+
for chunk in stream:
|
|
120
|
+
if chunk.choices[0].delta.content:
|
|
121
|
+
print(chunk.choices[0].delta.content, end="", flush=True)
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
## Configuration
|
|
125
|
+
|
|
126
|
+
### Backend LLM Configuration
|
|
127
|
+
|
|
128
|
+
CacheAI acts as a caching layer in front of your preferred LLM provider. Configure the backend:
|
|
129
|
+
|
|
130
|
+
```python
|
|
131
|
+
from cacheai import Client
|
|
132
|
+
|
|
133
|
+
client = Client(
|
|
134
|
+
api_key="your-cacheai-api-key",
|
|
135
|
+
backend_provider="openai", # "openai", "anthropic", "google", etc.
|
|
136
|
+
backend_api_key="your-openai-key", # Backend LLM API key
|
|
137
|
+
)
|
|
138
|
+
|
|
139
|
+
# Or use environment variables:
|
|
140
|
+
# CACHEAI_BACKEND_PROVIDER=openai
|
|
141
|
+
# CACHEAI_BACKEND_API_KEY=sk-...
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
### Cache Control
|
|
145
|
+
|
|
146
|
+
```python
|
|
147
|
+
from cacheai import Client
|
|
148
|
+
|
|
149
|
+
# Disable caching (for debugging/testing)
|
|
150
|
+
client = Client(
|
|
151
|
+
api_key="your-cacheai-api-key",
|
|
152
|
+
enable_cache=False
|
|
153
|
+
)
|
|
154
|
+
|
|
155
|
+
# Or via environment variable:
|
|
156
|
+
# CACHEAI_ENABLE_CACHE=false
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
## Advanced Usage
|
|
160
|
+
|
|
161
|
+
### Context Manager
|
|
162
|
+
|
|
163
|
+
```python
|
|
164
|
+
from cacheai import Client
|
|
165
|
+
|
|
166
|
+
with Client(api_key="your-api-key") as client:
|
|
167
|
+
response = client.chat.completions.create(
|
|
168
|
+
model="gpt-3.5-turbo",
|
|
169
|
+
messages=[{"role": "user", "content": "Hello!"}]
|
|
170
|
+
)
|
|
171
|
+
print(response.choices[0].message.content)
|
|
172
|
+
# Connection is automatically closed
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
### Custom Timeout and Retries
|
|
176
|
+
|
|
177
|
+
```python
|
|
178
|
+
from cacheai import Client
|
|
179
|
+
|
|
180
|
+
client = Client(
|
|
181
|
+
api_key="your-api-key",
|
|
182
|
+
timeout=30.0, # Request timeout in seconds
|
|
183
|
+
max_retries=3 # Maximum retry attempts
|
|
184
|
+
)
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
### Error Handling
|
|
188
|
+
|
|
189
|
+
```python
|
|
190
|
+
from cacheai import Client, CacheAIError, AuthenticationError, RateLimitError
|
|
191
|
+
|
|
192
|
+
client = Client(api_key="your-api-key")
|
|
193
|
+
|
|
194
|
+
try:
|
|
195
|
+
response = client.chat.completions.create(
|
|
196
|
+
model="gpt-3.5-turbo",
|
|
197
|
+
messages=[{"role": "user", "content": "Hello!"}]
|
|
198
|
+
)
|
|
199
|
+
except AuthenticationError as e:
|
|
200
|
+
print(f"Invalid API key: {e}")
|
|
201
|
+
except RateLimitError as e:
|
|
202
|
+
print(f"Rate limit exceeded: {e}")
|
|
203
|
+
except CacheAIError as e:
|
|
204
|
+
print(f"API error: {e}")
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
## API Reference
|
|
208
|
+
|
|
209
|
+
### Client
|
|
210
|
+
|
|
211
|
+
```python
|
|
212
|
+
Client(
|
|
213
|
+
api_key: Optional[str] = None, # CacheAI API key
|
|
214
|
+
base_url: Optional[str] = None, # API base URL
|
|
215
|
+
timeout: float = 60.0, # Request timeout
|
|
216
|
+
max_retries: int = 2, # Max retry attempts
|
|
217
|
+
enable_cache: bool = True, # Enable semantic caching
|
|
218
|
+
backend_provider: Optional[str] = None, # Backend LLM provider
|
|
219
|
+
backend_api_key: Optional[str] = None, # Backend API key
|
|
220
|
+
backend_base_url: Optional[str] = None # Custom backend URL
|
|
221
|
+
)
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
### Chat Completions
|
|
225
|
+
|
|
226
|
+
```python
|
|
227
|
+
client.chat.completions.create(
|
|
228
|
+
model: str, # Model ID
|
|
229
|
+
messages: List[Dict[str, str]], # Conversation messages
|
|
230
|
+
temperature: Optional[float] = None, # Sampling temperature (0-2)
|
|
231
|
+
max_tokens: Optional[int] = None, # Max tokens to generate
|
|
232
|
+
top_p: Optional[float] = None, # Nucleus sampling
|
|
233
|
+
frequency_penalty: Optional[float] = None, # Frequency penalty
|
|
234
|
+
presence_penalty: Optional[float] = None, # Presence penalty
|
|
235
|
+
stop: Optional[Union[str, List[str]]] = None, # Stop sequences
|
|
236
|
+
stream: bool = False # Enable streaming
|
|
237
|
+
) -> ChatCompletion
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
## Environment Variables
|
|
241
|
+
|
|
242
|
+
| Variable | Description | Default |
|
|
243
|
+
|----------|-------------|---------|
|
|
244
|
+
| `CACHEAI_API_KEY` | CacheAI API key | (required) |
|
|
245
|
+
| `CACHEAI_BASE_URL` | API base URL | `https://api.cacheai.tech/v1` |
|
|
246
|
+
| `CACHEAI_ENABLE_CACHE` | Enable semantic caching | `true` |
|
|
247
|
+
| `CACHEAI_BACKEND_PROVIDER` | Backend LLM provider | (optional) |
|
|
248
|
+
| `CACHEAI_BACKEND_API_KEY` | Backend LLM API key | (optional) |
|
|
249
|
+
| `CACHEAI_BACKEND_BASE_URL` | Custom backend URL | (optional) |
|
|
250
|
+
|
|
251
|
+
## Migration from OpenAI
|
|
252
|
+
|
|
253
|
+
CacheAI is designed to be a drop-in replacement for OpenAI:
|
|
254
|
+
|
|
255
|
+
```python
|
|
256
|
+
# Before (OpenAI)
|
|
257
|
+
from openai import OpenAI
|
|
258
|
+
client = OpenAI(api_key="sk-...")
|
|
259
|
+
response = client.chat.completions.create(...)
|
|
260
|
+
|
|
261
|
+
# After (CacheAI)
|
|
262
|
+
from cacheai import Client
|
|
263
|
+
client = Client(api_key="ca-...", backend_provider="openai", backend_api_key="sk-...")
|
|
264
|
+
response = client.chat.completions.create(...)
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
## Examples
|
|
268
|
+
|
|
269
|
+
See the [examples](./examples/) directory for more usage examples:
|
|
270
|
+
|
|
271
|
+
- [Basic Chat](./examples/chat_example.py)
|
|
272
|
+
- [Streaming](./examples/streaming_example.py)
|
|
273
|
+
- [Error Handling](./examples/error_handling.py)
|
|
274
|
+
|
|
275
|
+
## Development
|
|
276
|
+
|
|
277
|
+
### Install Development Dependencies
|
|
278
|
+
|
|
279
|
+
```bash
|
|
280
|
+
pip install -e ".[dev]"
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
### Run Tests
|
|
284
|
+
|
|
285
|
+
```bash
|
|
286
|
+
pytest
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
### Type Checking
|
|
290
|
+
|
|
291
|
+
```bash
|
|
292
|
+
mypy cacheai
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
### Code Formatting
|
|
296
|
+
|
|
297
|
+
```bash
|
|
298
|
+
black cacheai
|
|
299
|
+
ruff cacheai
|
|
300
|
+
```
|
|
301
|
+
|
|
302
|
+
## Support
|
|
303
|
+
|
|
304
|
+
- **Documentation**: [https://docs.cacheai.tech](https://docs.cacheai.tech)
|
|
305
|
+
- **Issues**: [GitHub Issues](https://github.com/cacheaitechnologies/cacheai-python/issues)
|
|
306
|
+
- **Email**: info@cacheaitechnologies.com
|
|
307
|
+
|
|
308
|
+
## License
|
|
309
|
+
|
|
310
|
+
MIT License - see [LICENSE](LICENSE) file for details.
|
|
311
|
+
|
|
312
|
+
## Links
|
|
313
|
+
|
|
314
|
+
- [Homepage](https://www.cacheaitechnologies.com)
|
|
315
|
+
- [Documentation](https://docs.cacheai.tech)
|
|
316
|
+
- [GitHub Repository](https://github.com/cacheaitechnologies/cacheai-python)
|
|
317
|
+
- [PyPI Package](https://pypi.org/project/cacheai/)
|
cacheai-0.1.0/README.md
ADDED
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
# CacheAI Python API
|
|
2
|
+
|
|
3
|
+
Official Python API for CacheAI - Semantic Caching for Large Language Models
|
|
4
|
+
|
|
5
|
+
[](https://badge.fury.io/py/cacheai)
|
|
6
|
+
[](https://pypi.org/project/cacheai/)
|
|
7
|
+
[](https://opensource.org/licenses/MIT)
|
|
8
|
+
|
|
9
|
+
## Features
|
|
10
|
+
|
|
11
|
+
- **OpenAI Compatible**: Drop-in replacement for OpenAI Python SDK
|
|
12
|
+
- **Semantic Caching**: Automatic caching of similar queries using advanced semantic similarity
|
|
13
|
+
- **Multiple LLM Backends**: Support for OpenAI, Anthropic, Google AI, and more
|
|
14
|
+
- **Streaming Support**: Full support for streaming responses
|
|
15
|
+
- **Type-Safe**: Complete type hints for better IDE support
|
|
16
|
+
- **Easy Integration**: Minimal code changes required
|
|
17
|
+
|
|
18
|
+
## Installation
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
pip install cacheai
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Quick Start
|
|
25
|
+
|
|
26
|
+
### Basic Usage
|
|
27
|
+
|
|
28
|
+
```python
|
|
29
|
+
from cacheai import Client
|
|
30
|
+
|
|
31
|
+
# Initialize client
|
|
32
|
+
client = Client(
|
|
33
|
+
api_key="your-cacheai-api-key",
|
|
34
|
+
base_url="https://api.cacheai.tech/v1" # Optional, this is the default
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
# Create a chat completion
|
|
38
|
+
response = client.chat.completions.create(
|
|
39
|
+
model="gpt-3.5-turbo",
|
|
40
|
+
messages=[
|
|
41
|
+
{"role": "user", "content": "Hello, how are you?"}
|
|
42
|
+
]
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
print(response.choices[0].message.content)
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### With Environment Variables
|
|
49
|
+
|
|
50
|
+
```python
|
|
51
|
+
import os
|
|
52
|
+
from cacheai import Client
|
|
53
|
+
|
|
54
|
+
# Set environment variables
|
|
55
|
+
os.environ["CACHEAI_API_KEY"] = "your-cacheai-api-key"
|
|
56
|
+
os.environ["CACHEAI_BACKEND_PROVIDER"] = "openai"
|
|
57
|
+
os.environ["CACHEAI_BACKEND_API_KEY"] = "your-openai-api-key"
|
|
58
|
+
|
|
59
|
+
# Initialize client (reads from environment)
|
|
60
|
+
client = Client()
|
|
61
|
+
|
|
62
|
+
response = client.chat.completions.create(
|
|
63
|
+
model="gpt-3.5-turbo",
|
|
64
|
+
messages=[{"role": "user", "content": "What is Python?"}]
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
print(response.choices[0].message.content)
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### Streaming
|
|
71
|
+
|
|
72
|
+
```python
|
|
73
|
+
from cacheai import Client
|
|
74
|
+
|
|
75
|
+
client = Client(api_key="your-cacheai-api-key")
|
|
76
|
+
|
|
77
|
+
stream = client.chat.completions.create(
|
|
78
|
+
model="gpt-3.5-turbo",
|
|
79
|
+
messages=[{"role": "user", "content": "Tell me a story"}],
|
|
80
|
+
stream=True
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
for chunk in stream:
|
|
84
|
+
if chunk.choices[0].delta.content:
|
|
85
|
+
print(chunk.choices[0].delta.content, end="", flush=True)
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## Configuration
|
|
89
|
+
|
|
90
|
+
### Backend LLM Configuration
|
|
91
|
+
|
|
92
|
+
CacheAI acts as a caching layer in front of your preferred LLM provider. Configure the backend:
|
|
93
|
+
|
|
94
|
+
```python
|
|
95
|
+
from cacheai import Client
|
|
96
|
+
|
|
97
|
+
client = Client(
|
|
98
|
+
api_key="your-cacheai-api-key",
|
|
99
|
+
backend_provider="openai", # "openai", "anthropic", "google", etc.
|
|
100
|
+
backend_api_key="your-openai-key", # Backend LLM API key
|
|
101
|
+
)
|
|
102
|
+
|
|
103
|
+
# Or use environment variables:
|
|
104
|
+
# CACHEAI_BACKEND_PROVIDER=openai
|
|
105
|
+
# CACHEAI_BACKEND_API_KEY=sk-...
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### Cache Control
|
|
109
|
+
|
|
110
|
+
```python
|
|
111
|
+
from cacheai import Client
|
|
112
|
+
|
|
113
|
+
# Disable caching (for debugging/testing)
|
|
114
|
+
client = Client(
|
|
115
|
+
api_key="your-cacheai-api-key",
|
|
116
|
+
enable_cache=False
|
|
117
|
+
)
|
|
118
|
+
|
|
119
|
+
# Or via environment variable:
|
|
120
|
+
# CACHEAI_ENABLE_CACHE=false
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
## Advanced Usage
|
|
124
|
+
|
|
125
|
+
### Context Manager
|
|
126
|
+
|
|
127
|
+
```python
|
|
128
|
+
from cacheai import Client
|
|
129
|
+
|
|
130
|
+
with Client(api_key="your-api-key") as client:
|
|
131
|
+
response = client.chat.completions.create(
|
|
132
|
+
model="gpt-3.5-turbo",
|
|
133
|
+
messages=[{"role": "user", "content": "Hello!"}]
|
|
134
|
+
)
|
|
135
|
+
print(response.choices[0].message.content)
|
|
136
|
+
# Connection is automatically closed
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
### Custom Timeout and Retries
|
|
140
|
+
|
|
141
|
+
```python
|
|
142
|
+
from cacheai import Client
|
|
143
|
+
|
|
144
|
+
client = Client(
|
|
145
|
+
api_key="your-api-key",
|
|
146
|
+
timeout=30.0, # Request timeout in seconds
|
|
147
|
+
max_retries=3 # Maximum retry attempts
|
|
148
|
+
)
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
### Error Handling
|
|
152
|
+
|
|
153
|
+
```python
|
|
154
|
+
from cacheai import Client, CacheAIError, AuthenticationError, RateLimitError
|
|
155
|
+
|
|
156
|
+
client = Client(api_key="your-api-key")
|
|
157
|
+
|
|
158
|
+
try:
|
|
159
|
+
response = client.chat.completions.create(
|
|
160
|
+
model="gpt-3.5-turbo",
|
|
161
|
+
messages=[{"role": "user", "content": "Hello!"}]
|
|
162
|
+
)
|
|
163
|
+
except AuthenticationError as e:
|
|
164
|
+
print(f"Invalid API key: {e}")
|
|
165
|
+
except RateLimitError as e:
|
|
166
|
+
print(f"Rate limit exceeded: {e}")
|
|
167
|
+
except CacheAIError as e:
|
|
168
|
+
print(f"API error: {e}")
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
## API Reference
|
|
172
|
+
|
|
173
|
+
### Client
|
|
174
|
+
|
|
175
|
+
```python
|
|
176
|
+
Client(
|
|
177
|
+
api_key: Optional[str] = None, # CacheAI API key
|
|
178
|
+
base_url: Optional[str] = None, # API base URL
|
|
179
|
+
timeout: float = 60.0, # Request timeout
|
|
180
|
+
max_retries: int = 2, # Max retry attempts
|
|
181
|
+
enable_cache: bool = True, # Enable semantic caching
|
|
182
|
+
backend_provider: Optional[str] = None, # Backend LLM provider
|
|
183
|
+
backend_api_key: Optional[str] = None, # Backend API key
|
|
184
|
+
backend_base_url: Optional[str] = None # Custom backend URL
|
|
185
|
+
)
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
### Chat Completions
|
|
189
|
+
|
|
190
|
+
```python
|
|
191
|
+
client.chat.completions.create(
|
|
192
|
+
model: str, # Model ID
|
|
193
|
+
messages: List[Dict[str, str]], # Conversation messages
|
|
194
|
+
temperature: Optional[float] = None, # Sampling temperature (0-2)
|
|
195
|
+
max_tokens: Optional[int] = None, # Max tokens to generate
|
|
196
|
+
top_p: Optional[float] = None, # Nucleus sampling
|
|
197
|
+
frequency_penalty: Optional[float] = None, # Frequency penalty
|
|
198
|
+
presence_penalty: Optional[float] = None, # Presence penalty
|
|
199
|
+
stop: Optional[Union[str, List[str]]] = None, # Stop sequences
|
|
200
|
+
stream: bool = False # Enable streaming
|
|
201
|
+
) -> ChatCompletion
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
## Environment Variables
|
|
205
|
+
|
|
206
|
+
| Variable | Description | Default |
|
|
207
|
+
|----------|-------------|---------|
|
|
208
|
+
| `CACHEAI_API_KEY` | CacheAI API key | (required) |
|
|
209
|
+
| `CACHEAI_BASE_URL` | API base URL | `https://api.cacheai.tech/v1` |
|
|
210
|
+
| `CACHEAI_ENABLE_CACHE` | Enable semantic caching | `true` |
|
|
211
|
+
| `CACHEAI_BACKEND_PROVIDER` | Backend LLM provider | (optional) |
|
|
212
|
+
| `CACHEAI_BACKEND_API_KEY` | Backend LLM API key | (optional) |
|
|
213
|
+
| `CACHEAI_BACKEND_BASE_URL` | Custom backend URL | (optional) |
|
|
214
|
+
|
|
215
|
+
## Migration from OpenAI
|
|
216
|
+
|
|
217
|
+
CacheAI is designed to be a drop-in replacement for OpenAI:
|
|
218
|
+
|
|
219
|
+
```python
|
|
220
|
+
# Before (OpenAI)
|
|
221
|
+
from openai import OpenAI
|
|
222
|
+
client = OpenAI(api_key="sk-...")
|
|
223
|
+
response = client.chat.completions.create(...)
|
|
224
|
+
|
|
225
|
+
# After (CacheAI)
|
|
226
|
+
from cacheai import Client
|
|
227
|
+
client = Client(api_key="ca-...", backend_provider="openai", backend_api_key="sk-...")
|
|
228
|
+
response = client.chat.completions.create(...)
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
## Examples
|
|
232
|
+
|
|
233
|
+
See the [examples](./examples/) directory for more usage examples:
|
|
234
|
+
|
|
235
|
+
- [Basic Chat](./examples/chat_example.py)
|
|
236
|
+
- [Streaming](./examples/streaming_example.py)
|
|
237
|
+
- [Error Handling](./examples/error_handling.py)
|
|
238
|
+
|
|
239
|
+
## Development
|
|
240
|
+
|
|
241
|
+
### Install Development Dependencies
|
|
242
|
+
|
|
243
|
+
```bash
|
|
244
|
+
pip install -e ".[dev]"
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
### Run Tests
|
|
248
|
+
|
|
249
|
+
```bash
|
|
250
|
+
pytest
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
### Type Checking
|
|
254
|
+
|
|
255
|
+
```bash
|
|
256
|
+
mypy cacheai
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
### Code Formatting
|
|
260
|
+
|
|
261
|
+
```bash
|
|
262
|
+
black cacheai
|
|
263
|
+
ruff cacheai
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
## Support
|
|
267
|
+
|
|
268
|
+
- **Documentation**: [https://docs.cacheai.tech](https://docs.cacheai.tech)
|
|
269
|
+
- **Issues**: [GitHub Issues](https://github.com/cacheaitechnologies/cacheai-python/issues)
|
|
270
|
+
- **Email**: info@cacheaitechnologies.com
|
|
271
|
+
|
|
272
|
+
## License
|
|
273
|
+
|
|
274
|
+
MIT License - see [LICENSE](LICENSE) file for details.
|
|
275
|
+
|
|
276
|
+
## Links
|
|
277
|
+
|
|
278
|
+
- [Homepage](https://www.cacheaitechnologies.com)
|
|
279
|
+
- [Documentation](https://docs.cacheai.tech)
|
|
280
|
+
- [GitHub Repository](https://github.com/cacheaitechnologies/cacheai-python)
|
|
281
|
+
- [PyPI Package](https://pypi.org/project/cacheai/)
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61.0", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "cacheai"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Official Python API for CacheAI - Semantic Caching for Large Language Models"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
license = {text = "MIT"}
|
|
12
|
+
authors = [
|
|
13
|
+
{name = "CacheAI Technologies, Inc.", email = "info@cacheaitechnologies.com"}
|
|
14
|
+
]
|
|
15
|
+
keywords = ["cacheai", "llm", "caching", "openai", "chatgpt", "semantic-cache"]
|
|
16
|
+
classifiers = [
|
|
17
|
+
"Development Status :: 4 - Beta",
|
|
18
|
+
"Intended Audience :: Developers",
|
|
19
|
+
"License :: OSI Approved :: MIT License",
|
|
20
|
+
"Programming Language :: Python :: 3",
|
|
21
|
+
"Programming Language :: Python :: 3.10",
|
|
22
|
+
"Programming Language :: Python :: 3.11",
|
|
23
|
+
"Programming Language :: Python :: 3.12",
|
|
24
|
+
"Programming Language :: Python :: 3.13",
|
|
25
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
26
|
+
"Topic :: Scientific/Engineering :: Artificial Intelligence",
|
|
27
|
+
]
|
|
28
|
+
|
|
29
|
+
dependencies = [
|
|
30
|
+
"requests>=2.28.0",
|
|
31
|
+
"pydantic>=2.0.0",
|
|
32
|
+
"typing-extensions>=4.5.0",
|
|
33
|
+
"urllib3>=1.26.0",
|
|
34
|
+
]
|
|
35
|
+
|
|
36
|
+
[project.optional-dependencies]
|
|
37
|
+
dev = [
|
|
38
|
+
"pytest>=7.0.0",
|
|
39
|
+
"pytest-cov>=4.0.0",
|
|
40
|
+
"mypy>=1.0.0",
|
|
41
|
+
"black>=23.0.0",
|
|
42
|
+
"ruff>=0.1.0",
|
|
43
|
+
"types-requests>=2.28.0",
|
|
44
|
+
]
|
|
45
|
+
|
|
46
|
+
[project.urls]
|
|
47
|
+
Homepage = "https://www.cacheaitechnologies.com"
|
|
48
|
+
Documentation = "https://docs.cacheai.tech"
|
|
49
|
+
Repository = "https://github.com/cacheaitechnologies/cacheai"
|
|
50
|
+
Changelog = "https://github.com/cacheaitechnologies/cacheai/blob/main/CHANGELOG.md"
|
|
51
|
+
"Bug Tracker" = "https://github.com/cacheaitechnologies/cacheai/issues"
|
|
52
|
+
|
|
53
|
+
[tool.setuptools.packages.find]
|
|
54
|
+
where = ["src"]
|
|
55
|
+
include = ["cacheai*"]
|
|
56
|
+
exclude = ["tests*"]
|
|
57
|
+
|
|
58
|
+
[tool.black]
|
|
59
|
+
line-length = 100
|
|
60
|
+
target-version = ['py310', 'py311', 'py312', 'py313']
|
|
61
|
+
|
|
62
|
+
[tool.ruff]
|
|
63
|
+
line-length = 100
|
|
64
|
+
target-version = "py310"
|
|
65
|
+
|
|
66
|
+
[tool.mypy]
|
|
67
|
+
python_version = "3.10"
|
|
68
|
+
warn_return_any = true
|
|
69
|
+
warn_unused_configs = true
|
|
70
|
+
disallow_untyped_defs = false
|
|
71
|
+
|
|
72
|
+
[tool.pytest.ini_options]
|
|
73
|
+
testpaths = ["tests"]
|
|
74
|
+
python_files = ["test_*.py"]
|
|
75
|
+
python_classes = ["Test*"]
|
|
76
|
+
python_functions = ["test_*"]
|
cacheai-0.1.0/setup.cfg
ADDED