Skip to content
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

feat[resource] add sdk indicator #194

Merged
merged 3 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/qianfan/resources/console/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from qianfan.errors import InvalidArgumentError
from qianfan.resources.requestor.console_requestor import ConsoleAPIRequestor
from qianfan.resources.typing import ParamSpec, QfRequest, QfResponse, RetryConfig
from qianfan.version import VERSION

P = ParamSpec("P")

Expand Down Expand Up @@ -56,6 +57,7 @@ def inner(*args: Any, **kwargs: Any) -> QfResponse:
),
)
req = func(*args, **kwargs)
req.headers["request-source"] = f"qianfan_py_sdk-{VERSION}"
return ConsoleAPIRequestor(**kwargs)._request_console_api(
req, ak, sk, retry_config
)
Expand Down Expand Up @@ -95,6 +97,7 @@ async def inner(*args: Any, **kwargs: Any) -> QfResponse:
),
)
req = await func(*args, **kwargs)
req.headers["request-source"] = f"qianfan_py_sdk-{VERSION}"
return await ConsoleAPIRequestor(**kwargs)._async_request_console_api(
req, ak, sk, retry_config
)
Expand Down
2 changes: 2 additions & 0 deletions src/qianfan/resources/llm/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
from qianfan.resources.requestor.openapi_requestor import create_api_requestor
from qianfan.resources.typing import JsonBody, QfLLMInfo, QfResponse, RetryConfig
from qianfan.utils import log_info, log_warn, utils
from qianfan.version import VERSION

# This is used when user provides `endpoint`
# In such cases, SDK cannot know which model the user is using
Expand Down Expand Up @@ -488,6 +489,7 @@ def _generate_body(
f"The required key `{key}` is not provided."
)
kwargs["stream"] = stream
kwargs["extra_parameters"] = {"user_agent": f"qianfan_py_sdk-{VERSION}"}
return kwargs

def _data_postprocess(self, data: QfResponse) -> QfResponse:
Expand Down
8 changes: 8 additions & 0 deletions src/qianfan/tests/completion_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import qianfan
import qianfan.tests.utils
from qianfan.tests.utils import EnvHelper, fake_access_token
from qianfan.version import VERSION

QIANFAN_SUPPORT_COMPLETION_MOCK_MODEL = {
"ERNIE-Bot",
Expand Down Expand Up @@ -421,3 +422,10 @@ async def test_batch_predict_async():
assert 5 >= time.time() - start_time >= 3
for input, output in zip(prompt_list, results):
assert input in output["result"]


def test_sdk_indicator():
res = qianfan.Completion().do("hi")
assert (
res["_request"]["extra_parameters"]["user_agent"] == f"qianfan_py_sdk-{VERSION}"
)
7 changes: 7 additions & 0 deletions src/qianfan/tests/service_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

from qianfan.resources import Service
from qianfan.resources.console.consts import DeployPoolType, ServiceType
from qianfan.version import VERSION


def test_create_service():
Expand Down Expand Up @@ -72,3 +73,9 @@ def test_service_list():
resp = Service.list(api_type_filter=[item])
for common_service in resp["result"]["common"]:
assert common_service["apiType"] == item.value


def test_sdk_console_indicator():
res = Service.list()
# header不区分大小写,flask受到后将起转换成大写:
assert res["_header"]["Request-Source"] == f"qianfan_py_sdk-{VERSION}"
Loading