mirror of
https://github.com/microsoft/autogen.git
synced 2025-09-25 08:05:36 +00:00
Notebook/hierarchy flow (#482)
* Notebook showing how to use select speaker to control conversation flow. * pytest associated with notebook. * Added llm_config to assistant and user proxy agent, and clarified why we set use_cache to false, as requested in the review. * Added a @pytest.mark.skipif decorator like other tests to run it only in one py version, 3.10 * Fixed config warning. * Removd llm_config to UserProxyAgent * Fixed minor typos. * Reran outputs * Remopved llm_config from user_proxy_agent * Colab Badge link updated. * pre-commit formatting changes. * Fixed base_url --------- Co-authored-by: Chi Wang <wang.chi@microsoft.com>
This commit is contained in:
parent
65ec0b19de
commit
2a96e4d9d2
392
notebook/agentchat_hierarchy_flow_using_select_speaker.ipynb
Normal file
392
notebook/agentchat_hierarchy_flow_using_select_speaker.ipynb
Normal file
File diff suppressed because one or more lines are too long
@ -1,92 +1,100 @@
|
||||
import sys
|
||||
import os
|
||||
import pytest
|
||||
|
||||
try:
|
||||
import openai
|
||||
|
||||
skip = False
|
||||
except ImportError:
|
||||
skip = True
|
||||
|
||||
|
||||
here = os.path.abspath(os.path.dirname(__file__))
|
||||
|
||||
|
||||
def run_notebook(input_nb, output_nb="executed_openai_notebook.ipynb", save=False):
|
||||
import nbformat
|
||||
from nbconvert.preprocessors import ExecutePreprocessor
|
||||
from nbconvert.preprocessors import CellExecutionError
|
||||
|
||||
try:
|
||||
nb_loc = os.path.join(here, os.pardir, "notebook")
|
||||
file_path = os.path.join(nb_loc, input_nb)
|
||||
with open(file_path) as nb_file:
|
||||
nb = nbformat.read(nb_file, as_version=4)
|
||||
preprocessor = ExecutePreprocessor(timeout=4800, kernel_name="python3")
|
||||
preprocessor.preprocess(nb, {"metadata": {"path": nb_loc}})
|
||||
|
||||
output_file_name = "executed_openai_notebook_output.txt"
|
||||
output_file = os.path.join(here, output_file_name)
|
||||
with open(output_file, "a") as nb_output_file:
|
||||
for cell in nb.cells:
|
||||
if cell.cell_type == "code" and "outputs" in cell:
|
||||
for output in cell.outputs:
|
||||
if "text" in output:
|
||||
nb_output_file.write(output["text"].strip() + "\n")
|
||||
elif "data" in output and "text/plain" in output["data"]:
|
||||
nb_output_file.write(output["data"]["text/plain"].strip() + "\n")
|
||||
except CellExecutionError:
|
||||
raise
|
||||
finally:
|
||||
if save:
|
||||
with open(os.path.join(here, output_nb), "w", encoding="utf-8") as nb_executed_file:
|
||||
nbformat.write(nb, nb_executed_file)
|
||||
|
||||
|
||||
@pytest.mark.skipif(
|
||||
skip or not sys.version.startswith("3.11"),
|
||||
reason="do not run if openai is not installed or py!=3.11",
|
||||
)
|
||||
def test_agentchat_auto_feedback_from_code(save=False):
|
||||
run_notebook("agentchat_auto_feedback_from_code_execution.ipynb", save=save)
|
||||
|
||||
|
||||
@pytest.mark.skipif(
|
||||
skip or not sys.version.startswith("3.10"),
|
||||
reason="do not run if openai is not installed or py!=3.10",
|
||||
)
|
||||
def _test_oai_completion(save=False):
|
||||
run_notebook("oai_completion.ipynb", save=save)
|
||||
|
||||
|
||||
@pytest.mark.skipif(
|
||||
skip or not sys.version.startswith("3.10"),
|
||||
reason="do not run if openai is not installed or py!=3.10",
|
||||
)
|
||||
def test_agentchat_function_call(save=False):
|
||||
run_notebook("agentchat_function_call.ipynb", save=save)
|
||||
|
||||
|
||||
@pytest.mark.skipif(
|
||||
skip or not sys.version.startswith("3.10"),
|
||||
reason="do not run if openai is not installed or py!=3.10",
|
||||
)
|
||||
def _test_agentchat_MathChat(save=False):
|
||||
run_notebook("agentchat_MathChat.ipynb", save=save)
|
||||
|
||||
|
||||
@pytest.mark.skipif(
|
||||
skip or not sys.version.startswith("3.11"),
|
||||
reason="do not run if openai is not installed or py!=3.11",
|
||||
)
|
||||
def _test_oai_chatgpt_gpt4(save=False):
|
||||
run_notebook("oai_chatgpt_gpt4.ipynb", save=save)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_agentchat_auto_feedback_from_code(save=True)
|
||||
# test_oai_chatgpt_gpt4(save=True)
|
||||
# test_oai_completion(save=True)
|
||||
# test_agentchat_MathChat(save=True)
|
||||
# test_agentchat_function_call(save=True)
|
||||
import sys
|
||||
import os
|
||||
import pytest
|
||||
|
||||
try:
|
||||
import openai
|
||||
|
||||
skip = False
|
||||
except ImportError:
|
||||
skip = True
|
||||
|
||||
|
||||
here = os.path.abspath(os.path.dirname(__file__))
|
||||
|
||||
|
||||
def run_notebook(input_nb, output_nb="executed_openai_notebook.ipynb", save=False):
|
||||
import nbformat
|
||||
from nbconvert.preprocessors import ExecutePreprocessor
|
||||
from nbconvert.preprocessors import CellExecutionError
|
||||
|
||||
try:
|
||||
nb_loc = os.path.join(here, os.pardir, "notebook")
|
||||
file_path = os.path.join(nb_loc, input_nb)
|
||||
with open(file_path) as nb_file:
|
||||
nb = nbformat.read(nb_file, as_version=4)
|
||||
preprocessor = ExecutePreprocessor(timeout=4800, kernel_name="python3")
|
||||
preprocessor.preprocess(nb, {"metadata": {"path": nb_loc}})
|
||||
|
||||
output_file_name = "executed_openai_notebook_output.txt"
|
||||
output_file = os.path.join(here, output_file_name)
|
||||
with open(output_file, "a") as nb_output_file:
|
||||
for cell in nb.cells:
|
||||
if cell.cell_type == "code" and "outputs" in cell:
|
||||
for output in cell.outputs:
|
||||
if "text" in output:
|
||||
nb_output_file.write(output["text"].strip() + "\n")
|
||||
elif "data" in output and "text/plain" in output["data"]:
|
||||
nb_output_file.write(output["data"]["text/plain"].strip() + "\n")
|
||||
except CellExecutionError:
|
||||
raise
|
||||
finally:
|
||||
if save:
|
||||
with open(os.path.join(here, output_nb), "w", encoding="utf-8") as nb_executed_file:
|
||||
nbformat.write(nb, nb_executed_file)
|
||||
|
||||
|
||||
@pytest.mark.skipif(
|
||||
skip or not sys.version.startswith("3.11"),
|
||||
reason="do not run if openai is not installed or py!=3.11",
|
||||
)
|
||||
def test_agentchat_auto_feedback_from_code(save=False):
|
||||
run_notebook("agentchat_auto_feedback_from_code_execution.ipynb", save=save)
|
||||
|
||||
|
||||
@pytest.mark.skipif(
|
||||
skip or not sys.version.startswith("3.10"),
|
||||
reason="do not run if openai is not installed or py!=3.10",
|
||||
)
|
||||
def _test_oai_completion(save=False):
|
||||
run_notebook("oai_completion.ipynb", save=save)
|
||||
|
||||
|
||||
@pytest.mark.skipif(
|
||||
skip or not sys.version.startswith("3.10"),
|
||||
reason="do not run if openai is not installed or py!=3.10",
|
||||
)
|
||||
def test_agentchat_function_call(save=False):
|
||||
run_notebook("agentchat_function_call.ipynb", save=save)
|
||||
|
||||
|
||||
@pytest.mark.skipif(
|
||||
skip or not sys.version.startswith("3.10"),
|
||||
reason="do not run if openai is not installed or py!=3.10",
|
||||
)
|
||||
def _test_agentchat_MathChat(save=False):
|
||||
run_notebook("agentchat_MathChat.ipynb", save=save)
|
||||
|
||||
|
||||
@pytest.mark.skipif(
|
||||
skip or not sys.version.startswith("3.11"),
|
||||
reason="do not run if openai is not installed or py!=3.11",
|
||||
)
|
||||
def _test_oai_chatgpt_gpt4(save=False):
|
||||
run_notebook("oai_chatgpt_gpt4.ipynb", save=save)
|
||||
|
||||
|
||||
@pytest.mark.skipif(
|
||||
skip or not sys.version.startswith("3.10"),
|
||||
reason="do not run if openai is not installed or py!=3.10",
|
||||
)
|
||||
def test_hierarchy_flow_using_select_speaker(save=False):
|
||||
run_notebook("agentchat_hierarchy_flow_using_select_speaker.ipynb", save=save)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_agentchat_auto_feedback_from_code(save=True)
|
||||
# test_oai_chatgpt_gpt4(save=True)
|
||||
# test_oai_completion(save=True)
|
||||
# test_agentchat_MathChat(save=True)
|
||||
# test_agentchat_function_call(save=True)
|
||||
|
Loading…
x
Reference in New Issue
Block a user