You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_notebook.py 4.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import sys
  2. import os
  3. import pytest
  4. from conftest import skip_openai
  5. try:
  6. import openai
  7. except ImportError:
  8. skip = True
  9. else:
  10. skip = False or skip_openai
  11. here = os.path.abspath(os.path.dirname(__file__))
  12. def run_notebook(input_nb, output_nb="executed_openai_notebook.ipynb", save=False):
  13. import nbformat
  14. from nbconvert.preprocessors import ExecutePreprocessor
  15. from nbconvert.preprocessors import CellExecutionError
  16. try:
  17. nb_loc = os.path.join(here, os.pardir, "notebook")
  18. file_path = os.path.join(nb_loc, input_nb)
  19. with open(file_path) as nb_file:
  20. nb = nbformat.read(nb_file, as_version=4)
  21. preprocessor = ExecutePreprocessor(timeout=4800, kernel_name="python3")
  22. preprocessor.preprocess(nb, {"metadata": {"path": nb_loc}})
  23. output_file_name = "executed_openai_notebook_output.txt"
  24. output_file = os.path.join(here, output_file_name)
  25. with open(output_file, "a") as nb_output_file:
  26. for cell in nb.cells:
  27. if cell.cell_type == "code" and "outputs" in cell:
  28. for output in cell.outputs:
  29. if "text" in output:
  30. nb_output_file.write(output["text"].strip() + "\n")
  31. elif "data" in output and "text/plain" in output["data"]:
  32. nb_output_file.write(output["data"]["text/plain"].strip() + "\n")
  33. except CellExecutionError:
  34. raise
  35. finally:
  36. if save:
  37. with open(os.path.join(here, output_nb), "w", encoding="utf-8") as nb_executed_file:
  38. nbformat.write(nb, nb_executed_file)
  39. @pytest.mark.skipif(
  40. skip or not sys.version.startswith("3.10"),
  41. reason="do not run if openai is not installed or py!=3.10",
  42. )
  43. def test_agentchat_auto_feedback_from_code(save=False):
  44. run_notebook("agentchat_auto_feedback_from_code_execution.ipynb", save=save)
  45. @pytest.mark.skipif(
  46. skip or not sys.version.startswith("3.11"),
  47. reason="do not run if openai is not installed or py!=3.11",
  48. )
  49. def _test_oai_completion(save=False):
  50. run_notebook("oai_completion.ipynb", save=save)
  51. @pytest.mark.skipif(
  52. skip or not sys.version.startswith("3.12"),
  53. reason="do not run if openai is not installed or py!=3.12",
  54. )
  55. def test_agentchat_function_call(save=False):
  56. run_notebook("agentchat_function_call.ipynb", save=save)
  57. @pytest.mark.skipif(
  58. skip or not sys.version.startswith("3.10"),
  59. reason="do not run if openai is not installed or py!=3.10",
  60. )
  61. def test_agentchat_function_call_currency_calculator(save=False):
  62. run_notebook("agentchat_function_call_currency_calculator.ipynb", save=save)
  63. @pytest.mark.skipif(
  64. skip or not sys.version.startswith("3.11"),
  65. reason="do not run if openai is not installed or py!=3.11",
  66. )
  67. def test_agentchat_function_call_async(save=False):
  68. run_notebook("agentchat_function_call_async.ipynb", save=save)
  69. @pytest.mark.skipif(
  70. skip or not sys.version.startswith("3.12"),
  71. reason="do not run if openai is not installed or py!=3.12",
  72. )
  73. def _test_agentchat_MathChat(save=False):
  74. run_notebook("agentchat_MathChat.ipynb", save=save)
  75. @pytest.mark.skipif(
  76. skip or not sys.version.startswith("3.10"),
  77. reason="do not run if openai is not installed or py!=3.10",
  78. )
  79. def _test_oai_chatgpt_gpt4(save=False):
  80. run_notebook("oai_chatgpt_gpt4.ipynb", save=save)
  81. @pytest.mark.skipif(
  82. skip or not sys.version.startswith("3.11"),
  83. reason="do not run if openai is not installed or py!=3.11",
  84. )
  85. def _test_hierarchy_flow_using_select_speaker(save=False):
  86. # TODO: recover this test after rewriting after the new group chat api
  87. run_notebook("agentchat_hierarchy_flow_using_select_speaker.ipynb", save=save)
  88. @pytest.mark.skipif(
  89. skip or not sys.version.startswith("3.12"),
  90. reason="do not run if openai is not installed or py!=3.12",
  91. )
  92. def test_graph_modelling_language_using_select_speaker(save=False):
  93. run_notebook("agentchat_graph_modelling_language_using_select_speaker.ipynb", save=save)
  94. @pytest.mark.skipif(
  95. skip or not sys.version.startswith("3.10"),
  96. reason="do not run if openai is not installed or py!=3.10",
  97. )
  98. def test_agentchat_cost_token_tracking(save=False):
  99. run_notebook("agentchat_cost_token_tracking.ipynb", save=save)
  100. if __name__ == "__main__":
  101. # test_agentchat_auto_feedback_from_code(save=True)
  102. # test_oai_chatgpt_gpt4(save=True)
  103. # test_oai_completion(save=True)
  104. # test_agentchat_MathChat(save=True)
  105. # test_agentchat_function_call(save=True)
  106. # test_graph_modelling_language_using_select_speaker(save=True)
  107. test_agentchat_function_call_async(save=True)