oa.base¶
Base oa functionality
Functions
|
|
|
|
|
Compute the price of a model given the number of input and output tokens |
|
|
|
Get embeddings for a text or texts. |
|
List the available engine IDs. |
|
Get information about a model |
|
Ensures the type of texts is an iterable of strings |
|
Check if text (a string or iterable of strings) is/are valid for a given model. |
|
- oa.base.compute_price(model, num_input_tokens=None, num_output_tokens=None)[source]¶
Compute the price of a model given the number of input and output tokens
- oa.base.embeddings(texts=None, *, batch_size=2048, egress=None, batch_callback=None, validate=True, valid_text_getter=<function _raise_if_any_invalid>, model='text-embedding-3-small', client=None, dimensions=NOT_GIVEN, encoding_format=<openai.Omit object>, user=<openai.Omit object>, extra_headers=None, extra_query=None, extra_body=None, timeout=NOT_GIVEN)[source]¶
Get embeddings for a text or texts.
- Parameters:
texts (
Union[str,Iterable[str],Mapping[TypeVar(KT),str]]) – A string, an iterable of strings, or a dictionary of stringsegress (
str|None) – A function that takes the embeddings and returns the desired output. If None, the output will be a list of embeddings. If False, no output will be returned, so the batch_callback had better be set to accumulate the results.batch_callback (
Callable[[int,list[list]],Any] |None) – A function that is called after each batch of embeddings is computed. This can be used for logging, saving, etc. One common use case is to save the intermediate results, in files, database, or in a list. This can be useful if you’re worried about the process failing and want to be able to resume from where you left off instead of having to start over (wasting time, and money). To accumulate the results in a list, you can setresults = []and then use a lambda function like this:batch_callback = lambda i, batch: results.extend(batch).validate (
bool|Callable|None) – If True, validate the text(s) before getting embeddingsvalid_text_getter – A function that gets valid texts from the input texts
model – The model to use for embeddings
client – The OpenAI client to use
dimensions (
int|None) – If given will reduce the dimensions of the full size embedding vectors to that sizebatch_size (
int|None) – The maximum number of texts to send in a single requestextra_embeddings_params – Extra parameters to pass to the embeddings API
>>> from functools import partial >>> dimensions = 3 >>> embeddings_ = partial(embeddings, dimensions=dimensions, validate=True)Test with a single word:
>>> text = "vector" >>> result = embeddings_(text) >>> result [-0.4096039831638336, 0.3794299364089966, -0.8296127915382385] >>> isinstance(result, list) True >>> len(result) == dimensions == 3 TrueTest with a list of words
>>> texts = ["semantic", "vector"] >>> result = embeddings_(texts) >>> isinstance(result, list) True >>> len(result) 2Two vectors; one for each word. Note that the second vector is the vector of “vector”, which we’ve seen before.
>>> result[1] [-0.4096039831638336, 0.3794299364089966, -0.8296127915382385]>>> len(result[1]) == dimensions == 3 TrueTest with a dictionary of words
>>> texts = {"adj": "semantic", "noun": "vector"} >>> result = embeddings_(texts) >>> isinstance(result, dict) True >>> len(result) 2 >>> result["noun"] [-0.4096039831638336, 0.3794299364089966, -0.8296127915382385] >>> len(result["adj"]) == len(result["noun"]) == dimensions == 3 TrueIf you don’t specify
texts, you will get a “partial” function that you can use later to compute embeddings for texts. This is useful if you want to set some parameters (likedimensions,validate, etc.) and then use the resulting function to compute embeddings for different texts later on.>>> embeddings_with_10_dimensions = embeddings_(texts=None, dimensions=10) >>> isinstance(embeddings_with_10_dimensions, Callable) True
- oa.base.list_engine_ids(pattern=None)[source]¶
List the available engine IDs. Optionally filter by a regex pattern.
- oa.base.text_is_valid(texts, token_count=True, *, model='text-embedding-3-small', max_tokens=None)[source]¶
Check if text (a string or iterable of strings) is/are valid for a given model.
Text is valid if
it is not empty
the number of tokens in the text is less than or equal to the max_tokens
- Parameters:
If token_count is an integer, it will check if it is less than or equal to the
max_tokens.If token_count is True, it will compute the number of tokens in the text using the model specified by
modeland check if it is less than or equal tomax_tokens.If token_count is False, it will not check the token count.
If token_count is an iterable, it will apply the same mechanism as above, to each text in
textsand the corresponding token count intoken_count. This means both texts and token_count(s) must be of the same length.Examples
>>> text_is_valid('Hello, world!') True >>> text_is_valid('') False >>> text_is_valid('Alice ' * 9000) False >>> text_is_valid('Alice ' * 9000, token_count=False) True >>> list(text_is_valid(['Bob', '', 'Alice ' * 9000])) [True, False, False] >>> list(text_is_valid(['Bob', '', 'Alice ' * 9000], token_count=False)) [True, False, True]