feat: controls displaying the results from function call (#111)

This commit is contained in:
sigoden
2024-10-18 18:28:03 +08:00
committed by GitHub
parent b5377bf28b
commit 6eb391bdc7
6 changed files with 235 additions and 20 deletions
+44 -3
View File
@@ -11,7 +11,7 @@ def main():
agent_data = parse_raw_data(raw_data)
root_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
setup_env(root_dir, agent_name)
setup_env(root_dir, agent_name, agent_func)
agent_tools_path = os.path.join(root_dir, f"agents/{agent_name}/tools.py")
run(agent_tools_path, agent_func, agent_data)
@@ -49,10 +49,11 @@ def parse_argv(this_file_name):
return agent_name, agent_func, agent_data
def setup_env(root_dir, agent_name):
def setup_env(root_dir, agent_name, agent_func):
load_env(os.path.join(root_dir, ".env"))
os.environ["LLM_ROOT_DIR"] = root_dir
os.environ["LLM_AGENT_NAME"] = agent_name
os.environ["LLM_AGENT_FUNC"] = agent_func
os.environ["LLM_AGENT_ROOT_DIR"] = os.path.join(root_dir, "agents", agent_name)
os.environ["LLM_AGENT_CACHE_DIR"] = os.path.join(root_dir, "cache", agent_name)
@@ -86,6 +87,42 @@ def run(agent_path, agent_func, agent_data):
value = getattr(mod, agent_func)(**agent_data)
return_to_llm(value)
dump_result()
def dump_result():
if not os.isatty(1):
return
if not os.getenv("LLM_OUTPUT"):
return
show_result = False
agent_name = os.environ["LLM_AGENT_NAME"].upper().replace("-", "_")
agent_env_name = f"LLM_AGENT_DUMP_RESULT_{agent_name}"
agent_env_value = os.getenv(agent_env_name)
func_name = os.environ["LLM_AGENT_FUNC"].upper().replace("-", "_")
func_env_name = f"{agent_env_name}_{func_name}"
func_env_value = os.getenv(func_env_name)
if agent_env_value in ("1", "true"):
if func_env_value not in ("0", "false"):
show_result = True
else:
if func_env_value in ("1", "true"):
show_result = True
if not show_result:
return
try:
with open(os.environ["LLM_OUTPUT"], "r", encoding="utf-8") as f:
data = f.read()
except:
return
print(f"\x1b[2m----------------------\n{data}\n----------------------\x1b[0m")
def return_to_llm(value):
@@ -107,4 +144,8 @@ def return_to_llm(value):
if __name__ == "__main__":
main()
try:
main()
except Exception as e:
print(e, file=sys.stderr)
sys.exit(1)