2023-11-03 21:01:49 -07:00
|
|
|
import pytest
|
|
|
|
from autogen import OpenAIWrapper, config_list_from_json, config_list_openai_aoai
|
|
|
|
from test_utils import OAI_CONFIG_LIST, KEY_LOC
|
|
|
|
|
2023-12-09 22:28:13 -05:00
|
|
|
TOOL_ENABLED = False
|
2023-11-03 21:01:49 -07:00
|
|
|
try:
|
|
|
|
from openai import OpenAI
|
2023-12-09 22:28:13 -05:00
|
|
|
from openai.types.chat.chat_completion import ChatCompletionMessage
|
2023-11-03 21:01:49 -07:00
|
|
|
except ImportError:
|
|
|
|
skip = True
|
|
|
|
else:
|
|
|
|
skip = False
|
2023-12-09 22:28:13 -05:00
|
|
|
import openai
|
|
|
|
|
|
|
|
if openai.__version__ >= "1.1.0":
|
|
|
|
TOOL_ENABLED = True
|
2023-11-03 21:01:49 -07:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skipif(skip, reason="openai>=1 not installed")
|
|
|
|
def test_aoai_chat_completion():
|
|
|
|
config_list = config_list_from_json(
|
|
|
|
env_or_file=OAI_CONFIG_LIST,
|
|
|
|
file_location=KEY_LOC,
|
2023-12-25 17:07:20 +01:00
|
|
|
filter_dict={"api_type": ["azure"], "model": ["gpt-3.5-turbo", "gpt-35-turbo"]},
|
2023-11-03 21:01:49 -07:00
|
|
|
)
|
|
|
|
client = OpenAIWrapper(config_list=config_list)
|
2023-11-08 15:39:02 -08:00
|
|
|
# for config in config_list:
|
|
|
|
# print(config)
|
|
|
|
# client = OpenAIWrapper(**config)
|
|
|
|
# response = client.create(messages=[{"role": "user", "content": "2+2="}], cache_seed=None)
|
|
|
|
response = client.create(messages=[{"role": "user", "content": "2+2="}], cache_seed=None)
|
2023-11-03 21:01:49 -07:00
|
|
|
print(response)
|
2023-12-09 22:28:13 -05:00
|
|
|
print(client.extract_text_or_completion_object(response))
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skipif(skip and not TOOL_ENABLED, reason="openai>=1.1.0 not installed")
|
|
|
|
def test_oai_tool_calling_extraction():
|
|
|
|
config_list = config_list_from_json(
|
|
|
|
env_or_file=OAI_CONFIG_LIST,
|
|
|
|
file_location=KEY_LOC,
|
2023-12-25 17:07:20 +01:00
|
|
|
filter_dict={"api_type": ["azure"], "model": ["gpt-3.5-turbo", "gpt-35-turbo"]},
|
2023-12-09 22:28:13 -05:00
|
|
|
)
|
|
|
|
client = OpenAIWrapper(config_list=config_list)
|
|
|
|
response = client.create(
|
|
|
|
messages=[
|
|
|
|
{
|
|
|
|
"role": "user",
|
|
|
|
"content": "What is the weather in San Francisco?",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
tools=[
|
|
|
|
{
|
|
|
|
"type": "function",
|
|
|
|
"function": {
|
|
|
|
"name": "getCurrentWeather",
|
|
|
|
"description": "Get the weather in location",
|
|
|
|
"parameters": {
|
|
|
|
"type": "object",
|
|
|
|
"properties": {
|
|
|
|
"location": {"type": "string", "description": "The city and state e.g. San Francisco, CA"},
|
|
|
|
"unit": {"type": "string", "enum": ["c", "f"]},
|
|
|
|
},
|
|
|
|
"required": ["location"],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
],
|
|
|
|
)
|
|
|
|
print(response)
|
|
|
|
print(client.extract_text_or_completion_object(response))
|
2023-11-03 21:01:49 -07:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skipif(skip, reason="openai>=1 not installed")
|
|
|
|
def test_chat_completion():
|
|
|
|
config_list = config_list_from_json(
|
|
|
|
env_or_file=OAI_CONFIG_LIST,
|
|
|
|
file_location=KEY_LOC,
|
|
|
|
)
|
|
|
|
client = OpenAIWrapper(config_list=config_list)
|
|
|
|
response = client.create(messages=[{"role": "user", "content": "1+1="}])
|
|
|
|
print(response)
|
2023-12-09 22:28:13 -05:00
|
|
|
print(client.extract_text_or_completion_object(response))
|
2023-11-03 21:01:49 -07:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skipif(skip, reason="openai>=1 not installed")
|
|
|
|
def test_completion():
|
|
|
|
config_list = config_list_openai_aoai(KEY_LOC)
|
|
|
|
client = OpenAIWrapper(config_list=config_list)
|
|
|
|
response = client.create(prompt="1+1=", model="gpt-3.5-turbo-instruct")
|
|
|
|
print(response)
|
2023-12-09 22:28:13 -05:00
|
|
|
print(client.extract_text_or_completion_object(response))
|
2023-11-03 21:01:49 -07:00
|
|
|
|
|
|
|
|
2023-11-28 14:47:53 -05:00
|
|
|
@pytest.mark.skipif(skip, reason="openai>=1 not installed")
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"cache_seed, model",
|
|
|
|
[
|
|
|
|
(None, "gpt-3.5-turbo-instruct"),
|
|
|
|
(42, "gpt-3.5-turbo-instruct"),
|
|
|
|
(None, "text-ada-001"),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_cost(cache_seed, model):
|
|
|
|
config_list = config_list_openai_aoai(KEY_LOC)
|
|
|
|
client = OpenAIWrapper(config_list=config_list, cache_seed=cache_seed)
|
|
|
|
response = client.create(prompt="1+3=", model=model)
|
|
|
|
print(response.cost)
|
|
|
|
|
|
|
|
|
2023-12-03 16:06:46 -05:00
|
|
|
@pytest.mark.skipif(skip, reason="openai>=1 not installed")
|
|
|
|
def test_usage_summary():
|
|
|
|
config_list = config_list_openai_aoai(KEY_LOC)
|
|
|
|
client = OpenAIWrapper(config_list=config_list)
|
2023-12-05 13:09:05 -08:00
|
|
|
response = client.create(prompt="1+3=", model="gpt-3.5-turbo-instruct", cache_seed=None)
|
2023-12-03 16:06:46 -05:00
|
|
|
|
|
|
|
# usage should be recorded
|
|
|
|
assert client.actual_usage_summary["total_cost"] > 0, "total_cost should be greater than 0"
|
|
|
|
assert client.total_usage_summary["total_cost"] > 0, "total_cost should be greater than 0"
|
|
|
|
|
|
|
|
# check print
|
|
|
|
client.print_usage_summary()
|
|
|
|
|
|
|
|
# check update
|
|
|
|
client._update_usage_summary(response, use_cache=True)
|
|
|
|
assert (
|
2023-12-06 22:43:30 -05:00
|
|
|
client.total_usage_summary["total_cost"] == response.cost * 2
|
2023-12-03 16:06:46 -05:00
|
|
|
), "total_cost should be equal to response.cost * 2"
|
|
|
|
|
|
|
|
# check clear
|
|
|
|
client.clear_usage_summary()
|
|
|
|
assert client.actual_usage_summary is None, "actual_usage_summary should be None"
|
|
|
|
assert client.total_usage_summary is None, "total_usage_summary should be None"
|
|
|
|
|
|
|
|
# actual usage and all usage should be different
|
|
|
|
response = client.create(prompt="1+3=", model="gpt-3.5-turbo-instruct", cache_seed=42)
|
|
|
|
assert client.total_usage_summary["total_cost"] > 0, "total_cost should be greater than 0"
|
|
|
|
assert client.actual_usage_summary is None, "No actual cost should be recorded"
|
|
|
|
|
|
|
|
|
2023-11-03 21:01:49 -07:00
|
|
|
if __name__ == "__main__":
|
|
|
|
test_aoai_chat_completion()
|
2023-12-09 22:28:13 -05:00
|
|
|
test_oai_tool_calling_extraction()
|
2023-11-03 21:01:49 -07:00
|
|
|
test_chat_completion()
|
|
|
|
test_completion()
|
2023-11-28 14:47:53 -05:00
|
|
|
test_cost()
|
2023-12-03 16:06:46 -05:00
|
|
|
test_usage_summary()
|