|
| 1 | +# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import httpx |
| 6 | + |
| 7 | +from ..types import retrieve_documents_params |
| 8 | +from .._types import Body, Omit, Query, Headers, NotGiven, omit, not_given |
| 9 | +from .._utils import maybe_transform, async_maybe_transform |
| 10 | +from .._compat import cached_property |
| 11 | +from .._resource import SyncAPIResource, AsyncAPIResource |
| 12 | +from .._response import ( |
| 13 | + to_raw_response_wrapper, |
| 14 | + to_streamed_response_wrapper, |
| 15 | + async_to_raw_response_wrapper, |
| 16 | + async_to_streamed_response_wrapper, |
| 17 | +) |
| 18 | +from .._base_client import make_request_options |
| 19 | +from ..types.retrieve_documents_response import RetrieveDocumentsResponse |
| 20 | + |
| 21 | +__all__ = ["RetrieveResource", "AsyncRetrieveResource"] |
| 22 | + |
| 23 | + |
| 24 | +class RetrieveResource(SyncAPIResource): |
| 25 | + @cached_property |
| 26 | + def with_raw_response(self) -> RetrieveResourceWithRawResponse: |
| 27 | + """ |
| 28 | + This property can be used as a prefix for any HTTP method call to return |
| 29 | + the raw response object instead of the parsed content. |
| 30 | +
|
| 31 | + For more information, see https://www.github.com/digitalocean/gradient-python#accessing-raw-response-data-eg-headers |
| 32 | + """ |
| 33 | + return RetrieveResourceWithRawResponse(self) |
| 34 | + |
| 35 | + @cached_property |
| 36 | + def with_streaming_response(self) -> RetrieveResourceWithStreamingResponse: |
| 37 | + """ |
| 38 | + An alternative to `.with_raw_response` that doesn't eagerly read the response body. |
| 39 | +
|
| 40 | + For more information, see https://www.github.com/digitalocean/gradient-python#with_streaming_response |
| 41 | + """ |
| 42 | + return RetrieveResourceWithStreamingResponse(self) |
| 43 | + |
| 44 | + def documents( |
| 45 | + self, |
| 46 | + knowledge_base_id: str, |
| 47 | + *, |
| 48 | + num_results: int, |
| 49 | + query: str, |
| 50 | + alpha: float | Omit = omit, |
| 51 | + filters: retrieve_documents_params.Filters | Omit = omit, |
| 52 | + # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs. |
| 53 | + # The extra values given here take precedence over values defined on the client or passed to this method. |
| 54 | + extra_headers: Headers | None = None, |
| 55 | + extra_query: Query | None = None, |
| 56 | + extra_body: Body | None = None, |
| 57 | + timeout: float | httpx.Timeout | None | NotGiven = not_given, |
| 58 | + ) -> RetrieveDocumentsResponse: |
| 59 | + """ |
| 60 | + Retrieve relevant documents from a knowledge base using semantic search. |
| 61 | +
|
| 62 | + This endpoint: |
| 63 | +
|
| 64 | + 1. Authenticates the request using the provided bearer token |
| 65 | + 2. Generates embeddings for the query using the knowledge base's configured |
| 66 | + model |
| 67 | + 3. Performs vector similarity search in the knowledge base |
| 68 | + 4. Returns the most relevant document chunks |
| 69 | +
|
| 70 | + The search supports hybrid search combining: |
| 71 | +
|
| 72 | + - Vector similarity (semantic search) |
| 73 | + - Keyword matching (BM25) |
| 74 | + - Custom metadata filters |
| 75 | +
|
| 76 | + Args: |
| 77 | + num_results: Number of results to return |
| 78 | +
|
| 79 | + query: The search query text |
| 80 | +
|
| 81 | + alpha: |
| 82 | + Weight for hybrid search (0-1): |
| 83 | +
|
| 84 | + - 0 = pure keyword search (BM25) |
| 85 | + - 1 = pure vector search (default) |
| 86 | + - 0.5 = balanced hybrid search |
| 87 | +
|
| 88 | + filters: Metadata filters to apply to the search |
| 89 | +
|
| 90 | + extra_headers: Send extra headers |
| 91 | +
|
| 92 | + extra_query: Add additional query parameters to the request |
| 93 | +
|
| 94 | + extra_body: Add additional JSON properties to the request |
| 95 | +
|
| 96 | + timeout: Override the client-level default timeout for this request, in seconds |
| 97 | + """ |
| 98 | + if not knowledge_base_id: |
| 99 | + raise ValueError(f"Expected a non-empty value for `knowledge_base_id` but received {knowledge_base_id!r}") |
| 100 | + return self._post( |
| 101 | + f"/{knowledge_base_id}/retrieve" |
| 102 | + if self._client._base_url_overridden |
| 103 | + else f"https://kbaas.do-ai.run/v1/{knowledge_base_id}/retrieve", |
| 104 | + body=maybe_transform( |
| 105 | + { |
| 106 | + "num_results": num_results, |
| 107 | + "query": query, |
| 108 | + "alpha": alpha, |
| 109 | + "filters": filters, |
| 110 | + }, |
| 111 | + retrieve_documents_params.RetrieveDocumentsParams, |
| 112 | + ), |
| 113 | + options=make_request_options( |
| 114 | + extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout |
| 115 | + ), |
| 116 | + cast_to=RetrieveDocumentsResponse, |
| 117 | + ) |
| 118 | + |
| 119 | + |
| 120 | +class AsyncRetrieveResource(AsyncAPIResource): |
| 121 | + @cached_property |
| 122 | + def with_raw_response(self) -> AsyncRetrieveResourceWithRawResponse: |
| 123 | + """ |
| 124 | + This property can be used as a prefix for any HTTP method call to return |
| 125 | + the raw response object instead of the parsed content. |
| 126 | +
|
| 127 | + For more information, see https://www.github.com/digitalocean/gradient-python#accessing-raw-response-data-eg-headers |
| 128 | + """ |
| 129 | + return AsyncRetrieveResourceWithRawResponse(self) |
| 130 | + |
| 131 | + @cached_property |
| 132 | + def with_streaming_response(self) -> AsyncRetrieveResourceWithStreamingResponse: |
| 133 | + """ |
| 134 | + An alternative to `.with_raw_response` that doesn't eagerly read the response body. |
| 135 | +
|
| 136 | + For more information, see https://www.github.com/digitalocean/gradient-python#with_streaming_response |
| 137 | + """ |
| 138 | + return AsyncRetrieveResourceWithStreamingResponse(self) |
| 139 | + |
| 140 | + async def documents( |
| 141 | + self, |
| 142 | + knowledge_base_id: str, |
| 143 | + *, |
| 144 | + num_results: int, |
| 145 | + query: str, |
| 146 | + alpha: float | Omit = omit, |
| 147 | + filters: retrieve_documents_params.Filters | Omit = omit, |
| 148 | + # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs. |
| 149 | + # The extra values given here take precedence over values defined on the client or passed to this method. |
| 150 | + extra_headers: Headers | None = None, |
| 151 | + extra_query: Query | None = None, |
| 152 | + extra_body: Body | None = None, |
| 153 | + timeout: float | httpx.Timeout | None | NotGiven = not_given, |
| 154 | + ) -> RetrieveDocumentsResponse: |
| 155 | + """ |
| 156 | + Retrieve relevant documents from a knowledge base using semantic search. |
| 157 | +
|
| 158 | + This endpoint: |
| 159 | +
|
| 160 | + 1. Authenticates the request using the provided bearer token |
| 161 | + 2. Generates embeddings for the query using the knowledge base's configured |
| 162 | + model |
| 163 | + 3. Performs vector similarity search in the knowledge base |
| 164 | + 4. Returns the most relevant document chunks |
| 165 | +
|
| 166 | + The search supports hybrid search combining: |
| 167 | +
|
| 168 | + - Vector similarity (semantic search) |
| 169 | + - Keyword matching (BM25) |
| 170 | + - Custom metadata filters |
| 171 | +
|
| 172 | + Args: |
| 173 | + num_results: Number of results to return |
| 174 | +
|
| 175 | + query: The search query text |
| 176 | +
|
| 177 | + alpha: |
| 178 | + Weight for hybrid search (0-1): |
| 179 | +
|
| 180 | + - 0 = pure keyword search (BM25) |
| 181 | + - 1 = pure vector search (default) |
| 182 | + - 0.5 = balanced hybrid search |
| 183 | +
|
| 184 | + filters: Metadata filters to apply to the search |
| 185 | +
|
| 186 | + extra_headers: Send extra headers |
| 187 | +
|
| 188 | + extra_query: Add additional query parameters to the request |
| 189 | +
|
| 190 | + extra_body: Add additional JSON properties to the request |
| 191 | +
|
| 192 | + timeout: Override the client-level default timeout for this request, in seconds |
| 193 | + """ |
| 194 | + if not knowledge_base_id: |
| 195 | + raise ValueError(f"Expected a non-empty value for `knowledge_base_id` but received {knowledge_base_id!r}") |
| 196 | + return await self._post( |
| 197 | + f"/{knowledge_base_id}/retrieve" |
| 198 | + if self._client._base_url_overridden |
| 199 | + else f"https://kbaas.do-ai.run/v1/{knowledge_base_id}/retrieve", |
| 200 | + body=await async_maybe_transform( |
| 201 | + { |
| 202 | + "num_results": num_results, |
| 203 | + "query": query, |
| 204 | + "alpha": alpha, |
| 205 | + "filters": filters, |
| 206 | + }, |
| 207 | + retrieve_documents_params.RetrieveDocumentsParams, |
| 208 | + ), |
| 209 | + options=make_request_options( |
| 210 | + extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout |
| 211 | + ), |
| 212 | + cast_to=RetrieveDocumentsResponse, |
| 213 | + ) |
| 214 | + |
| 215 | + |
| 216 | +class RetrieveResourceWithRawResponse: |
| 217 | + def __init__(self, retrieve: RetrieveResource) -> None: |
| 218 | + self._retrieve = retrieve |
| 219 | + |
| 220 | + self.documents = to_raw_response_wrapper( |
| 221 | + retrieve.documents, |
| 222 | + ) |
| 223 | + |
| 224 | + |
| 225 | +class AsyncRetrieveResourceWithRawResponse: |
| 226 | + def __init__(self, retrieve: AsyncRetrieveResource) -> None: |
| 227 | + self._retrieve = retrieve |
| 228 | + |
| 229 | + self.documents = async_to_raw_response_wrapper( |
| 230 | + retrieve.documents, |
| 231 | + ) |
| 232 | + |
| 233 | + |
| 234 | +class RetrieveResourceWithStreamingResponse: |
| 235 | + def __init__(self, retrieve: RetrieveResource) -> None: |
| 236 | + self._retrieve = retrieve |
| 237 | + |
| 238 | + self.documents = to_streamed_response_wrapper( |
| 239 | + retrieve.documents, |
| 240 | + ) |
| 241 | + |
| 242 | + |
| 243 | +class AsyncRetrieveResourceWithStreamingResponse: |
| 244 | + def __init__(self, retrieve: AsyncRetrieveResource) -> None: |
| 245 | + self._retrieve = retrieve |
| 246 | + |
| 247 | + self.documents = async_to_streamed_response_wrapper( |
| 248 | + retrieve.documents, |
| 249 | + ) |
0 commit comments