mirror of
https://github.com/cloudflare/cloudflare-python.git
synced 2026-01-16 23:01:03 +00:00
feat(api): OpenAPI spec update via Stainless API (#135)
This commit is contained in:
parent
9ea82d193c
commit
b2dbacf4c2
6 changed files with 306 additions and 46 deletions
2
api.md
2
api.md
|
|
@ -2652,7 +2652,7 @@ from cloudflare.types.workers import AIRunResponse
|
|||
|
||||
Methods:
|
||||
|
||||
- <code title="post /accounts/{account_id}/ai/run/{model_name}">client.workers.ai.<a href="./src/cloudflare/resources/workers/ai.py">run</a>(model_name, \*, account_id, \*\*<a href="src/cloudflare/types/workers/ai_run_params.py">params</a>) -> <a href="./src/cloudflare/types/workers/ai_run_response.py">object</a></code>
|
||||
- <code title="post /accounts/{account_id}/ai/run/{model_name}">client.workers.ai.<a href="./src/cloudflare/resources/workers/ai.py">run</a>(model_name, \*, account_id, \*\*<a href="src/cloudflare/types/workers/ai_run_params.py">params</a>) -> <a href="./src/cloudflare/types/workers/ai_run_response.py">Optional</a></code>
|
||||
|
||||
## Scripts
|
||||
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Type, cast
|
||||
from typing import Any, Optional, cast
|
||||
|
||||
import httpx
|
||||
|
||||
|
|
@ -23,7 +23,7 @@ from ..._wrappers import ResultWrapper
|
|||
from ..._base_client import (
|
||||
make_request_options,
|
||||
)
|
||||
from ...types.workers import ai_run_params
|
||||
from ...types.workers import AIRunResponse, ai_run_params
|
||||
|
||||
__all__ = ["AI", "AsyncAI"]
|
||||
|
||||
|
|
@ -42,14 +42,14 @@ class AI(SyncAPIResource):
|
|||
model_name: str,
|
||||
*,
|
||||
account_id: str,
|
||||
body: object,
|
||||
body: ai_run_params.Body,
|
||||
# Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
|
||||
# The extra values given here take precedence over values defined on the client or passed to this method.
|
||||
extra_headers: Headers | None = None,
|
||||
extra_query: Query | None = None,
|
||||
extra_body: Body | None = None,
|
||||
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
|
||||
) -> object:
|
||||
) -> Optional[AIRunResponse]:
|
||||
"""
|
||||
This endpoint provides users with the capability to run specific AI models
|
||||
on-demand.
|
||||
|
|
@ -74,17 +74,22 @@ class AI(SyncAPIResource):
|
|||
raise ValueError(f"Expected a non-empty value for `account_id` but received {account_id!r}")
|
||||
if not model_name:
|
||||
raise ValueError(f"Expected a non-empty value for `model_name` but received {model_name!r}")
|
||||
return self._post(
|
||||
f"/accounts/{account_id}/ai/run/{model_name}",
|
||||
body=maybe_transform(body, ai_run_params.AIRunParams),
|
||||
options=make_request_options(
|
||||
extra_headers=extra_headers,
|
||||
extra_query=extra_query,
|
||||
extra_body=extra_body,
|
||||
timeout=timeout,
|
||||
post_parser=ResultWrapper._unwrapper,
|
||||
return cast(
|
||||
Optional[AIRunResponse],
|
||||
self._post(
|
||||
f"/accounts/{account_id}/ai/run/{model_name}",
|
||||
body=maybe_transform(body, ai_run_params.AIRunParams),
|
||||
options=make_request_options(
|
||||
extra_headers=extra_headers,
|
||||
extra_query=extra_query,
|
||||
extra_body=extra_body,
|
||||
timeout=timeout,
|
||||
post_parser=ResultWrapper._unwrapper,
|
||||
),
|
||||
cast_to=cast(
|
||||
Any, ResultWrapper[AIRunResponse]
|
||||
), # Union types cannot be passed in as arguments in the type system
|
||||
),
|
||||
cast_to=cast(Type[object], ResultWrapper[object]),
|
||||
)
|
||||
|
||||
|
||||
|
|
@ -102,14 +107,14 @@ class AsyncAI(AsyncAPIResource):
|
|||
model_name: str,
|
||||
*,
|
||||
account_id: str,
|
||||
body: object,
|
||||
body: ai_run_params.Body,
|
||||
# Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
|
||||
# The extra values given here take precedence over values defined on the client or passed to this method.
|
||||
extra_headers: Headers | None = None,
|
||||
extra_query: Query | None = None,
|
||||
extra_body: Body | None = None,
|
||||
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
|
||||
) -> object:
|
||||
) -> Optional[AIRunResponse]:
|
||||
"""
|
||||
This endpoint provides users with the capability to run specific AI models
|
||||
on-demand.
|
||||
|
|
@ -134,17 +139,22 @@ class AsyncAI(AsyncAPIResource):
|
|||
raise ValueError(f"Expected a non-empty value for `account_id` but received {account_id!r}")
|
||||
if not model_name:
|
||||
raise ValueError(f"Expected a non-empty value for `model_name` but received {model_name!r}")
|
||||
return await self._post(
|
||||
f"/accounts/{account_id}/ai/run/{model_name}",
|
||||
body=await async_maybe_transform(body, ai_run_params.AIRunParams),
|
||||
options=make_request_options(
|
||||
extra_headers=extra_headers,
|
||||
extra_query=extra_query,
|
||||
extra_body=extra_body,
|
||||
timeout=timeout,
|
||||
post_parser=ResultWrapper._unwrapper,
|
||||
return cast(
|
||||
Optional[AIRunResponse],
|
||||
await self._post(
|
||||
f"/accounts/{account_id}/ai/run/{model_name}",
|
||||
body=await async_maybe_transform(body, ai_run_params.AIRunParams),
|
||||
options=make_request_options(
|
||||
extra_headers=extra_headers,
|
||||
extra_query=extra_query,
|
||||
extra_body=extra_body,
|
||||
timeout=timeout,
|
||||
post_parser=ResultWrapper._unwrapper,
|
||||
),
|
||||
cast_to=cast(
|
||||
Any, ResultWrapper[AIRunResponse]
|
||||
), # Union types cannot be passed in as arguments in the type system
|
||||
),
|
||||
cast_to=cast(Type[object], ResultWrapper[object]),
|
||||
)
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ from .ai_run_params import AIRunParams as AIRunParams
|
|||
from .workers_domain import WorkersDomain as WorkersDomain
|
||||
from .workers_routes import WorkersRoutes as WorkersRoutes
|
||||
from .workers_script import WorkersScript as WorkersScript
|
||||
from .ai_run_response import AIRunResponse as AIRunResponse
|
||||
from .workers_filters import WorkersFilters as WorkersFilters
|
||||
from .domain_list_params import DomainListParams as DomainListParams
|
||||
from .route_create_params import RouteCreateParams as RouteCreateParams
|
||||
|
|
|
|||
|
|
@ -2,12 +2,136 @@
|
|||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import List, Union, Iterable
|
||||
from typing_extensions import Required, TypedDict
|
||||
|
||||
__all__ = ["AIRunParams"]
|
||||
from ..._types import FileTypes
|
||||
|
||||
__all__ = [
|
||||
"AIRunParams",
|
||||
"Body",
|
||||
"BodyTextClassification",
|
||||
"BodyTextToImage",
|
||||
"BodySentenceSimilarity",
|
||||
"BodyTextEmbeddings",
|
||||
"BodyAudio",
|
||||
"BodyImage",
|
||||
"BodyUnionMember10",
|
||||
"BodyUnionMember11",
|
||||
"BodyUnionMember11Message",
|
||||
"BodyTranslation",
|
||||
"BodySummarization",
|
||||
"BodyUnionMember15",
|
||||
]
|
||||
|
||||
|
||||
class AIRunParams(TypedDict, total=False):
|
||||
account_id: Required[str]
|
||||
|
||||
body: Required[object]
|
||||
body: Required[Body]
|
||||
|
||||
|
||||
class BodyTextClassification(TypedDict, total=False):
|
||||
text: Required[str]
|
||||
|
||||
|
||||
class BodyTextToImage(TypedDict, total=False):
|
||||
prompt: Required[str]
|
||||
|
||||
guidance: float
|
||||
|
||||
image: Iterable[float]
|
||||
|
||||
mask: Iterable[float]
|
||||
|
||||
num_steps: int
|
||||
|
||||
strength: float
|
||||
|
||||
|
||||
class BodySentenceSimilarity(TypedDict, total=False):
|
||||
sentences: Required[List[str]]
|
||||
|
||||
source: Required[str]
|
||||
|
||||
|
||||
class BodyTextEmbeddings(TypedDict, total=False):
|
||||
text: Required[Union[str, List[str]]]
|
||||
|
||||
|
||||
class BodyAudio(TypedDict, total=False):
|
||||
audio: Iterable[float]
|
||||
|
||||
|
||||
class BodyImage(TypedDict, total=False):
|
||||
image: Iterable[float]
|
||||
|
||||
|
||||
class BodyImage(TypedDict, total=False):
|
||||
image: Iterable[float]
|
||||
|
||||
|
||||
class BodyUnionMember10(TypedDict, total=False):
|
||||
prompt: Required[str]
|
||||
|
||||
max_tokens: int
|
||||
|
||||
raw: bool
|
||||
|
||||
stream: bool
|
||||
|
||||
|
||||
class BodyUnionMember11Message(TypedDict, total=False):
|
||||
content: Required[str]
|
||||
|
||||
role: Required[str]
|
||||
|
||||
|
||||
class BodyUnionMember11(TypedDict, total=False):
|
||||
messages: Required[Iterable[BodyUnionMember11Message]]
|
||||
|
||||
max_tokens: int
|
||||
|
||||
stream: bool
|
||||
|
||||
|
||||
class BodyTranslation(TypedDict, total=False):
|
||||
target_lang: Required[str]
|
||||
|
||||
text: Required[str]
|
||||
|
||||
source_lang: str
|
||||
|
||||
|
||||
class BodySummarization(TypedDict, total=False):
|
||||
input_text: Required[str]
|
||||
|
||||
max_length: int
|
||||
|
||||
|
||||
class BodyUnionMember15(TypedDict, total=False):
|
||||
image: Iterable[float]
|
||||
|
||||
max_tokens: int
|
||||
|
||||
prompt: str
|
||||
|
||||
|
||||
Body = Union[
|
||||
BodyTextClassification,
|
||||
BodyTextToImage,
|
||||
BodySentenceSimilarity,
|
||||
BodyTextEmbeddings,
|
||||
FileTypes,
|
||||
BodyAudio,
|
||||
FileTypes,
|
||||
BodyImage,
|
||||
FileTypes,
|
||||
BodyImage,
|
||||
BodyUnionMember10,
|
||||
BodyUnionMember11,
|
||||
BodyTranslation,
|
||||
BodySummarization,
|
||||
FileTypes,
|
||||
BodyUnionMember15,
|
||||
]
|
||||
|
|
|
|||
104
src/cloudflare/types/workers/ai_run_response.py
Normal file
104
src/cloudflare/types/workers/ai_run_response.py
Normal file
|
|
@ -0,0 +1,104 @@
|
|||
# File generated from our OpenAPI spec by Stainless.
|
||||
|
||||
from typing import List, Union, Optional
|
||||
|
||||
from ..._models import BaseModel
|
||||
|
||||
__all__ = [
|
||||
"AIRunResponse",
|
||||
"TextClassification",
|
||||
"TextEmbeddings",
|
||||
"SpeechRecognition",
|
||||
"SpeechRecognitionWord",
|
||||
"ImageClassification",
|
||||
"ObjectDetection",
|
||||
"ObjectDetectionBox",
|
||||
"Response",
|
||||
"Translation",
|
||||
"Summarization",
|
||||
"ImageToText",
|
||||
]
|
||||
|
||||
|
||||
class TextClassification(BaseModel):
|
||||
label: Optional[str] = None
|
||||
|
||||
score: Optional[float] = None
|
||||
|
||||
|
||||
class TextEmbeddings(BaseModel):
|
||||
data: Optional[List[List[float]]] = None
|
||||
|
||||
shape: Optional[List[float]] = None
|
||||
|
||||
|
||||
class SpeechRecognitionWord(BaseModel):
|
||||
end: Optional[float] = None
|
||||
|
||||
start: Optional[float] = None
|
||||
|
||||
word: Optional[str] = None
|
||||
|
||||
|
||||
class SpeechRecognition(BaseModel):
|
||||
text: str
|
||||
|
||||
word_count: Optional[float] = None
|
||||
|
||||
words: Optional[List[SpeechRecognitionWord]] = None
|
||||
|
||||
|
||||
class ImageClassification(BaseModel):
|
||||
label: Optional[str] = None
|
||||
|
||||
score: Optional[float] = None
|
||||
|
||||
|
||||
class ObjectDetectionBox(BaseModel):
|
||||
xmax: Optional[float] = None
|
||||
|
||||
xmin: Optional[float] = None
|
||||
|
||||
ymax: Optional[float] = None
|
||||
|
||||
ymin: Optional[float] = None
|
||||
|
||||
|
||||
class ObjectDetection(BaseModel):
|
||||
box: Optional[ObjectDetectionBox] = None
|
||||
|
||||
label: Optional[str] = None
|
||||
|
||||
score: Optional[float] = None
|
||||
|
||||
|
||||
class Response(BaseModel):
|
||||
response: Optional[str] = None
|
||||
|
||||
|
||||
class Translation(BaseModel):
|
||||
translated_text: Optional[str] = None
|
||||
|
||||
|
||||
class Summarization(BaseModel):
|
||||
summary: Optional[str] = None
|
||||
|
||||
|
||||
class ImageToText(BaseModel):
|
||||
description: Optional[str] = None
|
||||
|
||||
|
||||
AIRunResponse = Union[
|
||||
List[TextClassification],
|
||||
object,
|
||||
List[float],
|
||||
TextEmbeddings,
|
||||
SpeechRecognition,
|
||||
List[ImageClassification],
|
||||
List[ObjectDetection],
|
||||
Response,
|
||||
object,
|
||||
Translation,
|
||||
Summarization,
|
||||
ImageToText,
|
||||
]
|
||||
|
|
@ -3,12 +3,13 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
from typing import Any, cast
|
||||
from typing import Any, Optional, cast
|
||||
|
||||
import pytest
|
||||
|
||||
from cloudflare import Cloudflare, AsyncCloudflare
|
||||
from tests.utils import assert_matches_type
|
||||
from cloudflare.types.workers import AIRunResponse
|
||||
|
||||
base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")
|
||||
|
||||
|
|
@ -22,9 +23,19 @@ class TestAI:
|
|||
ai = client.workers.ai.run(
|
||||
"string",
|
||||
account_id="023e105f4ecef8ad9ca31a8372d0c353",
|
||||
body={},
|
||||
body={"text": "string"},
|
||||
)
|
||||
assert_matches_type(object, ai, path=["response"])
|
||||
assert_matches_type(Optional[AIRunResponse], ai, path=["response"])
|
||||
|
||||
@pytest.mark.skip()
|
||||
@parametrize
|
||||
def test_method_run_with_all_params(self, client: Cloudflare) -> None:
|
||||
ai = client.workers.ai.run(
|
||||
"string",
|
||||
account_id="023e105f4ecef8ad9ca31a8372d0c353",
|
||||
body={"text": "string"},
|
||||
)
|
||||
assert_matches_type(Optional[AIRunResponse], ai, path=["response"])
|
||||
|
||||
@pytest.mark.skip()
|
||||
@parametrize
|
||||
|
|
@ -32,13 +43,13 @@ class TestAI:
|
|||
response = client.workers.ai.with_raw_response.run(
|
||||
"string",
|
||||
account_id="023e105f4ecef8ad9ca31a8372d0c353",
|
||||
body={},
|
||||
body={"text": "string"},
|
||||
)
|
||||
|
||||
assert response.is_closed is True
|
||||
assert response.http_request.headers.get("X-Stainless-Lang") == "python"
|
||||
ai = response.parse()
|
||||
assert_matches_type(object, ai, path=["response"])
|
||||
assert_matches_type(Optional[AIRunResponse], ai, path=["response"])
|
||||
|
||||
@pytest.mark.skip()
|
||||
@parametrize
|
||||
|
|
@ -46,13 +57,13 @@ class TestAI:
|
|||
with client.workers.ai.with_streaming_response.run(
|
||||
"string",
|
||||
account_id="023e105f4ecef8ad9ca31a8372d0c353",
|
||||
body={},
|
||||
body={"text": "string"},
|
||||
) as response:
|
||||
assert not response.is_closed
|
||||
assert response.http_request.headers.get("X-Stainless-Lang") == "python"
|
||||
|
||||
ai = response.parse()
|
||||
assert_matches_type(object, ai, path=["response"])
|
||||
assert_matches_type(Optional[AIRunResponse], ai, path=["response"])
|
||||
|
||||
assert cast(Any, response.is_closed) is True
|
||||
|
||||
|
|
@ -63,14 +74,14 @@ class TestAI:
|
|||
client.workers.ai.with_raw_response.run(
|
||||
"string",
|
||||
account_id="",
|
||||
body={},
|
||||
body={"text": "string"},
|
||||
)
|
||||
|
||||
with pytest.raises(ValueError, match=r"Expected a non-empty value for `model_name` but received ''"):
|
||||
client.workers.ai.with_raw_response.run(
|
||||
"",
|
||||
account_id="023e105f4ecef8ad9ca31a8372d0c353",
|
||||
body={},
|
||||
body={"text": "string"},
|
||||
)
|
||||
|
||||
|
||||
|
|
@ -83,9 +94,19 @@ class TestAsyncAI:
|
|||
ai = await async_client.workers.ai.run(
|
||||
"string",
|
||||
account_id="023e105f4ecef8ad9ca31a8372d0c353",
|
||||
body={},
|
||||
body={"text": "string"},
|
||||
)
|
||||
assert_matches_type(object, ai, path=["response"])
|
||||
assert_matches_type(Optional[AIRunResponse], ai, path=["response"])
|
||||
|
||||
@pytest.mark.skip()
|
||||
@parametrize
|
||||
async def test_method_run_with_all_params(self, async_client: AsyncCloudflare) -> None:
|
||||
ai = await async_client.workers.ai.run(
|
||||
"string",
|
||||
account_id="023e105f4ecef8ad9ca31a8372d0c353",
|
||||
body={"text": "string"},
|
||||
)
|
||||
assert_matches_type(Optional[AIRunResponse], ai, path=["response"])
|
||||
|
||||
@pytest.mark.skip()
|
||||
@parametrize
|
||||
|
|
@ -93,13 +114,13 @@ class TestAsyncAI:
|
|||
response = await async_client.workers.ai.with_raw_response.run(
|
||||
"string",
|
||||
account_id="023e105f4ecef8ad9ca31a8372d0c353",
|
||||
body={},
|
||||
body={"text": "string"},
|
||||
)
|
||||
|
||||
assert response.is_closed is True
|
||||
assert response.http_request.headers.get("X-Stainless-Lang") == "python"
|
||||
ai = await response.parse()
|
||||
assert_matches_type(object, ai, path=["response"])
|
||||
assert_matches_type(Optional[AIRunResponse], ai, path=["response"])
|
||||
|
||||
@pytest.mark.skip()
|
||||
@parametrize
|
||||
|
|
@ -107,13 +128,13 @@ class TestAsyncAI:
|
|||
async with async_client.workers.ai.with_streaming_response.run(
|
||||
"string",
|
||||
account_id="023e105f4ecef8ad9ca31a8372d0c353",
|
||||
body={},
|
||||
body={"text": "string"},
|
||||
) as response:
|
||||
assert not response.is_closed
|
||||
assert response.http_request.headers.get("X-Stainless-Lang") == "python"
|
||||
|
||||
ai = await response.parse()
|
||||
assert_matches_type(object, ai, path=["response"])
|
||||
assert_matches_type(Optional[AIRunResponse], ai, path=["response"])
|
||||
|
||||
assert cast(Any, response.is_closed) is True
|
||||
|
||||
|
|
@ -124,12 +145,12 @@ class TestAsyncAI:
|
|||
await async_client.workers.ai.with_raw_response.run(
|
||||
"string",
|
||||
account_id="",
|
||||
body={},
|
||||
body={"text": "string"},
|
||||
)
|
||||
|
||||
with pytest.raises(ValueError, match=r"Expected a non-empty value for `model_name` but received ''"):
|
||||
await async_client.workers.ai.with_raw_response.run(
|
||||
"",
|
||||
account_id="023e105f4ecef8ad9ca31a8372d0c353",
|
||||
body={},
|
||||
body={"text": "string"},
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue