alita-sdk 0.3.532__py3-none-any.whl → 0.3.554__py3-none-any.whl
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.
Potentially problematic release.
This version of alita-sdk might be problematic. Click here for more details.
- alita_sdk/community/__init__.py +8 -4
- alita_sdk/configurations/__init__.py +1 -0
- alita_sdk/configurations/openapi.py +111 -0
- alita_sdk/runtime/clients/client.py +161 -5
- alita_sdk/runtime/langchain/langraph_agent.py +2 -2
- alita_sdk/runtime/langchain/utils.py +46 -0
- alita_sdk/runtime/skills/__init__.py +91 -0
- alita_sdk/runtime/skills/callbacks.py +498 -0
- alita_sdk/runtime/skills/discovery.py +540 -0
- alita_sdk/runtime/skills/executor.py +610 -0
- alita_sdk/runtime/skills/input_builder.py +371 -0
- alita_sdk/runtime/skills/models.py +330 -0
- alita_sdk/runtime/skills/registry.py +355 -0
- alita_sdk/runtime/skills/skill_runner.py +330 -0
- alita_sdk/runtime/toolkits/__init__.py +2 -0
- alita_sdk/runtime/toolkits/skill_router.py +238 -0
- alita_sdk/runtime/toolkits/tools.py +76 -9
- alita_sdk/runtime/tools/__init__.py +3 -1
- alita_sdk/runtime/tools/llm.py +241 -73
- alita_sdk/runtime/tools/loop.py +3 -1
- alita_sdk/runtime/tools/loop_output.py +3 -1
- alita_sdk/runtime/tools/skill_router.py +776 -0
- alita_sdk/runtime/tools/tool.py +3 -1
- alita_sdk/runtime/tools/vectorstore.py +7 -2
- alita_sdk/runtime/tools/vectorstore_base.py +7 -2
- alita_sdk/tools/__init__.py +41 -1
- alita_sdk/tools/ado/work_item/ado_wrapper.py +33 -2
- alita_sdk/tools/base_indexer_toolkit.py +36 -24
- alita_sdk/tools/confluence/api_wrapper.py +5 -6
- alita_sdk/tools/confluence/loader.py +4 -2
- alita_sdk/tools/openapi/__init__.py +280 -120
- alita_sdk/tools/openapi/api_wrapper.py +883 -0
- alita_sdk/tools/openapi/tool.py +20 -0
- alita_sdk/tools/pandas/dataframe/generator/base.py +3 -1
- alita_sdk/tools/servicenow/__init__.py +9 -9
- alita_sdk/tools/servicenow/api_wrapper.py +1 -1
- {alita_sdk-0.3.532.dist-info → alita_sdk-0.3.554.dist-info}/METADATA +1 -1
- {alita_sdk-0.3.532.dist-info → alita_sdk-0.3.554.dist-info}/RECORD +42 -29
- {alita_sdk-0.3.532.dist-info → alita_sdk-0.3.554.dist-info}/WHEEL +0 -0
- {alita_sdk-0.3.532.dist-info → alita_sdk-0.3.554.dist-info}/entry_points.txt +0 -0
- {alita_sdk-0.3.532.dist-info → alita_sdk-0.3.554.dist-info}/licenses/LICENSE +0 -0
- {alita_sdk-0.3.532.dist-info → alita_sdk-0.3.554.dist-info}/top_level.txt +0 -0
alita_sdk/runtime/tools/llm.py
CHANGED
|
@@ -6,7 +6,6 @@ from typing import Any, Optional, List, Union, Literal
|
|
|
6
6
|
from langchain_core.messages import HumanMessage, SystemMessage, AIMessage
|
|
7
7
|
from langchain_core.runnables import RunnableConfig
|
|
8
8
|
from langchain_core.tools import BaseTool, ToolException
|
|
9
|
-
from langchain_core.exceptions import OutputParserException
|
|
10
9
|
from langchain_core.callbacks import dispatch_custom_event
|
|
11
10
|
from pydantic import Field
|
|
12
11
|
|
|
@@ -44,6 +43,17 @@ logger = logging.getLogger(__name__)
|
|
|
44
43
|
|
|
45
44
|
# return supports_reasoning
|
|
46
45
|
|
|
46
|
+
JSON_INSTRUCTION_TEMPLATE = (
|
|
47
|
+
"\n\n**IMPORTANT: You MUST respond with ONLY a valid JSON object.**\n\n"
|
|
48
|
+
"Required JSON fields:\n{field_descriptions}\n\n"
|
|
49
|
+
"Example format:\n"
|
|
50
|
+
"{{\n{example_fields}\n}}\n\n"
|
|
51
|
+
"Rules:\n"
|
|
52
|
+
"1. Output ONLY the JSON object - no markdown, no explanations, no extra text\n"
|
|
53
|
+
"2. Ensure all required fields are present\n"
|
|
54
|
+
"3. Use proper JSON syntax with double quotes for strings\n"
|
|
55
|
+
"4. Do not wrap the JSON in code blocks or backticks"
|
|
56
|
+
)
|
|
47
57
|
|
|
48
58
|
class LLMNode(BaseTool):
|
|
49
59
|
"""Enhanced LLM node with chat history and tool binding support"""
|
|
@@ -67,6 +77,221 @@ class LLMNode(BaseTool):
|
|
|
67
77
|
steps_limit: Optional[int] = Field(default=25, description='Maximum steps for tool execution')
|
|
68
78
|
tool_execution_timeout: Optional[int] = Field(default=900, description='Timeout (seconds) for tool execution. Default is 15 minutes.')
|
|
69
79
|
|
|
80
|
+
def _prepare_structured_output_params(self) -> dict:
|
|
81
|
+
"""
|
|
82
|
+
Prepare structured output parameters from structured_output_dict.
|
|
83
|
+
|
|
84
|
+
Returns:
|
|
85
|
+
Dictionary with parameter definitions for creating Pydantic model
|
|
86
|
+
"""
|
|
87
|
+
struct_params = {
|
|
88
|
+
key: {
|
|
89
|
+
"type": 'list[str]' if 'list' in value else value,
|
|
90
|
+
"description": ""
|
|
91
|
+
}
|
|
92
|
+
for key, value in (self.structured_output_dict or {}).items()
|
|
93
|
+
}
|
|
94
|
+
# Add default output field for proper response to user
|
|
95
|
+
struct_params[ELITEA_RS] = {
|
|
96
|
+
'description': 'final output to user (summarized output from LLM)',
|
|
97
|
+
'type': 'str',
|
|
98
|
+
"default": None
|
|
99
|
+
}
|
|
100
|
+
return struct_params
|
|
101
|
+
|
|
102
|
+
def _invoke_with_structured_output(self, llm_client: Any, messages: List, struct_model: Any, config: RunnableConfig):
|
|
103
|
+
"""
|
|
104
|
+
Invoke LLM with structured output, handling tool calls if present.
|
|
105
|
+
|
|
106
|
+
Args:
|
|
107
|
+
llm_client: LLM client instance
|
|
108
|
+
messages: List of conversation messages
|
|
109
|
+
struct_model: Pydantic model for structured output
|
|
110
|
+
config: Runnable configuration
|
|
111
|
+
|
|
112
|
+
Returns:
|
|
113
|
+
Tuple of (completion, initial_completion, final_messages)
|
|
114
|
+
"""
|
|
115
|
+
initial_completion = llm_client.invoke(messages, config=config)
|
|
116
|
+
|
|
117
|
+
if hasattr(initial_completion, 'tool_calls') and initial_completion.tool_calls:
|
|
118
|
+
# Handle tool calls first, then apply structured output
|
|
119
|
+
new_messages, _ = self._run_async_in_sync_context(
|
|
120
|
+
self.__perform_tool_calling(initial_completion, messages, llm_client, config)
|
|
121
|
+
)
|
|
122
|
+
llm = self.__get_struct_output_model(llm_client, struct_model)
|
|
123
|
+
completion = llm.invoke(new_messages, config=config)
|
|
124
|
+
return completion, initial_completion, new_messages
|
|
125
|
+
else:
|
|
126
|
+
# Direct structured output without tool calls
|
|
127
|
+
llm = self.__get_struct_output_model(llm_client, struct_model)
|
|
128
|
+
completion = llm.invoke(messages, config=config)
|
|
129
|
+
return completion, initial_completion, messages
|
|
130
|
+
|
|
131
|
+
def _build_json_instruction(self, struct_model: Any) -> str:
|
|
132
|
+
"""
|
|
133
|
+
Build JSON instruction message for fallback handling.
|
|
134
|
+
|
|
135
|
+
Args:
|
|
136
|
+
struct_model: Pydantic model with field definitions
|
|
137
|
+
|
|
138
|
+
Returns:
|
|
139
|
+
Formatted JSON instruction string
|
|
140
|
+
"""
|
|
141
|
+
field_descriptions = []
|
|
142
|
+
for name, field in struct_model.model_fields.items():
|
|
143
|
+
field_type = field.annotation.__name__ if hasattr(field.annotation, '__name__') else str(field.annotation)
|
|
144
|
+
field_desc = field.description or field_type
|
|
145
|
+
field_descriptions.append(f" - {name} ({field_type}): {field_desc}")
|
|
146
|
+
|
|
147
|
+
example_fields = ",\n".join([
|
|
148
|
+
f' "{k}": <{field.annotation.__name__ if hasattr(field.annotation, "__name__") else "value"}>'
|
|
149
|
+
for k, field in struct_model.model_fields.items()
|
|
150
|
+
])
|
|
151
|
+
|
|
152
|
+
return JSON_INSTRUCTION_TEMPLATE.format(
|
|
153
|
+
field_descriptions="\n".join(field_descriptions),
|
|
154
|
+
example_fields=example_fields
|
|
155
|
+
)
|
|
156
|
+
|
|
157
|
+
def _create_fallback_completion(self, content: str, struct_model: Any) -> Any:
|
|
158
|
+
"""
|
|
159
|
+
Create a fallback completion object when JSON parsing fails.
|
|
160
|
+
|
|
161
|
+
Args:
|
|
162
|
+
content: Plain text content from LLM
|
|
163
|
+
struct_model: Pydantic model to construct
|
|
164
|
+
|
|
165
|
+
Returns:
|
|
166
|
+
Pydantic model instance with fallback values
|
|
167
|
+
"""
|
|
168
|
+
result_dict = {}
|
|
169
|
+
for k, field in struct_model.model_fields.items():
|
|
170
|
+
if k == ELITEA_RS:
|
|
171
|
+
result_dict[k] = content
|
|
172
|
+
elif field.is_required():
|
|
173
|
+
# Set default values for required fields based on type
|
|
174
|
+
result_dict[k] = field.default if field.default is not None else None
|
|
175
|
+
else:
|
|
176
|
+
result_dict[k] = field.default
|
|
177
|
+
return struct_model.model_construct(**result_dict)
|
|
178
|
+
|
|
179
|
+
def _handle_structured_output_fallback(self, llm_client: Any, messages: List, struct_model: Any,
|
|
180
|
+
config: RunnableConfig, original_error: Exception) -> Any:
|
|
181
|
+
"""
|
|
182
|
+
Handle structured output fallback through multiple strategies.
|
|
183
|
+
|
|
184
|
+
Tries fallback methods in order:
|
|
185
|
+
1. json_mode with explicit instructions
|
|
186
|
+
2. function_calling method
|
|
187
|
+
3. Plain text with JSON extraction
|
|
188
|
+
|
|
189
|
+
Args:
|
|
190
|
+
llm_client: LLM client instance
|
|
191
|
+
messages: Original conversation messages
|
|
192
|
+
struct_model: Pydantic model for structured output
|
|
193
|
+
config: Runnable configuration
|
|
194
|
+
original_error: The original ValueError that triggered fallback
|
|
195
|
+
|
|
196
|
+
Returns:
|
|
197
|
+
Completion with structured output (best effort)
|
|
198
|
+
|
|
199
|
+
Raises:
|
|
200
|
+
Propagates exceptions from LLM invocation
|
|
201
|
+
"""
|
|
202
|
+
logger.error(f"Error invoking structured output model: {format_exc()}")
|
|
203
|
+
logger.info("Attempting to fall back to json mode")
|
|
204
|
+
|
|
205
|
+
# Build JSON instruction once
|
|
206
|
+
json_instruction = self._build_json_instruction(struct_model)
|
|
207
|
+
|
|
208
|
+
# Add instruction to messages
|
|
209
|
+
modified_messages = messages.copy()
|
|
210
|
+
if modified_messages and isinstance(modified_messages[-1], HumanMessage):
|
|
211
|
+
modified_messages[-1] = HumanMessage(
|
|
212
|
+
content=modified_messages[-1].content + json_instruction
|
|
213
|
+
)
|
|
214
|
+
else:
|
|
215
|
+
modified_messages.append(HumanMessage(content=json_instruction))
|
|
216
|
+
|
|
217
|
+
# Try json_mode with explicit instructions
|
|
218
|
+
try:
|
|
219
|
+
completion = self.__get_struct_output_model(
|
|
220
|
+
llm_client, struct_model, method="json_mode"
|
|
221
|
+
).invoke(modified_messages, config=config)
|
|
222
|
+
return completion
|
|
223
|
+
except Exception as json_mode_error:
|
|
224
|
+
logger.warning(f"json_mode also failed: {json_mode_error}")
|
|
225
|
+
logger.info("Falling back to function_calling method")
|
|
226
|
+
|
|
227
|
+
# Try function_calling as a third fallback
|
|
228
|
+
try:
|
|
229
|
+
completion = self.__get_struct_output_model(
|
|
230
|
+
llm_client, struct_model, method="function_calling"
|
|
231
|
+
).invoke(modified_messages, config=config)
|
|
232
|
+
return completion
|
|
233
|
+
except Exception as function_calling_error:
|
|
234
|
+
logger.error(f"function_calling also failed: {function_calling_error}")
|
|
235
|
+
logger.info("Final fallback: using plain LLM response")
|
|
236
|
+
|
|
237
|
+
# Last resort: get plain text response and wrap in structure
|
|
238
|
+
plain_completion = llm_client.invoke(modified_messages, config=config)
|
|
239
|
+
content = plain_completion.content.strip() if hasattr(plain_completion, 'content') else str(plain_completion)
|
|
240
|
+
|
|
241
|
+
# Try to extract JSON from the response
|
|
242
|
+
import json
|
|
243
|
+
import re
|
|
244
|
+
|
|
245
|
+
json_match = re.search(r'\{.*\}', content, re.DOTALL)
|
|
246
|
+
if json_match:
|
|
247
|
+
try:
|
|
248
|
+
parsed_json = json.loads(json_match.group(0))
|
|
249
|
+
# Validate it has expected fields and wrap in pydantic model
|
|
250
|
+
completion = struct_model(**parsed_json)
|
|
251
|
+
return completion
|
|
252
|
+
except (json.JSONDecodeError, Exception) as parse_error:
|
|
253
|
+
logger.warning(f"Could not parse extracted JSON: {parse_error}")
|
|
254
|
+
return self._create_fallback_completion(content, struct_model)
|
|
255
|
+
else:
|
|
256
|
+
# No JSON found, create response with content in elitea_response
|
|
257
|
+
return self._create_fallback_completion(content, struct_model)
|
|
258
|
+
|
|
259
|
+
def _format_structured_output_result(self, result: dict, messages: List, initial_completion: Any) -> dict:
|
|
260
|
+
"""
|
|
261
|
+
Format structured output result with properly formatted messages.
|
|
262
|
+
|
|
263
|
+
Args:
|
|
264
|
+
result: Result dictionary from model_dump()
|
|
265
|
+
messages: Original conversation messages
|
|
266
|
+
initial_completion: Initial completion before tool calls
|
|
267
|
+
|
|
268
|
+
Returns:
|
|
269
|
+
Formatted result dictionary with messages
|
|
270
|
+
"""
|
|
271
|
+
# Ensure messages are properly formatted
|
|
272
|
+
if result.get('messages') and isinstance(result['messages'], list):
|
|
273
|
+
result['messages'] = [{'role': 'assistant', 'content': '\n'.join(result['messages'])}]
|
|
274
|
+
else:
|
|
275
|
+
# Extract content from initial_completion, handling thinking blocks
|
|
276
|
+
fallback_content = result.get(ELITEA_RS, '')
|
|
277
|
+
if not fallback_content and initial_completion:
|
|
278
|
+
content_parts = self._extract_content_from_completion(initial_completion)
|
|
279
|
+
fallback_content = content_parts.get('text') or ''
|
|
280
|
+
thinking = content_parts.get('thinking')
|
|
281
|
+
|
|
282
|
+
# Log thinking if present
|
|
283
|
+
if thinking:
|
|
284
|
+
logger.debug(f"Thinking content present in structured output: {thinking[:100]}...")
|
|
285
|
+
|
|
286
|
+
if not fallback_content:
|
|
287
|
+
# Final fallback to raw content
|
|
288
|
+
content = initial_completion.content
|
|
289
|
+
fallback_content = content if isinstance(content, str) else str(content)
|
|
290
|
+
|
|
291
|
+
result['messages'] = messages + [AIMessage(content=fallback_content)]
|
|
292
|
+
|
|
293
|
+
return result
|
|
294
|
+
|
|
70
295
|
def get_filtered_tools(self) -> List[BaseTool]:
|
|
71
296
|
"""
|
|
72
297
|
Filter available tools based on tool_names list.
|
|
@@ -164,8 +389,6 @@ class LLMNode(BaseTool):
|
|
|
164
389
|
if func_args.get('system') is None or func_args.get('task') is None:
|
|
165
390
|
raise ToolException(f"LLMNode requires 'system' and 'task' parameters in input mapping. "
|
|
166
391
|
f"Actual params: {func_args}")
|
|
167
|
-
raise ToolException(f"LLMNode requires 'system' and 'task' parameters in input mapping. "
|
|
168
|
-
f"Actual params: {func_args}")
|
|
169
392
|
# cast to str in case user passes variable different from str
|
|
170
393
|
messages = [SystemMessage(content=str(func_args.get('system'))), *func_args.get('chat_history', []), HumanMessage(content=str(func_args.get('task')))]
|
|
171
394
|
# Remove pre-last item if last two messages are same type and content
|
|
@@ -197,78 +420,23 @@ class LLMNode(BaseTool):
|
|
|
197
420
|
try:
|
|
198
421
|
if self.structured_output and self.output_variables:
|
|
199
422
|
# Handle structured output
|
|
200
|
-
struct_params =
|
|
201
|
-
key: {
|
|
202
|
-
"type": 'list[str]' if 'list' in value else value,
|
|
203
|
-
"description": ""
|
|
204
|
-
}
|
|
205
|
-
for key, value in (self.structured_output_dict or {}).items()
|
|
206
|
-
}
|
|
207
|
-
# Add default output field for proper response to user
|
|
208
|
-
struct_params['elitea_response'] = {
|
|
209
|
-
'description': 'final output to user (summarized output from LLM)', 'type': 'str',
|
|
210
|
-
"default": None}
|
|
423
|
+
struct_params = self._prepare_structured_output_params()
|
|
211
424
|
struct_model = create_pydantic_model(f"LLMOutput", struct_params)
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
425
|
+
|
|
426
|
+
try:
|
|
427
|
+
completion, initial_completion, final_messages = self._invoke_with_structured_output(
|
|
428
|
+
llm_client, messages, struct_model, config
|
|
216
429
|
)
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
try:
|
|
228
|
-
# Fallback to regular LLM with JSON extraction
|
|
229
|
-
completion = self.__get_struct_output_model(llm_client, struct_model,
|
|
230
|
-
method="json_mode").invoke(messages, config=config)
|
|
231
|
-
except (ValueError, OutputParserException) as e2:
|
|
232
|
-
logger.error(f"json_mode fallback also failed: {format_exc()}")
|
|
233
|
-
logger.info("Attempting to fall back to function_calling")
|
|
234
|
-
# Final fallback to function_calling method
|
|
235
|
-
completion = self.__get_struct_output_model(llm_client, struct_model,
|
|
236
|
-
method="json_schema").invoke(messages, config=config)
|
|
237
|
-
result = completion.model_dump()
|
|
238
|
-
|
|
239
|
-
# Ensure messages are properly formatted
|
|
240
|
-
if result.get('messages') and isinstance(result['messages'], list):
|
|
241
|
-
result['messages'] = [{'role': 'assistant', 'content': '\n'.join(result['messages'])}]
|
|
242
|
-
else:
|
|
243
|
-
# Extract content from initial_completion, handling thinking blocks
|
|
244
|
-
fallback_content = result.get(ELITEA_RS, '')
|
|
245
|
-
if not fallback_content and initial_completion:
|
|
246
|
-
content_parts = self._extract_content_from_completion(initial_completion)
|
|
247
|
-
fallback_content = content_parts.get('text') or ''
|
|
248
|
-
thinking = content_parts.get('thinking')
|
|
249
|
-
|
|
250
|
-
# Dispatch thinking event if present
|
|
251
|
-
if thinking:
|
|
252
|
-
try:
|
|
253
|
-
model_name = getattr(llm_client, 'model_name', None) or getattr(llm_client, 'model', 'LLM')
|
|
254
|
-
dispatch_custom_event(
|
|
255
|
-
name="thinking_step",
|
|
256
|
-
data={
|
|
257
|
-
"message": thinking,
|
|
258
|
-
"tool_name": f"LLM ({model_name})",
|
|
259
|
-
"toolkit": "reasoning",
|
|
260
|
-
},
|
|
261
|
-
config=config,
|
|
262
|
-
)
|
|
263
|
-
except Exception as e:
|
|
264
|
-
logger.warning(f"Failed to dispatch thinking event: {e}")
|
|
265
|
-
|
|
266
|
-
if not fallback_content:
|
|
267
|
-
# Final fallback to raw content
|
|
268
|
-
content = initial_completion.content
|
|
269
|
-
fallback_content = content if isinstance(content, str) else str(content)
|
|
270
|
-
|
|
271
|
-
result['messages'] = messages + [AIMessage(content=fallback_content)]
|
|
430
|
+
except ValueError as e:
|
|
431
|
+
# Handle fallback for structured output failures
|
|
432
|
+
completion = self._handle_structured_output_fallback(
|
|
433
|
+
llm_client, messages, struct_model, config, e
|
|
434
|
+
)
|
|
435
|
+
initial_completion = None
|
|
436
|
+
final_messages = messages
|
|
437
|
+
|
|
438
|
+
result = completion.model_dump()
|
|
439
|
+
result = self._format_structured_output_result(result, final_messages, initial_completion or completion)
|
|
272
440
|
|
|
273
441
|
return result
|
|
274
442
|
else:
|
alita_sdk/runtime/tools/loop.py
CHANGED
|
@@ -102,7 +102,9 @@ Input Data:
|
|
|
102
102
|
logger.debug(f"LoopNode input: {predict_input}")
|
|
103
103
|
completion = self.client.invoke(predict_input, config=config)
|
|
104
104
|
logger.debug(f"LoopNode pure output: {completion}")
|
|
105
|
-
|
|
105
|
+
from ..langchain.utils import extract_text_from_completion
|
|
106
|
+
content_text = extract_text_from_completion(completion)
|
|
107
|
+
loop_data = _old_extract_json(content_text.strip())
|
|
106
108
|
logger.debug(f"LoopNode output: {loop_data}")
|
|
107
109
|
if self.return_type == "str":
|
|
108
110
|
accumulated_response = ''
|
|
@@ -93,7 +93,9 @@ Answer must be JSON only extractable by JSON.LOADS."""
|
|
|
93
93
|
else:
|
|
94
94
|
input_[-1].content += self.unstructured_output
|
|
95
95
|
completion = self.client.invoke(input_, config=config)
|
|
96
|
-
|
|
96
|
+
from ..langchain.utils import extract_text_from_completion
|
|
97
|
+
content_text = extract_text_from_completion(completion)
|
|
98
|
+
result = _extract_json(content_text.strip())
|
|
97
99
|
try:
|
|
98
100
|
tool_result: dict | List[dict] = self.tool.invoke(result, config=config, kwargs=kwargs)
|
|
99
101
|
dispatch_custom_event(
|