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
+32
View File
@@ -82,6 +82,7 @@ def run(tool_path, tool_func, tool_data):
value = getattr(mod, tool_func)(**tool_data)
return_to_llm(value)
dump_result()
def return_to_llm(value):
@@ -102,6 +103,37 @@ def return_to_llm(value):
writer.write(value_str)
def dump_result():
if not os.isatty(1):
return
if not os.getenv("LLM_OUTPUT"):
return
show_result = False
tool_name = os.environ["LLM_TOOL_NAME"].upper().replace("-", "_")
env_name = f"LLM_TOOL_DUMP_RESULT_{tool_name}"
env_value = os.getenv(env_name)
if os.getenv("LLM_TOOL_DUMP_RESULT") in ("1", "true"):
if env_value not in ("0", "false"):
show_result = True
else:
if 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")
if __name__ == "__main__":
try:
main()