|
| 1 | +# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from typing import List, Optional |
| 6 | +from typing_extensions import Literal |
| 7 | + |
| 8 | +import httpx |
| 9 | + |
| 10 | +from ..types import reranking_create_params |
| 11 | +from .._types import NOT_GIVEN, Body, Query, Headers, NotGiven |
| 12 | +from .._utils import ( |
| 13 | + maybe_transform, |
| 14 | + async_maybe_transform, |
| 15 | +) |
| 16 | +from .._compat import cached_property |
| 17 | +from .._resource import SyncAPIResource, AsyncAPIResource |
| 18 | +from .._response import ( |
| 19 | + to_raw_response_wrapper, |
| 20 | + to_streamed_response_wrapper, |
| 21 | + async_to_raw_response_wrapper, |
| 22 | + async_to_streamed_response_wrapper, |
| 23 | +) |
| 24 | +from .._base_client import make_request_options |
| 25 | +from ..types.reranking import Reranking |
| 26 | + |
| 27 | +__all__ = ["RerankingsResource", "AsyncRerankingsResource"] |
| 28 | + |
| 29 | + |
| 30 | +class RerankingsResource(SyncAPIResource): |
| 31 | + @cached_property |
| 32 | + def with_raw_response(self) -> RerankingsResourceWithRawResponse: |
| 33 | + """ |
| 34 | + This property can be used as a prefix for any HTTP method call to return |
| 35 | + the raw response object instead of the parsed content. |
| 36 | +
|
| 37 | + For more information, see https://www.github.com/isaacus-dev/isaacus-python#accessing-raw-response-data-eg-headers |
| 38 | + """ |
| 39 | + return RerankingsResourceWithRawResponse(self) |
| 40 | + |
| 41 | + @cached_property |
| 42 | + def with_streaming_response(self) -> RerankingsResourceWithStreamingResponse: |
| 43 | + """ |
| 44 | + An alternative to `.with_raw_response` that doesn't eagerly read the response body. |
| 45 | +
|
| 46 | + For more information, see https://www.github.com/isaacus-dev/isaacus-python#with_streaming_response |
| 47 | + """ |
| 48 | + return RerankingsResourceWithStreamingResponse(self) |
| 49 | + |
| 50 | + def create( |
| 51 | + self, |
| 52 | + *, |
| 53 | + model: Literal["kanon-universal-classifier", "kanon-universal-classifier-mini"], |
| 54 | + query: str, |
| 55 | + texts: List[str], |
| 56 | + chunking_options: Optional[reranking_create_params.ChunkingOptions] | NotGiven = NOT_GIVEN, |
| 57 | + is_iql: bool | NotGiven = NOT_GIVEN, |
| 58 | + scoring_method: Literal["auto", "chunk_max", "chunk_avg", "chunk_min"] | NotGiven = NOT_GIVEN, |
| 59 | + top_n: Optional[int] | NotGiven = NOT_GIVEN, |
| 60 | + # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs. |
| 61 | + # The extra values given here take precedence over values defined on the client or passed to this method. |
| 62 | + extra_headers: Headers | None = None, |
| 63 | + extra_query: Query | None = None, |
| 64 | + extra_body: Body | None = None, |
| 65 | + timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN, |
| 66 | + ) -> Reranking: |
| 67 | + """ |
| 68 | + Rerank legal documents by their relevance to a query with an Isaacus legal AI |
| 69 | + reranker. |
| 70 | +
|
| 71 | + Args: |
| 72 | + model: The ID of the [model](https://docs.isaacus.com/models#reranking) to use for |
| 73 | + reranking. |
| 74 | +
|
| 75 | + query: The query to evaluate the relevance of the texts to. |
| 76 | +
|
| 77 | + The query must contain at least one non-whitespace character. |
| 78 | +
|
| 79 | + Unlike the texts being reranked, the query cannot be so long that it exceeds the |
| 80 | + maximum input length of the reranker. |
| 81 | +
|
| 82 | + texts: The texts to rerank. |
| 83 | +
|
| 84 | + There must be at least one text. |
| 85 | +
|
| 86 | + The texts must contain at least one non-whitespace character. |
| 87 | +
|
| 88 | + chunking_options: Options for how to split text into smaller chunks. |
| 89 | +
|
| 90 | + is_iql: Whether the query should be interpreted as an |
| 91 | + [Isaacus Query Language (IQL)](https://docs.isaacus.com/iql) query, which is not |
| 92 | + the case by default. |
| 93 | +
|
| 94 | + If you allow untrusted users to construct their own queries, think carefully |
| 95 | + before enabling IQL since queries can be crafted to consume an excessively large |
| 96 | + amount of tokens. |
| 97 | +
|
| 98 | + scoring_method: The method to use for producing an overall relevance score for a text. |
| 99 | +
|
| 100 | + `auto` is the default scoring method and is recommended for most use cases. |
| 101 | + Currently, it is equivalent to `chunk_max`. In the future, it will automatically |
| 102 | + select the best method based on the model and inputs. |
| 103 | +
|
| 104 | + `chunk_max` uses the highest relevance score of all of a text's chunks. |
| 105 | +
|
| 106 | + `chunk_avg` averages the relevance scores of all of a text's chunks. |
| 107 | +
|
| 108 | + `chunk_min` uses the lowest relevance score of all of a text's chunks. |
| 109 | +
|
| 110 | + top_n: A whole number greater than or equal to 1. |
| 111 | +
|
| 112 | + extra_headers: Send extra headers |
| 113 | +
|
| 114 | + extra_query: Add additional query parameters to the request |
| 115 | +
|
| 116 | + extra_body: Add additional JSON properties to the request |
| 117 | +
|
| 118 | + timeout: Override the client-level default timeout for this request, in seconds |
| 119 | + """ |
| 120 | + return self._post( |
| 121 | + "/rerankings", |
| 122 | + body=maybe_transform( |
| 123 | + { |
| 124 | + "model": model, |
| 125 | + "query": query, |
| 126 | + "texts": texts, |
| 127 | + "chunking_options": chunking_options, |
| 128 | + "is_iql": is_iql, |
| 129 | + "scoring_method": scoring_method, |
| 130 | + "top_n": top_n, |
| 131 | + }, |
| 132 | + reranking_create_params.RerankingCreateParams, |
| 133 | + ), |
| 134 | + options=make_request_options( |
| 135 | + extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout |
| 136 | + ), |
| 137 | + cast_to=Reranking, |
| 138 | + ) |
| 139 | + |
| 140 | + |
| 141 | +class AsyncRerankingsResource(AsyncAPIResource): |
| 142 | + @cached_property |
| 143 | + def with_raw_response(self) -> AsyncRerankingsResourceWithRawResponse: |
| 144 | + """ |
| 145 | + This property can be used as a prefix for any HTTP method call to return |
| 146 | + the raw response object instead of the parsed content. |
| 147 | +
|
| 148 | + For more information, see https://www.github.com/isaacus-dev/isaacus-python#accessing-raw-response-data-eg-headers |
| 149 | + """ |
| 150 | + return AsyncRerankingsResourceWithRawResponse(self) |
| 151 | + |
| 152 | + @cached_property |
| 153 | + def with_streaming_response(self) -> AsyncRerankingsResourceWithStreamingResponse: |
| 154 | + """ |
| 155 | + An alternative to `.with_raw_response` that doesn't eagerly read the response body. |
| 156 | +
|
| 157 | + For more information, see https://www.github.com/isaacus-dev/isaacus-python#with_streaming_response |
| 158 | + """ |
| 159 | + return AsyncRerankingsResourceWithStreamingResponse(self) |
| 160 | + |
| 161 | + async def create( |
| 162 | + self, |
| 163 | + *, |
| 164 | + model: Literal["kanon-universal-classifier", "kanon-universal-classifier-mini"], |
| 165 | + query: str, |
| 166 | + texts: List[str], |
| 167 | + chunking_options: Optional[reranking_create_params.ChunkingOptions] | NotGiven = NOT_GIVEN, |
| 168 | + is_iql: bool | NotGiven = NOT_GIVEN, |
| 169 | + scoring_method: Literal["auto", "chunk_max", "chunk_avg", "chunk_min"] | NotGiven = NOT_GIVEN, |
| 170 | + top_n: Optional[int] | NotGiven = NOT_GIVEN, |
| 171 | + # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs. |
| 172 | + # The extra values given here take precedence over values defined on the client or passed to this method. |
| 173 | + extra_headers: Headers | None = None, |
| 174 | + extra_query: Query | None = None, |
| 175 | + extra_body: Body | None = None, |
| 176 | + timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN, |
| 177 | + ) -> Reranking: |
| 178 | + """ |
| 179 | + Rerank legal documents by their relevance to a query with an Isaacus legal AI |
| 180 | + reranker. |
| 181 | +
|
| 182 | + Args: |
| 183 | + model: The ID of the [model](https://docs.isaacus.com/models#reranking) to use for |
| 184 | + reranking. |
| 185 | +
|
| 186 | + query: The query to evaluate the relevance of the texts to. |
| 187 | +
|
| 188 | + The query must contain at least one non-whitespace character. |
| 189 | +
|
| 190 | + Unlike the texts being reranked, the query cannot be so long that it exceeds the |
| 191 | + maximum input length of the reranker. |
| 192 | +
|
| 193 | + texts: The texts to rerank. |
| 194 | +
|
| 195 | + There must be at least one text. |
| 196 | +
|
| 197 | + The texts must contain at least one non-whitespace character. |
| 198 | +
|
| 199 | + chunking_options: Options for how to split text into smaller chunks. |
| 200 | +
|
| 201 | + is_iql: Whether the query should be interpreted as an |
| 202 | + [Isaacus Query Language (IQL)](https://docs.isaacus.com/iql) query, which is not |
| 203 | + the case by default. |
| 204 | +
|
| 205 | + If you allow untrusted users to construct their own queries, think carefully |
| 206 | + before enabling IQL since queries can be crafted to consume an excessively large |
| 207 | + amount of tokens. |
| 208 | +
|
| 209 | + scoring_method: The method to use for producing an overall relevance score for a text. |
| 210 | +
|
| 211 | + `auto` is the default scoring method and is recommended for most use cases. |
| 212 | + Currently, it is equivalent to `chunk_max`. In the future, it will automatically |
| 213 | + select the best method based on the model and inputs. |
| 214 | +
|
| 215 | + `chunk_max` uses the highest relevance score of all of a text's chunks. |
| 216 | +
|
| 217 | + `chunk_avg` averages the relevance scores of all of a text's chunks. |
| 218 | +
|
| 219 | + `chunk_min` uses the lowest relevance score of all of a text's chunks. |
| 220 | +
|
| 221 | + top_n: A whole number greater than or equal to 1. |
| 222 | +
|
| 223 | + extra_headers: Send extra headers |
| 224 | +
|
| 225 | + extra_query: Add additional query parameters to the request |
| 226 | +
|
| 227 | + extra_body: Add additional JSON properties to the request |
| 228 | +
|
| 229 | + timeout: Override the client-level default timeout for this request, in seconds |
| 230 | + """ |
| 231 | + return await self._post( |
| 232 | + "/rerankings", |
| 233 | + body=await async_maybe_transform( |
| 234 | + { |
| 235 | + "model": model, |
| 236 | + "query": query, |
| 237 | + "texts": texts, |
| 238 | + "chunking_options": chunking_options, |
| 239 | + "is_iql": is_iql, |
| 240 | + "scoring_method": scoring_method, |
| 241 | + "top_n": top_n, |
| 242 | + }, |
| 243 | + reranking_create_params.RerankingCreateParams, |
| 244 | + ), |
| 245 | + options=make_request_options( |
| 246 | + extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout |
| 247 | + ), |
| 248 | + cast_to=Reranking, |
| 249 | + ) |
| 250 | + |
| 251 | + |
| 252 | +class RerankingsResourceWithRawResponse: |
| 253 | + def __init__(self, rerankings: RerankingsResource) -> None: |
| 254 | + self._rerankings = rerankings |
| 255 | + |
| 256 | + self.create = to_raw_response_wrapper( |
| 257 | + rerankings.create, |
| 258 | + ) |
| 259 | + |
| 260 | + |
| 261 | +class AsyncRerankingsResourceWithRawResponse: |
| 262 | + def __init__(self, rerankings: AsyncRerankingsResource) -> None: |
| 263 | + self._rerankings = rerankings |
| 264 | + |
| 265 | + self.create = async_to_raw_response_wrapper( |
| 266 | + rerankings.create, |
| 267 | + ) |
| 268 | + |
| 269 | + |
| 270 | +class RerankingsResourceWithStreamingResponse: |
| 271 | + def __init__(self, rerankings: RerankingsResource) -> None: |
| 272 | + self._rerankings = rerankings |
| 273 | + |
| 274 | + self.create = to_streamed_response_wrapper( |
| 275 | + rerankings.create, |
| 276 | + ) |
| 277 | + |
| 278 | + |
| 279 | +class AsyncRerankingsResourceWithStreamingResponse: |
| 280 | + def __init__(self, rerankings: AsyncRerankingsResource) -> None: |
| 281 | + self._rerankings = rerankings |
| 282 | + |
| 283 | + self.create = async_to_streamed_response_wrapper( |
| 284 | + rerankings.create, |
| 285 | + ) |
0 commit comments