futureagi 0.2.2__tar.gz → 0.3.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.
- {futureagi-0.2.2 → futureagi-0.3.0}/LICENSE.md +1 -1
- {futureagi-0.2.2/futureagi.egg-info → futureagi-0.3.0}/PKG-INFO +40 -46
- {futureagi-0.2.2 → futureagi-0.3.0}/README.md +19 -27
- futureagi-0.3.0/fi/__init__.py +1 -0
- futureagi-0.3.0/fi/api/__init__.py +3 -0
- futureagi-0.3.0/fi/api/auth.py +178 -0
- futureagi-0.3.0/fi/api/types.py +25 -0
- futureagi-0.3.0/fi/client.py +318 -0
- futureagi-0.3.0/fi/evals/__init__.py +148 -0
- futureagi-0.3.0/fi/evals/evaluator.py +184 -0
- futureagi-0.3.0/fi/evals/templates.py +971 -0
- futureagi-0.3.0/fi/evals/types.py +115 -0
- futureagi-0.3.0/fi/integrations/providers/__init__.py +3879 -0
- futureagi-0.3.0/fi/integrations/providers/types.py +29 -0
- futureagi-0.3.0/fi/testcases/__init__.py +6 -0
- futureagi-0.3.0/fi/testcases/conversational_test_case.py +22 -0
- futureagi-0.3.0/fi/testcases/general.py +22 -0
- futureagi-0.3.0/fi/testcases/llm_test_case.py +10 -0
- futureagi-0.3.0/fi/testcases/mllm_test_case.py +58 -0
- futureagi-0.3.0/fi/testcases/utils.py +24 -0
- futureagi-0.3.0/fi/utils/__init__.py +1 -0
- {futureagi-0.2.2 → futureagi-0.3.0}/fi/utils/constants.py +11 -6
- {futureagi-0.2.2 → futureagi-0.3.0}/fi/utils/errors.py +49 -21
- futureagi-0.2.2/fi/bounded_executor.py → futureagi-0.3.0/fi/utils/executor.py +1 -1
- {futureagi-0.2.2 → futureagi-0.3.0}/fi/utils/logging.py +2 -1
- futureagi-0.3.0/fi/utils/routes.py +16 -0
- {futureagi-0.2.2 → futureagi-0.3.0}/fi/utils/types.py +14 -9
- {futureagi-0.2.2 → futureagi-0.3.0}/fi/utils/utils.py +5 -4
- futureagi-0.3.0/pyproject.toml +35 -0
- futureagi-0.2.2/MANIFEST.in +0 -2
- futureagi-0.2.2/PKG-INFO +0 -248
- futureagi-0.2.2/fi/__init__.py +0 -1
- futureagi-0.2.2/fi/client.py +0 -311
- futureagi-0.2.2/fi/examples/__init__.py +0 -0
- futureagi-0.2.2/fi/examples/prompt_template.py +0 -27
- futureagi-0.2.2/fi/examples/rag_file.py +0 -30
- futureagi-0.2.2/fi/utils/__init__.py +0 -1
- futureagi-0.2.2/futureagi.egg-info/SOURCES.txt +0 -22
- futureagi-0.2.2/futureagi.egg-info/dependency_links.txt +0 -1
- futureagi-0.2.2/futureagi.egg-info/requires.txt +0 -12
- futureagi-0.2.2/futureagi.egg-info/top_level.txt +0 -2
- futureagi-0.2.2/setup.cfg +0 -4
- futureagi-0.2.2/setup.py +0 -22
- {futureagi-0.2.2/docs → futureagi-0.3.0/fi/integrations}/__init__.py +0 -0
|
@@ -13,4 +13,4 @@ Redistribution and use in source and binary forms, with or without modification,
|
|
|
13
13
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
|
14
14
|
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
15
15
|
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
16
|
-
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
16
|
+
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
@@ -1,38 +1,39 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: futureagi
|
|
3
|
-
Version: 0.
|
|
4
|
-
Summary:
|
|
5
|
-
Home-page: https://github.com/future-agi/client
|
|
3
|
+
Version: 0.3.0
|
|
4
|
+
Summary: We help GenAI teams maintain high-accuracy for their Models in production.
|
|
6
5
|
Author: Future AGI
|
|
7
|
-
Author-email:
|
|
6
|
+
Author-email: no-reply@futureagi.com
|
|
7
|
+
Requires-Python: >3.9
|
|
8
8
|
Classifier: Programming Language :: Python :: 3
|
|
9
|
-
Classifier:
|
|
10
|
-
Classifier:
|
|
11
|
-
|
|
9
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
13
|
+
Requires-Dist: certifi (==2024.6.2)
|
|
14
|
+
Requires-Dist: charset-normalizer (==3.3.2)
|
|
15
|
+
Requires-Dist: idna (==3.7)
|
|
16
|
+
Requires-Dist: numpy (==1.26.4)
|
|
17
|
+
Requires-Dist: pandas (==2.2.2)
|
|
18
|
+
Requires-Dist: pydantic (>=2.9.2,<3.0.0)
|
|
19
|
+
Requires-Dist: python-dateutil (==2.9.0.post0)
|
|
20
|
+
Requires-Dist: pytz (==2024.1)
|
|
21
|
+
Requires-Dist: requests (==2.32.3)
|
|
22
|
+
Requires-Dist: requests-futures (==1.0.1)
|
|
23
|
+
Requires-Dist: six (==1.16.0)
|
|
24
|
+
Requires-Dist: tzdata (==2024.1)
|
|
25
|
+
Requires-Dist: urllib3 (==2.2.2)
|
|
12
26
|
Description-Content-Type: text/markdown
|
|
13
|
-
License-File: LICENSE.md
|
|
14
|
-
Requires-Dist: certifi==2024.6.2
|
|
15
|
-
Requires-Dist: charset-normalizer==3.3.2
|
|
16
|
-
Requires-Dist: idna==3.7
|
|
17
|
-
Requires-Dist: numpy==1.26.4
|
|
18
|
-
Requires-Dist: pandas==2.2.2
|
|
19
|
-
Requires-Dist: python-dateutil==2.9.0.post0
|
|
20
|
-
Requires-Dist: pytz==2024.1
|
|
21
|
-
Requires-Dist: requests==2.32.3
|
|
22
|
-
Requires-Dist: requests-futures==1.0.1
|
|
23
|
-
Requires-Dist: six==1.16.0
|
|
24
|
-
Requires-Dist: tzdata==2024.1
|
|
25
|
-
Requires-Dist: urllib3==2.2.2
|
|
26
27
|
|
|
27
28
|
# Future AGI
|
|
28
29
|
|
|
29
30
|

|
|
30
31
|
|
|
31
|
-
Welcome to
|
|
32
|
+
Welcome to Future AGI - Empowering GenAI Teams with Advanced Performance Management
|
|
32
33
|
|
|
33
34
|
# Overview
|
|
34
35
|
|
|
35
|
-
|
|
36
|
+
Future AGI provides a cutting-edge platform designed to help GenAI teams maintain peak model accuracy in production environments.
|
|
36
37
|
Our solution is purpose-built, scalable, and delivers results 10x faster than traditional methods.
|
|
37
38
|
|
|
38
39
|
**Key Features**
|
|
@@ -47,7 +48,7 @@ Our solution is purpose-built, scalable, and delivers results 10x faster than tr
|
|
|
47
48
|
|
|
48
49
|
To install the client, you can clone the repository or install the library:
|
|
49
50
|
|
|
50
|
-
Install the
|
|
51
|
+
Install the library in an environment using Python >= 3.6.
|
|
51
52
|
```
|
|
52
53
|
$ pip3 install futureagi
|
|
53
54
|
```
|
|
@@ -59,20 +60,19 @@ $ git clone https://github.com/future-agi/client
|
|
|
59
60
|
|
|
60
61
|
**Initialisation**
|
|
61
62
|
|
|
62
|
-
To initialise the Future
|
|
63
|
+
To initialise the Future AGI Client, you need to provide your fi_api_key and fi_secret_key, which are associated with your Future AGI account.
|
|
63
64
|
|
|
64
65
|
Get your service API key When you create an account, we generate a service API key. You will need this API Key and your Space Key for logging authentication.
|
|
65
|
-
Instrument your code Python Client If you are using the Future
|
|
66
|
+
Instrument your code Python Client If you are using the Future AGI python client, add a few lines to your code to log your data. Logs are sent to us asynchronously.
|
|
66
67
|
|
|
67
68
|
```
|
|
68
69
|
from fi.client import Client
|
|
69
70
|
|
|
70
|
-
|
|
71
|
-
|
|
71
|
+
fi_api_key = os.environ["FI_API_KEY"]
|
|
72
|
+
fi_secret_key = os.environ["FI_SECRET_KEY"]
|
|
72
73
|
base_url = os.environ["FI_API_URL"]
|
|
73
74
|
|
|
74
|
-
client = Client(
|
|
75
|
-
uri=base_url,
|
|
75
|
+
client = Client(fi_api_key=fi_api_key, fi_secret_key=fi_secret_key,
|
|
76
76
|
max_workers=8,
|
|
77
77
|
max_queue_bound=5000,
|
|
78
78
|
timeout=200,
|
|
@@ -80,12 +80,12 @@ client = Client(api_key=api_key, secret_key=secret_key,
|
|
|
80
80
|
)
|
|
81
81
|
```
|
|
82
82
|
|
|
83
|
-
**
|
|
84
|
-
*
|
|
85
|
-
*
|
|
86
|
-
* _uri_: URI to send your records to
|
|
87
|
-
* _max_workers_: maximum number of concurrent requests to
|
|
88
|
-
* _max_queue_bound_: maximum number of concurrent future objects generated for publishing to
|
|
83
|
+
**Initialises the Future AGI Client**
|
|
84
|
+
* _fi_api_key_: provided API key associated with your account.
|
|
85
|
+
* _fi_secret_key_: provided identifier to connect records to spaces.
|
|
86
|
+
* _uri_: URI to send your records to Future AGI client.
|
|
87
|
+
* _max_workers_: maximum number of concurrent requests to Future AGI. Defaults to 8.
|
|
88
|
+
* _max_queue_bound_: maximum number of concurrent future objects generated for publishing to Future AGI. Defaults to 5000.
|
|
89
89
|
* _timeout_: how long to wait for the server to send data before giving up. Defaults to 200.
|
|
90
90
|
* _additional_headers_: Dictionary of additional headers to append to request
|
|
91
91
|
|
|
@@ -169,7 +169,6 @@ The chat_graph must be a dictionary with the following keys:
|
|
|
169
169
|
```
|
|
170
170
|
|
|
171
171
|
1. **Logging data all at once:** This involves logging structured conversations in a unified format:
|
|
172
|
-
|
|
173
172
|
```
|
|
174
173
|
[{
|
|
175
174
|
"conversation_id": "",
|
|
@@ -195,7 +194,6 @@ The chat_graph must be a dictionary with the following keys:
|
|
|
195
194
|
}
|
|
196
195
|
}]
|
|
197
196
|
}]
|
|
198
|
-
|
|
199
197
|
```
|
|
200
198
|
**Error Handling**
|
|
201
199
|
The client raises specific exceptions for different types of errors:
|
|
@@ -210,26 +208,26 @@ The client raises specific exceptions for different types of errors:
|
|
|
210
208
|
|
|
211
209
|
1. Q: How do you give a performance score without human in the loop?
|
|
212
210
|
|
|
213
|
-
Our secret Sauce is a Critique AI agent that
|
|
211
|
+
Our secret Sauce is a Critique AI agent that can deliver powerful evaluation framework without need for human in the loop. What’s more is that it is 100% configurable as per new evolving use cases. Now anything that you can imagine your AI system should deliver - you can configure our platform to manage it.
|
|
214
212
|
|
|
215
|
-
2. Q: What all inputs
|
|
213
|
+
2. Q: What all inputs Future AGI platform needs?
|
|
216
214
|
|
|
217
215
|
We would need only the input-output database, training dataset if available, and User-analytics. We do not need to understand the model and how it is taking decisions.
|
|
218
216
|
|
|
219
217
|
3. Q: I don't want to share data with Future AGI, can I still use it?
|
|
220
218
|
|
|
221
|
-
Yes, you can now install our SDK in your private cloud and take advantage of our strong platform to align your
|
|
219
|
+
Yes, you can now install our SDK in your private cloud and take advantage of our strong platform to align your AI system to your users.
|
|
222
220
|
|
|
223
221
|
|
|
224
222
|
4. Q: My use case is unique, would you provide service to customise your platform as per my use case?
|
|
225
223
|
|
|
226
|
-
Our platform
|
|
224
|
+
Our platform is 100% customisable and easy to configure for all types of models and modalities by the AI teams. However, our customer-success engineer would be happy to assist you for figuring out solutions to your unique use cases.
|
|
227
225
|
|
|
228
226
|
5. Q: My app uses multiple models with multiple modalities, can you work with images and videos also?
|
|
229
227
|
|
|
230
228
|
Yes we can.
|
|
231
229
|
|
|
232
|
-
6. Q: How much time does it take to integrate the
|
|
230
|
+
6. Q: How much time does it take to integrate the Future AGI platform? How much bandwidth would be required?
|
|
233
231
|
|
|
234
232
|
It takes just 2 minutes to integrate a few lines of code and your data starts showing on our platform. Try it today.
|
|
235
233
|
|
|
@@ -242,7 +240,3 @@ It takes just 2 minutes to integrate a few lines of code and your data starts sh
|
|
|
242
240
|
|
|
243
241
|
**PyPI** : https://pypi.org/project/futureagi/
|
|
244
242
|
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
@@ -2,11 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|

|
|
4
4
|
|
|
5
|
-
Welcome to
|
|
5
|
+
Welcome to Future AGI - Empowering GenAI Teams with Advanced Performance Management
|
|
6
6
|
|
|
7
7
|
# Overview
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
Future AGI provides a cutting-edge platform designed to help GenAI teams maintain peak model accuracy in production environments.
|
|
10
10
|
Our solution is purpose-built, scalable, and delivers results 10x faster than traditional methods.
|
|
11
11
|
|
|
12
12
|
**Key Features**
|
|
@@ -21,7 +21,7 @@ Our solution is purpose-built, scalable, and delivers results 10x faster than tr
|
|
|
21
21
|
|
|
22
22
|
To install the client, you can clone the repository or install the library:
|
|
23
23
|
|
|
24
|
-
Install the
|
|
24
|
+
Install the library in an environment using Python >= 3.6.
|
|
25
25
|
```
|
|
26
26
|
$ pip3 install futureagi
|
|
27
27
|
```
|
|
@@ -33,20 +33,19 @@ $ git clone https://github.com/future-agi/client
|
|
|
33
33
|
|
|
34
34
|
**Initialisation**
|
|
35
35
|
|
|
36
|
-
To initialise the Future
|
|
36
|
+
To initialise the Future AGI Client, you need to provide your fi_api_key and fi_secret_key, which are associated with your Future AGI account.
|
|
37
37
|
|
|
38
38
|
Get your service API key When you create an account, we generate a service API key. You will need this API Key and your Space Key for logging authentication.
|
|
39
|
-
Instrument your code Python Client If you are using the Future
|
|
39
|
+
Instrument your code Python Client If you are using the Future AGI python client, add a few lines to your code to log your data. Logs are sent to us asynchronously.
|
|
40
40
|
|
|
41
41
|
```
|
|
42
42
|
from fi.client import Client
|
|
43
43
|
|
|
44
|
-
|
|
45
|
-
|
|
44
|
+
fi_api_key = os.environ["FI_API_KEY"]
|
|
45
|
+
fi_secret_key = os.environ["FI_SECRET_KEY"]
|
|
46
46
|
base_url = os.environ["FI_API_URL"]
|
|
47
47
|
|
|
48
|
-
client = Client(
|
|
49
|
-
uri=base_url,
|
|
48
|
+
client = Client(fi_api_key=fi_api_key, fi_secret_key=fi_secret_key,
|
|
50
49
|
max_workers=8,
|
|
51
50
|
max_queue_bound=5000,
|
|
52
51
|
timeout=200,
|
|
@@ -54,12 +53,12 @@ client = Client(api_key=api_key, secret_key=secret_key,
|
|
|
54
53
|
)
|
|
55
54
|
```
|
|
56
55
|
|
|
57
|
-
**
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
* _uri_: URI to send your records to
|
|
61
|
-
* _max_workers_: maximum number of concurrent requests to
|
|
62
|
-
* _max_queue_bound_: maximum number of concurrent future objects generated for publishing to
|
|
56
|
+
**Initialises the Future AGI Client**
|
|
57
|
+
* _fi_api_key_: provided API key associated with your account.
|
|
58
|
+
* _fi_secret_key_: provided identifier to connect records to spaces.
|
|
59
|
+
* _uri_: URI to send your records to Future AGI client.
|
|
60
|
+
* _max_workers_: maximum number of concurrent requests to Future AGI. Defaults to 8.
|
|
61
|
+
* _max_queue_bound_: maximum number of concurrent future objects generated for publishing to Future AGI. Defaults to 5000.
|
|
63
62
|
* _timeout_: how long to wait for the server to send data before giving up. Defaults to 200.
|
|
64
63
|
* _additional_headers_: Dictionary of additional headers to append to request
|
|
65
64
|
|
|
@@ -143,7 +142,6 @@ The chat_graph must be a dictionary with the following keys:
|
|
|
143
142
|
```
|
|
144
143
|
|
|
145
144
|
1. **Logging data all at once:** This involves logging structured conversations in a unified format:
|
|
146
|
-
|
|
147
145
|
```
|
|
148
146
|
[{
|
|
149
147
|
"conversation_id": "",
|
|
@@ -169,7 +167,6 @@ The chat_graph must be a dictionary with the following keys:
|
|
|
169
167
|
}
|
|
170
168
|
}]
|
|
171
169
|
}]
|
|
172
|
-
|
|
173
170
|
```
|
|
174
171
|
**Error Handling**
|
|
175
172
|
The client raises specific exceptions for different types of errors:
|
|
@@ -184,26 +181,26 @@ The client raises specific exceptions for different types of errors:
|
|
|
184
181
|
|
|
185
182
|
1. Q: How do you give a performance score without human in the loop?
|
|
186
183
|
|
|
187
|
-
Our secret Sauce is a Critique AI agent that
|
|
184
|
+
Our secret Sauce is a Critique AI agent that can deliver powerful evaluation framework without need for human in the loop. What’s more is that it is 100% configurable as per new evolving use cases. Now anything that you can imagine your AI system should deliver - you can configure our platform to manage it.
|
|
188
185
|
|
|
189
|
-
2. Q: What all inputs
|
|
186
|
+
2. Q: What all inputs Future AGI platform needs?
|
|
190
187
|
|
|
191
188
|
We would need only the input-output database, training dataset if available, and User-analytics. We do not need to understand the model and how it is taking decisions.
|
|
192
189
|
|
|
193
190
|
3. Q: I don't want to share data with Future AGI, can I still use it?
|
|
194
191
|
|
|
195
|
-
Yes, you can now install our SDK in your private cloud and take advantage of our strong platform to align your
|
|
192
|
+
Yes, you can now install our SDK in your private cloud and take advantage of our strong platform to align your AI system to your users.
|
|
196
193
|
|
|
197
194
|
|
|
198
195
|
4. Q: My use case is unique, would you provide service to customise your platform as per my use case?
|
|
199
196
|
|
|
200
|
-
Our platform
|
|
197
|
+
Our platform is 100% customisable and easy to configure for all types of models and modalities by the AI teams. However, our customer-success engineer would be happy to assist you for figuring out solutions to your unique use cases.
|
|
201
198
|
|
|
202
199
|
5. Q: My app uses multiple models with multiple modalities, can you work with images and videos also?
|
|
203
200
|
|
|
204
201
|
Yes we can.
|
|
205
202
|
|
|
206
|
-
6. Q: How much time does it take to integrate the
|
|
203
|
+
6. Q: How much time does it take to integrate the Future AGI platform? How much bandwidth would be required?
|
|
207
204
|
|
|
208
205
|
It takes just 2 minutes to integrate a few lines of code and your data starts showing on our platform. Try it today.
|
|
209
206
|
|
|
@@ -215,8 +212,3 @@ It takes just 2 minutes to integrate a few lines of code and your data starts sh
|
|
|
215
212
|
**Documentation**: https://docs.futureagi.com/
|
|
216
213
|
|
|
217
214
|
**PyPI** : https://pypi.org/project/futureagi/
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.0.1"
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import time
|
|
3
|
+
from abc import ABC, abstractmethod
|
|
4
|
+
from typing import Dict, Generic, Optional, TypeVar, Union
|
|
5
|
+
|
|
6
|
+
from requests import Response
|
|
7
|
+
from requests_futures.sessions import FuturesSession
|
|
8
|
+
|
|
9
|
+
from fi.api.types import RequestConfig
|
|
10
|
+
from fi.integrations.providers.types import ApiKeyName
|
|
11
|
+
from fi.utils.constants import (
|
|
12
|
+
API_KEY_ENVVAR_NAME,
|
|
13
|
+
BASE_URL,
|
|
14
|
+
DEFAULT_MAX_QUEUE,
|
|
15
|
+
DEFAULT_MAX_WORKERS,
|
|
16
|
+
DEFAULT_TIMEOUT,
|
|
17
|
+
SECRET_KEY_ENVVAR_NAME,
|
|
18
|
+
)
|
|
19
|
+
from fi.utils.errors import AuthError
|
|
20
|
+
from fi.utils.executor import BoundedExecutor
|
|
21
|
+
|
|
22
|
+
T = TypeVar("T")
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class ResponseHandler(Generic[T], ABC):
|
|
26
|
+
"""Handles response parsing and validation"""
|
|
27
|
+
|
|
28
|
+
@classmethod
|
|
29
|
+
def parse(cls, response: Response) -> T:
|
|
30
|
+
"""Parse the response into the expected type"""
|
|
31
|
+
if not response.ok:
|
|
32
|
+
cls._handle_error(response)
|
|
33
|
+
return cls._parse_success(response)
|
|
34
|
+
|
|
35
|
+
@classmethod
|
|
36
|
+
@abstractmethod
|
|
37
|
+
def _parse_success(cls, response: Response) -> T:
|
|
38
|
+
"""Parse successful response"""
|
|
39
|
+
pass
|
|
40
|
+
|
|
41
|
+
@classmethod
|
|
42
|
+
@abstractmethod
|
|
43
|
+
def _handle_error(cls, response: Response) -> None:
|
|
44
|
+
"""Handle error responses"""
|
|
45
|
+
pass
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
class HttpClient:
|
|
49
|
+
"""Base HTTP client with improved request handling"""
|
|
50
|
+
|
|
51
|
+
def __init__(
|
|
52
|
+
self,
|
|
53
|
+
base_url: Optional[str] = None,
|
|
54
|
+
session: Optional[FuturesSession] = None,
|
|
55
|
+
default_headers: Optional[Dict[str, str]] = None,
|
|
56
|
+
**kwargs,
|
|
57
|
+
):
|
|
58
|
+
self._base_url = base_url.rstrip("/") if base_url else BASE_URL.rstrip("/")
|
|
59
|
+
self._session = session or FuturesSession(
|
|
60
|
+
executor=BoundedExecutor(
|
|
61
|
+
bound=kwargs.get("max_queue", DEFAULT_MAX_QUEUE),
|
|
62
|
+
max_workers=kwargs.get("max_workers", DEFAULT_MAX_WORKERS),
|
|
63
|
+
),
|
|
64
|
+
)
|
|
65
|
+
self._default_headers = default_headers or {}
|
|
66
|
+
self._default_timeout = kwargs.get("timeout", DEFAULT_TIMEOUT)
|
|
67
|
+
|
|
68
|
+
def request(
|
|
69
|
+
self,
|
|
70
|
+
config: RequestConfig,
|
|
71
|
+
response_handler: Optional[ResponseHandler[T]] = None,
|
|
72
|
+
) -> Union[Response, T]:
|
|
73
|
+
"""Make an HTTP request with retries and response handling"""
|
|
74
|
+
|
|
75
|
+
url = config.url
|
|
76
|
+
headers = {**self._default_headers, **(config.headers or {})}
|
|
77
|
+
params = config.params or {}
|
|
78
|
+
json = config.json or {}
|
|
79
|
+
timeout = config.timeout or self._default_timeout
|
|
80
|
+
for attempt in range(config.retry_attempts):
|
|
81
|
+
try:
|
|
82
|
+
response = self._session.request(
|
|
83
|
+
method=config.method.value,
|
|
84
|
+
url=url,
|
|
85
|
+
headers=headers,
|
|
86
|
+
params=params,
|
|
87
|
+
json=json,
|
|
88
|
+
timeout=timeout,
|
|
89
|
+
).result()
|
|
90
|
+
|
|
91
|
+
if response_handler:
|
|
92
|
+
return response_handler.parse(response=response)
|
|
93
|
+
return response
|
|
94
|
+
|
|
95
|
+
except Exception as e:
|
|
96
|
+
if attempt == config.retry_attempts - 1:
|
|
97
|
+
raise e
|
|
98
|
+
time.sleep(config.retry_delay)
|
|
99
|
+
|
|
100
|
+
def close(self):
|
|
101
|
+
"""Close the client session"""
|
|
102
|
+
self._session.close()
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
class APIKeyAuth(HttpClient):
|
|
106
|
+
_fi_api_key: str = None
|
|
107
|
+
_fi_secret_key: str = None
|
|
108
|
+
|
|
109
|
+
def __init__(
|
|
110
|
+
self,
|
|
111
|
+
fi_api_key: Optional[str] = None,
|
|
112
|
+
fi_secret_key: Optional[str] = None,
|
|
113
|
+
fi_base_url: Optional[str] = None,
|
|
114
|
+
**kwargs,
|
|
115
|
+
):
|
|
116
|
+
self.__class__._fi_api_key = fi_api_key or os.environ.get(API_KEY_ENVVAR_NAME)
|
|
117
|
+
self.__class__._fi_secret_key = fi_secret_key or os.environ.get(
|
|
118
|
+
SECRET_KEY_ENVVAR_NAME
|
|
119
|
+
)
|
|
120
|
+
if self._fi_api_key is None or self._fi_secret_key is None:
|
|
121
|
+
raise AuthError(self._fi_api_key, self._fi_secret_key)
|
|
122
|
+
|
|
123
|
+
super().__init__(
|
|
124
|
+
base_url=fi_base_url,
|
|
125
|
+
default_headers={
|
|
126
|
+
"X-Api-Key": self._fi_api_key,
|
|
127
|
+
"X-Secret-Key": self._fi_secret_key,
|
|
128
|
+
},
|
|
129
|
+
**kwargs,
|
|
130
|
+
)
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
class APIKeyManager(APIKeyAuth):
|
|
134
|
+
_instance = None
|
|
135
|
+
|
|
136
|
+
def __new__(cls):
|
|
137
|
+
if cls._instance is None:
|
|
138
|
+
cls._instance = super().__new__(cls)
|
|
139
|
+
cls._instance._initialize()
|
|
140
|
+
return cls._instance
|
|
141
|
+
|
|
142
|
+
@property
|
|
143
|
+
def url(self) -> str:
|
|
144
|
+
return self.BASE_URL + "/model_hub/api-keys"
|
|
145
|
+
|
|
146
|
+
@property
|
|
147
|
+
def headers(self) -> dict:
|
|
148
|
+
return {
|
|
149
|
+
"X-Api-Key": self._fi_api_key,
|
|
150
|
+
"X-Secret-Key": self._fi_secret_key,
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
def _initialize(self):
|
|
154
|
+
self._api_keys: Dict[ApiKeyName, Optional[str]] = {
|
|
155
|
+
key: os.getenv(key.value) for key in ApiKeyName
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
def get_api_key(self, provider: ApiKeyName) -> Optional[str]:
|
|
159
|
+
if provider not in self._api_keys:
|
|
160
|
+
raise ValueError(f"Provider {provider} not found in API keys") # noqa: E713
|
|
161
|
+
return self._api_keys[provider]
|
|
162
|
+
|
|
163
|
+
def set_api_key(self, provider: ApiKeyName, key: str) -> None:
|
|
164
|
+
self._api_keys[provider] = key
|
|
165
|
+
|
|
166
|
+
def validate_required_keys(self, required_providers: list[ApiKeyName]) -> bool:
|
|
167
|
+
if not required_providers:
|
|
168
|
+
return True
|
|
169
|
+
|
|
170
|
+
missing_keys = [
|
|
171
|
+
provider.value
|
|
172
|
+
for provider in required_providers
|
|
173
|
+
if provider not in self._api_keys
|
|
174
|
+
]
|
|
175
|
+
|
|
176
|
+
if missing_keys:
|
|
177
|
+
raise ValueError(f"Missing required API keys: {', '.join(missing_keys)}")
|
|
178
|
+
return True
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
from enum import Enum
|
|
2
|
+
from typing import Any, Dict, Optional
|
|
3
|
+
|
|
4
|
+
from pydantic import BaseModel
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class HttpMethod(Enum):
|
|
8
|
+
GET = "GET"
|
|
9
|
+
POST = "POST"
|
|
10
|
+
PUT = "PUT"
|
|
11
|
+
DELETE = "DELETE"
|
|
12
|
+
PATCH = "PATCH"
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class RequestConfig(BaseModel):
|
|
16
|
+
"""Configuration for an HTTP request"""
|
|
17
|
+
|
|
18
|
+
method: HttpMethod
|
|
19
|
+
url: str
|
|
20
|
+
headers: Optional[Dict[str, str]] = {}
|
|
21
|
+
params: Optional[Dict[str, Any]] = {}
|
|
22
|
+
json: Optional[Dict[str, Any]] = {}
|
|
23
|
+
timeout: Optional[int] = None
|
|
24
|
+
retry_attempts: int = 3
|
|
25
|
+
retry_delay: float = 1.0
|