futureagi 0.6.2__tar.gz → 0.6.4__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.6.2 → futureagi-0.6.4}/PKG-INFO +1 -1
- {futureagi-0.6.2 → futureagi-0.6.4}/fi/api/types.py +5 -1
- {futureagi-0.6.2 → futureagi-0.6.4}/fi/datasets/dataset.py +61 -23
- {futureagi-0.6.2 → futureagi-0.6.4}/fi/kb/client.py +148 -52
- futureagi-0.6.4/fi/prompt/__init__.py +19 -0
- futureagi-0.6.4/fi/prompt/client.py +777 -0
- {futureagi-0.6.2 → futureagi-0.6.4}/fi/prompt/types.py +2 -2
- {futureagi-0.6.2 → futureagi-0.6.4}/fi/utils/errors.py +49 -1
- {futureagi-0.6.2 → futureagi-0.6.4}/fi/utils/routes.py +11 -2
- {futureagi-0.6.2 → futureagi-0.6.4}/pyproject.toml +1 -1
- futureagi-0.6.2/LICENSE.md +0 -16
- futureagi-0.6.2/fi/prompt/__init__.py +0 -3
- futureagi-0.6.2/fi/prompt/client.py +0 -321
- {futureagi-0.6.2 → futureagi-0.6.4}/README.md +0 -0
- {futureagi-0.6.2 → futureagi-0.6.4}/fi/__init__.py +0 -0
- {futureagi-0.6.2 → futureagi-0.6.4}/fi/api/__init__.py +0 -0
- {futureagi-0.6.2 → futureagi-0.6.4}/fi/api/apikeys.py +0 -0
- {futureagi-0.6.2 → futureagi-0.6.4}/fi/api/auth.py +0 -0
- {futureagi-0.6.2 → futureagi-0.6.4}/fi/client.py +0 -0
- {futureagi-0.6.2 → futureagi-0.6.4}/fi/datasets/__init__.py +0 -0
- {futureagi-0.6.2 → futureagi-0.6.4}/fi/datasets/types.py +0 -0
- {futureagi-0.6.2 → futureagi-0.6.4}/fi/kb/__init__.py +0 -0
- {futureagi-0.6.2 → futureagi-0.6.4}/fi/kb/types.py +0 -0
- {futureagi-0.6.2 → futureagi-0.6.4}/fi/testcases/__init__.py +0 -0
- {futureagi-0.6.2 → futureagi-0.6.4}/fi/testcases/conversational_test_case.py +0 -0
- {futureagi-0.6.2 → futureagi-0.6.4}/fi/testcases/general.py +0 -0
- {futureagi-0.6.2 → futureagi-0.6.4}/fi/testcases/llm_test_case.py +0 -0
- {futureagi-0.6.2 → futureagi-0.6.4}/fi/testcases/mllm_test_case.py +0 -0
- {futureagi-0.6.2 → futureagi-0.6.4}/fi/testcases/unified_test_case.py +0 -0
- {futureagi-0.6.2 → futureagi-0.6.4}/fi/testcases/utils.py +0 -0
- {futureagi-0.6.2 → futureagi-0.6.4}/fi/utils/__init__.py +0 -0
- {futureagi-0.6.2 → futureagi-0.6.4}/fi/utils/constants.py +0 -0
- {futureagi-0.6.2 → futureagi-0.6.4}/fi/utils/executor.py +0 -0
- {futureagi-0.6.2 → futureagi-0.6.4}/fi/utils/logging.py +0 -0
- {futureagi-0.6.2 → futureagi-0.6.4}/fi/utils/types.py +0 -0
- {futureagi-0.6.2 → futureagi-0.6.4}/fi/utils/utils.py +0 -0
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
from enum import Enum
|
|
2
2
|
from typing import Any, Dict, Optional
|
|
3
3
|
|
|
4
|
-
from pydantic import BaseModel
|
|
4
|
+
from pydantic import BaseModel, ConfigDict
|
|
5
5
|
|
|
6
6
|
|
|
7
7
|
class HttpMethod(Enum):
|
|
@@ -26,6 +26,10 @@ class RequestConfig(BaseModel):
|
|
|
26
26
|
retry_attempts: int = 3
|
|
27
27
|
retry_delay: float = 1.0
|
|
28
28
|
|
|
29
|
+
# Prevent pydantic from warning about the `json` field shadowing the
|
|
30
|
+
# `.json()` model method (see https://errors.pydantic.dev/latest/warnings/#field-name-shadowing).
|
|
31
|
+
model_config = ConfigDict(protected_namespaces=())
|
|
32
|
+
|
|
29
33
|
|
|
30
34
|
class ModelProvider(str, Enum):
|
|
31
35
|
OPENAI = "openai"
|
|
@@ -23,7 +23,7 @@ try :
|
|
|
23
23
|
from fi.evals.evaluator import EvalInfoResponseHandler
|
|
24
24
|
from fi.evals.templates import EvalTemplate
|
|
25
25
|
except ImportError:
|
|
26
|
-
logging.warning(f"ai-
|
|
26
|
+
logging.warning(f"ai-evaluation is not installed. Please install it to add evaluations to your dataset.")
|
|
27
27
|
|
|
28
28
|
from fi.utils.constants import (
|
|
29
29
|
DATASET_TEMP_FILE_PREFIX,
|
|
@@ -38,7 +38,8 @@ from fi.utils.errors import (
|
|
|
38
38
|
DatasetValidationError,
|
|
39
39
|
RateLimitError,
|
|
40
40
|
ServerError,
|
|
41
|
-
ServiceUnavailableError
|
|
41
|
+
ServiceUnavailableError,
|
|
42
|
+
UnexpectedDataFormatError
|
|
42
43
|
)
|
|
43
44
|
from fi.utils.routes import Routes
|
|
44
45
|
from fi.utils.utils import get_tempfile_path, get_keys_from_env, get_base_url_from_env
|
|
@@ -396,6 +397,11 @@ class Dataset(APIKeyAuth):
|
|
|
396
397
|
raise DatasetValidationError("Model cannot be empty for run prompt.")
|
|
397
398
|
if not messages:
|
|
398
399
|
raise DatasetValidationError("Messages list cannot be empty for run prompt.")
|
|
400
|
+
for msg in messages:
|
|
401
|
+
if not isinstance(msg, dict):
|
|
402
|
+
raise UnexpectedDataFormatError("Each message must be a dictionary.")
|
|
403
|
+
if "role" not in msg or "content" not in msg:
|
|
404
|
+
raise UnexpectedDataFormatError("Each message must have 'role' and 'content' fields.")
|
|
399
405
|
|
|
400
406
|
processed_messages = []
|
|
401
407
|
referenced_columns = set()
|
|
@@ -406,50 +412,82 @@ class Dataset(APIKeyAuth):
|
|
|
406
412
|
|
|
407
413
|
if "content" in msg:
|
|
408
414
|
content = msg["content"]
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
415
|
+
|
|
416
|
+
# Convert string content to the expected list format
|
|
417
|
+
if isinstance(content, str):
|
|
418
|
+
# Handle column references in string content
|
|
419
|
+
column_refs = re.findall(r"\{\{(.*?)\}\}", content)
|
|
420
|
+
for col_name in column_refs:
|
|
421
|
+
col_id = self.get_column_id(col_name)
|
|
422
|
+
if not col_id:
|
|
423
|
+
raise DatasetError(
|
|
424
|
+
f"Referenced column '{{{{{col_name}}}}}' not found in dataset '{self.dataset_config.name}'"
|
|
425
|
+
)
|
|
426
|
+
referenced_columns.add(col_name)
|
|
427
|
+
content = content.replace(
|
|
428
|
+
f"{{{{{col_name}}}}}", f"{{{{{col_id}}}}}"
|
|
416
429
|
)
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
430
|
+
|
|
431
|
+
# Convert to expected format: list of dictionaries
|
|
432
|
+
msg["content"] = [{"type": "text", "text": content}]
|
|
433
|
+
|
|
434
|
+
elif isinstance(content, list):
|
|
435
|
+
# Handle list content (already in expected format)
|
|
436
|
+
processed_content = []
|
|
437
|
+
for content_item in content:
|
|
438
|
+
if isinstance(content_item, dict):
|
|
439
|
+
# Handle column references in dict content
|
|
440
|
+
if "text" in content_item:
|
|
441
|
+
text_content = content_item["text"]
|
|
442
|
+
column_refs = re.findall(r"\{\{(.*?)\}\}", text_content)
|
|
443
|
+
for col_name in column_refs:
|
|
444
|
+
col_id = self.get_column_id(col_name)
|
|
445
|
+
if not col_id:
|
|
446
|
+
raise DatasetError(
|
|
447
|
+
f"Referenced column '{{{{{col_name}}}}}' not found in dataset '{self.dataset_config.name}'"
|
|
448
|
+
)
|
|
449
|
+
referenced_columns.add(col_name)
|
|
450
|
+
text_content = text_content.replace(
|
|
451
|
+
f"{{{{{col_name}}}}}", f"{{{{{col_id}}}}}"
|
|
452
|
+
)
|
|
453
|
+
content_item["text"] = text_content
|
|
454
|
+
processed_content.append(content_item)
|
|
455
|
+
else:
|
|
456
|
+
# If list item is not a dict, treat as text
|
|
457
|
+
processed_content.append({"type": "text", "text": str(content_item)})
|
|
458
|
+
msg["content"] = processed_content
|
|
422
459
|
|
|
423
460
|
processed_messages.append(msg)
|
|
424
461
|
|
|
425
462
|
config = {
|
|
426
|
-
"
|
|
463
|
+
"dataset_id": str(self.dataset_config.id),
|
|
427
464
|
"name": name,
|
|
428
465
|
"config": {
|
|
429
466
|
"model": model,
|
|
430
|
-
"
|
|
467
|
+
"output_format": output_format,
|
|
431
468
|
"concurrency": concurrency,
|
|
432
469
|
"messages": processed_messages,
|
|
433
|
-
"
|
|
470
|
+
"max_tokens": max_tokens,
|
|
434
471
|
"temperature": temperature,
|
|
435
|
-
"
|
|
436
|
-
"
|
|
437
|
-
"
|
|
472
|
+
"presence_penalty": presence_penalty,
|
|
473
|
+
"frequency_penalty": frequency_penalty,
|
|
474
|
+
"top_p": top_p,
|
|
438
475
|
},
|
|
439
476
|
}
|
|
440
477
|
|
|
441
478
|
if tools:
|
|
442
479
|
config["config"]["tools"] = tools
|
|
443
480
|
if tool_choice is not None:
|
|
444
|
-
config["config"]["
|
|
481
|
+
config["config"]["tool_choice"] = tool_choice
|
|
445
482
|
if response_format:
|
|
446
|
-
config["config"]["
|
|
483
|
+
config["config"]["response_format"] = response_format
|
|
447
484
|
|
|
448
485
|
url = f"{self._base_url}/{Routes.dataset_add_run_prompt_column.value}"
|
|
449
486
|
self.request_with_retry(
|
|
450
487
|
config=RequestConfig(method=HttpMethod.POST, url=url, json=config, timeout=DEFAULT_API_TIMEOUT),
|
|
451
488
|
response_handler=DatasetResponseHandler,
|
|
452
489
|
)
|
|
490
|
+
|
|
453
491
|
|
|
454
492
|
return self
|
|
455
493
|
|
|
@@ -467,7 +505,7 @@ class Dataset(APIKeyAuth):
|
|
|
467
505
|
from fi.evals.evaluator import EvalInfoResponseHandler
|
|
468
506
|
from fi.evals.templates import EvalTemplate
|
|
469
507
|
except ImportError:
|
|
470
|
-
raise DatasetError("ai-
|
|
508
|
+
raise DatasetError("ai-evaluation is not installed. Please install it to add evaluations to your dataset.")
|
|
471
509
|
|
|
472
510
|
if not self.dataset_config or not self.dataset_config.id:
|
|
473
511
|
raise DatasetError("Dataset must be configured with an ID to add an evaluation.")
|
|
@@ -16,6 +16,10 @@ class KBResponseHandler(ResponseHandler[Dict, KnowledgeBaseConfig]):
|
|
|
16
16
|
"""Handles responses for prompt requests"""
|
|
17
17
|
data = response.json()
|
|
18
18
|
|
|
19
|
+
if data is not None and data.get("status") is False:
|
|
20
|
+
err_msg = data.get("result") or data.get("detail") or "Knowledge Base operation failed."
|
|
21
|
+
raise SDKException(err_msg)
|
|
22
|
+
|
|
19
23
|
if response.request.method == HttpMethod.POST.value and response.url.endswith(
|
|
20
24
|
Routes.knowledge_base.value
|
|
21
25
|
):
|
|
@@ -35,6 +39,45 @@ class KBResponseHandler(ResponseHandler[Dict, KnowledgeBaseConfig]):
|
|
|
35
39
|
def _handle_error(cls, response) -> None:
|
|
36
40
|
if response.status_code == 403:
|
|
37
41
|
raise InvalidAuthError()
|
|
42
|
+
elif response.status_code == 429:
|
|
43
|
+
# Too many requests / quota exceeded – backend typically returns JSON with the reason.
|
|
44
|
+
try:
|
|
45
|
+
data = response.json()
|
|
46
|
+
err_msg = (
|
|
47
|
+
data.get("result")
|
|
48
|
+
or data.get("detail")
|
|
49
|
+
or data.get("message")
|
|
50
|
+
or "Rate limit exceeded. Please try again later."
|
|
51
|
+
)
|
|
52
|
+
except Exception:
|
|
53
|
+
err_msg = "Rate limit exceeded. Please try again later."
|
|
54
|
+
raise SDKException(err_msg)
|
|
55
|
+
elif 400 <= response.status_code < 500:
|
|
56
|
+
# Other client-side errors (validation, not found, etc.)
|
|
57
|
+
try:
|
|
58
|
+
data = response.json()
|
|
59
|
+
err_msg = (
|
|
60
|
+
data.get("result")
|
|
61
|
+
or data.get("detail")
|
|
62
|
+
or data.get("message")
|
|
63
|
+
or f"Client error {response.status_code}."
|
|
64
|
+
)
|
|
65
|
+
except Exception:
|
|
66
|
+
err_msg = f"Client error {response.status_code}."
|
|
67
|
+
raise SDKException(err_msg)
|
|
68
|
+
elif response.status_code >= 500:
|
|
69
|
+
# Server-side error
|
|
70
|
+
try:
|
|
71
|
+
data = response.json()
|
|
72
|
+
err_msg = (
|
|
73
|
+
data.get("result")
|
|
74
|
+
or data.get("detail")
|
|
75
|
+
or data.get("message")
|
|
76
|
+
or "Server encountered an error. Please try again later."
|
|
77
|
+
)
|
|
78
|
+
except Exception:
|
|
79
|
+
err_msg = "Server encountered an error. Please try again later."
|
|
80
|
+
raise SDKException(err_msg)
|
|
38
81
|
else:
|
|
39
82
|
response.raise_for_status()
|
|
40
83
|
|
|
@@ -42,30 +85,43 @@ class KnowledgeBase(APIKeyAuth):
|
|
|
42
85
|
|
|
43
86
|
def __init__(
|
|
44
87
|
self,
|
|
45
|
-
|
|
88
|
+
kb_name: Optional[str] = None,
|
|
46
89
|
fi_api_key: Optional[str] = None,
|
|
47
90
|
fi_secret_key: Optional[str] = None,
|
|
48
91
|
fi_base_url: Optional[str] = None,
|
|
49
92
|
**kwargs,
|
|
50
93
|
):
|
|
94
|
+
"""Create a KnowledgeBase client.
|
|
95
|
+
|
|
96
|
+
Args:
|
|
97
|
+
kb_name (Optional[str]): Name of an existing Knowledge Base you want this client to work with.
|
|
98
|
+
If provided, the SDK will look it up server-side and cache its id for subsequent calls.
|
|
99
|
+
"""
|
|
100
|
+
|
|
51
101
|
super().__init__(
|
|
52
102
|
fi_api_key=fi_api_key,
|
|
53
103
|
fi_secret_key=fi_secret_key,
|
|
54
104
|
fi_base_url=fi_base_url,
|
|
55
105
|
**kwargs,
|
|
56
106
|
)
|
|
57
|
-
|
|
58
|
-
|
|
107
|
+
|
|
108
|
+
# Internal cache of the current KB (instance of KnowledgeBaseConfig)
|
|
109
|
+
self.kb: Optional[KnowledgeBaseConfig] = None
|
|
110
|
+
|
|
111
|
+
if kb_name:
|
|
59
112
|
try:
|
|
60
|
-
self.kb = self._get_kb_from_name(
|
|
113
|
+
self.kb = self._get_kb_from_name(kb_name)
|
|
61
114
|
except Exception:
|
|
62
|
-
raise SDKException(
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
if self.kb and self.kb.id:
|
|
66
|
-
raise SDKException("Knowledge Base already exists. Please delete the existing knowledge base before running.")
|
|
115
|
+
raise SDKException(
|
|
116
|
+
f"Knowledge Base with name '{kb_name}' not found. Please create it first or verify the name."
|
|
117
|
+
)
|
|
67
118
|
|
|
68
|
-
def update_kb(
|
|
119
|
+
def update_kb(
|
|
120
|
+
self,
|
|
121
|
+
kb_name: str,
|
|
122
|
+
new_name: Optional[str] = None,
|
|
123
|
+
file_paths: Optional[Union[str, List[str]]] = None,
|
|
124
|
+
):
|
|
69
125
|
"""
|
|
70
126
|
Update name of Knowledge Base and/or add files to it.
|
|
71
127
|
|
|
@@ -79,8 +135,14 @@ class KnowledgeBase(APIKeyAuth):
|
|
|
79
135
|
try:
|
|
80
136
|
import requests
|
|
81
137
|
|
|
82
|
-
if not
|
|
83
|
-
|
|
138
|
+
# Resolve the KB if not already cached or if a different kb_name is provided
|
|
139
|
+
if not self.kb or self.kb.name != kb_name:
|
|
140
|
+
try:
|
|
141
|
+
self.kb = self._get_kb_from_name(kb_name)
|
|
142
|
+
except Exception:
|
|
143
|
+
raise SDKException(
|
|
144
|
+
f"Knowledge Base named '{kb_name}' not found when attempting to update."
|
|
145
|
+
)
|
|
84
146
|
|
|
85
147
|
if file_paths:
|
|
86
148
|
try:
|
|
@@ -94,12 +156,10 @@ class KnowledgeBase(APIKeyAuth):
|
|
|
94
156
|
|
|
95
157
|
url = self._base_url + "/" + Routes.knowledge_base.value
|
|
96
158
|
|
|
97
|
-
data = {
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
"kb_id": str(self.kb.id)
|
|
102
|
-
})
|
|
159
|
+
data = {
|
|
160
|
+
"kb_id": str(self.kb.id),
|
|
161
|
+
"name": self.kb.name if new_name is None else new_name,
|
|
162
|
+
}
|
|
103
163
|
|
|
104
164
|
files = []
|
|
105
165
|
|
|
@@ -142,6 +202,7 @@ class KnowledgeBase(APIKeyAuth):
|
|
|
142
202
|
raise SDKException("Server reported that some files were not uploaded successfully.")
|
|
143
203
|
|
|
144
204
|
if parsed_result_data:
|
|
205
|
+
# Server may return updated info – refresh local cache
|
|
145
206
|
self.kb.id = parsed_result_data.get("id", self.kb.id)
|
|
146
207
|
self.kb.name = parsed_result_data.get("name", self.kb.name)
|
|
147
208
|
if "files" in parsed_result_data:
|
|
@@ -162,7 +223,11 @@ class KnowledgeBase(APIKeyAuth):
|
|
|
162
223
|
fh.close()
|
|
163
224
|
raise SDKException("Failed to update the Knowledge Base due to an unexpected error.", cause=e)
|
|
164
225
|
|
|
165
|
-
def delete_files_from_kb(
|
|
226
|
+
def delete_files_from_kb(
|
|
227
|
+
self,
|
|
228
|
+
file_names: List[str],
|
|
229
|
+
kb_name: Optional[str] = None,
|
|
230
|
+
):
|
|
166
231
|
"""
|
|
167
232
|
Delete files from the Knowledge Base.
|
|
168
233
|
|
|
@@ -173,8 +238,19 @@ class KnowledgeBase(APIKeyAuth):
|
|
|
173
238
|
self for chaining
|
|
174
239
|
"""
|
|
175
240
|
try:
|
|
241
|
+
# If kb_name provided, resolve it; otherwise fallback to cached self.kb
|
|
242
|
+
if kb_name is not None:
|
|
243
|
+
try:
|
|
244
|
+
self.kb = self._get_kb_from_name(kb_name)
|
|
245
|
+
except SDKException:
|
|
246
|
+
raise SDKException(
|
|
247
|
+
f"Knowledge Base named '{kb_name}' not found when attempting to delete files."
|
|
248
|
+
)
|
|
249
|
+
|
|
176
250
|
if not self.kb or not self.kb.id:
|
|
177
|
-
raise SDKException(
|
|
251
|
+
raise SDKException(
|
|
252
|
+
"No Knowledge Base targeted. Provide kb_name or have a current Knowledge Base configured."
|
|
253
|
+
)
|
|
178
254
|
|
|
179
255
|
if not file_names:
|
|
180
256
|
raise SDKException("Files to be deleted not found or list is empty. Please provide correct File Names.")
|
|
@@ -204,7 +280,11 @@ class KnowledgeBase(APIKeyAuth):
|
|
|
204
280
|
except Exception as e:
|
|
205
281
|
raise SDKException("Failed to delete files from the Knowledge Base due to an unexpected error.", cause=e)
|
|
206
282
|
|
|
207
|
-
def delete_kb(
|
|
283
|
+
def delete_kb(
|
|
284
|
+
self,
|
|
285
|
+
kb_ids: Optional[Union[str, List[str]]] = None,
|
|
286
|
+
kb_names: Optional[Union[str, List[str]]] = None,
|
|
287
|
+
):
|
|
208
288
|
"""
|
|
209
289
|
Delete a Knowledge Base and return the Knowledge Base client.
|
|
210
290
|
|
|
@@ -213,25 +293,50 @@ class KnowledgeBase(APIKeyAuth):
|
|
|
213
293
|
kb_ids Optional[Union[str, List[str]]]: List of kb_ids to delete
|
|
214
294
|
|
|
215
295
|
"""
|
|
216
|
-
try:
|
|
217
|
-
|
|
218
|
-
|
|
296
|
+
try:
|
|
297
|
+
# Resolve provided kb_names into ids (if any)
|
|
298
|
+
resolved_ids: list[str] = []
|
|
299
|
+
|
|
300
|
+
if kb_names is not None:
|
|
301
|
+
names_list = (
|
|
302
|
+
[kb_names]
|
|
303
|
+
if isinstance(kb_names, str)
|
|
304
|
+
else kb_names # assume list
|
|
305
|
+
)
|
|
306
|
+
for name in names_list:
|
|
307
|
+
try:
|
|
308
|
+
kb_conf = self._get_kb_from_name(name)
|
|
309
|
+
resolved_ids.append(str(kb_conf.id))
|
|
310
|
+
except SDKException:
|
|
311
|
+
# If a name isn't found we skip & warn later
|
|
312
|
+
continue
|
|
313
|
+
|
|
314
|
+
if not resolved_ids:
|
|
315
|
+
raise SDKException(
|
|
316
|
+
"None of the provided Knowledge Base names could be resolved to existing IDs."
|
|
317
|
+
)
|
|
318
|
+
|
|
319
|
+
# Handle legacy kb_ids arg
|
|
320
|
+
if kb_ids is not None:
|
|
219
321
|
if isinstance(kb_ids, str):
|
|
220
|
-
|
|
322
|
+
resolved_ids.append(kb_ids)
|
|
221
323
|
elif isinstance(kb_ids, list):
|
|
222
|
-
|
|
324
|
+
resolved_ids.extend([str(kb_id) for kb_id in kb_ids])
|
|
223
325
|
else:
|
|
224
326
|
raise SDKException("kb_ids must be a string or a list of strings.")
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
327
|
+
|
|
328
|
+
# Fallback to cached KB if nothing else provided
|
|
329
|
+
if not resolved_ids and self.kb and self.kb.id:
|
|
330
|
+
resolved_ids.append(str(self.kb.id))
|
|
331
|
+
|
|
332
|
+
if not resolved_ids:
|
|
333
|
+
raise SDKException(
|
|
334
|
+
"No Knowledge Base specified for deletion (provide kb_names, kb_ids, or have a current KB cached)."
|
|
335
|
+
)
|
|
229
336
|
|
|
230
337
|
method = HttpMethod.DELETE
|
|
231
338
|
url = self._base_url + "/" + Routes.knowledge_base.value
|
|
232
|
-
json_payload = {
|
|
233
|
-
"kb_ids": target_kb_ids_to_delete
|
|
234
|
-
}
|
|
339
|
+
json_payload = {"kb_ids": resolved_ids}
|
|
235
340
|
|
|
236
341
|
response = self.request(
|
|
237
342
|
config=RequestConfig(
|
|
@@ -243,7 +348,7 @@ class KnowledgeBase(APIKeyAuth):
|
|
|
243
348
|
response_handler=KBResponseHandler,
|
|
244
349
|
)
|
|
245
350
|
|
|
246
|
-
if self.kb and self.kb.id and str(self.kb.id) in
|
|
351
|
+
if self.kb and self.kb.id and str(self.kb.id) in resolved_ids:
|
|
247
352
|
self.kb = None
|
|
248
353
|
|
|
249
354
|
return self
|
|
@@ -253,7 +358,7 @@ class KnowledgeBase(APIKeyAuth):
|
|
|
253
358
|
except Exception as e:
|
|
254
359
|
raise SDKException("Failed to delete Knowledge Base(s) due to an unexpected error.", cause=e)
|
|
255
360
|
|
|
256
|
-
def create_kb(self, name
|
|
361
|
+
def create_kb(self, name: Optional[str] = None, file_paths: Optional[Union[str, List[str]]] = None):
|
|
257
362
|
"""
|
|
258
363
|
Create a Knowledge Base and return the Knowledge Base client.
|
|
259
364
|
|
|
@@ -266,17 +371,7 @@ class KnowledgeBase(APIKeyAuth):
|
|
|
266
371
|
"""
|
|
267
372
|
import requests
|
|
268
373
|
|
|
269
|
-
|
|
270
|
-
raise SDKException(
|
|
271
|
-
f"Cannot create Knowledge Base: an existing Knowledge Base '{self.kb.name}' (ID: {self.kb.id}) is already configured for this client instance."
|
|
272
|
-
)
|
|
273
|
-
|
|
274
|
-
final_kb_name = name
|
|
275
|
-
if not final_kb_name:
|
|
276
|
-
if self.kb and self.kb.name:
|
|
277
|
-
final_kb_name = self.kb.name
|
|
278
|
-
else:
|
|
279
|
-
final_kb_name = "Unnamed KB"
|
|
374
|
+
final_kb_name = name or "Unnamed KB"
|
|
280
375
|
|
|
281
376
|
try:
|
|
282
377
|
data = {"name": final_kb_name}
|
|
@@ -318,7 +413,6 @@ class KnowledgeBase(APIKeyAuth):
|
|
|
318
413
|
headers=headers,
|
|
319
414
|
timeout=300
|
|
320
415
|
)
|
|
321
|
-
|
|
322
416
|
KBResponseHandler._handle_error(response)
|
|
323
417
|
parsed_result_data = KBResponseHandler._parse_success(response)
|
|
324
418
|
|
|
@@ -326,9 +420,9 @@ class KnowledgeBase(APIKeyAuth):
|
|
|
326
420
|
raise SDKException("Server reported that some files were not uploaded during Knowledge Base creation.")
|
|
327
421
|
|
|
328
422
|
self.kb = KnowledgeBaseConfig(
|
|
329
|
-
id=parsed_result_data.get("kbId"),
|
|
330
|
-
name=parsed_result_data.get("kbName"),
|
|
331
|
-
files=parsed_result_data.get("fileIds", [])
|
|
423
|
+
id=parsed_result_data.get("kbId"),
|
|
424
|
+
name=parsed_result_data.get("kbName"),
|
|
425
|
+
files=parsed_result_data.get("fileIds", []),
|
|
332
426
|
)
|
|
333
427
|
return self
|
|
334
428
|
|
|
@@ -432,5 +526,7 @@ class KnowledgeBase(APIKeyAuth):
|
|
|
432
526
|
),
|
|
433
527
|
response_handler=KBResponseHandler,
|
|
434
528
|
)
|
|
435
|
-
data = response[
|
|
436
|
-
|
|
529
|
+
data = response["result"].get("tableData")
|
|
530
|
+
if not data:
|
|
531
|
+
raise SDKException(f"Knowledge Base with name '{kb_name}' not found.")
|
|
532
|
+
return KnowledgeBaseConfig(id=data[0].get("id"), name=data[0].get("name"))
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
from .client import Prompt
|
|
2
|
+
from .types import (
|
|
3
|
+
MessageBase,
|
|
4
|
+
SystemMessage,
|
|
5
|
+
AssistantMessage,
|
|
6
|
+
UserMessage,
|
|
7
|
+
ModelConfig,
|
|
8
|
+
PromptTemplate,
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
__all__ = [
|
|
12
|
+
"Prompt",
|
|
13
|
+
"MessageBase",
|
|
14
|
+
"SystemMessage",
|
|
15
|
+
"AssistantMessage",
|
|
16
|
+
"UserMessage",
|
|
17
|
+
"ModelConfig",
|
|
18
|
+
"PromptTemplate",
|
|
19
|
+
]
|