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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import sys
  2. import os
  3. import pytest
  4. try:
  5. import openai
  6. skip = False
  7. except ImportError:
  8. skip = True
  9. here = os.path.abspath(os.path.dirname(__file__))
  10. def run_notebook(input_nb, output_nb="executed_openai_notebook.ipynb", save=False):
  11. import nbformat
  12. from nbconvert.preprocessors import ExecutePreprocessor
  13. from nbconvert.preprocessors import CellExecutionError
  14. try:
  15. nb_loc = os.path.join(here, os.pardir, "notebook")
  16. file_path = os.path.join(nb_loc, input_nb)
  17. with open(file_path) as nb_file:
  18. nb = nbformat.read(nb_file, as_version=4)
  19. preprocessor = ExecutePreprocessor(timeout=4800, kernel_name="python3")
  20. preprocessor.preprocess(nb, {"metadata": {"path": nb_loc}})
  21. output_file_name = "executed_openai_notebook_output.txt"
  22. output_file = os.path.join(here, output_file_name)
  23. with open(output_file, "a") as nb_output_file:
  24. for cell in nb.cells:
  25. if cell.cell_type == "code" and "outputs" in cell:
  26. for output in cell.outputs:
  27. if "text" in output:
  28. nb_output_file.write(output["text"].strip() + "\n")
  29. elif "data" in output and "text/plain" in output["data"]:
  30. nb_output_file.write(output["data"]["text/plain"].strip() + "\n")
  31. except CellExecutionError:
  32. raise
  33. finally:
  34. if save:
  35. with open(os.path.join(here, output_nb), "w", encoding="utf-8") as nb_executed_file:
  36. nbformat.write(nb, nb_executed_file)
  37. @pytest.mark.skipif(
  38. skip or not sys.version.startswith("3.11"),
  39. reason="do not run if openai is not installed or py!=3.11",
  40. )
  41. def test_agentchat_auto_feedback_from_code(save=False):
  42. run_notebook("agentchat_auto_feedback_from_code_execution.ipynb", save=save)
  43. @pytest.mark.skipif(
  44. skip or not sys.version.startswith("3.10"),
  45. reason="do not run if openai is not installed or py!=3.10",
  46. )
  47. def _test_oai_completion(save=False):
  48. run_notebook("oai_completion.ipynb", save=save)
  49. @pytest.mark.skipif(
  50. skip or not sys.version.startswith("3.10"),
  51. reason="do not run if openai is not installed or py!=3.10",
  52. )
  53. def test_agentchat_function_call(save=False):
  54. run_notebook("agentchat_function_call.ipynb", save=save)
  55. @pytest.mark.skipif(
  56. skip or not sys.version.startswith("3.10"),
  57. reason="do not run if openai is not installed or py!=3.10",
  58. )
  59. def _test_agentchat_MathChat(save=False):
  60. run_notebook("agentchat_MathChat.ipynb", save=save)
  61. @pytest.mark.skipif(
  62. skip or not sys.version.startswith("3.11"),
  63. reason="do not run if openai is not installed or py!=3.11",
  64. )
  65. def _test_oai_chatgpt_gpt4(save=False):
  66. run_notebook("oai_chatgpt_gpt4.ipynb", save=save)
  67. @pytest.mark.skipif(
  68. skip or not sys.version.startswith("3.10"),
  69. reason="do not run if openai is not installed or py!=3.10",
  70. )
  71. def test_hierarchy_flow_using_select_speaker(save=False):
  72. run_notebook("agentchat_hierarchy_flow_using_select_speaker.ipynb", save=save)
  73. if __name__ == "__main__":
  74. test_agentchat_auto_feedback_from_code(save=True)
  75. # test_oai_chatgpt_gpt4(save=True)
  76. # test_oai_completion(save=True)
  77. # test_agentchat_MathChat(save=True)
  78. # test_agentchat_function_call(save=True)