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.7 kB

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