-
Notifications
You must be signed in to change notification settings - Fork 0
Add semantic agent search via agent_vec and find_agents.py #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1aecc4b
Add diverge generation strategy: parallel cognitive stances with judg…
claude b4a1e12
Revert "Add diverge generation strategy: parallel cognitive stances w…
AaronGoldsmith 415f24e
Add semantic agent search via agent_vec and find_agents.py
AaronGoldsmith 918abde
Fix PR review comments: add diverge command, validate inputs, ensure …
AaronGoldsmith File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| """Find agents semantically similar to a query. | ||
|
|
||
| Usage: | ||
| python find_agents.py "build a REST API with authentication" | ||
| python find_agents.py "debug memory leaks" --top 5 | ||
|
|
||
| Returns JSON array of matching agents ranked by relevance. | ||
| """ | ||
|
|
||
| import json | ||
| import sys | ||
|
|
||
| sys.path.insert(0, "src") | ||
|
|
||
| from mobius.config import get_config | ||
| from mobius.db import init_db, vec_to_blob | ||
| from mobius.embedder import embed | ||
| from mobius.registry import Registry | ||
|
|
||
|
|
||
| def main(): | ||
| if len(sys.argv) < 2: | ||
| print("Usage: python find_agents.py '<query>' [--top N]") | ||
| sys.exit(1) | ||
|
|
||
| query = sys.argv[1] | ||
| top_k = 10 | ||
| if "--top" in sys.argv: | ||
| idx = sys.argv.index("--top") | ||
| if idx + 1 < len(sys.argv): | ||
| top_k = int(sys.argv[idx + 1]) | ||
|
|
||
| config = get_config() | ||
| conn, vec_available = init_db(config) | ||
|
|
||
| if not vec_available: | ||
| print(json.dumps({"error": "sqlite-vec not available"})) | ||
| sys.exit(1) | ||
|
|
||
| # Check if agent_vec has any rows | ||
| count = conn.execute("SELECT COUNT(*) as cnt FROM agent_vec").fetchone()["cnt"] | ||
| if count == 0: | ||
| print(json.dumps({"error": "No agent embeddings found. Run backfill first."})) | ||
| sys.exit(1) | ||
|
|
||
| # Embed query and search | ||
| query_vec = embed(query, config) | ||
| query_blob = vec_to_blob(query_vec) | ||
|
|
||
| rows = conn.execute( | ||
| """ | ||
| SELECT av.id, av.distance | ||
| FROM agent_vec av | ||
| WHERE av.description_embedding MATCH ? | ||
| AND k = ? | ||
| ORDER BY av.distance | ||
| """, | ||
| (query_blob, top_k), | ||
| ).fetchall() | ||
|
|
||
| if not rows: | ||
| print(json.dumps([])) | ||
| conn.close() | ||
| return | ||
|
|
||
| # Fetch full agent details for matches | ||
| registry = Registry(conn, config, vec_available) | ||
| results = [] | ||
| for row in rows: | ||
| agent = registry.get_agent(row["id"]) | ||
| if agent is None: | ||
| continue | ||
| distance = row["distance"] | ||
| similarity = 1.0 - (distance**2 / 2.0) | ||
| results.append({ | ||
| "slug": agent.slug, | ||
| "name": agent.name, | ||
| "description": agent.description, | ||
| "provider": agent.provider, | ||
| "model": agent.model, | ||
| "specializations": agent.specializations, | ||
| "elo": round(agent.elo_rating), | ||
| "win_rate": round(agent.win_rate, 3), | ||
| "matches": agent.total_matches, | ||
| "champion": agent.is_champion, | ||
| "similarity": round(similarity, 4), | ||
| }) | ||
|
|
||
| print(json.dumps(results, indent=2)) | ||
| conn.close() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.