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

test: added multi-round tests for azure_openai_model #1531

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
65 changes: 64 additions & 1 deletion test/models/test_azure_openai_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,43 @@
"""

import re
from unittest.mock import MagicMock, patch

import pytest
from openai.types.chat.chat_completion import Choice
from openai.types.chat.chat_completion_message import ChatCompletionMessage
from openai.types.completion_usage import CompletionUsage

from camel.configs import ChatGPTConfig
from camel.models import AzureOpenAIModel, ModelFactory
from camel.types import ModelPlatformType, ModelType
from camel.types import ChatCompletion, ModelPlatformType, ModelType
from camel.utils import OpenAITokenCounter

model_backend_rsp = ChatCompletion(
id="mock_response_id",
choices=[
Choice(
finish_reason="stop",
index=0,
logprobs=None,
message=ChatCompletionMessage(
content="This is a mock response content.",
role="assistant",
function_call=None,
tool_calls=None,
),
)
],
created=123456789,
model="gpt-4o-2024-05-13",
object="chat.completion",
usage=CompletionUsage(
completion_tokens=32,
prompt_tokens=15,
total_tokens=47,
),
)


@pytest.mark.model_backend
@pytest.mark.parametrize(
Expand Down Expand Up @@ -72,6 +101,40 @@ def test_openai_model_create(model_type: ModelType):
assert model.model_type == model_type


@pytest.mark.model_backend
@pytest.mark.parametrize(
"model_type",
[
ModelType.GPT_3_5_TURBO,
ModelType.GPT_4,
ModelType.GPT_4_TURBO,
ModelType.GPT_4O,
ModelType.GPT_4O_MINI,
],
)
@patch("camel.models.azure_openai_model.AzureOpenAI")
def test_openai_model_run(
mock_azure_openai, model_type: ModelType, max_steps=5
):
# Mock the client creation function AzureOpenAI
mock_client = MagicMock()
mock_azure_openai.return_value = mock_client
mock_client.chat.completions.create.return_value = model_backend_rsp

model = ModelFactory.create(
model_platform=ModelPlatformType.AZURE,
model_type=model_type,
model_config_dict=ChatGPTConfig(temperature=0.8, n=3).as_dict(),
)
for _ in range(max_steps):
model_inference = model.run(messages=[])
if isinstance(model_inference, ChatCompletion):
assert (
model_inference.choices[0].message.content
== model_backend_rsp.choices[0].message.content
)


@pytest.mark.model_backend
def test_openai_model_unexpected_argument():
model_type = ModelType.GPT_4
Expand Down
Loading