AutoGPT/tests/integration/conftest.py

56 lines
1.6 KiB
Python
Raw Normal View History

2023-05-02 18:07:50 +00:00
import os
import openai
2023-04-26 16:57:05 +00:00
import pytest
from tests.conftest import PROXY
2023-05-02 18:07:50 +00:00
from tests.vcr.vcr_filter import before_record_request, before_record_response
2023-04-26 16:57:05 +00:00
@pytest.fixture(scope="session")
2023-04-26 16:57:05 +00:00
def vcr_config():
# this fixture is called by the pytest-recording vcr decorator.
return {
"record_mode": "new_episodes",
"before_record_request": before_record_request,
2023-04-26 20:11:08 +00:00
"before_record_response": before_record_response,
2023-04-26 16:57:05 +00:00
"filter_headers": [
2023-04-26 20:11:08 +00:00
"Authorization",
2023-04-26 16:57:05 +00:00
"X-OpenAI-Client-User-Agent",
"User-Agent",
],
"match_on": ["method", "body"],
2023-04-26 16:57:05 +00:00
}
def patch_api_base(requestor):
new_api_base = f"{PROXY}/v1"
requestor.api_base = new_api_base
return requestor
@pytest.fixture
def patched_api_requestor(mocker):
original_init = openai.api_requestor.APIRequestor.__init__
original_validate_headers = openai.api_requestor.APIRequestor._validate_headers
def patched_init(requestor, *args, **kwargs):
original_init(requestor, *args, **kwargs)
patch_api_base(requestor)
def patched_validate_headers(self, supplied_headers):
headers = original_validate_headers(self, supplied_headers)
headers["AGENT-MODE"] = os.environ.get("AGENT_MODE")
headers["AGENT-TYPE"] = os.environ.get("AGENT_TYPE")
return headers
if PROXY:
mocker.patch("openai.api_requestor.APIRequestor.__init__", new=patched_init)
mocker.patch.object(
openai.api_requestor.APIRequestor,
"_validate_headers",
new=patched_validate_headers,
)
return mocker