Skip to content
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
2 changes: 1 addition & 1 deletion sdk/openai/azure-openai/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@


# for pytest.parametrize
GA = "2024-06-01"
GA = "2024-10-21"
PREVIEW = "2024-10-01-preview"
LATEST = PREVIEW

Expand Down
57 changes: 54 additions & 3 deletions sdk/openai/azure-openai/tests/test_chat_completions.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,15 @@ def test_chat_completion(self, client, api_type, api_version, **kwargs):
@configure
@pytest.mark.parametrize(
"api_type, api_version",
[(AZURE, GA), (AZURE, PREVIEW), (OPENAI, "v1")]
[(GPT_4_AZURE, GA), (GPT_4_AZURE, PREVIEW), (OPENAI, "v1")]
)
def test_streamed_chat_completions(self, client, api_type, api_version, **kwargs):
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "How do I bake a chocolate cake?"}
]

response = client.chat.completions.create(messages=messages, stream=True, **kwargs)
response = client.chat.completions.create(messages=messages, stream=True, stream_options={"include_usage": True}, **kwargs)

for completion in response:
# API versions after 2023-05-15 send an empty first completion with RAI
Expand All @@ -100,6 +100,10 @@ def test_streamed_chat_completions(self, client, api_type, api_version, **kwargs
for c in completion.choices:
assert c.index is not None
assert c.delta is not None
if completion.usage:
assert completion.usage.completion_tokens is not None
assert completion.usage.prompt_tokens is not None
assert completion.usage.total_tokens == completion.usage.completion_tokens + completion.usage.prompt_tokens

@configure
@pytest.mark.parametrize(
Expand Down Expand Up @@ -1164,7 +1168,7 @@ def test_chat_completion_logprobs(self, client, api_type, api_version, **kwargs)
assert logprob.bytes is not None

@configure
@pytest.mark.parametrize("api_type, api_version", [(GPT_4_AZURE, PREVIEW), (GPT_4_OPENAI, "v1")])
@pytest.mark.parametrize("api_type, api_version", [(GPT_4_AZURE, PREVIEW), (GPT_4_AZURE, GA), (GPT_4_OPENAI, "v1")])
def test_chat_completion_structured_outputs(self, client, api_type, api_version, **kwargs):

class Step(BaseModel):
Expand Down Expand Up @@ -1202,3 +1206,50 @@ class MathResponse(BaseModel):
assert step.explanation
assert step.output
assert completion.choices[0].message.parsed.final_answer

@configure
@pytest.mark.parametrize("api_type, api_version", [(GPT_4_AZURE, GA), (GPT_4_AZURE, PREVIEW), (GPT_4_OPENAI, "v1")])
def test_chat_completion_parallel_tool_calls_disable(self, client, api_type, api_version, **kwargs):
messages = [
{"role": "system", "content": "Don't make assumptions about what values to plug into tools. Ask for clarification if a user request is ambiguous."},
{"role": "user", "content": "What's the weather like today in Seattle and Los Angeles?"}
]
tools = [
{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA",
},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
},
"required": ["location"],
},
}
}
]

completion = client.chat.completions.create(
messages=messages,
tools=tools,
parallel_tool_calls=False,
**kwargs
)
assert completion.id
assert completion.object == "chat.completion"
assert completion.model
assert completion.created
assert completion.usage.completion_tokens is not None
assert completion.usage.prompt_tokens is not None
assert completion.usage.total_tokens == completion.usage.completion_tokens + completion.usage.prompt_tokens
assert len(completion.choices) == 1
assert completion.choices[0].finish_reason
assert completion.choices[0].index is not None
assert completion.choices[0].message.role
assert len(completion.choices[0].message.tool_calls) == 1
58 changes: 55 additions & 3 deletions sdk/openai/azure-openai/tests/test_chat_completions_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,15 @@ async def test_chat_completion(self, client_async, api_type, api_version, **kwar
@pytest.mark.asyncio
@pytest.mark.parametrize(
"api_type, api_version",
[(AZURE, GA), (AZURE, PREVIEW), (OPENAI, "v1")]
[(GPT_4_AZURE, GA), (GPT_4_AZURE, PREVIEW), (OPENAI, "v1")]
)
async def test_streamed_chat_completions(self, client_async, api_type, api_version, **kwargs):
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "How do I bake a chocolate cake?"}
]

response = await client_async.chat.completions.create(messages=messages, stream=True, **kwargs)
response = await client_async.chat.completions.create(messages=messages, stream=True, stream_options={"include_usage": True}, **kwargs)

async for completion in response:
# API versions after 2023-05-15 send an empty first completion with RAI
Expand All @@ -103,6 +103,10 @@ async def test_streamed_chat_completions(self, client_async, api_type, api_versi
for c in completion.choices:
assert c.index is not None
assert c.delta is not None
if completion.usage:
assert completion.usage.completion_tokens is not None
assert completion.usage.prompt_tokens is not None
assert completion.usage.total_tokens == completion.usage.completion_tokens + completion.usage.prompt_tokens

@configure_async
@pytest.mark.asyncio
Expand Down Expand Up @@ -1191,7 +1195,7 @@ async def test_chat_completion_logprobs(self, client_async, api_type, api_versio

@configure_async
@pytest.mark.asyncio
@pytest.mark.parametrize("api_type, api_version", [(GPT_4_AZURE, PREVIEW), (GPT_4_OPENAI, "v1")])
@pytest.mark.parametrize("api_type, api_version", [(GPT_4_AZURE, PREVIEW), (GPT_4_AZURE, GA), (GPT_4_OPENAI, "v1")])
async def test_chat_completion_structured_outputs(self, client_async, api_type, api_version, **kwargs):

class Step(BaseModel):
Expand Down Expand Up @@ -1228,3 +1232,51 @@ class MathResponse(BaseModel):
assert step.explanation
assert step.output
assert completion.choices[0].message.parsed.final_answer

@configure_async
@pytest.mark.asyncio
@pytest.mark.parametrize("api_type, api_version", [(GPT_4_AZURE, GA), (GPT_4_AZURE, PREVIEW), (GPT_4_OPENAI, "v1")])
async def test_chat_completion_parallel_tool_calls_disable(self, client_async, api_type, api_version, **kwargs):
messages = [
{"role": "system", "content": "Don't make assumptions about what values to plug into tools. Ask for clarification if a user request is ambiguous."},
{"role": "user", "content": "What's the weather like today in Seattle and Los Angeles?"}
]
tools = [
{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA",
},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
},
"required": ["location"],
},
}
}
]

completion = await client_async.chat.completions.create(
messages=messages,
tools=tools,
parallel_tool_calls=False,
**kwargs
)
assert completion.id
assert completion.object == "chat.completion"
assert completion.model
assert completion.created
assert completion.usage.completion_tokens is not None
assert completion.usage.prompt_tokens is not None
assert completion.usage.total_tokens == completion.usage.completion_tokens + completion.usage.prompt_tokens
assert len(completion.choices) == 1
assert completion.choices[0].finish_reason
assert completion.choices[0].index is not None
assert completion.choices[0].message.role
assert len(completion.choices[0].message.tool_calls) == 1