Skip to content

Commit

Permalink
Facade changes for agent by code (#56)
Browse files Browse the repository at this point in the history
* facade changes for edit by code

* Code facade example

* Re-arraging the example files

* Futher improving facade

* Naming to_flo back to what it was

* Fix build method issue

* Fix for creating routed team

* Simplified facade using create method

* Fix for delegator facade
  • Loading branch information
vizsatiz authored Nov 17, 2024
1 parent 306a864 commit 4d4d4ec
Show file tree
Hide file tree
Showing 29 changed files with 237 additions and 221 deletions.
6 changes: 3 additions & 3 deletions examples/agentic_rag.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"metadata": {},
"outputs": [
{
Expand All @@ -66,7 +66,7 @@
],
"source": [
"# load the document and split it into chunks\n",
"loader = TextLoader(\"./rag_document.txt\")\n",
"loader = TextLoader(\"./data/rag_document.txt\")\n",
"documents = loader.load()\n",
"\n",
"# split it into chunks\n",
Expand Down Expand Up @@ -229,7 +229,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": ".venv",
"language": "python",
"name": "python3"
},
Expand Down
170 changes: 53 additions & 117 deletions examples/build_agents_by_code.ipynb

Large diffs are not rendered by default.

File renamed without changes.
4 changes: 2 additions & 2 deletions examples/email_reply_agent.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -127,7 +127,7 @@
"from langchain_text_splitters import CharacterTextSplitter\n",
"\n",
"# load the document and split it into chunks\n",
"loader = TextLoader(\"./rag_document.txt\")\n",
"loader = TextLoader(\"./data/rag_document.txt\")\n",
"documents = loader.load()\n",
"\n",
"# split it into chunks\n",
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion examples/rag_tool.py → examples/python/rag_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
session = FloSession(llm, log_level='ERROR')

# load the document and split it into chunks
loader = TextLoader('./examples/rag_document.txt')
loader = TextLoader('./examples/data/rag_document.txt')
documents = loader.load()

# split it into chunks
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
12 changes: 8 additions & 4 deletions flo_ai/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
from flo_ai.core import Flo as Flo
from flo_ai.models.flo_agent import FloAgent as FloAgent
from flo_ai.router.flo_supervisor import FloSupervisor as FloSupervisor
from flo_ai.models.flo_team import FloTeam as FloTeam
from flo_ai.models.flo_agent import FloAgent as FloAgent
from flo_ai.router.flo_linear import FloLinear as FloLinear
from flo_ai.router.flo_router import FloRouter as FloRouter
from flo_ai.state.flo_session import FloSession as FloSession
from flo_ai.models.flo_llm_agent import FloLLMAgent as FloLLMAgent
from flo_ai.models.flo_tool_agent import FloToolAgent as FloToolAgent
from flo_ai.router.flo_llm_router import FloLLMRouter as FloLLMRouter
from flo_ai.router.flo_supervisor import FloSupervisor as FloSupervisor
from flo_ai.retrievers.flo_retriever import FloRagBuilder as FloRagBuilder
from flo_ai.common.flo_logger import get_logger as get_logger
from flo_ai.common.flo_langchain_logger import FloLangchainLogger as FloLangchainLogger
from flo_ai.models.flo_delegation_agent import FloDelegatorAgent as FloDelegatorAgent
from flo_ai.models.flo_reflection_agent import FloReflectionAgent as FloReflectionAgent
10 changes: 6 additions & 4 deletions flo_ai/builders/yaml_builder.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from typing import Union
from flo_ai.models.flo_team import FloTeam
from flo_ai.yaml.config import (
FloRoutedTeamConfig,
Expand All @@ -14,9 +15,10 @@
from flo_ai.common.flo_logger import get_logger


def build_supervised_team(session: FloSession) -> ExecutableFlo:
def build_supervised_team(
session: FloSession, flo_config: Union[FloRoutedTeamConfig, FloAgentConfig]
) -> ExecutableFlo:
name_set = set()
flo_config = session.config
if isinstance(flo_config, FloRoutedTeamConfig):
team_config: TeamConfig = flo_config.team
team = parse_and_build_subteams(session, team_config, name_set)
Expand All @@ -41,15 +43,15 @@ def parse_and_build_subteams(
members = [AgentFactory.create(session, agent) for agent in team_config.agents]
flo_team = FloTeam.Builder(session, team_config.name, members=members).build()
router = FloRouterFactory.create(session, team_config, flo_team)
flo_routed_team = router.to_flo()
flo_routed_team = router.build_routed_team()
else:
flo_teams = []
for subteam in team_config.subteams:
flo_subteam = parse_and_build_subteams(session, subteam, name_set)
flo_teams.append(flo_subteam)
flo_team = FloTeam.Builder(session, team_config.name, members=flo_teams).build()
router = FloRouterFactory.create(session, team_config, flo_team)
flo_routed_team = router.to_flo()
flo_routed_team = router.build_routed_team()
return flo_routed_team


Expand Down
32 changes: 24 additions & 8 deletions flo_ai/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
import warnings
import logging
from typing import Optional
from langchain_core.runnables import Runnable
from flo_ai.yaml.config import to_supervised_team
from flo_ai.builders.yaml_builder import build_supervised_team, FloRoutedTeamConfig
from flo_ai.builders.yaml_builder import build_supervised_team
from typing import Any, Iterator, Union
from flo_ai.router.flo_router import FloRouter
from flo_ai.state.flo_session import FloSession
from flo_ai.models.flo_executable import ExecutableFlo
from flo_ai.error.flo_exception import FloException
Expand All @@ -20,11 +22,9 @@


class Flo:
def __init__(self, session: FloSession, config: FloRoutedTeamConfig) -> None:
def __init__(self, session: FloSession, executable: Runnable) -> None:
self.session = session
self.config = config
session.config = config
self.runnable: ExecutableFlo = build_supervised_team(session)
self.runnable: ExecutableFlo = executable

self.langchain_logger = session.langchain_logger
get_logger().info('Flo instance created ...', session)
Expand All @@ -50,7 +50,12 @@ def async_invoke(self, query, config=None) -> Iterator[Union[dict[str, Any], Any
return self.runnable.ainvoke(query, config)

@staticmethod
def build(session: FloSession, yaml: str, log_level: Optional[str] = None):
def build(
session: FloSession,
yaml: Optional[str] = None,
routed_team: Optional[FloRouter] = None,
log_level: Optional[str] = None,
):
if log_level:
warnings.warn(
'`log_level` is deprecated and will be removed in a future version. '
Expand All @@ -59,8 +64,19 @@ def build(session: FloSession, yaml: str, log_level: Optional[str] = None):
stacklevel=2,
)
Flo.set_log_level(log_level)
get_logger().info('Building Flo instance from YAML ...', session)
return Flo(session, to_supervised_team(yaml))
if yaml is not None:
get_logger().info('Building Flo instance from YAML ...', session)
executable: ExecutableFlo = build_supervised_team(
session, to_supervised_team(yaml)
)
return Flo(session, executable)
if routed_team is not None:
return Flo(session, routed_team.build_routed_team())
raise FloException("""Either yaml or routed_team should be not None""")

@staticmethod
def create(session: FloSession, routed_team: FloRouter):
return Flo(session, routed_team.build_routed_team())

@staticmethod
def set_log_level(log_level: str):
Expand Down
24 changes: 23 additions & 1 deletion flo_ai/models/flo_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,28 @@ def __init__(
self.agent: Runnable = (agent,)
self.executor: AgentExecutor = executor

@staticmethod
def create(
session: FloSession,
name: str,
job: str,
tools: list[BaseTool],
role: Optional[str] = None,
on_error: Union[str, Callable] = True,
llm: Union[BaseLanguageModel, None] = None,
):
model_name = 'default' if llm is None else llm.name
return FloAgent.Builder(
session=session,
name=name,
job=job,
tools=tools,
role=role,
on_error=on_error,
llm=llm,
model_name=model_name,
).build()

class Builder:
def __init__(
self,
Expand All @@ -31,7 +53,7 @@ def __init__(
job: str,
tools: list[BaseTool],
role: Optional[str] = None,
verbose: bool = True,
verbose: bool = False,
llm: Union[BaseLanguageModel, None] = None,
on_error: Union[str, Callable] = True,
model_name: Union[str, None] = 'default',
Expand Down
18 changes: 18 additions & 0 deletions flo_ai/models/flo_delegation_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,24 @@ def __init__(
self.executor = executor
self.model_name = model_name

@staticmethod
def create(
session: FloSession,
name: str,
job: str,
to: Delegate,
llm: Optional[BaseLanguageModel] = None,
):
model_name = 'default' if llm is None else llm.name
return FloDelegatorAgent.Builder(
session=session,
name=name,
job=job,
delegate=to,
llm=llm,
model_name=model_name,
).build()

class Builder:
def __init__(
self,
Expand Down
18 changes: 18 additions & 0 deletions flo_ai/models/flo_llm_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,24 @@ def __init__(self, name: str, executor: Runnable, model_name: str) -> None:
self.executor: Runnable = executor
self.model_name: str = model_name

@staticmethod
def create(
session: FloSession,
name: str,
job: str,
role: Optional[str] = None,
llm: Union[BaseLanguageModel, None] = None,
):
model_name = 'default' if llm is None else llm.name
return FloLLMAgent.Builder(
session=session,
name=name,
job=job,
role=role,
llm=llm,
model_name=model_name,
).build()

class Builder:
def __init__(
self,
Expand Down
76 changes: 0 additions & 76 deletions flo_ai/models/flo_rag.py

This file was deleted.

20 changes: 20 additions & 0 deletions flo_ai/models/flo_reflection_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,26 @@ def __init__(
self.model_name = model_name
self.delegate = delegate

@staticmethod
def create(
session: FloSession,
name: str,
job: str,
to: Delegate,
role: Optional[str] = None,
llm: Optional[BaseLanguageModel] = None,
):
model_name = 'default' if llm is None else llm.name
return FloReflectionAgent.Builder(
session=session,
name=name,
job=job,
to=to,
role=role,
llm=llm,
model_name=model_name,
).build()

class Builder:
def __init__(
self,
Expand Down
10 changes: 9 additions & 1 deletion flo_ai/models/flo_team.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,21 @@ def __init__(
self.members = members
self.session = session

@staticmethod
def create(session: FloSession, name: str, members: list[FloMember]):
return FloTeam.Builder(session=session, name=name, members=members).build()

class Builder:
def __init__(
self, session: FloSession, name: str, members: list[FloMember]
) -> None:
from flo_ai import Flo

self.name = name
self.session = session
self.members = members
self.members = list(
map(lambda x: x.runnable if isinstance(x, Flo) else x, members)
)
self.member_names = list(map(lambda x: x.name, self.members))

def build(self):
Expand Down
Loading

0 comments on commit 4d4d4ec

Please sign in to comment.