Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refine the stream response handle to compatible with both 2023-03-15 and 2023-07-01 #88

Merged
merged 4 commits into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/promptflow-tools/promptflow/tools/aoai.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ def completion(
if stream:
def generator():
for chunk in response:
yield chunk.choices[0].text
if chunk.choices:
yield getattr(chunk.choices[0], "text", "")

# We must return the generator object, not using yield directly here.
# Otherwise, the function itself will become a generator, despite whether stream is True or False.
Expand Down
3 changes: 2 additions & 1 deletion src/promptflow-tools/promptflow/tools/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,8 @@ def post_process_chat_api_response(completion, stream, functions):

def generator():
for chunk in completion:
yield getattr(chunk.choices[0]["delta"], "content", "")
if chunk.choices:
yield getattr(chunk.choices[0]["delta"], "content", "")

# We must return the generator object, not using yield directly here.
# Otherwise, the function itself will become a generator, despite whether stream is True or False.
Expand Down
3 changes: 2 additions & 1 deletion src/promptflow-tools/promptflow/tools/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ def completion(
if stream:
def generator():
for chunk in response:
yield chunk.choices[0].text
if chunk.choices:
yield getattr(chunk.choices[0], "text", "")

# We must return the generator object, not using yield directly here.
# Otherwise, the function itself will become a generator, despite whether stream is True or False.
Expand Down
26 changes: 26 additions & 0 deletions src/promptflow-tools/tests/test_aoai.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ def test_aoai_completion(self, aoai_provider):
prompt=prompt_template, deployment_name="text-ada-001", stop=[], logit_bias={}
)

def test_aoai_stream_completion(self, aoai_provider):
prompt_template = "please complete this sentence: world war II "
# test whether tool can handle param "stop" with value empty list in stream mode
# as openai raises "[] is not valid under any of the given schemas - 'stop'"
aoai_provider.completion(
prompt=prompt_template, deployment_name="text-ada-001", stop=[], logit_bias={}, stream=True
)

def test_aoai_chat(self, aoai_provider, example_prompt_template, chat_history):
result = aoai_provider.chat(
prompt=example_prompt_template,
Expand Down Expand Up @@ -69,6 +77,24 @@ def test_aoai_chat_message_with_no_content(self, aoai_provider):
prompt = "user:\n"
aoai_provider.chat(prompt=prompt, deployment_name="gpt-35-turbo")

def test_aoai_stream_chat(self, aoai_provider, example_prompt_template, chat_history):
result = aoai_provider.chat(
prompt=example_prompt_template,
deployment_name="gpt-35-turbo",
max_tokens="32",
temperature=0,
user_input="Fill in more detalis about trend 2.",
chat_history=chat_history,
stream=True,
)
answer = ""
while True:
try:
answer += next(result)
except Exception:
break
assert "details about trend 2" in answer.lower()

@pytest.mark.parametrize(
"params, expected",
[
Expand Down
22 changes: 22 additions & 0 deletions src/promptflow-tools/tests/test_openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ def test_openai_completion(self, openai_provider):
prompt_template = "please complete this sentence: world war II "
openai_provider.completion(prompt=prompt_template)

def test_openai_stream_completion(self, openai_provider):
prompt_template = "please complete this sentence: world war II "
openai_provider.completion(prompt=prompt_template, stream=True)

def test_openai_completion_api(self, open_ai_connection):
prompt_template = "please complete this sentence: world war II "
completion(open_ai_connection, prompt=prompt_template)
Expand All @@ -31,6 +35,24 @@ def test_openai_chat(self, openai_provider, example_prompt_template, chat_histor
)
assert "details about trend 2" in result.lower()

def test_openai_stream_chat(self, openai_provider, example_prompt_template, chat_history):
result = openai_provider.chat(
prompt=example_prompt_template,
model="gpt-3.5-turbo",
max_tokens=32,
temperature=0,
user_input="Fill in more detalis about trend 2.",
chat_history=chat_history,
stream=True,
)
answer = ""
while True:
try:
answer += next(result)
except Exception:
break
assert "details about trend 2" in answer.lower()

def test_openai_chat_api(self, open_ai_connection, example_prompt_template, chat_history):
result = chat(
connection=open_ai_connection,
Expand Down
Loading