|
| 1 | +from typing import Any, Dict, List, Union, cast, Literal |
| 2 | +from typing_extensions import NotRequired, TypedDict |
| 3 | +from .request import Request, RequestConfig |
| 4 | +from .async_request import AsyncRequest |
| 5 | +from typing import List, Union |
| 6 | +from ._config import ClientConfig |
| 7 | + |
| 8 | + |
| 9 | +class EmbeddingParams(TypedDict): |
| 10 | + text: Union[str, List[str]] |
| 11 | + """ |
| 12 | + The text to summarize. |
| 13 | + """ |
| 14 | + |
| 15 | + type: NotRequired[Literal["text", "points"]] |
| 16 | + |
| 17 | + """ |
| 18 | + The summary result type. Supported values are: text, points |
| 19 | + """ |
| 20 | + url: NotRequired[str] |
| 21 | + file_store_key: NotRequired[str] |
| 22 | + max_points: NotRequired[int] |
| 23 | + max_characters: NotRequired[int] |
| 24 | + |
| 25 | + |
| 26 | +class EmbeddingResponse(TypedDict): |
| 27 | + success: bool |
| 28 | + """ |
| 29 | + Indicates whether the translation was successful. |
| 30 | + """ |
| 31 | + summary: str |
| 32 | + """ |
| 33 | + The summarized text. |
| 34 | + """ |
| 35 | + |
| 36 | + |
| 37 | +class Embedding(ClientConfig): |
| 38 | + |
| 39 | + config: RequestConfig |
| 40 | + |
| 41 | + def __init__( |
| 42 | + self, |
| 43 | + api_key: str, |
| 44 | + api_url: str, |
| 45 | + disable_request_logging: Union[bool, None] = False, |
| 46 | + ): |
| 47 | + super().__init__(api_key, api_url, disable_request_logging) |
| 48 | + self.config = RequestConfig( |
| 49 | + api_url=api_url, |
| 50 | + api_key=api_key, |
| 51 | + disable_request_logging=disable_request_logging, |
| 52 | + ) |
| 53 | + |
| 54 | + |
| 55 | +class AsyncEmbedding(ClientConfig): |
| 56 | + |
| 57 | + config: RequestConfig |
| 58 | + |
| 59 | + def __init__( |
| 60 | + self, |
| 61 | + api_key: str, |
| 62 | + api_url: str, |
| 63 | + disable_request_logging: Union[bool, None] = False, |
| 64 | + ): |
| 65 | + super().__init__(api_key, api_url, disable_request_logging) |
| 66 | + self.config = RequestConfig( |
| 67 | + api_url=api_url, |
| 68 | + api_key=api_key, |
| 69 | + disable_request_logging=disable_request_logging, |
| 70 | + ) |
| 71 | + |
| 72 | + async def execute(self, params: EmbeddingParams) -> EmbeddingResponse: |
| 73 | + path = "/ai/embedding" |
| 74 | + resp = await AsyncRequest( |
| 75 | + config=self.config, |
| 76 | + path=path, |
| 77 | + params=cast(Dict[Any, Any], params), |
| 78 | + verb="post", |
| 79 | + ).perform_with_content() |
| 80 | + return resp |
0 commit comments