-
Notifications
You must be signed in to change notification settings - Fork 14
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: Add Python test infrastructure and OpenAI wrapper tests #478
Open
devin-ai-integration
wants to merge
13
commits into
main
Choose a base branch
from
devin/1733892933-add-openai-wrapper-tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
5d86099
test: Add Python test infrastructure and OpenAI wrapper tests
devin-ai-integration[bot] b3e1951
test: Fix response mocking configuration
devin-ai-integration[bot] becf834
test: Simplify response mocking configuration
devin-ai-integration[bot] 01c864b
test: Add httpx target to response mocks
devin-ai-integration[bot] 4199bb1
style: Apply black formatting
devin-ai-integration[bot] 42311ad
test: Fix matchers.Any attribute name
devin-ai-integration[bot] 3b93bbf
style: Apply black formatting to test file
devin-ai-integration[bot] 6ecbaa5
ci: Add test execution to workflow and fix type errors
devin-ai-integration[bot] e556f84
chore: Move test dependencies to setup.py and update workflow
devin-ai-integration[bot] c001b05
chore: Remove requirements.txt in favor of setup.py
devin-ai-integration[bot] 2ef578f
fix: Update OpenAI wrapper to use model_dump() instead of deprecated …
devin-ai-integration[bot] 948b969
style: Apply black formatting to test file
devin-ai-integration[bot] ede791b
fix: Update type comment syntax for Python 3.8 compatibility
devin-ai-integration[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Empty file.
Empty file.
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,103 @@ | ||
import json | ||
import logging | ||
from inspect import iscoroutinefunction | ||
from typing import TYPE_CHECKING | ||
|
||
import httpx | ||
import openai | ||
import pytest | ||
from braintrust.oai import wrap_openai | ||
from openai.types import CompletionUsage | ||
from openai.types.chat import ChatCompletion | ||
from openai.types.chat.chat_completion import ChatCompletionMessage, Choice | ||
|
||
if TYPE_CHECKING: | ||
reveal_type = print # For type checking only | ||
else: | ||
|
||
def reveal_type(obj): | ||
pass # No-op at runtime | ||
|
||
|
||
logging.basicConfig(level=logging.DEBUG) | ||
|
||
|
||
@pytest.fixture | ||
def openai_client(): | ||
return openai.OpenAI(api_key="sk-test", base_url="https://api.openai.com/v1/") | ||
|
||
|
||
@pytest.fixture | ||
def mock_completion(): | ||
return { | ||
"id": "test-id", | ||
"object": "chat.completion", | ||
"created": 1677652288, | ||
"model": "gpt-3.5-turbo", | ||
"choices": [ | ||
{ | ||
"index": 0, | ||
"message": {"role": "assistant", "content": "Hello, how can I help you?"}, | ||
"finish_reason": "stop", | ||
} | ||
], | ||
"usage": {"prompt_tokens": 10, "completion_tokens": 20, "total_tokens": 30}, | ||
} | ||
|
||
|
||
@pytest.fixture | ||
def setup_responses(mock_completion, httpx_mock): | ||
httpx_mock.add_response( | ||
method="POST", | ||
url="https://api.openai.com/v1/chat/completions", | ||
json=mock_completion, | ||
headers={"Content-Type": "application/json"}, | ||
status_code=200, | ||
) | ||
return httpx_mock | ||
|
||
|
||
def test_wrap_openai_sync_types(openai_client): | ||
wrapped = wrap_openai(openai_client) | ||
reveal_type(wrapped) # type: ignore # Expected type: openai.OpenAI | ||
reveal_type(wrapped.chat.completions) # type: ignore # Expected type: openai.resources.chat.completions.Completions | ||
assert hasattr(wrapped.chat.completions, "create") | ||
assert not hasattr(wrapped.chat.completions, "acreate") | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_wrap_openai_async_types(): | ||
async_client = openai.AsyncOpenAI( | ||
api_key="sk-test", base_url="https://api.openai.com/v1/", default_headers={"OpenAI-Version": "2020-10-01"} | ||
) | ||
wrapped = wrap_openai(async_client) | ||
reveal_type(wrapped) # type: ignore # Expected type: openai.AsyncOpenAI | ||
reveal_type(wrapped.chat.completions) # type: ignore # Expected type: openai.resources.chat.completions.AsyncCompletions | ||
assert hasattr(wrapped.chat.completions, "create") | ||
assert iscoroutinefunction(wrapped.chat.completions.create) | ||
|
||
|
||
def test_wrap_openai_sync_response_types(openai_client, mock_completion, setup_responses): | ||
wrapped = wrap_openai(openai_client) | ||
response = wrapped.chat.completions.create(model="gpt-3.5-turbo", messages=[{"role": "user", "content": "Hello"}]) | ||
reveal_type(response) # type: ignore # Expected type: openai.types.chat.ChatCompletion | ||
reveal_type(response.choices[0]) # type: ignore # Expected type: openai.types.chat.chat_completion.Choice | ||
reveal_type(response.usage) # type: ignore # Expected type: openai.types.completion_usage.CompletionUsage | ||
assert isinstance(response, ChatCompletion) | ||
assert isinstance(response.choices[0], Choice) | ||
assert isinstance(response.usage, CompletionUsage) | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_wrap_openai_async_response_types(mock_completion, setup_responses): | ||
async_client = openai.AsyncOpenAI(api_key="sk-test", base_url="https://api.openai.com/v1/") | ||
wrapped = wrap_openai(async_client) | ||
response = await wrapped.chat.completions.create( | ||
model="gpt-3.5-turbo", messages=[{"role": "user", "content": "Hello"}] | ||
) | ||
reveal_type(response) # type: ignore # Expected type: openai.types.chat.ChatCompletion | ||
reveal_type(response.choices[0]) # type: ignore # Expected type: openai.types.chat.chat_completion.Choice | ||
reveal_type(response.usage) # type: ignore # Expected type: openai.types.completion_usage.CompletionUsage | ||
assert isinstance(response, ChatCompletion) | ||
assert isinstance(response.choices[0], Choice) | ||
assert isinstance(response.usage, CompletionUsage) |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this actually testing anything? I want the unit test not a third party tool testing