lionagi 0.1.2__py3-none-any.whl → 0.2.1__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.
- lionagi/__init__.py +60 -5
- lionagi/core/__init__.py +0 -25
- lionagi/core/_setting/_setting.py +59 -0
- lionagi/core/action/__init__.py +14 -0
- lionagi/core/action/function_calling.py +136 -0
- lionagi/core/action/manual.py +1 -0
- lionagi/core/action/node.py +109 -0
- lionagi/core/action/tool.py +114 -0
- lionagi/core/action/tool_manager.py +356 -0
- lionagi/core/agent/base_agent.py +27 -13
- lionagi/core/agent/eval/evaluator.py +1 -0
- lionagi/core/agent/eval/vote.py +40 -0
- lionagi/core/agent/learn/learner.py +59 -0
- lionagi/core/agent/plan/unit_template.py +1 -0
- lionagi/core/collections/__init__.py +17 -0
- lionagi/core/{generic/data_logger.py → collections/_logger.py} +69 -55
- lionagi/core/collections/abc/__init__.py +53 -0
- lionagi/core/collections/abc/component.py +615 -0
- lionagi/core/collections/abc/concepts.py +297 -0
- lionagi/core/collections/abc/exceptions.py +150 -0
- lionagi/core/collections/abc/util.py +45 -0
- lionagi/core/collections/exchange.py +161 -0
- lionagi/core/collections/flow.py +426 -0
- lionagi/core/collections/model.py +419 -0
- lionagi/core/collections/pile.py +913 -0
- lionagi/core/collections/progression.py +236 -0
- lionagi/core/collections/util.py +64 -0
- lionagi/core/director/direct.py +314 -0
- lionagi/core/director/director.py +2 -0
- lionagi/core/{execute/branch_executor.py → engine/branch_engine.py} +134 -97
- lionagi/core/{execute/instruction_map_executor.py → engine/instruction_map_engine.py} +80 -55
- lionagi/{experimental/directive/evaluator → core/engine}/script_engine.py +17 -1
- lionagi/core/executor/base_executor.py +90 -0
- lionagi/core/{execute/structure_executor.py → executor/graph_executor.py} +62 -66
- lionagi/core/{execute → executor}/neo4j_executor.py +70 -67
- lionagi/core/generic/__init__.py +3 -33
- lionagi/core/generic/edge.py +29 -79
- lionagi/core/generic/edge_condition.py +16 -0
- lionagi/core/generic/graph.py +236 -0
- lionagi/core/generic/hyperedge.py +1 -0
- lionagi/core/generic/node.py +156 -221
- lionagi/core/generic/tree.py +48 -0
- lionagi/core/generic/tree_node.py +79 -0
- lionagi/core/mail/__init__.py +12 -0
- lionagi/core/mail/mail.py +25 -0
- lionagi/core/mail/mail_manager.py +139 -58
- lionagi/core/mail/package.py +45 -0
- lionagi/core/mail/start_mail.py +36 -0
- lionagi/core/message/__init__.py +19 -0
- lionagi/core/message/action_request.py +133 -0
- lionagi/core/message/action_response.py +135 -0
- lionagi/core/message/assistant_response.py +95 -0
- lionagi/core/message/instruction.py +234 -0
- lionagi/core/message/message.py +101 -0
- lionagi/core/message/system.py +86 -0
- lionagi/core/message/util.py +283 -0
- lionagi/core/report/__init__.py +4 -0
- lionagi/core/report/base.py +217 -0
- lionagi/core/report/form.py +231 -0
- lionagi/core/report/report.py +166 -0
- lionagi/core/report/util.py +28 -0
- lionagi/core/rule/_default.py +16 -0
- lionagi/core/rule/action.py +99 -0
- lionagi/core/rule/base.py +238 -0
- lionagi/core/rule/boolean.py +56 -0
- lionagi/core/rule/choice.py +47 -0
- lionagi/core/rule/mapping.py +96 -0
- lionagi/core/rule/number.py +71 -0
- lionagi/core/rule/rulebook.py +109 -0
- lionagi/core/rule/string.py +52 -0
- lionagi/core/rule/util.py +35 -0
- lionagi/core/session/branch.py +431 -0
- lionagi/core/session/directive_mixin.py +287 -0
- lionagi/core/session/session.py +229 -903
- lionagi/core/structure/__init__.py +1 -0
- lionagi/core/structure/chain.py +1 -0
- lionagi/core/structure/forest.py +1 -0
- lionagi/core/structure/graph.py +1 -0
- lionagi/core/structure/tree.py +1 -0
- lionagi/core/unit/__init__.py +5 -0
- lionagi/core/unit/parallel_unit.py +245 -0
- lionagi/core/unit/template/action.py +81 -0
- lionagi/core/unit/template/base.py +51 -0
- lionagi/core/unit/template/plan.py +84 -0
- lionagi/core/unit/template/predict.py +109 -0
- lionagi/core/unit/template/score.py +124 -0
- lionagi/core/unit/template/select.py +104 -0
- lionagi/core/unit/unit.py +362 -0
- lionagi/core/unit/unit_form.py +305 -0
- lionagi/core/unit/unit_mixin.py +1168 -0
- lionagi/core/unit/util.py +71 -0
- lionagi/core/validator/validator.py +364 -0
- lionagi/core/work/work.py +76 -0
- lionagi/core/work/work_function.py +101 -0
- lionagi/core/work/work_queue.py +103 -0
- lionagi/core/work/worker.py +258 -0
- lionagi/core/work/worklog.py +120 -0
- lionagi/experimental/compressor/base.py +46 -0
- lionagi/experimental/compressor/llm_compressor.py +247 -0
- lionagi/experimental/compressor/llm_summarizer.py +61 -0
- lionagi/experimental/compressor/util.py +70 -0
- lionagi/experimental/directive/__init__.py +19 -0
- lionagi/experimental/directive/parser/base_parser.py +69 -2
- lionagi/experimental/directive/{template_ → template}/base_template.py +17 -1
- lionagi/{libs/ln_tokenizer.py → experimental/directive/tokenizer.py} +16 -0
- lionagi/experimental/{directive/evaluator → evaluator}/ast_evaluator.py +16 -0
- lionagi/experimental/{directive/evaluator → evaluator}/base_evaluator.py +16 -0
- lionagi/experimental/knowledge/base.py +10 -0
- lionagi/experimental/memory/__init__.py +0 -0
- lionagi/experimental/strategies/__init__.py +0 -0
- lionagi/experimental/strategies/base.py +1 -0
- lionagi/integrations/bridge/langchain_/documents.py +4 -0
- lionagi/integrations/bridge/llamaindex_/index.py +30 -0
- lionagi/integrations/bridge/llamaindex_/llama_index_bridge.py +6 -0
- lionagi/integrations/chunker/chunk.py +161 -24
- lionagi/integrations/config/oai_configs.py +34 -3
- lionagi/integrations/config/openrouter_configs.py +14 -2
- lionagi/integrations/loader/load.py +122 -21
- lionagi/integrations/loader/load_util.py +6 -77
- lionagi/integrations/provider/_mapping.py +46 -0
- lionagi/integrations/provider/litellm.py +2 -1
- lionagi/integrations/provider/mlx_service.py +16 -9
- lionagi/integrations/provider/oai.py +91 -4
- lionagi/integrations/provider/ollama.py +6 -5
- lionagi/integrations/provider/openrouter.py +115 -8
- lionagi/integrations/provider/services.py +2 -2
- lionagi/integrations/provider/transformers.py +18 -22
- lionagi/integrations/storage/__init__.py +3 -3
- lionagi/integrations/storage/neo4j.py +52 -60
- lionagi/integrations/storage/storage_util.py +44 -46
- lionagi/integrations/storage/structure_excel.py +43 -26
- lionagi/integrations/storage/to_excel.py +11 -4
- lionagi/libs/__init__.py +22 -1
- lionagi/libs/ln_api.py +75 -20
- lionagi/libs/ln_context.py +37 -0
- lionagi/libs/ln_convert.py +21 -9
- lionagi/libs/ln_func_call.py +69 -28
- lionagi/libs/ln_image.py +107 -0
- lionagi/libs/ln_nested.py +26 -11
- lionagi/libs/ln_parse.py +82 -23
- lionagi/libs/ln_queue.py +16 -0
- lionagi/libs/ln_tokenize.py +164 -0
- lionagi/libs/ln_validate.py +16 -0
- lionagi/libs/special_tokens.py +172 -0
- lionagi/libs/sys_util.py +95 -24
- lionagi/lions/coder/code_form.py +13 -0
- lionagi/lions/coder/coder.py +50 -3
- lionagi/lions/coder/util.py +30 -25
- lionagi/tests/libs/test_func_call.py +23 -21
- lionagi/tests/libs/test_nested.py +36 -21
- lionagi/tests/libs/test_parse.py +1 -1
- lionagi/tests/test_core/collections/__init__.py +0 -0
- lionagi/tests/test_core/collections/test_component.py +206 -0
- lionagi/tests/test_core/collections/test_exchange.py +138 -0
- lionagi/tests/test_core/collections/test_flow.py +145 -0
- lionagi/tests/test_core/collections/test_pile.py +171 -0
- lionagi/tests/test_core/collections/test_progression.py +129 -0
- lionagi/tests/test_core/generic/test_edge.py +67 -0
- lionagi/tests/test_core/generic/test_graph.py +96 -0
- lionagi/tests/test_core/generic/test_node.py +106 -0
- lionagi/tests/test_core/generic/test_tree_node.py +73 -0
- lionagi/tests/test_core/test_branch.py +115 -294
- lionagi/tests/test_core/test_form.py +46 -0
- lionagi/tests/test_core/test_report.py +105 -0
- lionagi/tests/test_core/test_validator.py +111 -0
- lionagi/version.py +1 -1
- lionagi-0.2.1.dist-info/LICENSE +202 -0
- lionagi-0.2.1.dist-info/METADATA +272 -0
- lionagi-0.2.1.dist-info/RECORD +240 -0
- lionagi/core/branch/base.py +0 -653
- lionagi/core/branch/branch.py +0 -474
- lionagi/core/branch/flow_mixin.py +0 -96
- lionagi/core/branch/util.py +0 -323
- lionagi/core/direct/__init__.py +0 -19
- lionagi/core/direct/cot.py +0 -123
- lionagi/core/direct/plan.py +0 -164
- lionagi/core/direct/predict.py +0 -166
- lionagi/core/direct/react.py +0 -171
- lionagi/core/direct/score.py +0 -279
- lionagi/core/direct/select.py +0 -170
- lionagi/core/direct/sentiment.py +0 -1
- lionagi/core/direct/utils.py +0 -110
- lionagi/core/direct/vote.py +0 -64
- lionagi/core/execute/base_executor.py +0 -47
- lionagi/core/flow/baseflow.py +0 -23
- lionagi/core/flow/monoflow/ReAct.py +0 -240
- lionagi/core/flow/monoflow/__init__.py +0 -9
- lionagi/core/flow/monoflow/chat.py +0 -95
- lionagi/core/flow/monoflow/chat_mixin.py +0 -253
- lionagi/core/flow/monoflow/followup.py +0 -215
- lionagi/core/flow/polyflow/__init__.py +0 -1
- lionagi/core/flow/polyflow/chat.py +0 -251
- lionagi/core/form/action_form.py +0 -26
- lionagi/core/form/field_validator.py +0 -287
- lionagi/core/form/form.py +0 -302
- lionagi/core/form/mixin.py +0 -214
- lionagi/core/form/scored_form.py +0 -13
- lionagi/core/generic/action.py +0 -26
- lionagi/core/generic/component.py +0 -532
- lionagi/core/generic/condition.py +0 -46
- lionagi/core/generic/mail.py +0 -90
- lionagi/core/generic/mailbox.py +0 -36
- lionagi/core/generic/relation.py +0 -70
- lionagi/core/generic/signal.py +0 -22
- lionagi/core/generic/structure.py +0 -362
- lionagi/core/generic/transfer.py +0 -20
- lionagi/core/generic/work.py +0 -40
- lionagi/core/graph/graph.py +0 -126
- lionagi/core/graph/tree.py +0 -190
- lionagi/core/mail/schema.py +0 -63
- lionagi/core/messages/schema.py +0 -325
- lionagi/core/tool/__init__.py +0 -5
- lionagi/core/tool/tool.py +0 -28
- lionagi/core/tool/tool_manager.py +0 -283
- lionagi/experimental/report/form.py +0 -64
- lionagi/experimental/report/report.py +0 -138
- lionagi/experimental/report/util.py +0 -47
- lionagi/experimental/tool/function_calling.py +0 -43
- lionagi/experimental/tool/manual.py +0 -66
- lionagi/experimental/tool/schema.py +0 -59
- lionagi/experimental/tool/tool_manager.py +0 -138
- lionagi/experimental/tool/util.py +0 -16
- lionagi/experimental/validator/rule.py +0 -139
- lionagi/experimental/validator/validator.py +0 -56
- lionagi/experimental/work/__init__.py +0 -10
- lionagi/experimental/work/async_queue.py +0 -54
- lionagi/experimental/work/schema.py +0 -73
- lionagi/experimental/work/work_function.py +0 -67
- lionagi/experimental/work/worker.py +0 -56
- lionagi/experimental/work2/form.py +0 -371
- lionagi/experimental/work2/report.py +0 -289
- lionagi/experimental/work2/schema.py +0 -30
- lionagi/experimental/work2/tests.py +0 -72
- lionagi/experimental/work2/work_function.py +0 -89
- lionagi/experimental/work2/worker.py +0 -12
- lionagi/integrations/bridge/llamaindex_/get_index.py +0 -294
- lionagi/tests/test_core/generic/test_component.py +0 -89
- lionagi/tests/test_core/test_base_branch.py +0 -426
- lionagi/tests/test_core/test_chat_flow.py +0 -63
- lionagi/tests/test_core/test_mail_manager.py +0 -75
- lionagi/tests/test_core/test_prompts.py +0 -51
- lionagi/tests/test_core/test_session.py +0 -254
- lionagi/tests/test_core/test_session_base_util.py +0 -313
- lionagi/tests/test_core/test_tool_manager.py +0 -95
- lionagi-0.1.2.dist-info/LICENSE +0 -9
- lionagi-0.1.2.dist-info/METADATA +0 -174
- lionagi-0.1.2.dist-info/RECORD +0 -206
- /lionagi/core/{branch → _setting}/__init__.py +0 -0
- /lionagi/core/{execute → agent/eval}/__init__.py +0 -0
- /lionagi/core/{flow → agent/learn}/__init__.py +0 -0
- /lionagi/core/{form → agent/plan}/__init__.py +0 -0
- /lionagi/core/{branch/executable_branch.py → agent/plan/plan.py} +0 -0
- /lionagi/core/{graph → director}/__init__.py +0 -0
- /lionagi/core/{messages → engine}/__init__.py +0 -0
- /lionagi/{experimental/directive/evaluator → core/engine}/sandbox_.py +0 -0
- /lionagi/{experimental/directive/evaluator → core/executor}/__init__.py +0 -0
- /lionagi/{experimental/directive/template_ → core/rule}/__init__.py +0 -0
- /lionagi/{experimental/report → core/unit/template}/__init__.py +0 -0
- /lionagi/{experimental/tool → core/validator}/__init__.py +0 -0
- /lionagi/{experimental/validator → core/work}/__init__.py +0 -0
- /lionagi/experimental/{work2 → compressor}/__init__.py +0 -0
- /lionagi/{core/flow/mono_chat_mixin.py → experimental/directive/template/__init__.py} +0 -0
- /lionagi/experimental/directive/{schema.py → template/schema.py} +0 -0
- /lionagi/experimental/{work2/util.py → evaluator/__init__.py} +0 -0
- /lionagi/experimental/{work2/work.py → knowledge/__init__.py} +0 -0
- /lionagi/{tests/libs/test_async.py → experimental/knowledge/graph.py} +0 -0
- {lionagi-0.1.2.dist-info → lionagi-0.2.1.dist-info}/WHEEL +0 -0
- {lionagi-0.1.2.dist-info → lionagi-0.2.1.dist-info}/top_level.txt +0 -0
@@ -1,72 +0,0 @@
|
|
1
|
-
from .schema import Work, WorkStatus
|
2
|
-
from ..work.worklog import WorkLog
|
3
|
-
from .work_function import WorkFunction
|
4
|
-
|
5
|
-
import unittest
|
6
|
-
from unittest.mock import AsyncMock, patch
|
7
|
-
|
8
|
-
from lionagi.libs import func_call
|
9
|
-
|
10
|
-
|
11
|
-
class TestWork(unittest.TestCase):
|
12
|
-
def setUp(self):
|
13
|
-
self.work = Work(form_id="123")
|
14
|
-
|
15
|
-
def test_initial_status(self):
|
16
|
-
"""Test the initial status is set to PENDING."""
|
17
|
-
self.assertEqual(self.work.status, WorkStatus.PENDING)
|
18
|
-
|
19
|
-
def test_initial_deliverables(self):
|
20
|
-
"""Test the initial deliverables are empty."""
|
21
|
-
self.assertEqual(self.work.deliverables, {})
|
22
|
-
|
23
|
-
def test_initial_dependencies(self):
|
24
|
-
"""Test the initial dependencies are empty."""
|
25
|
-
self.assertEqual(self.work.dependencies, [])
|
26
|
-
|
27
|
-
|
28
|
-
class TestWorkLog(unittest.TestCase):
|
29
|
-
def setUp(self):
|
30
|
-
self.work_log = WorkLog()
|
31
|
-
self.work = Work(form_id="123")
|
32
|
-
self.work_log.append(self.work)
|
33
|
-
|
34
|
-
def test_append_work(self):
|
35
|
-
"""Test appending work adds to logs and pending queue."""
|
36
|
-
self.assertIn("123", self.work_log.logs)
|
37
|
-
self.assertIn("123", self.work_log.pending)
|
38
|
-
|
39
|
-
def test_get_by_status(self):
|
40
|
-
"""Test retrieving works by status."""
|
41
|
-
result = self.work_log.get_by_status(WorkStatus.PENDING)
|
42
|
-
self.assertEqual(result, {"123": self.work})
|
43
|
-
|
44
|
-
|
45
|
-
class TestWorkFunction(unittest.TestCase):
|
46
|
-
def setUp(self):
|
47
|
-
self.work_function = WorkFunction(function=AsyncMock(return_value="result"))
|
48
|
-
self.work = Work(form_id="123")
|
49
|
-
self.work_log = WorkLog()
|
50
|
-
self.work_log.append(self.work)
|
51
|
-
self.work_function.worklog = self.work_log
|
52
|
-
|
53
|
-
@patch("asyncio.sleep", new_callable=AsyncMock)
|
54
|
-
async def test_execute(self, mocked_sleep):
|
55
|
-
"""Test executing work changes its status and handles results."""
|
56
|
-
with patch.object(func_call, "rcall", new_callable=AsyncMock) as mock_rcall:
|
57
|
-
mock_rcall.return_value = "completed"
|
58
|
-
await self.work_function.execute()
|
59
|
-
self.assertEqual(self.work.status, WorkStatus.COMPLETED)
|
60
|
-
self.assertNotIn("123", self.work_function.worklog.pending)
|
61
|
-
|
62
|
-
@patch("asyncio.sleep", new_callable=AsyncMock)
|
63
|
-
async def test_execute_failure(self, mocked_sleep):
|
64
|
-
"""Test handling failure during work execution."""
|
65
|
-
with patch.object(func_call, "rcall", side_effect=Exception("Error")):
|
66
|
-
await self.work_function.execute()
|
67
|
-
self.assertEqual(self.work.status, WorkStatus.FAILED)
|
68
|
-
self.assertIn("123", self.work_function.worklog.errored)
|
69
|
-
|
70
|
-
|
71
|
-
if __name__ == "__main__":
|
72
|
-
unittest.main()
|
@@ -1,89 +0,0 @@
|
|
1
|
-
import asyncio
|
2
|
-
from typing import Any, Callable, Dict, List
|
3
|
-
from pydantic import Field
|
4
|
-
from functools import wraps
|
5
|
-
from lionagi import logging as _logging
|
6
|
-
from lionagi.libs import func_call
|
7
|
-
from lionagi.core.generic import BaseComponent
|
8
|
-
|
9
|
-
from .schema import Work, WorkStatus
|
10
|
-
from ..work.worklog import WorkLog
|
11
|
-
from .worker import Worker
|
12
|
-
|
13
|
-
|
14
|
-
class WorkFunction(BaseComponent):
|
15
|
-
"""Work function management and execution."""
|
16
|
-
|
17
|
-
function: Callable
|
18
|
-
args: List[Any] = Field(default_factory=list)
|
19
|
-
kwargs: Dict[str, Any] = Field(default_factory=dict)
|
20
|
-
retry_kwargs: Dict[str, Any] = Field(default_factory=dict)
|
21
|
-
worklog: WorkLog = Field(default_factory=WorkLog)
|
22
|
-
instruction: str = Field(
|
23
|
-
default="", description="Instruction for the work function"
|
24
|
-
)
|
25
|
-
refresh_time: float = Field(
|
26
|
-
default=0.5, description="Time to wait before checking for pending work"
|
27
|
-
)
|
28
|
-
|
29
|
-
@property
|
30
|
-
def name(self):
|
31
|
-
"""Get the name of the work function."""
|
32
|
-
return self.function.__name__
|
33
|
-
|
34
|
-
async def execute(self):
|
35
|
-
"""Execute pending work items."""
|
36
|
-
while self.worklog.pending:
|
37
|
-
work_id = self.worklog.pending.popleft()
|
38
|
-
work = self.worklog.logs[work_id]
|
39
|
-
if work.status == WorkStatus.PENDING:
|
40
|
-
try:
|
41
|
-
await func_call.rcall(self._execute, work, **work.retry_kwargs)
|
42
|
-
except Exception as e:
|
43
|
-
work.status = WorkStatus.FAILED
|
44
|
-
_logging.error(f"Work {work.id_} failed with error: {e}")
|
45
|
-
self.worklog.errored.append(work.id_)
|
46
|
-
else:
|
47
|
-
_logging.warning(
|
48
|
-
f"Work {work.id_} is in {work.status} state "
|
49
|
-
"and cannot be executed."
|
50
|
-
)
|
51
|
-
await asyncio.sleep(self.refresh_time)
|
52
|
-
|
53
|
-
async def _execute(self, work: Work):
|
54
|
-
"""Execute a single work item."""
|
55
|
-
work.status = WorkStatus.IN_PROGRESS
|
56
|
-
result = await self.function(*self.args, **self.kwargs)
|
57
|
-
work.deliverables = result
|
58
|
-
work.status = WorkStatus.COMPLETED
|
59
|
-
return result
|
60
|
-
|
61
|
-
|
62
|
-
def workfunc(func):
|
63
|
-
|
64
|
-
@wraps(func)
|
65
|
-
async def wrapper(self: Worker, *args, **kwargs):
|
66
|
-
# Retrieve the worker instance ('self')
|
67
|
-
if not hasattr(self, "work_functions"):
|
68
|
-
self.work_functions = {}
|
69
|
-
|
70
|
-
if func.__name__ not in self.work_functions:
|
71
|
-
# Create WorkFunction with the function and its docstring as instruction
|
72
|
-
self.work_functions[func.__name__] = WorkFunction(
|
73
|
-
function=func,
|
74
|
-
instruction=func.__doc__,
|
75
|
-
args=args,
|
76
|
-
kwargs=kwargs,
|
77
|
-
retry_kwargs=kwargs.pop("retry_kwargs", {}),
|
78
|
-
)
|
79
|
-
|
80
|
-
# Retrieve the existing WorkFunction
|
81
|
-
work_function: WorkFunction = self.work_functions[func.__name__]
|
82
|
-
# Update args and kwargs for this call
|
83
|
-
work_function.args = args
|
84
|
-
work_function.kwargs = kwargs
|
85
|
-
|
86
|
-
# Execute the function using WorkFunction's managed execution process
|
87
|
-
return await work_function.execute()
|
88
|
-
|
89
|
-
return wrapper
|
@@ -1,12 +0,0 @@
|
|
1
|
-
from abc import ABC
|
2
|
-
from pydantic import Field
|
3
|
-
from lionagi.core.generic import BaseComponent
|
4
|
-
|
5
|
-
|
6
|
-
class Worker(BaseComponent, ABC):
|
7
|
-
form_templates: dict = Field(
|
8
|
-
default={}, description="The form templates of the worker"
|
9
|
-
)
|
10
|
-
work_functions: dict = Field(
|
11
|
-
default={}, description="The work functions of the worker"
|
12
|
-
)
|
@@ -1,294 +0,0 @@
|
|
1
|
-
# TODO: Refactor this code to use the new llama_index API
|
2
|
-
|
3
|
-
# class BaseIndex:
|
4
|
-
|
5
|
-
# @staticmethod
|
6
|
-
# def _get_index(
|
7
|
-
# input_=None,
|
8
|
-
# # default to OpenAI
|
9
|
-
# llm=None,
|
10
|
-
# llm_provider=None,
|
11
|
-
# llm_kwargs={},
|
12
|
-
# service_context=None,
|
13
|
-
# service_context_kwargs={},
|
14
|
-
# index_type=None,
|
15
|
-
# index_kwargs={},
|
16
|
-
# rerank_=False,
|
17
|
-
# reranker_type=None,
|
18
|
-
# reranker=None,
|
19
|
-
# rerank_kwargs={},
|
20
|
-
# get_engine=False,
|
21
|
-
# engine_kwargs={},
|
22
|
-
# from_storage=False,
|
23
|
-
# storage_context=None,
|
24
|
-
# strorage_context_kwargs={},
|
25
|
-
# index_id=None,
|
26
|
-
# load_index_from_storage_kwargs={},
|
27
|
-
# ):
|
28
|
-
# """
|
29
|
-
# Creates and returns an index or query engine based on the provided parameters.
|
30
|
-
|
31
|
-
# Args:
|
32
|
-
# chunks: The input data to be indexed or queried.
|
33
|
-
# llm: An instance of a language model for indexing or querying.
|
34
|
-
# llm_provider: A function to provide an instance of a language model.
|
35
|
-
# llm_kwargs: Keyword arguments for configuring the language model.
|
36
|
-
# service_context: An instance of a service context.
|
37
|
-
# service_context_kwargs: Keyword arguments for configuring the service context.
|
38
|
-
# index_type: The type of index to create.
|
39
|
-
# index_kwargs: Keyword arguments for configuring the index.
|
40
|
-
# rerank_: Boolean flag indicating whether reranking should be applied.
|
41
|
-
# reranker_type: The type of reranker to use.
|
42
|
-
# reranker: An instance of a reranker.
|
43
|
-
# rerank_kwargs: Keyword arguments for configuring the reranker.
|
44
|
-
# get_engine: Boolean flag indicating whether to return a query engine.
|
45
|
-
# engine_kwargs: Keyword arguments for configuring the query engine.
|
46
|
-
|
47
|
-
# Returns:
|
48
|
-
# Index or Query Engine: Depending on the 'get_engine' flag, returns an index or query engine.
|
49
|
-
|
50
|
-
# Raises:
|
51
|
-
# Various exceptions if there are errors in creating the index or query engine.
|
52
|
-
# """
|
53
|
-
|
54
|
-
# if from_storage:
|
55
|
-
# from llama_index import StorageContext, load_index_from_storage
|
56
|
-
|
57
|
-
# storage_context = StorageContext.from_defaults(**strorage_context_kwargs)
|
58
|
-
|
59
|
-
# if index_id:
|
60
|
-
# index = load_index_from_storage(
|
61
|
-
# storage_context=storage_context,
|
62
|
-
# index_id=index_id,
|
63
|
-
# **load_index_from_storage_kwargs,
|
64
|
-
# )
|
65
|
-
# else:
|
66
|
-
# raise ValueError("Index ID is required for loading from storage.")
|
67
|
-
|
68
|
-
# if rerank_:
|
69
|
-
# if not reranker:
|
70
|
-
# if not reranker_type:
|
71
|
-
# from llama_index.postprocessor import LLMRerank
|
72
|
-
|
73
|
-
# reranker_type = LLMRerank
|
74
|
-
# reranker = reranker_type(
|
75
|
-
# service_context=service_context, **rerank_kwargs
|
76
|
-
# )
|
77
|
-
# engine_kwargs.update({"node_postprocessors": [reranker]})
|
78
|
-
|
79
|
-
# if get_engine:
|
80
|
-
# return (index, index.as_query_engine(**engine_kwargs))
|
81
|
-
# return index
|
82
|
-
|
83
|
-
# if not llm:
|
84
|
-
# if llm_provider:
|
85
|
-
# llm = llm_provider(**llm_kwargs)
|
86
|
-
# else:
|
87
|
-
# from llama_index.llms import OpenAI
|
88
|
-
|
89
|
-
# llm = OpenAI(**llm_kwargs)
|
90
|
-
|
91
|
-
# if not service_context:
|
92
|
-
# from llama_index import ServiceContext
|
93
|
-
|
94
|
-
# service_context = ServiceContext.from_defaults(
|
95
|
-
# llm=llm, **service_context_kwargs
|
96
|
-
# )
|
97
|
-
|
98
|
-
# if not index_type:
|
99
|
-
# from llama_index import VectorStoreIndex
|
100
|
-
|
101
|
-
# index_type = VectorStoreIndex
|
102
|
-
|
103
|
-
# index = index_type(input_, service_context=service_context, **index_kwargs)
|
104
|
-
|
105
|
-
# if index_id:
|
106
|
-
# index.index_id = index_id
|
107
|
-
|
108
|
-
# if rerank_:
|
109
|
-
# if not reranker:
|
110
|
-
# if not reranker_type:
|
111
|
-
# from llama_index.postprocessor import LLMRerank
|
112
|
-
|
113
|
-
# reranker_type = LLMRerank
|
114
|
-
# reranker = reranker_type(
|
115
|
-
# service_context=service_context, **rerank_kwargs
|
116
|
-
# )
|
117
|
-
# engine_kwargs.update({"node_postprocessors": [reranker]})
|
118
|
-
|
119
|
-
# if get_engine:
|
120
|
-
# return (index, index.as_query_engine(**engine_kwargs))
|
121
|
-
# return index
|
122
|
-
|
123
|
-
|
124
|
-
# class LlamaIndex:
|
125
|
-
|
126
|
-
# @staticmethod
|
127
|
-
# def kg_index(
|
128
|
-
# input_=None,
|
129
|
-
# # default to OpenAI
|
130
|
-
# llm=None,
|
131
|
-
# llm_provider=None,
|
132
|
-
# llm_kwargs={"temperature": 0.1, "model": "gpt-4-1106-preview"},
|
133
|
-
# service_context=None,
|
134
|
-
# service_context_kwargs={},
|
135
|
-
# index_kwargs={"include_embeddings": True},
|
136
|
-
# rerank_=False,
|
137
|
-
# reranker_type=None,
|
138
|
-
# reranker=None,
|
139
|
-
# rerank_kwargs={"choice_batch_size": 5, "top_n": 3},
|
140
|
-
# get_engine=False,
|
141
|
-
# engine_kwargs={"similarity_top_k": 3, "response_mode": "tree_summarize"},
|
142
|
-
# kg_triplet_extract_fn=None,
|
143
|
-
# from_storage=False,
|
144
|
-
# storage_context=None,
|
145
|
-
# strorage_context_kwargs={},
|
146
|
-
# index_id=None,
|
147
|
-
# load_index_from_storage_kwargs={},
|
148
|
-
# ):
|
149
|
-
# """
|
150
|
-
# Creates and returns a KnowledgeGraphIndex based on the provided parameters.
|
151
|
-
|
152
|
-
# Args:
|
153
|
-
# chunks: The input data to be indexed.
|
154
|
-
# llm: An instance of a language model for indexing.
|
155
|
-
# llm_provider: A function to provide an instance of a language model.
|
156
|
-
# llm_kwargs: Keyword arguments for configuring the language model.
|
157
|
-
# service_context: An instance of a service context.
|
158
|
-
# service_context_kwargs: Keyword arguments for configuring the service context.
|
159
|
-
# index_kwargs: Keyword arguments for configuring the index.
|
160
|
-
# rerank_: Boolean flag indicating whether reranking should be applied.
|
161
|
-
# reranker_type: The type of reranker to use.
|
162
|
-
# reranker: An instance of a reranker.
|
163
|
-
# rerank_kwargs: Keyword arguments for configuring the reranker.
|
164
|
-
# get_engine: Boolean flag indicating whether to return a query engine.
|
165
|
-
# engine_kwargs: Keyword arguments for configuring the query engine.
|
166
|
-
# kg_triplet_extract_fn: Optional function for extracting KG triplets.
|
167
|
-
|
168
|
-
# Returns:
|
169
|
-
# KnowledgeGraphIndex or Query Engine: Depending on the 'get_engine' flag,
|
170
|
-
# returns a KnowledgeGraphIndex or query engine.
|
171
|
-
|
172
|
-
# Raises:
|
173
|
-
# Various exceptions if there are errors in creating the index or query engine.
|
174
|
-
# """
|
175
|
-
# from llama_index import KnowledgeGraphIndex
|
176
|
-
|
177
|
-
# index_type_ = ""
|
178
|
-
# if not from_storage:
|
179
|
-
# from llama_index.graph_stores import SimpleGraphStore
|
180
|
-
# from llama_index.storage.storage_context import StorageContext
|
181
|
-
|
182
|
-
# graph_store = SimpleGraphStore()
|
183
|
-
# if storage_context is None:
|
184
|
-
# storage_context = StorageContext.from_defaults(
|
185
|
-
# graph_store=graph_store, **strorage_context_kwargs
|
186
|
-
# )
|
187
|
-
# index_kwargs.update({"storage_context": storage_context})
|
188
|
-
# index_type_ = KnowledgeGraphIndex.from_documents
|
189
|
-
|
190
|
-
# elif from_storage:
|
191
|
-
# index_type_ = KnowledgeGraphIndex
|
192
|
-
|
193
|
-
# if kg_triplet_extract_fn:
|
194
|
-
# index_kwargs.update({"kg_triplet_extract_fn": kg_triplet_extract_fn})
|
195
|
-
|
196
|
-
# if storage_context is None:
|
197
|
-
# from llama_index.graph_stores import SimpleGraphStore
|
198
|
-
# from llama_index.storage.storage_context import StorageContext
|
199
|
-
|
200
|
-
# storage_context = StorageContext.from_defaults(
|
201
|
-
# graph_store=SimpleGraphStore(), **strorage_context_kwargs
|
202
|
-
# )
|
203
|
-
|
204
|
-
# return BaseIndex._get_index(
|
205
|
-
# input_=input_,
|
206
|
-
# llm=llm,
|
207
|
-
# llm_provider=llm_provider,
|
208
|
-
# llm_kwargs=llm_kwargs,
|
209
|
-
# service_context=service_context,
|
210
|
-
# service_context_kwargs=service_context_kwargs,
|
211
|
-
# index_type=index_type_,
|
212
|
-
# index_kwargs=index_kwargs,
|
213
|
-
# rerank_=rerank_,
|
214
|
-
# reranker_type=reranker_type,
|
215
|
-
# reranker=reranker,
|
216
|
-
# rerank_kwargs=rerank_kwargs,
|
217
|
-
# get_engine=get_engine,
|
218
|
-
# engine_kwargs=engine_kwargs,
|
219
|
-
# from_storage=from_storage,
|
220
|
-
# storage_context=storage_context,
|
221
|
-
# strorage_context_kwargs=strorage_context_kwargs,
|
222
|
-
# index_id=index_id,
|
223
|
-
# load_index_from_storage_kwargs=load_index_from_storage_kwargs,
|
224
|
-
# )
|
225
|
-
|
226
|
-
# @staticmethod
|
227
|
-
# def vector_index(
|
228
|
-
# input_=None,
|
229
|
-
# # default to OpenAI
|
230
|
-
# llm=None,
|
231
|
-
# llm_provider=None,
|
232
|
-
# llm_kwargs={"temperature": 0.1, "model": "gpt-4-1106-preview"},
|
233
|
-
# service_context=None,
|
234
|
-
# service_context_kwargs={},
|
235
|
-
# index_kwargs={"include_embeddings": True},
|
236
|
-
# # default to LLMRerank
|
237
|
-
# rerank_=False,
|
238
|
-
# reranker_type=None,
|
239
|
-
# reranker=None,
|
240
|
-
# rerank_kwargs={"choice_batch_size": 5, "top_n": 3},
|
241
|
-
# get_engine=False,
|
242
|
-
# engine_kwargs={"similarity_top_k": 3, "response_mode": "tree_summarize"},
|
243
|
-
# from_storage=False,
|
244
|
-
# storage_context=None,
|
245
|
-
# strorage_context_kwargs={},
|
246
|
-
# index_id=None,
|
247
|
-
# load_index_from_storage_kwargs={},
|
248
|
-
# ):
|
249
|
-
# """
|
250
|
-
# Creates and returns a vector index or query engine based on the provided parameters.
|
251
|
-
|
252
|
-
# Args:
|
253
|
-
# chunks: The input data to be indexed or queried.
|
254
|
-
# llm: An instance of a language model for indexing or querying.
|
255
|
-
# llm_provider: A function to provide an instance of a language model.
|
256
|
-
# llm_kwargs: Keyword arguments for configuring the language model.
|
257
|
-
# service_context: An instance of a service context.
|
258
|
-
# service_context_kwargs: Keyword arguments for configuring the service context.
|
259
|
-
# index_kwargs: Keyword arguments for configuring the index.
|
260
|
-
# rerank_: Boolean flag indicating whether reranking should be applied.
|
261
|
-
# reranker_type: The type of reranker to use.
|
262
|
-
# reranker: An instance of a reranker.
|
263
|
-
# rerank_kwargs: Keyword arguments for configuring the reranker.
|
264
|
-
# get_engine: Boolean flag indicating whether to return a query engine.
|
265
|
-
# engine_kwargs: Keyword arguments for configuring the query engine.
|
266
|
-
|
267
|
-
# Returns:
|
268
|
-
# Vector Index or Query Engine: Depending on the 'get_engine' flag,
|
269
|
-
# returns a vector index or query engine.
|
270
|
-
|
271
|
-
# Raises:
|
272
|
-
# Various exceptions if there are errors in creating the index or query engine.
|
273
|
-
# """
|
274
|
-
|
275
|
-
# return BaseIndex._get_index(
|
276
|
-
# input_=input_,
|
277
|
-
# llm=llm,
|
278
|
-
# llm_provider=llm_provider,
|
279
|
-
# llm_kwargs=llm_kwargs,
|
280
|
-
# service_context=service_context,
|
281
|
-
# service_context_kwargs=service_context_kwargs,
|
282
|
-
# index_kwargs=index_kwargs,
|
283
|
-
# rerank_=rerank_,
|
284
|
-
# reranker_type=reranker_type,
|
285
|
-
# reranker=reranker,
|
286
|
-
# rerank_kwargs=rerank_kwargs,
|
287
|
-
# get_engine=get_engine,
|
288
|
-
# engine_kwargs=engine_kwargs,
|
289
|
-
# from_storage=from_storage,
|
290
|
-
# storage_context=storage_context,
|
291
|
-
# strorage_context_kwargs=strorage_context_kwargs,
|
292
|
-
# index_id=index_id,
|
293
|
-
# load_index_from_storage_kwargs=load_index_from_storage_kwargs,
|
294
|
-
# )
|
@@ -1,89 +0,0 @@
|
|
1
|
-
import unittest
|
2
|
-
from pydantic import BaseModel
|
3
|
-
from pandas import Series
|
4
|
-
import pandas as pd
|
5
|
-
from lionagi.core.generic.component import BaseNode
|
6
|
-
|
7
|
-
class TestBaseNode(unittest.TestCase):
|
8
|
-
|
9
|
-
def test_basic_properties(self):
|
10
|
-
node = BaseNode(content="Hello World")
|
11
|
-
self.assertIsInstance(node.id_, str)
|
12
|
-
self.assertEqual(len(node.id_), 32)
|
13
|
-
self.assertEqual(node.content, "Hello World")
|
14
|
-
self.assertEqual(node.metadata, {})
|
15
|
-
|
16
|
-
def test_serialization_json(self):
|
17
|
-
node = BaseNode(content="Hello World")
|
18
|
-
self.assertIn('id_', node.to_json_str())
|
19
|
-
|
20
|
-
def test_serialization_dict(self):
|
21
|
-
node = BaseNode(content="Hello World")
|
22
|
-
result = node.to_dict()
|
23
|
-
self.assertIn('id_', result)
|
24
|
-
self.assertEqual(result['content'], "Hello World")
|
25
|
-
|
26
|
-
def test_serialization_xml(self):
|
27
|
-
node = BaseNode(content="Hello World")
|
28
|
-
xml_str = node.to_xml()
|
29
|
-
self.assertIn('<BaseNode>', xml_str)
|
30
|
-
self.assertIn('</BaseNode>', xml_str)
|
31
|
-
|
32
|
-
def test_conversion_to_pandas_series(self):
|
33
|
-
node = BaseNode(content="Hello World")
|
34
|
-
series = node.to_pd_series()
|
35
|
-
self.assertIsInstance(series, Series)
|
36
|
-
self.assertEqual(series['content'], "Hello World")
|
37
|
-
|
38
|
-
def test_field_management(self):
|
39
|
-
node = BaseNode(content="Hello World")
|
40
|
-
node._add_field("new_field", annotation=str, default="default value")
|
41
|
-
self.assertIn('new_field', node._field_annotations)
|
42
|
-
self.assertEqual(getattr(node, 'new_field', None), "default value")
|
43
|
-
|
44
|
-
def test_creation_from_dict(self):
|
45
|
-
data = {"a": 1, "b": 2}
|
46
|
-
node = BaseNode.from_obj(data)
|
47
|
-
self.assertIn('a', node.to_dict())
|
48
|
-
|
49
|
-
def test_creation_from_json_str(self):
|
50
|
-
json_str = '{"a": 1, "b": 2}'
|
51
|
-
node = BaseNode.from_obj(json_str)
|
52
|
-
self.assertIn('a', node.to_dict())
|
53
|
-
|
54
|
-
def test_creation_from_fuzzy_json_str(self):
|
55
|
-
json_str = '{"name": "John", "age": 30, "city": ["New York", "DC", "LA"]'
|
56
|
-
node = BaseNode.from_obj(json_str, fuzzy_parse=True)
|
57
|
-
self.assertIn('name', node.to_dict())
|
58
|
-
|
59
|
-
def test_creation_from_pandas_series(self):
|
60
|
-
series = pd.Series({"a": 1, "b": 2})
|
61
|
-
node = BaseNode.from_obj(series)
|
62
|
-
self.assertIn('a', node.to_dict())
|
63
|
-
|
64
|
-
def test_creation_from_pandas_dataframe(self):
|
65
|
-
df = pd.DataFrame({"a": [1, 2], "b": [3, 4]}, index=["row1", "row2"])
|
66
|
-
nodes = BaseNode.from_obj(df)
|
67
|
-
self.assertIsInstance(nodes, list)
|
68
|
-
self.assertEqual(len(nodes), 2)
|
69
|
-
|
70
|
-
def test_creation_from_pydantic_model(self):
|
71
|
-
class CustomModel(BaseModel):
|
72
|
-
a: int
|
73
|
-
b: str
|
74
|
-
|
75
|
-
custom_model = CustomModel(a=1, b="hello")
|
76
|
-
node = BaseNode.from_obj(custom_model)
|
77
|
-
self.assertIn('a', node.to_dict())
|
78
|
-
|
79
|
-
def test_metadata_manipulation(self):
|
80
|
-
node = BaseNode()
|
81
|
-
node.meta_insert("key", "value")
|
82
|
-
self.assertEqual(node.metadata['key'], "value")
|
83
|
-
node.meta_change_key("key", "new_key")
|
84
|
-
self.assertIn("new_key", node.metadata)
|
85
|
-
node.meta_merge({"additional_key": "additional_value"})
|
86
|
-
self.assertIn("additional_key", node.metadata)
|
87
|
-
|
88
|
-
if __name__ == '__main__':
|
89
|
-
unittest.main()
|