Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor agent class to openssa.agent.dana.DANA #345

Merged
merged 5 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions examples/FinanceBench/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,33 @@ agent-solve:
agent-solve-w-knowledge:
@poetry run python htp_oodar_agent.py ${id} --knowledge

agent-solve-w-prog-space:
@poetry run python htp_oodar_agent.py ${id} --prog-space
agent-solve-w-prog-store:
@poetry run python htp_oodar_agent.py ${id} --prog-store

agent-solve-w-knowledge-and-prog-space:
@poetry run python htp_oodar_agent.py ${id} --knowledge --prog-space
agent-solve-w-knowledge-and-prog-store:
@poetry run python htp_oodar_agent.py ${id} --knowledge --prog-store

agent-solve-w-llama3:
@poetry run python htp_oodar_agent.py ${id} --llama3

agent-solve-w-knowledge-w-llama3:
@poetry run python htp_oodar_agent.py ${id} --knowledge --llama3

agent-solve-w-prog-space-w-llama3:
@poetry run python htp_oodar_agent.py ${id} --prog-space --llama3
agent-solve-w-prog-store-w-llama3:
@poetry run python htp_oodar_agent.py ${id} --prog-store --llama3

agent-solve-w-knowledge-and-prog-space-w-llama3:
@poetry run python htp_oodar_agent.py ${id} --knowledge --prog-space --llama3
agent-solve-w-knowledge-and-prog-store-w-llama3:
@poetry run python htp_oodar_agent.py ${id} --knowledge --prog-store --llama3

agent-solve-all-combos:
@poetry run python htp_oodar_agent.py ${id}
@poetry run python htp_oodar_agent.py ${id} --knowledge
@poetry run python htp_oodar_agent.py ${id} --prog-space
@poetry run python htp_oodar_agent.py ${id} --knowledge --prog-space
@poetry run python htp_oodar_agent.py ${id} --prog-store
@poetry run python htp_oodar_agent.py ${id} --knowledge --prog-store
@poetry run python htp_oodar_agent.py ${id} --llama3
@poetry run python htp_oodar_agent.py ${id} --knowledge --llama3
@poetry run python htp_oodar_agent.py ${id} --prog-space --llama3
@poetry run python htp_oodar_agent.py ${id} --knowledge --prog-space --llama3
@poetry run python htp_oodar_agent.py ${id} --prog-store --llama3
@poetry run python htp_oodar_agent.py ${id} --knowledge --prog-store --llama3


openai-assist:
Expand Down
6 changes: 3 additions & 3 deletions examples/FinanceBench/data_and_knowledge.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,15 +190,15 @@ class Category(StrEnum):
EXPERT_KNOWLEDGE: str = f.read()


EXPERT_PROGRAM_SPACE_FILE_PATH: Path = Path(__file__).parent / 'expert-program-space.yml'
with open(file=EXPERT_PROGRAM_SPACE_FILE_PATH,
EXPERT_PROGRAMS_FILE_PATH: Path = Path(__file__).parent / 'expert-programs.yml'
with open(file=EXPERT_PROGRAMS_FILE_PATH,
buffering=-1,
encoding='utf-8',
errors='strict',
newline=None,
closefd=True,
opener=None) as f:
EXPERT_PROGRAM_SPACE: dict[ExpertPlanId, HTPDict] = yaml.safe_load(stream=f)
EXPERT_PROGRAMS: dict[ExpertPlanId, HTPDict] = yaml.safe_load(stream=f)

EXPERT_HTP_COMPANY_KEY: str = 'COMPANY'
EXPERT_HTP_PERIOD_KEY: str = 'PERIOD'
Expand Down
62 changes: 31 additions & 31 deletions examples/FinanceBench/htp_oodar_agent.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from argparse import ArgumentParser
from functools import cache

from openssa import Agent, ProgramSpace, HTP, HTPlanner, FileResource, LMConfig
from openssa import DANA, ProgramStore, HTP, HTPlanner, FileResource, LMConfig
from openssa.core.util.lm.huggingface import HuggingFaceLM
from openssa.core.util.lm.openai import OpenAILM, default_llama_index_openai_lm

# pylint: disable=wrong-import-order,wrong-import-position
from data_and_knowledge import (DocName, FbId, Answer, Doc, FB_ID_COL_NAME, DOC_NAMES_BY_FB_ID, QS_BY_FB_ID,
EXPERT_KNOWLEDGE, EXPERT_PROGRAM_SPACE, EXPERT_HTP_COMPANY_KEY, EXPERT_HTP_PERIOD_KEY)
EXPERT_KNOWLEDGE, EXPERT_PROGRAMS, EXPERT_HTP_COMPANY_KEY, EXPERT_HTP_PERIOD_KEY)
from util import QAFunc, enable_batch_qa_and_eval, log_qa_and_update_output_file


Expand All @@ -17,33 +17,33 @@ def get_main_lm(use_llama3: bool = False):


@cache
def get_or_create_expert_program_space(use_llama3: bool = False) -> ProgramSpace:
program_space = ProgramSpace(lm=get_main_lm(use_llama3=use_llama3))
def get_or_create_expert_program_store(use_llama3: bool = False) -> ProgramStore:
program_store = ProgramStore(lm=get_main_lm(use_llama3=use_llama3))

for program_name, htp_dict in EXPERT_PROGRAM_SPACE.items():
for program_name, htp_dict in EXPERT_PROGRAMS.items():
htp = HTP.from_dict(htp_dict)
program_space.add_or_update_program(name=program_name, description=htp.task.ask, program=htp)
program_store.add_or_update_program(name=program_name, description=htp.task.ask, program=htp)

return program_space
return program_store


@cache
def get_or_create_agent(doc_name: DocName, expert_knowledge: bool = False, expert_program_space: bool = False,
def get_or_create_agent(doc_name: DocName, expert_knowledge: bool = False, expert_programs: bool = False,
max_depth=3, max_subtasks_per_decomp=6,
use_llama3: bool = False,
llama_index_openai_lm_name: str = LMConfig.OPENAI_DEFAULT_MODEL) -> Agent:
llama_index_openai_lm_name: str = LMConfig.OPENAI_DEFAULT_MODEL) -> DANA:
# pylint: disable=too-many-arguments
return Agent(knowledge={EXPERT_KNOWLEDGE} if expert_knowledge else None,
return DANA(knowledge={EXPERT_KNOWLEDGE} if expert_knowledge else None,

program_space=(get_or_create_expert_program_space(use_llama3=use_llama3)
if expert_program_space
else ProgramSpace()),
program_store=(get_or_create_expert_program_store(use_llama3=use_llama3)
if expert_programs
else ProgramStore()),

programmer=HTPlanner(lm=get_main_lm(use_llama3=use_llama3),
max_depth=max_depth, max_subtasks_per_decomp=max_subtasks_per_decomp),
programmer=HTPlanner(lm=get_main_lm(use_llama3=use_llama3),
max_depth=max_depth, max_subtasks_per_decomp=max_subtasks_per_decomp),

resources={FileResource(path=Doc(name=doc_name).dir_path,
lm=default_llama_index_openai_lm(llama_index_openai_lm_name))})
resources={FileResource(path=Doc(name=doc_name).dir_path,
lm=default_llama_index_openai_lm(llama_index_openai_lm_name))})


@cache
Expand All @@ -69,16 +69,16 @@ def solve_with_knowledge(fb_id: FbId) -> Answer:

@enable_batch_qa_and_eval(output_name='HTP-OODAR-wProgSpace')
@log_qa_and_update_output_file(output_name='HTP-OODAR-wProgSpace')
def solve_with_program_space(fb_id: FbId) -> Answer:
return get_or_create_agent(doc_name=DOC_NAMES_BY_FB_ID[fb_id], expert_program_space=True).solve(
def solve_with_program_store(fb_id: FbId) -> Answer:
return get_or_create_agent(doc_name=DOC_NAMES_BY_FB_ID[fb_id], expert_programs=True).solve(
problem=QS_BY_FB_ID[fb_id],
adaptations_from_known_programs=get_or_create_adaptations(doc_name=DOC_NAMES_BY_FB_ID[fb_id]))


@enable_batch_qa_and_eval(output_name='HTP-OODAR-wKnowledge-wProgSpace')
@log_qa_and_update_output_file(output_name='HTP-OODAR-wKnowledge-wProgSpace')
def solve_with_knowledge_and_program_space(fb_id: FbId) -> Answer:
return get_or_create_agent(DOC_NAMES_BY_FB_ID[fb_id], expert_knowledge=True, expert_program_space=True).solve(
def solve_with_knowledge_and_program_store(fb_id: FbId) -> Answer:
return get_or_create_agent(DOC_NAMES_BY_FB_ID[fb_id], expert_knowledge=True, expert_programs=True).solve(
problem=QS_BY_FB_ID[fb_id],
adaptations_from_known_programs=get_or_create_adaptations(doc_name=DOC_NAMES_BY_FB_ID[fb_id]))

Expand All @@ -101,16 +101,16 @@ def solve_with_knowledge_with_llama3(fb_id: FbId) -> Answer:

@enable_batch_qa_and_eval(output_name='HTP-OODAR-wProgSpace-wLlama3')
@log_qa_and_update_output_file(output_name='HTP-OODAR-wProgSpace-wLlama3')
def solve_with_program_space_with_llama3(fb_id: FbId) -> Answer:
return get_or_create_agent(doc_name=DOC_NAMES_BY_FB_ID[fb_id], expert_program_space=True, use_llama3=True).solve(
def solve_with_program_store_with_llama3(fb_id: FbId) -> Answer:
return get_or_create_agent(doc_name=DOC_NAMES_BY_FB_ID[fb_id], expert_programs=True, use_llama3=True).solve(
problem=QS_BY_FB_ID[fb_id],
adaptations_from_known_programs=get_or_create_adaptations(doc_name=DOC_NAMES_BY_FB_ID[fb_id]))


@enable_batch_qa_and_eval(output_name='HTP-OODAR-wKnowledge-wProgSpace-wLlama3')
@log_qa_and_update_output_file(output_name='HTP-OODAR-wKnowledge-wProgSpace-wLlama3')
def solve_with_knowledge_and_program_space_with_llama3(fb_id: FbId) -> Answer:
return get_or_create_agent(DOC_NAMES_BY_FB_ID[fb_id], expert_knowledge=True, expert_program_space=True, use_llama3=True).solve( # noqa: E501
def solve_with_knowledge_and_program_store_with_llama3(fb_id: FbId) -> Answer:
return get_or_create_agent(DOC_NAMES_BY_FB_ID[fb_id], expert_knowledge=True, expert_programs=True, use_llama3=True).solve( # noqa: E501
problem=QS_BY_FB_ID[fb_id],
adaptations_from_known_programs=get_or_create_adaptations(doc_name=DOC_NAMES_BY_FB_ID[fb_id]))

Expand All @@ -119,22 +119,22 @@ def solve_with_knowledge_and_program_space_with_llama3(fb_id: FbId) -> Answer:
arg_parser = ArgumentParser()
arg_parser.add_argument('fb_id')
arg_parser.add_argument('--knowledge', action='store_true')
arg_parser.add_argument('--prog-space', action='store_true')
arg_parser.add_argument('--prog-store', action='store_true')
arg_parser.add_argument('--llama3', action='store_true')
args = arg_parser.parse_args()

match (args.knowledge, args.prog_space, args.llama3):
match (args.knowledge, args.prog_store, args.llama3):
case (False, False, False):
solve_func: QAFunc = solve

case (True, False, False):
solve_func: QAFunc = solve_with_knowledge

case (False, True, False):
solve_func: QAFunc = solve_with_program_space
solve_func: QAFunc = solve_with_program_store

case (True, True, False):
solve_func: QAFunc = solve_with_knowledge_and_program_space
solve_func: QAFunc = solve_with_knowledge_and_program_store

case (False, False, True):
solve_func: QAFunc = solve_with_llama3
Expand All @@ -143,10 +143,10 @@ def solve_with_knowledge_and_program_space_with_llama3(fb_id: FbId) -> Answer:
solve_func: QAFunc = solve_with_knowledge_with_llama3

case (False, True, True):
solve_func: QAFunc = solve_with_program_space_with_llama3
solve_func: QAFunc = solve_with_program_store_with_llama3

case (True, True, True):
solve_func: QAFunc = solve_with_knowledge_and_program_space_with_llama3
solve_func: QAFunc = solve_with_knowledge_and_program_store_with_llama3

solve_func(fb_id
if (fb_id := args.fb_id).startswith(FB_ID_COL_NAME)
Expand Down
24 changes: 12 additions & 12 deletions examples/FinanceBench/make.bat
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
SET TARGET=%1

IF "%TARGET%"=="agent-solve" GOTO agent-solve
IF "%TARGET%"=="agent-solve-w-prog-space" GOTO agent-solve-w-prog-space
IF "%TARGET%"=="agent-solve-w-prog-store" GOTO agent-solve-w-prog-store
IF "%TARGET%"=="agent-solve-w-knowledge" GOTO agent-solve-w-knowledge
IF "%TARGET%"=="agent-solve-w-knowledge-and-prog-space" GOTO agent-solve-w-knowledge-and-prog-space
IF "%TARGET%"=="agent-solve-w-knowledge-and-prog-store" GOTO agent-solve-w-knowledge-and-prog-store
IF "%TARGET%"=="agent-solve-w-llama3" GOTO agent-solve-w-llama3
IF "%TARGET%"=="agent-solve-w-prog-space-w-llama3" GOTO agent-solve-w-prog-space-w-llama3
IF "%TARGET%"=="agent-solve-w-prog-store-w-llama3" GOTO agent-solve-w-prog-store-w-llama3
IF "%TARGET%"=="agent-solve-w-knowledge-w-llama3" GOTO agent-solve-w-knowledge-w-llama3
IF "%TARGET%"=="agent-solve-w-knowledge-and-prog-space-w-llama3" GOTO agent-solve-w-knowledge-and-prog-space-w-llama3
IF "%TARGET%"=="agent-solve-w-knowledge-and-prog-store-w-llama3" GOTO agent-solve-w-knowledge-and-prog-store-w-llama3

IF "%TARGET%"=="openai-assist" GOTO openai-assist

Expand All @@ -37,12 +37,12 @@ IF "%TARGET%"=="streamlit-run" GOTO streamlit-run
poetry run python htp_oodar_agent.py %2 --knowledge
GOTO end

:agent-solve-w-prog-space
poetry run python htp_oodar_agent.py %2 --prog-space
:agent-solve-w-prog-store
poetry run python htp_oodar_agent.py %2 --prog-store
GOTO end

:agent-solve-w-knowledge-and-prog-space
poetry run python htp_oodar_agent.py %2 --knowledge --prog-space
:agent-solve-w-knowledge-and-prog-store
poetry run python htp_oodar_agent.py %2 --knowledge --prog-store
GOTO end

:agent-solve-w-llama3
Expand All @@ -53,12 +53,12 @@ IF "%TARGET%"=="streamlit-run" GOTO streamlit-run
poetry run python htp_oodar_agent.py %2 --knowledge --llama3
GOTO end

:agent-solve-w-prog-space-w-llama3
poetry run python htp_oodar_agent.py %2 --prog-space --llama3
:agent-solve-w-prog-store-w-llama3
poetry run python htp_oodar_agent.py %2 --prog-store --llama3
GOTO end

:agent-solve-w-knowledge-and-prog-space-w-llama3
poetry run python htp_oodar_agent.py %2 --knowledge --prog-space --llama3
:agent-solve-w-knowledge-and-prog-store-w-llama3
poetry run python htp_oodar_agent.py %2 --knowledge --prog-store --llama3
GOTO end


Expand Down
8 changes: 4 additions & 4 deletions examples/FinanceBench/streamlit-main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
from loguru import logger
import streamlit as st

from data_and_knowledge import DocName, Doc, DOC_NAMES, ExpertPlanId as TaskId, EXPERT_PROGRAM_SPACE
from data_and_knowledge import DocName, Doc, DOC_NAMES, ExpertPlanId as TaskId, EXPERT_PROGRAMS
from htp_oodar_agent import get_or_create_agent, get_or_create_adaptations
from rag_default import get_or_create_file_resource


TASK_IDS: list[TaskId] = list(EXPERT_PROGRAM_SPACE)
TASK_IDS: list[TaskId] = list(EXPERT_PROGRAMS)
TASK_IDS.insert(0, '')

DOC_NAMES: list[DocName] = sorted(set(DOC_NAMES)
Expand Down Expand Up @@ -38,7 +38,7 @@

@cache
def task_statement_template(task_id: TaskId, /) -> str:
return EXPERT_PROGRAM_SPACE[task_id]['task']
return EXPERT_PROGRAMS[task_id]['task']


@cache
Expand Down Expand Up @@ -142,7 +142,7 @@ def task_statement(task_id: TaskId, doc_name: DocName) -> str:
logger.level('DEBUG')

agent = get_or_create_agent(doc_name=st.session_state.doc_name,
expert_knowledge=True, expert_program_space=True,
expert_knowledge=True, expert_programs=True,
max_depth=3, max_subtasks_per_decomp=6,
llama_index_openai_lm_name='gpt-4o-mini')

Expand Down
6 changes: 3 additions & 3 deletions examples/japanese-easy-demo/main.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from pathlib import Path
from dotenv import load_dotenv
from openssa import Agent, FileResource
from openssa import DANA, FileResource

load_dotenv()

LOCAL_CACHE_DOCS_DIR_PATH: Path = Path(__file__).parent / '.data'


def get_or_create_agent() -> Agent:
return Agent(
def get_or_create_agent() -> DANA:
return DANA(
resources={FileResource(path=LOCAL_CACHE_DOCS_DIR_PATH)}
)

Expand Down
22 changes: 11 additions & 11 deletions examples/semiconductor/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,27 @@
from argparse import ArgumentParser
from functools import cache

from openssa import Agent, ProgramSpace, HTP, HTPlanner, OpenAILM
from openssa import DANA, ProgramStore, HTP, HTPlanner, OpenAILM

# pylint: disable=wrong-import-order
from data_and_knowledge import EXPERT_PROGRAM_SPACE
from data_and_knowledge import EXPERT_PROGRAMS
from semikong_lm import SemiKongLM


@cache
def get_or_create_agent(use_semikong_lm: bool = True, max_depth=2, max_subtasks_per_decomp=4) -> Agent:
def get_or_create_agent(use_semikong_lm: bool = True, max_depth=2, max_subtasks_per_decomp=4) -> DANA:
lm = (SemiKongLM if use_semikong_lm else OpenAILM).from_defaults()

program_space = ProgramSpace(lm=lm)
if EXPERT_PROGRAM_SPACE:
for program_name, htp_dict in EXPERT_PROGRAM_SPACE.items():
program_store = ProgramStore(lm=lm)
if EXPERT_PROGRAMS:
for program_name, htp_dict in EXPERT_PROGRAMS.items():
htp = HTP.from_dict(htp_dict)
program_space.add_or_update_program(name=program_name, description=htp.task.ask, program=htp)
program_store.add_or_update_program(name=program_name, description=htp.task.ask, program=htp)

return Agent(knowledge={},
program_space=program_space,
programmer=HTPlanner(lm=lm, max_depth=max_depth, max_subtasks_per_decomp=max_subtasks_per_decomp),
resources={})
return DANA(knowledge={},
program_store=program_store,
programmer=HTPlanner(lm=lm, max_depth=max_depth, max_subtasks_per_decomp=max_subtasks_per_decomp),
resources={})


if __name__ == '__main__':
Expand Down
6 changes: 3 additions & 3 deletions examples/semiconductor/data_and_knowledge.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
load_dotenv()


EXPERT_PROGRAM_SPACE_FILE_PATH: Path = Path(__file__).parent / 'expert-program-space.yml'
with open(file=EXPERT_PROGRAM_SPACE_FILE_PATH,
EXPERT_PROGRAMS_FILE_PATH: Path = Path(__file__).parent / 'expert-program-store.yml'
with open(file=EXPERT_PROGRAMS_FILE_PATH,
buffering=-1,
encoding='utf-8',
errors='strict',
newline=None,
closefd=True,
opener=None) as f:
EXPERT_PROGRAM_SPACE: dict[str, HTPDict] = yaml.safe_load(stream=f)
EXPERT_PROGRAMS: dict[str, HTPDict] = yaml.safe_load(stream=f)
4 changes: 2 additions & 2 deletions openssa/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
from pathlib import Path
import tomllib

from .core.agent.agent import Agent
from .core.agent.dana import DANA

from .core.program_space import ProgramSpace
from .core.program_store import ProgramStore
from .core.programming.hierarchical.plan import HTP
from .core.programming.hierarchical.planner import HTPlanner

Expand Down
Loading