Spaces:
Configuration error
Configuration error
File size: 7,354 Bytes
447ebeb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
import json
import os
import sys
from datetime import datetime
from unittest.mock import AsyncMock, patch, MagicMock
sys.path.insert(
0, os.path.abspath("../..")
) # Adds the parent directory to the system path
import httpx
import pytest
from respx import MockRouter
import litellm
from litellm import Choices, Message, ModelResponse
from base_llm_unit_tests import BaseLLMChatTest, BaseOSeriesModelsTest
class TestAzureOpenAIO1(BaseOSeriesModelsTest, BaseLLMChatTest):
def get_base_completion_call_args(self):
return {
"model": "azure/o1",
"api_key": os.getenv("AZURE_OPENAI_O1_KEY"),
"api_base": "https://openai-prod-test.openai.azure.com",
"api_version": "2024-12-01-preview"
}
def get_client(self):
from openai import AzureOpenAI
return AzureOpenAI(
api_key="my-fake-o1-key",
base_url="https://openai-prod-test.openai.azure.com",
api_version="2024-02-15-preview",
)
def test_tool_call_no_arguments(self, tool_call_no_arguments):
"""Test that tool calls with no arguments is translated correctly. Relevant issue: https://github.com/BerriAI/litellm/issues/6833"""
pass
def test_basic_tool_calling(self):
pass
def test_prompt_caching(self):
"""Temporary override. o1 prompt caching is not working."""
pass
def test_override_fake_stream(self):
"""Test that native streaming is not supported for o1."""
router = litellm.Router(
model_list=[
{
"model_name": "azure/o1-preview",
"litellm_params": {
"model": "azure/o1-preview",
"api_key": "my-fake-o1-key",
"api_base": "https://openai-gpt-4-test-v-1.openai.azure.com",
},
"model_info": {
"supports_native_streaming": True,
},
}
]
)
## check model info
model_info = litellm.get_model_info(
model="azure/o1-preview", custom_llm_provider="azure"
)
assert model_info["supports_native_streaming"] is True
fake_stream = litellm.AzureOpenAIO1Config().should_fake_stream(
model="azure/o1-preview", stream=True
)
assert fake_stream is False
class TestAzureOpenAIO3(BaseOSeriesModelsTest):
def get_base_completion_call_args(self):
return {
"model": "azure/o3-mini",
"api_key": "my-fake-o1-key",
"api_base": "https://openai-gpt-4-test-v-1.openai.azure.com",
}
def get_client(self):
from openai import AzureOpenAI
return AzureOpenAI(
api_key="my-fake-o1-key",
base_url="https://openai-gpt-4-test-v-1.openai.azure.com",
api_version="2024-02-15-preview",
)
def test_azure_o3_streaming():
"""
Test that o3 models handles fake streaming correctly.
"""
from openai import AzureOpenAI
from litellm import completion
client = AzureOpenAI(
api_key="my-fake-o1-key",
base_url="https://openai-gpt-4-test-v-1.openai.azure.com",
api_version="2024-02-15-preview",
)
with patch.object(
client.chat.completions.with_raw_response, "create"
) as mock_create:
try:
completion(
model="azure/o3-mini",
messages=[{"role": "user", "content": "Hello, world!"}],
stream=True,
client=client,
)
except (
Exception
) as e: # expect output translation error as mock response doesn't return a json
print(e)
assert mock_create.call_count == 1
assert "stream" in mock_create.call_args.kwargs
def test_azure_o_series_routing():
"""
Allows user to pass model="azure/o_series/<any-deployment-name>" for explicit o_series model routing.
"""
from openai import AzureOpenAI
from litellm import completion
client = AzureOpenAI(
api_key="my-fake-o1-key",
base_url="https://openai-gpt-4-test-v-1.openai.azure.com",
api_version="2024-02-15-preview",
)
with patch.object(
client.chat.completions.with_raw_response, "create"
) as mock_create:
try:
completion(
model="azure/o_series/my-random-deployment-name",
messages=[{"role": "user", "content": "Hello, world!"}],
stream=True,
client=client,
)
except (
Exception
) as e: # expect output translation error as mock response doesn't return a json
print(e)
assert mock_create.call_count == 1
assert "stream" not in mock_create.call_args.kwargs
@patch("litellm.main.azure_o1_chat_completions._get_openai_client")
def test_openai_o_series_max_retries_0(mock_get_openai_client):
import litellm
litellm.set_verbose = True
response = litellm.completion(
model="azure/o1-preview",
messages=[{"role": "user", "content": "hi"}],
max_retries=0,
)
mock_get_openai_client.assert_called_once()
assert mock_get_openai_client.call_args.kwargs["max_retries"] == 0
@pytest.mark.asyncio
async def test_azure_o1_series_response_format_extra_params():
"""
Tool calling should work for all azure o_series models.
"""
litellm._turn_on_debug()
from openai import AsyncAzureOpenAI
litellm.set_verbose = True
client = AsyncAzureOpenAI(
api_key="fake-api-key",
base_url="https://openai-prod-test.openai.azure.com/openai/deployments/o1/chat/completions?api-version=2025-01-01-preview",
api_version="2025-01-01-preview"
)
tools = [{'type': 'function', 'function': {'name': 'get_current_time', 'description': 'Get the current time in a given location.', 'parameters': {'type': 'object', 'properties': {'location': {'type': 'string', 'description': 'The city name, e.g. San Francisco'}}, 'required': ['location']}}}]
response_format = {'type': 'json_object'}
tool_choice = "auto"
with patch.object(
client.chat.completions.with_raw_response, "create"
) as mock_client:
try:
await litellm.acompletion(
client=client,
model="azure/o_series/<my-deployment-name>",
api_key="xxxxx",
api_base="https://openai-prod-test.openai.azure.com/openai/deployments/o1/chat/completions?api-version=2025-01-01-preview",
api_version="2024-12-01-preview",
messages=[{"role": "user", "content": "Hello! return a json object"}],
tools=tools,
response_format=response_format,
tool_choice=tool_choice
)
except Exception as e:
print(f"Error: {e}")
mock_client.assert_called_once()
request_body = mock_client.call_args.kwargs
print("request_body: ", json.dumps(request_body, indent=4))
assert request_body["tools"] == tools
assert request_body["response_format"] == response_format
assert request_body["tool_choice"] == tool_choice
|