213 lines
7.9 KiB
Python
213 lines
7.9 KiB
Python
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import List, Union, Optional
|
|
from typing_extensions import Literal
|
|
|
|
import httpx
|
|
|
|
from ..types import embedding_create_params
|
|
from .._types import NOT_GIVEN, Body, Query, Headers, NotGiven
|
|
from .._utils import maybe_transform, async_maybe_transform
|
|
from .._compat import cached_property
|
|
from .._resource import SyncAPIResource, AsyncAPIResource
|
|
from .._response import (
|
|
to_raw_response_wrapper,
|
|
to_streamed_response_wrapper,
|
|
async_to_raw_response_wrapper,
|
|
async_to_streamed_response_wrapper,
|
|
)
|
|
from .._base_client import make_request_options
|
|
from ..types.create_embedding_response import CreateEmbeddingResponse
|
|
|
|
__all__ = ["Embeddings", "AsyncEmbeddings"]
|
|
|
|
|
|
class Embeddings(SyncAPIResource):
|
|
@cached_property
|
|
def with_raw_response(self) -> EmbeddingsWithRawResponse:
|
|
"""
|
|
This property can be used as a prefix for any HTTP method call to return
|
|
the raw response object instead of the parsed content.
|
|
|
|
For more information, see https://www.github.com/groq/groq-python#accessing-raw-response-data-eg-headers
|
|
"""
|
|
return EmbeddingsWithRawResponse(self)
|
|
|
|
@cached_property
|
|
def with_streaming_response(self) -> EmbeddingsWithStreamingResponse:
|
|
"""
|
|
An alternative to `.with_raw_response` that doesn't eagerly read the response body.
|
|
|
|
For more information, see https://www.github.com/groq/groq-python#with_streaming_response
|
|
"""
|
|
return EmbeddingsWithStreamingResponse(self)
|
|
|
|
def create(
|
|
self,
|
|
*,
|
|
input: Union[str, List[str]],
|
|
model: Union[str, Literal["nomic-embed-text-v1_5"]],
|
|
encoding_format: Literal["float", "base64"] | NotGiven = NOT_GIVEN,
|
|
user: Optional[str] | NotGiven = NOT_GIVEN,
|
|
# 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,
|
|
) -> CreateEmbeddingResponse:
|
|
"""
|
|
Creates an embedding vector representing the input text.
|
|
|
|
Args:
|
|
input: Input text to embed, encoded as a string or array of tokens. To embed multiple
|
|
inputs in a single request, pass an array of strings or array of token arrays.
|
|
The input must not exceed the max input tokens for the model, cannot be an empty
|
|
string, and any array must be 2048 dimensions or less.
|
|
|
|
model: ID of the model to use.
|
|
|
|
encoding_format: The format to return the embeddings in. Can only be `float` or `base64`.
|
|
|
|
user: A unique identifier representing your end-user, which can help us monitor and
|
|
detect abuse.
|
|
|
|
extra_headers: Send extra headers
|
|
|
|
extra_query: Add additional query parameters to the request
|
|
|
|
extra_body: Add additional JSON properties to the request
|
|
|
|
timeout: Override the client-level default timeout for this request, in seconds
|
|
"""
|
|
return self._post(
|
|
"/openai/v1/embeddings",
|
|
body=maybe_transform(
|
|
{
|
|
"input": input,
|
|
"model": model,
|
|
"encoding_format": encoding_format,
|
|
"user": user,
|
|
},
|
|
embedding_create_params.EmbeddingCreateParams,
|
|
),
|
|
options=make_request_options(
|
|
extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
|
|
),
|
|
cast_to=CreateEmbeddingResponse,
|
|
)
|
|
|
|
|
|
class AsyncEmbeddings(AsyncAPIResource):
|
|
@cached_property
|
|
def with_raw_response(self) -> AsyncEmbeddingsWithRawResponse:
|
|
"""
|
|
This property can be used as a prefix for any HTTP method call to return
|
|
the raw response object instead of the parsed content.
|
|
|
|
For more information, see https://www.github.com/groq/groq-python#accessing-raw-response-data-eg-headers
|
|
"""
|
|
return AsyncEmbeddingsWithRawResponse(self)
|
|
|
|
@cached_property
|
|
def with_streaming_response(self) -> AsyncEmbeddingsWithStreamingResponse:
|
|
"""
|
|
An alternative to `.with_raw_response` that doesn't eagerly read the response body.
|
|
|
|
For more information, see https://www.github.com/groq/groq-python#with_streaming_response
|
|
"""
|
|
return AsyncEmbeddingsWithStreamingResponse(self)
|
|
|
|
async def create(
|
|
self,
|
|
*,
|
|
input: Union[str, List[str]],
|
|
model: Union[str, Literal["nomic-embed-text-v1_5"]],
|
|
encoding_format: Literal["float", "base64"] | NotGiven = NOT_GIVEN,
|
|
user: Optional[str] | NotGiven = NOT_GIVEN,
|
|
# 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,
|
|
) -> CreateEmbeddingResponse:
|
|
"""
|
|
Creates an embedding vector representing the input text.
|
|
|
|
Args:
|
|
input: Input text to embed, encoded as a string or array of tokens. To embed multiple
|
|
inputs in a single request, pass an array of strings or array of token arrays.
|
|
The input must not exceed the max input tokens for the model, cannot be an empty
|
|
string, and any array must be 2048 dimensions or less.
|
|
|
|
model: ID of the model to use.
|
|
|
|
encoding_format: The format to return the embeddings in. Can only be `float` or `base64`.
|
|
|
|
user: A unique identifier representing your end-user, which can help us monitor and
|
|
detect abuse.
|
|
|
|
extra_headers: Send extra headers
|
|
|
|
extra_query: Add additional query parameters to the request
|
|
|
|
extra_body: Add additional JSON properties to the request
|
|
|
|
timeout: Override the client-level default timeout for this request, in seconds
|
|
"""
|
|
return await self._post(
|
|
"/openai/v1/embeddings",
|
|
body=await async_maybe_transform(
|
|
{
|
|
"input": input,
|
|
"model": model,
|
|
"encoding_format": encoding_format,
|
|
"user": user,
|
|
},
|
|
embedding_create_params.EmbeddingCreateParams,
|
|
),
|
|
options=make_request_options(
|
|
extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
|
|
),
|
|
cast_to=CreateEmbeddingResponse,
|
|
)
|
|
|
|
|
|
class EmbeddingsWithRawResponse:
|
|
def __init__(self, embeddings: Embeddings) -> None:
|
|
self._embeddings = embeddings
|
|
|
|
self.create = to_raw_response_wrapper(
|
|
embeddings.create,
|
|
)
|
|
|
|
|
|
class AsyncEmbeddingsWithRawResponse:
|
|
def __init__(self, embeddings: AsyncEmbeddings) -> None:
|
|
self._embeddings = embeddings
|
|
|
|
self.create = async_to_raw_response_wrapper(
|
|
embeddings.create,
|
|
)
|
|
|
|
|
|
class EmbeddingsWithStreamingResponse:
|
|
def __init__(self, embeddings: Embeddings) -> None:
|
|
self._embeddings = embeddings
|
|
|
|
self.create = to_streamed_response_wrapper(
|
|
embeddings.create,
|
|
)
|
|
|
|
|
|
class AsyncEmbeddingsWithStreamingResponse:
|
|
def __init__(self, embeddings: AsyncEmbeddings) -> None:
|
|
self._embeddings = embeddings
|
|
|
|
self.create = async_to_streamed_response_wrapper(
|
|
embeddings.create,
|
|
)
|