Skip to content

Commit

Permalink
Code formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
panregedit committed Dec 20, 2024
1 parent 8a6c177 commit 60cd89a
Show file tree
Hide file tree
Showing 259 changed files with 13,097 additions and 9,610 deletions.
48 changes: 31 additions & 17 deletions examples/general_dnc/agent/conclude/conclude.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
from pathlib import Path
from typing import List

from omagent_core.models.llms.base import BaseLLMBackend
from omagent_core.advanced_components.workflow.dnc.schemas.dnc_structure import \
TaskTree
from omagent_core.engine.worker.base import BaseWorker
from omagent_core.models.llms.prompt import PromptTemplate
from omagent_core.memories.ltms.ltm import LTM
from omagent_core.utils.registry import registry
from pydantic import Field
from omagent_core.advanced_components.workflow.dnc.schemas.dnc_structure import TaskTree
from omagent_core.models.llms.base import BaseLLMBackend
from omagent_core.models.llms.prompt import PromptTemplate
from omagent_core.utils.logger import logging
from omagent_core.utils.registry import registry
from openai import Stream

from pydantic import Field

CURRENT_PATH = root_path = Path(__file__).parents[0]

Expand All @@ -36,8 +36,8 @@ def _run(self, dnc_structure: dict, last_output: str, *args, **kwargs):
- Generates a final conclusion/summary of the entire task execution
- Formats and presents the final output in a clear way
- Cleans up any temporary state/memory used during execution
The conclude node is responsible for providing a coherent final response that
The conclude node is responsible for providing a coherent final response that
addresses the original root task objective based on all the work done by
previous nodes.
Expand All @@ -51,25 +51,39 @@ def _run(self, dnc_structure: dict, last_output: str, *args, **kwargs):
dict: Final response containing the conclusion/summary
"""
task = TaskTree(**dnc_structure)
self.callback.info(agent_id=self.workflow_instance_id, progress=f'Conclude', message=f'{task.get_current_node().task}')
self.callback.info(
agent_id=self.workflow_instance_id,
progress=f"Conclude",
message=f"{task.get_current_node().task}",
)
chat_complete_res = self.simple_infer(
task=task.get_root().task,
result=str(last_output),
img_placeholders="".join(list(self.stm(self.workflow_instance_id).get('image_cache', {}).keys())),
img_placeholders="".join(
list(self.stm(self.workflow_instance_id).get("image_cache", {}).keys())
),
)
if isinstance(chat_complete_res, Stream):
last_output = 'Answer: '
self.callback.send_incomplete(agent_id=self.workflow_instance_id, msg='Answer: ')
last_output = "Answer: "
self.callback.send_incomplete(
agent_id=self.workflow_instance_id, msg="Answer: "
)
for chunk in chat_complete_res:
if chunk.choices[0].delta.content is not None:
self.callback.send_incomplete(agent_id=self.workflow_instance_id, msg=f'{chunk.choices[0].delta.content}')
self.callback.send_incomplete(
agent_id=self.workflow_instance_id,
msg=f"{chunk.choices[0].delta.content}",
)
last_output += chunk.choices[0].delta.content
else:
self.callback.send_block(agent_id=self.workflow_instance_id, msg='')
last_output += ''
self.callback.send_block(agent_id=self.workflow_instance_id, msg="")
last_output += ""
break
else:
last_output = chat_complete_res["choices"][0]["message"]["content"]
self.callback.send_answer(agent_id=self.workflow_instance_id, msg=f'Answer: {chat_complete_res["choices"][0]["message"]["content"]}')
self.callback.send_answer(
agent_id=self.workflow_instance_id,
msg=f'Answer: {chat_complete_res["choices"][0]["message"]["content"]}',
)
self.stm(self.workflow_instance_id).clear()
return {'last_output': last_output}
return {"last_output": last_output}
28 changes: 15 additions & 13 deletions examples/general_dnc/agent/input_interface/input_interface.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
from pathlib import Path

from omagent_core.utils.registry import registry
from omagent_core.utils.general import read_image
from omagent_core.engine.worker.base import BaseWorker
from omagent_core.utils.general import read_image
from omagent_core.utils.logger import logging

from omagent_core.utils.registry import registry

CURRENT_PATH = Path(__file__).parents[0]


@registry.register_worker()
class InputInterface(BaseWorker):
"""Input interface processor that handles user instructions and image input.
This processor:
1. Reads user input containing question and image via input interface
2. Extracts text instruction and image path from the input
Expand All @@ -22,16 +21,19 @@ class InputInterface(BaseWorker):

def _run(self, *args, **kwargs):
# Read user input through configured input interface
user_input = self.input.read_input(workflow_instance_id=self.workflow_instance_id, input_prompt='Please input your question:')
messages = user_input['messages']
user_input = self.input.read_input(
workflow_instance_id=self.workflow_instance_id,
input_prompt="Please input your question:",
)
messages = user_input["messages"]
message = messages[-1]
image = None
text = None
for each_content in message['content']:
if each_content['type'] == 'image_url':
image = read_image(each_content['data'])
elif each_content['type'] == 'text':
text = each_content['data']
for each_content in message["content"]:
if each_content["type"] == "image_url":
image = read_image(each_content["data"])
elif each_content["type"] == "text":
text = each_content["data"]
if image is not None:
self.stm(self.workflow_instance_id)['image_cache'] = {f'<image_0>' : image}
return {'query': text}
self.stm(self.workflow_instance_id)["image_cache"] = {f"<image_0>": image}
return {"query": text}
8 changes: 4 additions & 4 deletions examples/general_dnc/compile_container.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# Import core modules and components
from omagent_core.utils.container import container

# Import workflow related modules
from pathlib import Path

from omagent_core.utils.container import container
from omagent_core.utils.registry import registry

# Set up path and import modules
CURRENT_PATH = root_path = Path(__file__).parents[0]
registry.import_module()

# Register required components
container.register_callback(callback='AppCallback')
container.register_input(input='AppInput')
container.register_callback(callback="AppCallback")
container.register_input(input="AppInput")
container.register_stm("RedisSTM")
# Compile container config
container.compile_config(CURRENT_PATH)
43 changes: 28 additions & 15 deletions examples/general_dnc/run_app.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,50 @@
# Import required modules and components
import os
from omagent_core.utils.container import container
from omagent_core.engine.workflow.conductor_workflow import ConductorWorkflow
from omagent_core.engine.workflow.task.simple_task import simple_task
from pathlib import Path
from omagent_core.utils.registry import registry
from omagent_core.clients.devices.app.client import AppClient
from omagent_core.utils.logger import logging

from agent.conclude.conclude import Conclude
from agent.input_interface.input_interface import InputInterface
from omagent_core.advanced_components.workflow.dnc.workflow import DnCWorkflow
from agent.conclude.conclude import Conclude
from omagent_core.clients.devices.app.client import AppClient
from omagent_core.engine.workflow.conductor_workflow import ConductorWorkflow
from omagent_core.engine.workflow.task.simple_task import simple_task
from omagent_core.utils.container import container
from omagent_core.utils.logger import logging
from omagent_core.utils.registry import registry

logging.init_logger("omagent", "omagent", level="INFO")

# Set current working directory path
CURRENT_PATH = root_path = Path(__file__).parents[0]

# Import registered modules
registry.import_module(CURRENT_PATH.joinpath('agent'))
registry.import_module(CURRENT_PATH.joinpath("agent"))

# Load container configuration from YAML file
container.register_stm("RedisSTM")
container.from_config(CURRENT_PATH.joinpath('container.yaml'))
container.from_config(CURRENT_PATH.joinpath("container.yaml"))

# Initialize simple VQA workflow
workflow = ConductorWorkflow(name='general_dnc')
workflow = ConductorWorkflow(name="general_dnc")

# Configure workflow tasks:
# 1. Input interface for user interaction
client_input_task = simple_task(task_def_name=InputInterface, task_reference_name='input_interface')
client_input_task = simple_task(
task_def_name=InputInterface, task_reference_name="input_interface"
)

dnc_workflow = DnCWorkflow()
dnc_workflow.set_input(query=client_input_task.output('query'))
dnc_workflow.set_input(query=client_input_task.output("query"))

# 6. Conclude task for task conclusion
conclude_task = simple_task(task_def_name=Conclude, task_reference_name='task_conclude', inputs={'dnc_structure': dnc_workflow.dnc_structure, 'last_output': dnc_workflow.last_output})
conclude_task = simple_task(
task_def_name=Conclude,
task_reference_name="task_conclude",
inputs={
"dnc_structure": dnc_workflow.dnc_structure,
"last_output": dnc_workflow.last_output,
},
)


# Configure workflow execution flow: Input -> Initialize global variables -> DnC Loop -> Conclude
Expand All @@ -43,6 +54,8 @@
workflow.register(overwrite=True)

# Initialize and start app client with workflow configuration
config_path = CURRENT_PATH.joinpath('configs')
app_client = AppClient(interactor=workflow, config_path=config_path, workers=[InputInterface()])
config_path = CURRENT_PATH.joinpath("configs")
app_client = AppClient(
interactor=workflow, config_path=config_path, workers=[InputInterface()]
)
app_client.start_interactor()
43 changes: 28 additions & 15 deletions examples/general_dnc/run_cli.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,50 @@
# Import required modules and components
import os
from omagent_core.utils.container import container
from omagent_core.engine.workflow.conductor_workflow import ConductorWorkflow
from omagent_core.engine.workflow.task.simple_task import simple_task
from pathlib import Path
from omagent_core.utils.registry import registry
from omagent_core.clients.devices.cli.client import DefaultClient
from omagent_core.utils.logger import logging

from agent.conclude.conclude import Conclude
from agent.input_interface.input_interface import InputInterface
from omagent_core.advanced_components.workflow.dnc.workflow import DnCWorkflow
from agent.conclude.conclude import Conclude
from omagent_core.clients.devices.cli.client import DefaultClient
from omagent_core.engine.workflow.conductor_workflow import ConductorWorkflow
from omagent_core.engine.workflow.task.simple_task import simple_task
from omagent_core.utils.container import container
from omagent_core.utils.logger import logging
from omagent_core.utils.registry import registry

logging.init_logger("omagent", "omagent", level="INFO")

# Set current working directory path
CURRENT_PATH = root_path = Path(__file__).parents[0]

# Import registered modules
registry.import_module(CURRENT_PATH.joinpath('agent'))
registry.import_module(CURRENT_PATH.joinpath("agent"))

# Load container configuration from YAML file
container.register_stm("RedisSTM")
container.from_config(CURRENT_PATH.joinpath('container.yaml'))
container.from_config(CURRENT_PATH.joinpath("container.yaml"))

# Initialize simple VQA workflow
workflow = ConductorWorkflow(name='general_dnc')
workflow = ConductorWorkflow(name="general_dnc")

# Configure workflow tasks:
# 1. Input interface for user interaction
client_input_task = simple_task(task_def_name=InputInterface, task_reference_name='input_interface')
client_input_task = simple_task(
task_def_name=InputInterface, task_reference_name="input_interface"
)

dnc_workflow = DnCWorkflow()
dnc_workflow.set_input(query=client_input_task.output('query'))
dnc_workflow.set_input(query=client_input_task.output("query"))

# 6. Conclude task for task conclusion
conclude_task = simple_task(task_def_name=Conclude, task_reference_name='task_conclude', inputs={'dnc_structure': dnc_workflow.dnc_structure, 'last_output': dnc_workflow.last_output})
conclude_task = simple_task(
task_def_name=Conclude,
task_reference_name="task_conclude",
inputs={
"dnc_structure": dnc_workflow.dnc_structure,
"last_output": dnc_workflow.last_output,
},
)


# Configure workflow execution flow: Input -> Initialize global variables -> DnC Loop -> Conclude
Expand All @@ -43,6 +54,8 @@
workflow.register(overwrite=True)

# Initialize and start app client with workflow configuration
config_path = CURRENT_PATH.joinpath('configs')
cli_client = DefaultClient(interactor=workflow, config_path=config_path, workers=[InputInterface()])
config_path = CURRENT_PATH.joinpath("configs")
cli_client = DefaultClient(
interactor=workflow, config_path=config_path, workers=[InputInterface()]
)
cli_client.start_interactor()
43 changes: 28 additions & 15 deletions examples/general_dnc/run_webpage.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,50 @@
# Import required modules and components
import os
from omagent_core.utils.container import container
from omagent_core.engine.workflow.conductor_workflow import ConductorWorkflow
from omagent_core.engine.workflow.task.simple_task import simple_task
from pathlib import Path
from omagent_core.utils.registry import registry
from omagent_core.clients.devices.webpage.client import WebpageClient
from omagent_core.utils.logger import logging

from agent.conclude.conclude import Conclude
from agent.input_interface.input_interface import InputInterface
from omagent_core.advanced_components.workflow.dnc.workflow import DnCWorkflow
from agent.conclude.conclude import Conclude
from omagent_core.clients.devices.webpage.client import WebpageClient
from omagent_core.engine.workflow.conductor_workflow import ConductorWorkflow
from omagent_core.engine.workflow.task.simple_task import simple_task
from omagent_core.utils.container import container
from omagent_core.utils.logger import logging
from omagent_core.utils.registry import registry

logging.init_logger("omagent", "omagent", level="INFO")

# Set current working directory path
CURRENT_PATH = root_path = Path(__file__).parents[0]

# Import registered modules
registry.import_module(CURRENT_PATH.joinpath('agent'))
registry.import_module(CURRENT_PATH.joinpath("agent"))

# Load container configuration from YAML file
container.register_stm("RedisSTM")
container.from_config(CURRENT_PATH.joinpath('container.yaml'))
container.from_config(CURRENT_PATH.joinpath("container.yaml"))

# Initialize simple VQA workflow
workflow = ConductorWorkflow(name='general_dnc')
workflow = ConductorWorkflow(name="general_dnc")

# Configure workflow tasks:
# 1. Input interface for user interaction
client_input_task = simple_task(task_def_name=InputInterface, task_reference_name='input_interface')
client_input_task = simple_task(
task_def_name=InputInterface, task_reference_name="input_interface"
)

dnc_workflow = DnCWorkflow()
dnc_workflow.set_input(query=client_input_task.output('query'))
dnc_workflow.set_input(query=client_input_task.output("query"))

# 6. Conclude task for task conclusion
conclude_task = simple_task(task_def_name=Conclude, task_reference_name='task_conclude', inputs={'dnc_structure': dnc_workflow.dnc_structure, 'last_output': dnc_workflow.last_output})
conclude_task = simple_task(
task_def_name=Conclude,
task_reference_name="task_conclude",
inputs={
"dnc_structure": dnc_workflow.dnc_structure,
"last_output": dnc_workflow.last_output,
},
)


# Configure workflow execution flow: Input -> Initialize global variables -> DnC Loop -> Conclude
Expand All @@ -43,6 +54,8 @@
workflow.register(overwrite=True)

# Initialize and start app client with workflow configuration
config_path = CURRENT_PATH.joinpath('configs')
webpage_client = WebpageClient(interactor=workflow, config_path=config_path, workers=[InputInterface()])
config_path = CURRENT_PATH.joinpath("configs")
webpage_client = WebpageClient(
interactor=workflow, config_path=config_path, workers=[InputInterface()]
)
webpage_client.start_interactor()
Loading

0 comments on commit 60cd89a

Please sign in to comment.