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