-
-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #314 from ohdearquant/merge_direct
v0.0.314 one step react
- Loading branch information
Showing
17 changed files
with
445 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
from .predict import predict | ||
from .select import select | ||
from .score import score | ||
from .react import react | ||
from .vote import vote | ||
|
||
__all__ = ["predict", "select", "score", "vote"] | ||
__all__ = ["predict", "select", "score", "vote", "react"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
from lionagi.libs import func_call, convert, AsyncUtil | ||
|
||
from lionagi.integrations.bridge.pydantic_.pydantic_bridge import Field | ||
from ..prompt.action_template import ActionedTemplate | ||
from ..branch import Branch | ||
|
||
|
||
class ReactTemplate(ActionedTemplate): | ||
template_name: str = "default_react" | ||
sentence: str | list | dict = Field( | ||
default_factory=str, | ||
description="the given sentence(s) to reason and take actions on", | ||
) | ||
|
||
def __init__( | ||
self, | ||
sentence=None, | ||
instruction=None, | ||
confidence_score=False, | ||
**kwargs, | ||
): | ||
super().__init__(**kwargs) | ||
|
||
self.sentence = sentence | ||
self.task = f"Think step by step. Perform reasoning and prepare actions with given tools only.Instruction: {instruction}. Absolutely DO NOT MAKE UP FUNCTIONS !!!" | ||
|
||
if confidence_score: | ||
self.output_fields.append("confidence_score") | ||
|
||
|
||
async def _react( | ||
sentence, | ||
*, | ||
instruction=None, | ||
branch=None, | ||
confidence_score=False, | ||
retries=2, | ||
delay=0.5, | ||
backoff_factor=2, | ||
default_value=None, | ||
timeout=None, | ||
branch_name=None, | ||
system=None, | ||
messages=None, | ||
service=None, | ||
sender=None, | ||
llmconfig=None, | ||
tools=None, | ||
datalogger=None, | ||
persist_path=None, | ||
tool_manager=None, | ||
return_branch=False, | ||
**kwargs, | ||
): | ||
|
||
if "temperature" not in kwargs: | ||
kwargs["temperature"] = 0.1 | ||
|
||
instruction = instruction or "" | ||
|
||
branch = branch or Branch( | ||
name=branch_name, | ||
system=system, | ||
messages=messages, | ||
service=service, | ||
sender=sender, | ||
llmconfig=llmconfig, | ||
tools=tools, | ||
datalogger=datalogger, | ||
persist_path=persist_path, | ||
tool_manager=tool_manager, | ||
) | ||
|
||
_template = ReactTemplate( | ||
sentence=sentence, | ||
instruction=instruction, | ||
confidence_score=confidence_score, | ||
) | ||
|
||
await func_call.rcall( | ||
branch.chat, | ||
prompt_template=_template, | ||
retries=retries, | ||
delay=delay, | ||
backoff_factor=backoff_factor, | ||
default=default_value, | ||
timeout=timeout, | ||
**kwargs, | ||
) | ||
|
||
if _template.action_needed: | ||
actions = _template.actions | ||
tasks = [branch.tool_manager.invoke(i.values()) for i in actions] | ||
results = await AsyncUtil.execute_tasks(*tasks) | ||
|
||
a = [] | ||
for idx, item in enumerate(actions): | ||
res = { | ||
"function": item["function"], | ||
"arguments": item["arguments"], | ||
"output": results[idx], | ||
} | ||
branch.add_message(response=res) | ||
a.append(res) | ||
|
||
_template.__setattr__("action_response", a) | ||
|
||
return (_template, branch) if return_branch else _template | ||
|
||
|
||
async def react( | ||
sentence, | ||
*, | ||
instruction=None, | ||
num_instances=1, | ||
branch=None, | ||
confidence_score=False, | ||
retries=2, | ||
delay=0.5, | ||
backoff_factor=2, | ||
default_value=None, | ||
timeout=None, | ||
branch_name=None, | ||
system=None, | ||
messages=None, | ||
service=None, | ||
sender=None, | ||
llmconfig=None, | ||
tools=None, | ||
datalogger=None, | ||
persist_path=None, | ||
tool_manager=None, | ||
return_branch=False, | ||
**kwargs, | ||
): | ||
|
||
async def _inner(i=0): | ||
return await _react( | ||
sentence=sentence, | ||
instruction=instruction, | ||
num_instances=num_instances, | ||
branch=branch, | ||
confidence_score=confidence_score, | ||
retries=retries, | ||
delay=delay, | ||
backoff_factor=backoff_factor, | ||
default_value=default_value, | ||
timeout=timeout, | ||
branch_name=branch_name, | ||
system=system, | ||
messages=messages, | ||
service=service, | ||
sender=sender, | ||
llmconfig=llmconfig, | ||
tools=tools, | ||
datalogger=datalogger, | ||
persist_path=persist_path, | ||
tool_manager=tool_manager, | ||
return_branch=return_branch, | ||
**kwargs, | ||
) | ||
|
||
if num_instances == 1: | ||
return await _inner() | ||
|
||
elif num_instances > 1: | ||
return await func_call.alcall(range(num_instances), _inner) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from typing import Any | ||
from lionagi.integrations.bridge.pydantic_.pydantic_bridge import Field | ||
|
||
from .scored_template import ScoredTemplate | ||
|
||
|
||
class ActionRequest: ... | ||
|
||
|
||
class ActionedTemplate(ScoredTemplate): | ||
|
||
action_needed: bool | None = Field( | ||
False, description="true if actions are needed else false" | ||
) | ||
|
||
actions: list[dict | ActionRequest | Any] | None = Field( | ||
default_factory=list, | ||
description="""provide The list of action(s) to take, each action in {"function": function_name, "arguments": {param1:..., param2:..., ...}}. Leave blank if no further actions are needed, you must use provided parameters for each action, DO NOT MAKE UP KWARG NAME!!!""", | ||
) | ||
|
||
answer: str | dict | Any | None = Field( | ||
default_factory=str, | ||
description="output answer to the questions asked if further actions are not needed, leave blank if an accurate answer cannot be provided from context during this step", | ||
) | ||
|
||
signature: str = "sentence -> reason, action_needed, actions, answer" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.