-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: consolidate azure with openai provider
Signed-off-by: Adrian Cole <[email protected]>
- Loading branch information
1 parent
c5dc482
commit 682f14b
Showing
5 changed files
with
133 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
interactions: | ||
- request: | ||
body: '{"messages": [{"role": "system", "content": "You are a helpful assistant."}, | ||
{"role": "user", "content": "Hello"}], "model": "gpt-4o-mini"}' | ||
headers: | ||
accept: | ||
- '*/*' | ||
accept-encoding: | ||
- gzip, deflate | ||
api-key: | ||
- test_azure_api_key | ||
connection: | ||
- keep-alive | ||
content-length: | ||
- '139' | ||
content-type: | ||
- application/json | ||
host: | ||
- test.openai.azure.com | ||
user-agent: | ||
- python-httpx/0.27.2 | ||
method: POST | ||
uri: https://test.openai.azure.com/openai/deployments/test-azure-deployment/chat/completions?api-version=2024-05-01-preview | ||
response: | ||
body: | ||
string: '{"choices":[{"content_filter_results":{"hate":{"filtered":false,"severity":"safe"},"self_harm":{"filtered":false,"severity":"safe"},"sexual":{"filtered":false,"severity":"safe"},"violence":{"filtered":false,"severity":"safe"}},"finish_reason":"stop","index":0,"logprobs":null,"message":{"content":"Hello! | ||
How can I assist you today?","role":"assistant"}}],"created":1727230065,"id":"chatcmpl-ABBjN3AoYlxkP7Vg2lBvUhYeA6j5K","model":"gpt-4-32k","object":"chat.completion","prompt_filter_results":[{"prompt_index":0,"content_filter_results":{"hate":{"filtered":false,"severity":"safe"},"self_harm":{"filtered":false,"severity":"safe"},"sexual":{"filtered":false,"severity":"safe"},"violence":{"filtered":false,"severity":"safe"}}}],"system_fingerprint":null,"usage":{"completion_tokens":9,"prompt_tokens":18,"total_tokens":27}} | ||
' | ||
headers: | ||
Cache-Control: | ||
- no-cache, must-revalidate | ||
Content-Length: | ||
- '825' | ||
Content-Type: | ||
- application/json | ||
Date: | ||
- Wed, 25 Sep 2024 02:07:45 GMT | ||
Set-Cookie: test_set_cookie | ||
Strict-Transport-Security: | ||
- max-age=31536000; includeSubDomains; preload | ||
access-control-allow-origin: | ||
- '*' | ||
apim-request-id: | ||
- 82e66ef8-ac07-4a43-b60f-9aecec1d8c81 | ||
azureml-model-session: | ||
- d145-20240919052126 | ||
openai-organization: test_openai_org_key | ||
x-accel-buffering: | ||
- 'no' | ||
x-content-type-options: | ||
- nosniff | ||
x-ms-client-request-id: | ||
- 82e66ef8-ac07-4a43-b60f-9aecec1d8c81 | ||
x-ms-rai-invoked: | ||
- 'true' | ||
x-ms-region: | ||
- Switzerland North | ||
x-ratelimit-remaining-requests: | ||
- '79' | ||
x-ratelimit-remaining-tokens: | ||
- '79984' | ||
x-request-id: | ||
- 38db9001-8b16-4efe-84c9-620e10f18c3c | ||
status: | ||
code: 200 | ||
message: OK | ||
version: 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,25 @@ | ||
import os | ||
from unittest.mock import patch | ||
|
||
import pytest | ||
from exchange import Message, Text | ||
from exchange.providers.azure import AzureProvider | ||
|
||
|
||
@pytest.fixture | ||
@patch.dict( | ||
os.environ, | ||
{ | ||
"AZURE_CHAT_COMPLETIONS_HOST_NAME": "https://test.openai.azure.com/", | ||
"AZURE_CHAT_COMPLETIONS_DEPLOYMENT_NAME": "test-deployment", | ||
"AZURE_CHAT_COMPLETIONS_DEPLOYMENT_API_VERSION": "2024-02-15-preview", | ||
"AZURE_CHAT_COMPLETIONS_KEY": "test_api_key", | ||
}, | ||
) | ||
def azure_provider(): | ||
return AzureProvider.from_env() | ||
|
||
from exchange import Text | ||
from exchange.providers.azure import AzureProvider | ||
from .conftest import complete | ||
|
||
@patch("httpx.Client.post") | ||
@patch("time.sleep", return_value=None) | ||
@patch("logging.warning") | ||
@patch("logging.error") | ||
def test_azure_completion(mock_error, mock_warning, mock_sleep, mock_post, azure_provider): | ||
mock_response = { | ||
"choices": [{"message": {"role": "assistant", "content": "Hello!"}}], | ||
"usage": {"prompt_tokens": 10, "completion_tokens": 25, "total_tokens": 35}, | ||
} | ||
|
||
mock_post.return_value.json.return_value = mock_response | ||
AZURE_MODEL = os.getenv("AZURE_MODEL", "gpt-4o-mini") | ||
|
||
model = "gpt-4" | ||
system = "You are a helpful assistant." | ||
messages = [Message.user("Hello")] | ||
tools = () | ||
|
||
reply_message, reply_usage = azure_provider.complete(model=model, system=system, messages=messages, tools=tools) | ||
@pytest.mark.vcr() | ||
def test_azure_complete(default_azure_env): | ||
reply_message, reply_usage = complete(AzureProvider, AZURE_MODEL) | ||
|
||
assert reply_message.content == [Text(text="Hello!")] | ||
assert reply_usage.total_tokens == 35 | ||
mock_post.assert_called_once_with( | ||
f"{azure_provider.client.base_url}/chat/completions?api-version={azure_provider.api_version}", | ||
json={ | ||
"messages": [ | ||
{"role": "system", "content": system}, | ||
{"role": "user", "content": "Hello"}, | ||
], | ||
}, | ||
) | ||
assert reply_message.content == [Text(text="Hello! How can I assist you today?")] | ||
assert reply_usage.total_tokens == 27 | ||
|
||
|
||
@pytest.mark.integration | ||
def test_azure_integration(): | ||
provider = AzureProvider.from_env() | ||
system = "You are a helpful assistant." | ||
messages = [Message.user("Hello")] | ||
|
||
reply = provider.complete(system=system, messages=messages, tools=None) | ||
def test_azure_complete_integration(): | ||
reply = complete(AzureProvider, AZURE_MODEL) | ||
|
||
assert reply[0].content is not None | ||
print("Completion content from Azure:", reply[0].content) | ||
print("Complete content from Azure:", reply[0].content) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters