ExAgent.Embeddings (ExAgent v3.0.0)

View Source

Buffered embeddings and document reranking through stock ReqLLM.

Stability — Candidate.

Each call dispatches at most one provider request. Use :max_items (64, up to 1000), :max_input_bytes (1 MiB of UTF-8), :max_output_bytes (4 MiB after decoding), and :timeout (30,000 ms, including admission). Input bounds apply across the batch and include the rerank query. Stock ReqLLM can allocate the provider response before these output checks.

Use :rate_limiter for provider or model quota. A matching TPM bucket requires a positive host reservation in :estimated_tokens. The limiter retains that reservation, while the returned usage reflects stock evidence. HTTP 429 cools down the selected bucket using a positive provider hint or one second by default. Use :spend_budget to reserve cents. A request with a complete estimated price settles the reserve. Missing or partial prices keep the hold for reconciliation. Set :price_per_token_cents to price the input tokens reported for an embedding. Rerank takes :price_per_search_unit_cents for reported search units.

Provider settings such as :dimensions, :encoding_format, :top_n, :api_key and :base_url go in :req_llm. ExAgent sets the deadline, disables retries and redirects and requests payload-free stock telemetry. Pass :observability for native OTel or inherit it in tools.

Summary

Functions

Embed one text or a batch. The result contains a vector list even for one text.

Read a tool result in the originating run's scope. The version 1 envelope contains "operation" and a "result" with "vectors" or "results".

Rank documents against a query. Each result refers to the index in the supplied document list. Hosts can set :top_n in :req_llm.

Build the configured embed_texts and rerank_documents tools. Each operation takes the direct-call options plus :model. Tool arguments are texts for embedding and query with documents for rerank. The host supplies a :result_store and can set :requires_approval.

Types

operation()

@type operation() :: :embed | :rerank

Functions

embed(model, text, opts \\ [])

Embed one text or a batch. The result contains a vector list even for one text.

{:ok, %{vectors: [vector], usage: usage}} =
  ExAgent.Embeddings.embed("openai:text-embedding-3-small", "a green lantern")

fetch(store, run_id, ref)

@spec fetch(
  ExAgent.Tool.ResultStore.t(),
  ExAgent.Tool.ResultStore.scope(),
  ExAgent.Tool.ResultStore.ref_id()
) ::
  {:ok, map()}
  | {:error, :expired | :unavailable | :invalid_embedding_reference}

Read a tool result in the originating run's scope. The version 1 envelope contains "operation" and a "result" with "vectors" or "results".

rerank(model, query, documents, opts \\ [])

Rank documents against a query. Each result refers to the index in the supplied document list. Hosts can set :top_n in :req_llm.

tools(opts)

@spec tools(keyword()) :: [ExAgent.Tool.t()]

Build the configured embed_texts and rerank_documents tools. Each operation takes the direct-call options plus :model. Tool arguments are texts for embedding and query with documents for rerank. The host supplies a :result_store and can set :requires_approval.

The tools return "ref" and "bytes" scoped to the originating run. They use the run's quota and spend account when present. Usage contributes once through the tool outcome. An ambiguous dispatch uses the normal continuation reconciliation flow for a write effect, including its possible charge. Keep provider and store bindings with the continuation definition.