2
2
from autogen import OpenAIWrapper , config_list_from_json , config_list_openai_aoai
3
3
from test_utils import OAI_CONFIG_LIST , KEY_LOC
4
4
5
+ TOOL_ENABLED = False
5
6
try :
6
7
from openai import OpenAI
8
+ from openai .types .chat .chat_completion import ChatCompletionMessage
7
9
except ImportError :
8
10
skip = True
9
11
else :
10
12
skip = False
13
+ import openai
14
+
15
+ if openai .__version__ >= "1.1.0" :
16
+ TOOL_ENABLED = True
11
17
12
18
13
19
@pytest .mark .skipif (skip , reason = "openai>=1 not installed" )
@@ -24,7 +30,44 @@ def test_aoai_chat_completion():
24
30
# response = client.create(messages=[{"role": "user", "content": "2+2="}], cache_seed=None)
25
31
response = client .create (messages = [{"role" : "user" , "content" : "2+2=" }], cache_seed = None )
26
32
print (response )
27
- print (client .extract_text_or_function_call (response ))
33
+ print (client .extract_text_or_completion_object (response ))
34
+
35
+
36
+ @pytest .mark .skipif (skip and not TOOL_ENABLED , reason = "openai>=1.1.0 not installed" )
37
+ def test_oai_tool_calling_extraction ():
38
+ config_list = config_list_from_json (
39
+ env_or_file = OAI_CONFIG_LIST ,
40
+ file_location = KEY_LOC ,
41
+ filter_dict = {"api_type" : ["azure" ], "model" : ["gpt-3.5-turbo" ]},
42
+ )
43
+ client = OpenAIWrapper (config_list = config_list )
44
+ response = client .create (
45
+ messages = [
46
+ {
47
+ "role" : "user" ,
48
+ "content" : "What is the weather in San Francisco?" ,
49
+ },
50
+ ],
51
+ tools = [
52
+ {
53
+ "type" : "function" ,
54
+ "function" : {
55
+ "name" : "getCurrentWeather" ,
56
+ "description" : "Get the weather in location" ,
57
+ "parameters" : {
58
+ "type" : "object" ,
59
+ "properties" : {
60
+ "location" : {"type" : "string" , "description" : "The city and state e.g. San Francisco, CA" },
61
+ "unit" : {"type" : "string" , "enum" : ["c" , "f" ]},
62
+ },
63
+ "required" : ["location" ],
64
+ },
65
+ },
66
+ }
67
+ ],
68
+ )
69
+ print (response )
70
+ print (client .extract_text_or_completion_object (response ))
28
71
29
72
30
73
@pytest .mark .skipif (skip , reason = "openai>=1 not installed" )
@@ -36,7 +79,7 @@ def test_chat_completion():
36
79
client = OpenAIWrapper (config_list = config_list )
37
80
response = client .create (messages = [{"role" : "user" , "content" : "1+1=" }])
38
81
print (response )
39
- print (client .extract_text_or_function_call (response ))
82
+ print (client .extract_text_or_completion_object (response ))
40
83
41
84
42
85
@pytest .mark .skipif (skip , reason = "openai>=1 not installed" )
@@ -45,7 +88,7 @@ def test_completion():
45
88
client = OpenAIWrapper (config_list = config_list )
46
89
response = client .create (prompt = "1+1=" , model = "gpt-3.5-turbo-instruct" )
47
90
print (response )
48
- print (client .extract_text_or_function_call (response ))
91
+ print (client .extract_text_or_completion_object (response ))
49
92
50
93
51
94
@pytest .mark .skipif (skip , reason = "openai>=1 not installed" )
@@ -96,6 +139,7 @@ def test_usage_summary():
96
139
97
140
if __name__ == "__main__" :
98
141
test_aoai_chat_completion ()
142
+ test_oai_tool_calling_extraction ()
99
143
test_chat_completion ()
100
144
test_completion ()
101
145
test_cost ()
0 commit comments