-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable streaming support for openai v1 (#597)
* Enable streaming support for openai v1 * Added tests for openai client streaming * Fix test_completion_stream
- Loading branch information
Showing
2 changed files
with
152 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
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, | ||
filter_dict={"api_type": ["azure"], "model": ["gpt-3.5-turbo"]}, | ||
) | ||
client = OpenAIWrapper(config_list=config_list) | ||
response = client.create(messages=[{"role": "user", "content": "2+2="}], seed=None, stream=True) | ||
print(response) | ||
print(client.extract_text_or_function_call(response)) | ||
|
||
|
||
@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, | ||
filter_dict={"model": ["gpt-3.5-turbo"]}, | ||
) | ||
client = OpenAIWrapper(config_list=config_list) | ||
response = client.create(messages=[{"role": "user", "content": "1+1="}], seed=None, stream=True) | ||
print(response) | ||
print(client.extract_text_or_function_call(response)) | ||
|
||
|
||
@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, | ||
filter_dict={"model": ["gpt-3.5-turbo"]}, | ||
) | ||
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, | ||
seed=None, | ||
stream=True, | ||
) | ||
print(response) | ||
print(client.extract_text_or_function_call(response)) | ||
|
||
|
||
@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) | ||
response = client.create(prompt="1+1=", model="gpt-3.5-turbo-instruct", seed=None, stream=True) | ||
print(response) | ||
print(client.extract_text_or_function_call(response)) | ||
|
||
|
||
if __name__ == "__main__": | ||
test_aoai_chat_completion_stream() | ||
test_chat_completion_stream() | ||
test_chat_functions_stream() | ||
test_completion_stream() |