Find Most Similar • AI Agent SDK

find_most_similar: Find the most similar texts to a query.

find_most_similar

Method

</div>

This is a method of the EmbeddingAgent class in the embedding_agent module.

Find the most similar texts to a query.

Signature

python
def find_most_similar(query: str, candidates: List[str], top_k: int, model: Optional[str]) -> List[Dict[str, Any]]

Parameters

ParamField str

Query text

ParamField List[str]

List of candidate texts to compare

ParamField int

Number of top results to return

ParamField Optional[str]

Override model for this call **kwargs: Additional parameters

Returns

ResponseField List[Dict[str, Any]]

List of dicts with ‘text’, ‘score’, and ‘index’ keys

Usage

python
agent = EmbeddingAgent()
    results = agent.find_most_similar(
        "What is AI?",
        ["Artificial intelligence is...", "Machine learning...", "Deep learning..."],
        top_k=2
    )
    for r in results:
        print(f"{r['score']:.2f}: {r['text'][:50]}...")

Uses

  • embed_batch
  • scores.sort

Source

View on GitHub

praisonaiagents/agent/embedding_agent.py at line 308