pyrompt.search#
Semantic search for prompts and templates using embeddings.
Provides similarity-based search over collections.
Functions
The embedding function used when none is passed: |
Classes
|
Semantic search index for prompts/templates. |
- class pyrompt.search.SemanticIndex(collection, *, auto_update=False, embedding_model='text-embedding-3-small', batch_size=100, embeddings=None)[source]#
Bases:
objectSemantic search index for prompts/templates.
Embeds through
embeddings(a keyword seam; by defaultoa.embeddings, elseaix.embeddings) to create vector representations, then supports similarity search.Examples
>>> from pyrompt import PromptCollection, SemanticIndex >>> prompts = PromptCollection('my_project') >>> prompts['python'] = "You are a Python expert." >>> prompts['data'] = "You specialize in data analysis." >>> index = SemanticIndex(prompts) >>> results = index.search("help with pandas", top_k=2)
- cluster(n_clusters=5)[source]#
Cluster prompts/templates into groups.
- Parameters:
n_clusters (
int) – Number of clusters- Returns:
Dict mapping cluster_id -> list of keys
- Raises:
ImportError – If scikit-learn not available
- search(query, top_k=5, filters=None, diversity_threshold=None)[source]#
Search for similar prompts.
- Parameters:
- Return type:
- Returns:
List of (key, similarity_score) tuples, sorted by score descending
Example
>>> results = index.search("help with data analysis", top_k=3) >>> for key, score in results: ... print(f"{key}: {score:.3f}")