AskReference
ProcessIntermediate

What is the purpose of the get_embeddings_in_batches() function and how does it handle large lists of texts?

The get_embeddings_in_batches() function generates embeddings for a list of texts by dividing the list into chunks of a specified batch_size and making one API call per chunk. It then collects all returned embeddings into a single list, allowing large lists to be processed even though the embedding model can only handle a limited number of texts per API call.

The function takes three inputs: the list of texts to embed, the name of the embedding model, and the batch size. It calculates how many batches are needed and loops through the list in steps of batch_size, slicing out one batch of texts at a time. For each batch it shows progress messages, calls the embedding model using a SEMANTIC_SIMILARITY task type, appends the embeddings from the API response to an overall list, and pauses for one second before the next batch. After all batches are processed, it returns the complete list of embeddings. This design is necessary because the number of texts a user may want to embed can be very large, while the API can only process a limited number at once; the batch_size variable makes this adjustable, with 100 used in the example.

Key points

  • get_embeddings_in_batches() generates embeddings for a list of texts in manageable API calls rather than one large call.
  • It takes a texts list, model name, and batch_size as inputs.
  • A for loop iterates through texts in steps of batch_size and sends each slice to the embedding model.
  • The function collects all response embeddings into an all_embeddings list and returns that list.
  • Batching accommodates large text collections because the API can only process a limited number of items per request.
  • The batch_size can be customized; the example uses 100 texts per batch.
Source:AI for Qualitative Research: A Hands-On Guide for Management Scholars· Information Retrieval and Retrieval-Augmented Generation· p. 152–159
Cover of AI for Qualitative Research: A Hands-On Guide for Management Scholars

AI for Qualitative Research: A Hands-On Guide for Management Scholars

Diana Garcia Quevedo

Palgrave Macmillan

View this ebook