lionagi 0.1.2__py3-none-any.whl → 0.2.0__py3-none-any.whl
Sign up to get free protection for your applications and to get access to all the features.
- 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 +74 -0
- lionagi/core/work/work_function.py +92 -0
- lionagi/core/work/work_queue.py +81 -0
- lionagi/core/work/worker.py +195 -0
- lionagi/core/work/worklog.py +124 -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.0.dist-info/LICENSE +202 -0
- lionagi-0.2.0.dist-info/METADATA +272 -0
- lionagi-0.2.0.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.0.dist-info}/WHEEL +0 -0
- {lionagi-0.1.2.dist-info → lionagi-0.2.0.dist-info}/top_level.txt +0 -0
@@ -1,95 +0,0 @@
|
|
1
|
-
"""
|
2
|
-
This module contains the MonoChat class for performing chat conversations with an LLM.
|
3
|
-
|
4
|
-
The MonoChat class allows for processing instructions and system messages, and optionally invoking tools
|
5
|
-
during the chat conversation. It extends the BaseMonoFlow and MonoChatMixin classes.
|
6
|
-
"""
|
7
|
-
|
8
|
-
from typing import Any
|
9
|
-
|
10
|
-
from lionagi.core.flow.baseflow import BaseMonoFlow
|
11
|
-
from lionagi.core.flow.monoflow.chat_mixin import MonoChatMixin
|
12
|
-
|
13
|
-
|
14
|
-
class MonoChat(BaseMonoFlow, MonoChatMixin):
|
15
|
-
"""
|
16
|
-
A class for performing a chat conversation with an LLM, processing instructions and system messages,
|
17
|
-
and optionally invoking tools.
|
18
|
-
|
19
|
-
Attributes:
|
20
|
-
branch: The Branch instance to perform chat operations.
|
21
|
-
|
22
|
-
Methods:
|
23
|
-
__init__(self, branch) -> None:
|
24
|
-
Initializes the MonoChat instance.
|
25
|
-
|
26
|
-
async chat(self, instruction=None, context=None, sender=None, system=None, tools=False,
|
27
|
-
out=True, invoke=True, output_fields=None, form=None, **kwargs) -> Any:
|
28
|
-
Performs a chat conversation with an LLM, processing instructions and system messages,
|
29
|
-
and optionally invoking tools.
|
30
|
-
"""
|
31
|
-
|
32
|
-
def __init__(self, branch) -> None:
|
33
|
-
"""
|
34
|
-
Initializes the MonoChat instance.
|
35
|
-
|
36
|
-
Args:
|
37
|
-
branch: The Branch instance to perform chat operations.
|
38
|
-
"""
|
39
|
-
super().__init__(branch)
|
40
|
-
|
41
|
-
async def chat(
|
42
|
-
self,
|
43
|
-
instruction=None,
|
44
|
-
context=None,
|
45
|
-
sender=None,
|
46
|
-
system=None,
|
47
|
-
tools=False,
|
48
|
-
out: bool = True,
|
49
|
-
invoke: bool = True,
|
50
|
-
output_fields=None,
|
51
|
-
form=None,
|
52
|
-
**kwargs,
|
53
|
-
) -> Any:
|
54
|
-
"""
|
55
|
-
Performs a chat conversation with an LLM, processing instructions and system messages,
|
56
|
-
and optionally invoking tools.
|
57
|
-
|
58
|
-
Args:
|
59
|
-
instruction (Union[Instruction, str]): The instruction for the chat.
|
60
|
-
context (Optional[Any]): Additional context for the chat.
|
61
|
-
sender (Optional[str]): The sender of the chat message.
|
62
|
-
system (Optional[Union[System, str, Dict[str, Any]]]): System message to be processed.
|
63
|
-
tools (Union[bool, Tool, List[Tool], str, List[str]]): Specifies tools to be invoked.
|
64
|
-
out (bool): If True, outputs the chat response.
|
65
|
-
invoke (bool): If True, invokes tools as part of the chat.
|
66
|
-
output_fields (Optional[Any]): The output fields for the chat.
|
67
|
-
form (Optional[Any]): The prompt template for the chat.
|
68
|
-
**kwargs: Arbitrary keyword arguments for chat completion.
|
69
|
-
|
70
|
-
Returns:
|
71
|
-
Any: The result of the chat conversation.
|
72
|
-
|
73
|
-
Examples:
|
74
|
-
>>> await MonoChat.chat(branch, "Ask about user preferences")
|
75
|
-
"""
|
76
|
-
|
77
|
-
config = self._create_chat_config(
|
78
|
-
instruction=instruction,
|
79
|
-
context=context,
|
80
|
-
sender=sender,
|
81
|
-
system=system,
|
82
|
-
form=form,
|
83
|
-
tools=tools,
|
84
|
-
output_fields=output_fields,
|
85
|
-
**kwargs,
|
86
|
-
)
|
87
|
-
|
88
|
-
await self._call_chatcompletion(**config)
|
89
|
-
|
90
|
-
return await self._output(
|
91
|
-
invoke=invoke,
|
92
|
-
out=out,
|
93
|
-
output_fields=output_fields,
|
94
|
-
form=form,
|
95
|
-
)
|
@@ -1,253 +0,0 @@
|
|
1
|
-
"""
|
2
|
-
This module contains mixins for configuring and invoking chatbots.
|
3
|
-
"""
|
4
|
-
|
5
|
-
from abc import ABC
|
6
|
-
from typing import Any
|
7
|
-
|
8
|
-
import re
|
9
|
-
from lionagi.libs import nested, func_call, convert, StringMatch, ParseUtil
|
10
|
-
from lionagi.core.tool.tool import TOOL_TYPE
|
11
|
-
from lionagi.core.messages.schema import Instruction
|
12
|
-
|
13
|
-
|
14
|
-
class MonoChatConfigMixin(ABC):
|
15
|
-
"""
|
16
|
-
Mixin class for configuring chatbots.
|
17
|
-
|
18
|
-
Methods:
|
19
|
-
_create_chat_config(self, instruction=None, context=None, sender=None, system=None,
|
20
|
-
output_fields=None, form=None, tools=False, **kwargs) -> Any:
|
21
|
-
Creates a chat configuration based on the provided parameters.
|
22
|
-
"""
|
23
|
-
|
24
|
-
def _create_chat_config(
|
25
|
-
self,
|
26
|
-
instruction: Instruction | str | dict[str, Any] = None,
|
27
|
-
context: Any | None = None,
|
28
|
-
sender: str | None = None,
|
29
|
-
system: str | dict[str, Any] | None = None,
|
30
|
-
output_fields=None,
|
31
|
-
form=None,
|
32
|
-
tools: TOOL_TYPE = False,
|
33
|
-
**kwargs,
|
34
|
-
) -> Any:
|
35
|
-
"""
|
36
|
-
Creates a chat configuration based on the provided parameters.
|
37
|
-
|
38
|
-
Args:
|
39
|
-
instruction (Instruction | str | dict[str, Any]): The instruction for the chatbot (optional).
|
40
|
-
context (Any): The context for the chatbot (optional).
|
41
|
-
sender (str): The sender of the message (optional).
|
42
|
-
system (str | dict[str, Any]): The system message for the chatbot (optional).
|
43
|
-
output_fields: The output fields for the chatbot (optional).
|
44
|
-
form: The prompt template for the chatbot (optional).
|
45
|
-
tools (TOOL_TYPE): The tools for the chatbot (default: False).
|
46
|
-
**kwargs: Additional keyword arguments for the chat configuration.
|
47
|
-
|
48
|
-
Returns:
|
49
|
-
Any: The chat configuration.
|
50
|
-
"""
|
51
|
-
if system:
|
52
|
-
self.branch.change_first_system_message(system)
|
53
|
-
|
54
|
-
if not form:
|
55
|
-
self.branch.add_message(
|
56
|
-
instruction=instruction,
|
57
|
-
context=context,
|
58
|
-
sender=sender,
|
59
|
-
output_fields=output_fields,
|
60
|
-
)
|
61
|
-
else:
|
62
|
-
instruct_ = Instruction.from_form(form)
|
63
|
-
self.branch.add_message(instruction=instruct_)
|
64
|
-
|
65
|
-
if "tool_parsed" in kwargs:
|
66
|
-
kwargs.pop("tool_parsed")
|
67
|
-
tool_kwarg = {"tools": tools}
|
68
|
-
kwargs = tool_kwarg | kwargs
|
69
|
-
elif tools and self.branch.has_tools:
|
70
|
-
kwargs = self.branch.tool_manager.parse_tool(tools=tools, **kwargs)
|
71
|
-
|
72
|
-
config = {**self.branch.llmconfig, **kwargs}
|
73
|
-
if sender is not None:
|
74
|
-
config["sender"] = sender
|
75
|
-
|
76
|
-
return config
|
77
|
-
|
78
|
-
|
79
|
-
class MonoChatInvokeMixin(ABC):
|
80
|
-
"""
|
81
|
-
Mixin class for invoking chatbots.
|
82
|
-
|
83
|
-
Methods:
|
84
|
-
async _output(self, invoke, out, output_fields, func_calls_=None, form=None,
|
85
|
-
return_template=True):
|
86
|
-
Processes the output of the chatbot.
|
87
|
-
|
88
|
-
_return_response(content_, output_fields) -> Any:
|
89
|
-
Returns the response from the chatbot.
|
90
|
-
|
91
|
-
async _invoke_tools(self, content_=None, func_calls_=None):
|
92
|
-
Invokes the tools associated with the chatbot.
|
93
|
-
|
94
|
-
_process_chatcompletion(self, payload, completion, sender):
|
95
|
-
Processes the chat completion.
|
96
|
-
|
97
|
-
async _call_chatcompletion(self, sender=None, with_sender=False, **kwargs):
|
98
|
-
Calls the chat completion API.
|
99
|
-
"""
|
100
|
-
|
101
|
-
async def _output(
|
102
|
-
self,
|
103
|
-
invoke,
|
104
|
-
out,
|
105
|
-
output_fields,
|
106
|
-
func_calls_=None,
|
107
|
-
form=None,
|
108
|
-
return_template=True,
|
109
|
-
):
|
110
|
-
"""
|
111
|
-
Processes the output of the chatbot.
|
112
|
-
|
113
|
-
Args:
|
114
|
-
invoke: Flag indicating whether to invoke the tools.
|
115
|
-
out: Flag indicating whether to return the output.
|
116
|
-
output_fields: The output fields for the chatbot.
|
117
|
-
func_calls_: The function calls for invoking the tools (optional).
|
118
|
-
form: The prompt template for the chatbot (optional).
|
119
|
-
return_template (bool): Flag indicating whether to return the prompt template (default: True).
|
120
|
-
"""
|
121
|
-
|
122
|
-
content_ = self.branch.last_message_content
|
123
|
-
|
124
|
-
if invoke:
|
125
|
-
try:
|
126
|
-
await self._invoke_tools(content_, func_calls_=func_calls_)
|
127
|
-
except Exception:
|
128
|
-
pass
|
129
|
-
|
130
|
-
response_ = self._return_response(content_, output_fields)
|
131
|
-
|
132
|
-
if form:
|
133
|
-
form._process_response(response_)
|
134
|
-
return form if return_template else form.outputs
|
135
|
-
|
136
|
-
if out:
|
137
|
-
return response_
|
138
|
-
|
139
|
-
@staticmethod
|
140
|
-
def _return_response(content_, output_fields):
|
141
|
-
"""
|
142
|
-
Returns the response from the chatbot.
|
143
|
-
|
144
|
-
Args:
|
145
|
-
content_: The content of the last message.
|
146
|
-
output_fields: The output fields for the chatbot.
|
147
|
-
|
148
|
-
Returns:
|
149
|
-
Any: The response from the chatbot.
|
150
|
-
"""
|
151
|
-
out_ = ""
|
152
|
-
|
153
|
-
if len(content_.items()) == 1 and len(nested.get_flattened_keys(content_)) == 1:
|
154
|
-
key = nested.get_flattened_keys(content_)[0]
|
155
|
-
out_ = content_[key]
|
156
|
-
|
157
|
-
if output_fields:
|
158
|
-
try:
|
159
|
-
return StringMatch.force_validate_dict(
|
160
|
-
out_, keys=list(output_fields.keys())
|
161
|
-
)
|
162
|
-
except Exception:
|
163
|
-
pass
|
164
|
-
|
165
|
-
if isinstance(out_, str):
|
166
|
-
try:
|
167
|
-
match = re.search(r"```json\n({.*?})\n```", out_, re.DOTALL)
|
168
|
-
if match:
|
169
|
-
out_ = ParseUtil.fuzzy_parse_json(match.group(1))
|
170
|
-
except Exception:
|
171
|
-
pass
|
172
|
-
|
173
|
-
return out_
|
174
|
-
|
175
|
-
async def _invoke_tools(self, content_=None, func_calls_=None):
|
176
|
-
"""
|
177
|
-
Invokes the tools associated with the chatbot.
|
178
|
-
|
179
|
-
Args:
|
180
|
-
content_: The content of the last message (optional).
|
181
|
-
func_calls_: The function calls for invoking the tools (optional).
|
182
|
-
|
183
|
-
Returns:
|
184
|
-
list: The results of invoking the tools.
|
185
|
-
"""
|
186
|
-
if func_calls_ is None and content_ is not None:
|
187
|
-
tool_uses = content_
|
188
|
-
func_calls_ = func_call.lcall(
|
189
|
-
[convert.to_dict(i) for i in tool_uses["action_request"]],
|
190
|
-
self.branch.tool_manager.get_function_call,
|
191
|
-
)
|
192
|
-
|
193
|
-
outs = await func_call.alcall(func_calls_, self.branch.tool_manager.invoke)
|
194
|
-
outs = convert.to_list(outs, flatten=True)
|
195
|
-
|
196
|
-
a = []
|
197
|
-
for out_, f in zip(outs, func_calls_):
|
198
|
-
res = {
|
199
|
-
"function": f[0],
|
200
|
-
"arguments": f[1],
|
201
|
-
"output": out_,
|
202
|
-
}
|
203
|
-
self.branch.add_message(response=res)
|
204
|
-
a.append(res)
|
205
|
-
|
206
|
-
return a
|
207
|
-
|
208
|
-
def _process_chatcompletion(self, payload, completion, sender):
|
209
|
-
"""
|
210
|
-
Processes the chat completion.
|
211
|
-
|
212
|
-
Args:
|
213
|
-
payload: The payload for the chat completion.
|
214
|
-
completion: The completed chat response.
|
215
|
-
sender: The sender of the message.
|
216
|
-
"""
|
217
|
-
if "choices" in completion:
|
218
|
-
add_msg_config = {"response": completion["choices"][0]}
|
219
|
-
if sender is not None:
|
220
|
-
add_msg_config["sender"] = sender
|
221
|
-
|
222
|
-
self.branch.datalogger.append(input_data=payload, output_data=completion)
|
223
|
-
self.branch.add_message(**add_msg_config)
|
224
|
-
self.branch.status_tracker.num_tasks_succeeded += 1
|
225
|
-
else:
|
226
|
-
self.branch.status_tracker.num_tasks_failed += 1
|
227
|
-
|
228
|
-
async def _call_chatcompletion(self, sender=None, with_sender=False, **kwargs):
|
229
|
-
"""
|
230
|
-
Calls the chat completion API.
|
231
|
-
|
232
|
-
Args:
|
233
|
-
sender: The sender of the message (optional).
|
234
|
-
with_sender (bool): Flag indicating whether to include the sender in the chat messages (default: False).
|
235
|
-
**kwargs: Additional keyword arguments for the chat completion API.
|
236
|
-
"""
|
237
|
-
messages = (
|
238
|
-
self.branch.chat_messages_with_sender
|
239
|
-
if with_sender
|
240
|
-
else self.branch.chat_messages
|
241
|
-
)
|
242
|
-
payload, completion = await self.branch.service.serve_chat(
|
243
|
-
messages=messages, **kwargs
|
244
|
-
)
|
245
|
-
self._process_chatcompletion(payload, completion, sender)
|
246
|
-
|
247
|
-
|
248
|
-
class MonoChatMixin(MonoChatConfigMixin, MonoChatInvokeMixin, ABC):
|
249
|
-
"""
|
250
|
-
Mixin class that combines MonoChatConfigMixin and MonoChatInvokeMixin.
|
251
|
-
"""
|
252
|
-
|
253
|
-
pass
|
@@ -1,215 +0,0 @@
|
|
1
|
-
"""
|
2
|
-
This module contains the MonoFollowup class for performing followup chats with an LLM.
|
3
|
-
|
4
|
-
The MonoFollowup class allows for conducting a series of followup chats with an LLM, with the ability to
|
5
|
-
process instructions, system messages, and invoke tools during the conversation. It extends the MonoChat class.
|
6
|
-
"""
|
7
|
-
|
8
|
-
from typing import Callable
|
9
|
-
from lionagi.core.messages.schema import Instruction
|
10
|
-
from lionagi.core.tool.tool import Tool
|
11
|
-
from lionagi.core.flow.monoflow.chat import MonoChat
|
12
|
-
|
13
|
-
|
14
|
-
class MonoFollowup(MonoChat):
|
15
|
-
"""
|
16
|
-
A class for performing followup chats with an LLM, processing instructions and system messages,
|
17
|
-
and optionally invoking tools.
|
18
|
-
|
19
|
-
Attributes:
|
20
|
-
FOLLOWUP_PROMPT (str): The default prompt for followup chats.
|
21
|
-
OUTPUT_PROMPT (str): The default prompt for presenting the final output to the user.
|
22
|
-
|
23
|
-
Methods:
|
24
|
-
async followup(self, instruction, context=None, sender=None, system=None, tools=None,
|
25
|
-
max_followup=1, followup_prompt=None, output_prompt=None, **kwargs):
|
26
|
-
Performs a series of followup chats with an LLM, processing instructions and system messages,
|
27
|
-
and optionally invoking tools.
|
28
|
-
|
29
|
-
_get_prompt(prompt=None, default=None, num_followup=None, instruction=None) -> str:
|
30
|
-
Retrieves the appropriate prompt for the followup chat based on the provided parameters.
|
31
|
-
|
32
|
-
_create_followup_config(self, tools, tool_choice="auto", **kwargs) -> dict:
|
33
|
-
Creates the configuration for the followup chat based on the provided tools and parameters.
|
34
|
-
|
35
|
-
async _followup(self, instruction, context=None, sender=None, system=None, tools=None,
|
36
|
-
max_followup=1, auto=False, followup_prompt=None, output_prompt=None,
|
37
|
-
out=True, **kwargs) -> str:
|
38
|
-
Performs the actual followup chats with the LLM, processing instructions and system messages,
|
39
|
-
and optionally invoking tools.
|
40
|
-
"""
|
41
|
-
|
42
|
-
FOLLOWUP_PROMPT = """
|
43
|
-
In the current task, you are allowed a maximum of another {num_followup} followup chats.
|
44
|
-
If further actions are needed, invoke tool usage.
|
45
|
-
If you are done, present the final result to the user without further tool usage.
|
46
|
-
"""
|
47
|
-
|
48
|
-
OUTPUT_PROMPT = "Notice: Present the final output to the user. Original user instruction: {instruction}"
|
49
|
-
|
50
|
-
async def followup(
|
51
|
-
self,
|
52
|
-
instruction: Instruction | str | dict[str, dict | str],
|
53
|
-
context=None,
|
54
|
-
sender=None,
|
55
|
-
system=None,
|
56
|
-
tools=None,
|
57
|
-
max_followup: int = 1,
|
58
|
-
followup_prompt=None,
|
59
|
-
output_prompt=None,
|
60
|
-
**kwargs,
|
61
|
-
):
|
62
|
-
"""
|
63
|
-
Performs a series of followup chats with an LLM, processing instructions and system messages,
|
64
|
-
and optionally invoking tools.
|
65
|
-
|
66
|
-
Args:
|
67
|
-
instruction (Instruction | str | dict[str, dict | str]): The instruction for the followup chat.
|
68
|
-
context (Optional[Any]): Additional context for the followup chat.
|
69
|
-
sender (Optional[str]): The sender of the followup chat message.
|
70
|
-
system (Optional[Any]): System message to be processed during the followup chat.
|
71
|
-
tools (Optional[Any]): Specifies tools to be invoked during the followup chat.
|
72
|
-
max_followup (int): The maximum number of followup chats allowed (default: 1).
|
73
|
-
followup_prompt (Optional[str]): The prompt to use for followup chats.
|
74
|
-
output_prompt (Optional[str]): The prompt to use for presenting the final output to the user.
|
75
|
-
**kwargs: Additional keyword arguments for the followup chat.
|
76
|
-
|
77
|
-
Returns:
|
78
|
-
str: The result of the followup chat.
|
79
|
-
"""
|
80
|
-
return await self._followup(
|
81
|
-
instruction=instruction,
|
82
|
-
context=context,
|
83
|
-
sender=sender,
|
84
|
-
system=system,
|
85
|
-
tools=tools,
|
86
|
-
max_followup=max_followup,
|
87
|
-
followup_prompt=followup_prompt,
|
88
|
-
output_prompt=output_prompt,
|
89
|
-
**kwargs,
|
90
|
-
)
|
91
|
-
|
92
|
-
@staticmethod
|
93
|
-
def _get_prompt(prompt=None, default=None, num_followup=None, instruction=None):
|
94
|
-
"""
|
95
|
-
Retrieves the appropriate prompt for the followup chat based on the provided parameters.
|
96
|
-
|
97
|
-
Args:
|
98
|
-
prompt (Optional[str]): The prompt to use for the followup chat.
|
99
|
-
default (Optional[str]): The default prompt to use if no specific prompt is provided.
|
100
|
-
num_followup (Optional[int]): The number of remaining followup chats.
|
101
|
-
instruction (Optional[Any]): The original user instruction.
|
102
|
-
|
103
|
-
Returns:
|
104
|
-
str: The appropriate prompt for the followup chat.
|
105
|
-
"""
|
106
|
-
if prompt is not None:
|
107
|
-
return prompt
|
108
|
-
|
109
|
-
try:
|
110
|
-
if num_followup is not None:
|
111
|
-
return default.format(num_followup=num_followup)
|
112
|
-
elif instruction is not None:
|
113
|
-
return default.format(instruction=instruction)
|
114
|
-
except (KeyError, ValueError):
|
115
|
-
pass
|
116
|
-
|
117
|
-
return default
|
118
|
-
|
119
|
-
def _create_followup_config(self, tools, tool_choice="auto", **kwargs):
|
120
|
-
"""
|
121
|
-
Creates the configuration for the followup chat based on the provided tools and parameters.
|
122
|
-
|
123
|
-
Args:
|
124
|
-
tools (Optional[Any]): Specifies tools to be invoked during the followup chat.
|
125
|
-
tool_choice (str): The choice of tools to use (default: "auto").
|
126
|
-
**kwargs: Additional keyword arguments for the followup chat configuration.
|
127
|
-
|
128
|
-
Returns:
|
129
|
-
dict: The configuration for the followup chat.
|
130
|
-
|
131
|
-
Raises:
|
132
|
-
ValueError: If no tools are found and registered.
|
133
|
-
"""
|
134
|
-
if tools and isinstance(tools, list) and isinstance(tools[0], (Callable, Tool)):
|
135
|
-
self.branch.tool_manager.register_tools(tools)
|
136
|
-
|
137
|
-
if not self.branch.tool_manager.has_tools:
|
138
|
-
raise ValueError("No tools found. You need to register tools.")
|
139
|
-
|
140
|
-
if tools is None:
|
141
|
-
tools = True
|
142
|
-
config = self.branch.tool_manager.parse_tool(tools=tools, **kwargs)
|
143
|
-
config["tool_parsed"] = True
|
144
|
-
config["tool_choice"] = tool_choice
|
145
|
-
return config
|
146
|
-
|
147
|
-
async def _followup(
|
148
|
-
self,
|
149
|
-
instruction: Instruction | str | dict[str, dict | str],
|
150
|
-
context=None,
|
151
|
-
sender=None,
|
152
|
-
system=None,
|
153
|
-
tools=None,
|
154
|
-
max_followup: int = 1,
|
155
|
-
auto=True,
|
156
|
-
followup_prompt=None,
|
157
|
-
output_prompt=None,
|
158
|
-
out=True,
|
159
|
-
**kwargs,
|
160
|
-
) -> None:
|
161
|
-
"""
|
162
|
-
Performs the actual followup chats with the LLM, processing instructions and system messages,
|
163
|
-
and optionally invoking tools.
|
164
|
-
|
165
|
-
Args:
|
166
|
-
instruction (Instruction | str | dict[str, dict | str]): The instruction for the followup chat.
|
167
|
-
context (Optional[Any]): Additional context for the followup chat.
|
168
|
-
sender (Optional[str]): The sender of the followup chat message.
|
169
|
-
system (Optional[Any]): System message to be processed during the followup chat.
|
170
|
-
tools (Optional[Any]): Specifies tools to be invoked during the followup chat.
|
171
|
-
max_followup (int): The maximum number of followup chats allowed (default: 1).
|
172
|
-
auto (bool): Flag indicating whether to automatically determine if the chat is finished (default: False).
|
173
|
-
followup_prompt (Optional[str]): The prompt to use for followup chats.
|
174
|
-
output_prompt (Optional[str]): The prompt to use for presenting the final output to the user.
|
175
|
-
out (bool): Flag indicating whether to return the output of the followup chat (default: True).
|
176
|
-
**kwargs: Additional keyword arguments for the followup chat.
|
177
|
-
|
178
|
-
Returns:
|
179
|
-
Optional[str]: The result of the followup chat, if `out` is True.
|
180
|
-
"""
|
181
|
-
config = self._create_followup_config(tools, **kwargs)
|
182
|
-
|
183
|
-
i = 0
|
184
|
-
_out = ""
|
185
|
-
|
186
|
-
while i < max_followup:
|
187
|
-
_prompt = self._get_prompt(
|
188
|
-
prompt=followup_prompt,
|
189
|
-
default=self.FOLLOWUP_PROMPT,
|
190
|
-
num_followup=max_followup - i,
|
191
|
-
)
|
192
|
-
|
193
|
-
if i == 0:
|
194
|
-
_prompt = {"NOTICE": _prompt, "TASK": instruction}
|
195
|
-
_out = await self.chat(
|
196
|
-
_prompt, context=context, sender=sender, system=system, **config
|
197
|
-
)
|
198
|
-
else:
|
199
|
-
_out = await self.chat(_prompt, sender=sender, **config)
|
200
|
-
|
201
|
-
if not self.branch._is_invoked():
|
202
|
-
return _out if out else None
|
203
|
-
|
204
|
-
i += 1
|
205
|
-
|
206
|
-
if not self.branch._is_invoked():
|
207
|
-
return _out if out else None
|
208
|
-
|
209
|
-
_prompt = self._get_prompt(
|
210
|
-
prompt=output_prompt,
|
211
|
-
default=self.OUTPUT_PROMPT,
|
212
|
-
instruction=instruction,
|
213
|
-
)
|
214
|
-
_out = await self.chat(_prompt, sender=sender, **kwargs)
|
215
|
-
return _out if out else None
|
@@ -1 +0,0 @@
|
|
1
|
-
from .chat import PolyChat
|