add tool call to chat completion agent (#35)

* add tool call to chat completion agent

* refactor function executor; tool executor in chat completion agent

* update example

* update orchestrator chat demo

* handle function execution result message type

* format

* temp fix for examples.

* fix

* update chat completion agent
This commit is contained in:
Eric Zhu
2024-05-30 09:01:35 -07:00
committed by GitHub
parent 2dc7af87ef
commit 04c30596ed
15 changed files with 516 additions and 361 deletions

View File

@@ -3,12 +3,17 @@
import tempfile
import pytest
from agnext.agent_components.code_executor import LocalCommandLineCodeExecutor, CodeBlock
from agnext.agent_components.func_with_reqs import FunctionWithRequirements, with_requirements
import polars
import pytest
from agnext.agent_components.code_executor import (
CodeBlock,
LocalCommandLineCodeExecutor,
)
from agnext.agent_components.func_with_reqs import (
FunctionWithRequirements,
with_requirements,
)
def add_two_numbers(a: int, b: int) -> int:
"""Add two numbers together."""
@@ -46,7 +51,9 @@ def function_missing_reqs() -> "polars.DataFrame":
def test_can_load_function_with_reqs() -> None:
with tempfile.TemporaryDirectory() as temp_dir:
executor = LocalCommandLineCodeExecutor(work_dir=temp_dir, functions=[load_data])
executor = LocalCommandLineCodeExecutor(
work_dir=temp_dir, functions=[load_data]
)
code = f"""from {executor.functions_module} import load_data
import polars
@@ -65,7 +72,9 @@ print(data['name'][0])"""
def test_can_load_function() -> None:
with tempfile.TemporaryDirectory() as temp_dir:
executor = LocalCommandLineCodeExecutor(work_dir=temp_dir, functions=[add_two_numbers])
executor = LocalCommandLineCodeExecutor(
work_dir=temp_dir, functions=[add_two_numbers]
)
code = f"""from {executor.functions_module} import add_two_numbers
print(add_two_numbers(1, 2))"""
@@ -80,7 +89,9 @@ print(add_two_numbers(1, 2))"""
def test_fails_for_function_incorrect_import() -> None:
with tempfile.TemporaryDirectory() as temp_dir:
executor = LocalCommandLineCodeExecutor(work_dir=temp_dir, functions=[function_incorrect_import])
executor = LocalCommandLineCodeExecutor(
work_dir=temp_dir, functions=[function_incorrect_import]
)
code = f"""from {executor.functions_module} import function_incorrect_import
function_incorrect_import()"""
@@ -94,7 +105,9 @@ function_incorrect_import()"""
def test_fails_for_function_incorrect_dep() -> None:
with tempfile.TemporaryDirectory() as temp_dir:
executor = LocalCommandLineCodeExecutor(work_dir=temp_dir, functions=[function_incorrect_dep])
executor = LocalCommandLineCodeExecutor(
work_dir=temp_dir, functions=[function_incorrect_dep]
)
code = f"""from {executor.functions_module} import function_incorrect_dep
function_incorrect_dep()"""
@@ -106,10 +119,11 @@ function_incorrect_dep()"""
)
def test_formatted_prompt() -> None:
with tempfile.TemporaryDirectory() as temp_dir:
executor = LocalCommandLineCodeExecutor(work_dir=temp_dir, functions=[add_two_numbers])
executor = LocalCommandLineCodeExecutor(
work_dir=temp_dir, functions=[add_two_numbers]
)
result = executor.format_functions_for_prompt()
assert (
@@ -140,7 +154,6 @@ def add_two_numbers(a: int, b: int) -> int:
)
def test_can_load_str_function_with_reqs() -> None:
with tempfile.TemporaryDirectory() as temp_dir:
func = FunctionWithRequirements.from_str(