2023-11-11 15:30:40 +01:00
|
|
|
import pytest
|
|
|
|
from autogen import OpenAIWrapper, config_list_from_json, config_list_openai_aoai
|
|
|
|
from test_utils import OAI_CONFIG_LIST, KEY_LOC
|
|
|
|
|
|
|
|
try:
|
|
|
|
from openai import OpenAI
|
|
|
|
except ImportError:
|
|
|
|
skip = True
|
|
|
|
else:
|
|
|
|
skip = False
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skipif(skip, reason="openai>=1 not installed")
|
|
|
|
def test_aoai_chat_completion_stream():
|
|
|
|
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-11 15:30:40 +01:00
|
|
|
)
|
|
|
|
client = OpenAIWrapper(config_list=config_list)
|
2023-11-11 22:03:51 -08:00
|
|
|
response = client.create(messages=[{"role": "user", "content": "2+2="}], stream=True)
|
2023-11-11 15:30:40 +01:00
|
|
|
print(response)
|
2023-12-09 22:28:13 -05:00
|
|
|
print(client.extract_text_or_completion_object(response))
|
2023-11-11 15:30:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skipif(skip, reason="openai>=1 not installed")
|
|
|
|
def test_chat_completion_stream():
|
|
|
|
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={"model": ["gpt-3.5-turbo", "gpt-35-turbo"]},
|
2023-11-11 15:30:40 +01:00
|
|
|
)
|
|
|
|
client = OpenAIWrapper(config_list=config_list)
|
2023-11-11 22:03:51 -08:00
|
|
|
response = client.create(messages=[{"role": "user", "content": "1+1="}], stream=True)
|
2023-11-11 15:30:40 +01:00
|
|
|
print(response)
|
2023-12-09 22:28:13 -05:00
|
|
|
print(client.extract_text_or_completion_object(response))
|
2023-11-11 15:30:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skipif(skip, reason="openai>=1 not installed")
|
|
|
|
def test_chat_functions_stream():
|
|
|
|
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={"model": ["gpt-3.5-turbo", "gpt-35-turbo"]},
|
2023-11-11 15:30:40 +01:00
|
|
|
)
|
|
|
|
functions = [
|
|
|
|
{
|
|
|
|
"name": "get_current_weather",
|
|
|
|
"description": "Get the current weather",
|
|
|
|
"parameters": {
|
|
|
|
"type": "object",
|
|
|
|
"properties": {
|
|
|
|
"location": {
|
|
|
|
"type": "string",
|
|
|
|
"description": "The city and state, e.g. San Francisco, CA",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"required": ["location"],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
]
|
|
|
|
client = OpenAIWrapper(config_list=config_list)
|
|
|
|
response = client.create(
|
|
|
|
messages=[{"role": "user", "content": "What's the weather like today in San Francisco?"}],
|
|
|
|
functions=functions,
|
|
|
|
stream=True,
|
|
|
|
)
|
|
|
|
print(response)
|
2023-12-09 22:28:13 -05:00
|
|
|
print(client.extract_text_or_completion_object(response))
|
2023-11-11 15:30:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skipif(skip, reason="openai>=1 not installed")
|
|
|
|
def test_completion_stream():
|
|
|
|
config_list = config_list_openai_aoai(KEY_LOC)
|
|
|
|
client = OpenAIWrapper(config_list=config_list)
|
2023-11-11 22:03:51 -08:00
|
|
|
response = client.create(prompt="1+1=", model="gpt-3.5-turbo-instruct", stream=True)
|
2023-11-11 15:30:40 +01:00
|
|
|
print(response)
|
2023-12-09 22:28:13 -05:00
|
|
|
print(client.extract_text_or_completion_object(response))
|
2023-11-11 15:30:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
test_aoai_chat_completion_stream()
|
|
|
|
test_chat_completion_stream()
|
|
|
|
test_chat_functions_stream()
|
|
|
|
test_completion_stream()
|