forked from ogx-ai/ogx-client-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_deprecated_inference.py
More file actions
141 lines (110 loc) · 6.43 KB
/
test_deprecated_inference.py
File metadata and controls
141 lines (110 loc) · 6.43 KB
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
import pytest
import warnings
import sys
from unittest import mock
from llama_stack_client import LlamaStackClient, AsyncLlamaStackClient
# Force all deprecation warnings to be shown, regardless of where they're emitted
warnings.filterwarnings("always", category=DeprecationWarning)
class TestDeprecatedInference:
@pytest.fixture
def client(self):
client = LlamaStackClient(base_url="http://test", api_key="test_key")
return client
def test_direct_warning_capture_chat_completion(self, client):
"""Test deprecation warning using manual warning capture."""
with mock.patch.object(client.inference, "_post") as mock_post:
mock_post.return_value = {"id": "test_id", "choices": []}
# Capture all warnings across all modules
with warnings.catch_warnings(record=True) as recorded_warnings:
# Ensure warnings are always shown
warnings.simplefilter("always")
# Call the deprecated method
client.inference.chat_completion(messages=[{"role": "user", "content": "Hello"}], model_id="test_model")
# Print warning details for debugging
for w in recorded_warnings:
print(f"\nWarning category: {w.category}")
print(f"Warning message: {str(w.message)}")
# Check for any DeprecationWarning with our message
assert any(
issubclass(w.category, DeprecationWarning)
and "Use chat.completions.create instead" in str(w.message)
for w in recorded_warnings
), "No matching deprecation warnings were emitted"
def test_completion_warning(self, client):
"""Test completion method emits deprecation warning."""
with mock.patch.object(client.inference, "_post") as mock_post:
mock_post.return_value = {"id": "test_id", "content": "test content"}
# Capture all warnings across all modules
with warnings.catch_warnings(record=True) as recorded_warnings:
warnings.simplefilter("always")
# Call the deprecated method
client.inference.completion(content="Hello", model_id="test_model")
# Check for any DeprecationWarning with our message
assert any(
issubclass(w.category, DeprecationWarning) and "Use completions.create instead" in str(w.message)
for w in recorded_warnings
), "No matching deprecation warnings were emitted"
def test_embeddings_warning(self, client):
"""Test embeddings method emits deprecation warning."""
with mock.patch.object(client.inference, "_post") as mock_post:
mock_post.return_value = {"data": [{"embedding": [0.1, 0.2]}]}
# Capture all warnings across all modules
with warnings.catch_warnings(record=True) as recorded_warnings:
warnings.simplefilter("always")
# Call the deprecated method
client.inference.embeddings(contents=["Hello"], model_id="test_model")
# Check for any DeprecationWarning with our message
assert any(
issubclass(w.category, DeprecationWarning) and "Use embeddings.create instead" in str(w.message)
for w in recorded_warnings
), "No matching deprecation warnings were emitted"
class TestAsyncDeprecatedInference:
@pytest.fixture
def async_client(self):
client = AsyncLlamaStackClient(base_url="http://test", api_key="test_key")
return client
@pytest.mark.asyncio
async def test_async_chat_completion_warning(self, async_client):
with mock.patch.object(async_client.inference, "_post", new_callable=mock.AsyncMock) as mock_post:
mock_post.return_value = {"id": "test_id", "choices": []}
# Capture all warnings across all modules
with warnings.catch_warnings(record=True) as recorded_warnings:
warnings.simplefilter("always")
# Call the deprecated method
await async_client.inference.chat_completion(
messages=[{"role": "user", "content": "Hello"}], model_id="test_model"
)
# Check for any DeprecationWarning with our message
assert any(
issubclass(w.category, DeprecationWarning)
and "Use chat.completions.create instead" in str(w.message)
for w in recorded_warnings
), "No matching deprecation warnings were emitted"
@pytest.mark.asyncio
async def test_async_completion_warning(self, async_client):
with mock.patch.object(async_client.inference, "_post", new_callable=mock.AsyncMock) as mock_post:
mock_post.return_value = {"id": "test_id", "content": "test content"}
# Capture all warnings across all modules
with warnings.catch_warnings(record=True) as recorded_warnings:
warnings.simplefilter("always")
# Call the deprecated method
await async_client.inference.completion(content="Hello", model_id="test_model")
# Check for any DeprecationWarning with our message
assert any(
issubclass(w.category, DeprecationWarning) and "Use completions.create instead" in str(w.message)
for w in recorded_warnings
), "No matching deprecation warnings were emitted"
@pytest.mark.asyncio
async def test_async_embeddings_warning(self, async_client):
with mock.patch.object(async_client.inference, "_post", new_callable=mock.AsyncMock) as mock_post:
mock_post.return_value = {"data": [{"embedding": [0.1, 0.2]}]}
# Capture all warnings across all modules
with warnings.catch_warnings(record=True) as recorded_warnings:
warnings.simplefilter("always")
# Call the deprecated method
await async_client.inference.embeddings(contents=["Hello"], model_id="test_model")
# Check for any DeprecationWarning with our message
assert any(
issubclass(w.category, DeprecationWarning) and "Use embeddings.create instead" in str(w.message)
for w in recorded_warnings
), "No matching deprecation warnings were emitted"