feat: solution for '8. Самописный поисковый агент на основе deep agents from scratch'

This commit is contained in:
2026-06-30 14:57:47 +03:00
parent b5a9604f58
commit df9e4f49d2
5 changed files with 374 additions and 232 deletions
+53 -23
View File
@@ -1,30 +1,60 @@
import os
import click
from .agent import SearchAgent
"""
Commandline interface for the SearchAgent.
@click.command()
@click.argument("query", nargs=-1, required=False)
def main(query):
"""
Command-line interface for the Deep Agents search agent.
Usage:
python -m src.main "search query here"
If QUERY is not provided as an argument, the user will be prompted to enter it.
"""
if not query:
query = click.prompt("Enter your query")
else:
query = " ".join(query)
The script will print the top 5 results with their relevance scores.
"""
api_key = os.getenv("BING_API_KEY")
if not api_key:
click.echo("Error: BING_API_KEY environment variable not set.")
return
import argparse
import sys
from src.agent import SearchAgent
def main() -> None:
parser = argparse.ArgumentParser(description="Deep Agent Search CLI")
parser.add_argument(
"query",
type=str,
help="Search query string",
)
parser.add_argument(
"--top",
type=int,
default=5,
help="Number of top results to display (default: 5)",
)
args = parser.parse_args()
# Example corpus in a real project this would be loaded from a file
corpus = [
"Deep learning models can capture complex patterns in data.",
"Search engines index documents to provide relevant results.",
"PyTorch is a popular deep learning framework.",
"Natural language processing involves understanding text.",
"Machine learning can be supervised or unsupervised.",
"The quick brown fox jumps over the lazy dog.",
"Artificial intelligence is transforming many industries.",
"Data science combines statistics and programming.",
"Neural networks consist of layers of interconnected nodes.",
"Optimization algorithms adjust model parameters during training.",
]
agent = SearchAgent(corpus)
results = agent.search(args.query, top_k=args.top)
if not results:
print("No results found.")
sys.exit(0)
print(f"Top {len(results)} results for query: '{args.query}'\n")
for i, res in enumerate(results, start=1):
print(f"{i}. [Doc {res.doc_id}] Score: {res.score:.4f}")
print(f" {res.text}\n")
agent = SearchAgent(api_key=api_key)
click.echo("Searching...")
result = agent.process_query(query)
click.echo("\nResult:\n")
click.echo(result)
if __name__ == "__main__":
main()