diff --git a/main.py b/main.py new file mode 100644 index 0000000..24e3a82 --- /dev/null +++ b/main.py @@ -0,0 +1,64 @@ +#!/usr/bin/env python3 +"""Deep agent that searches the web using Perplexity and writes results to a file. + +Usage: + python main.py "search query" + +The agent will: +1. Query Perplexity via LangChain's Perplexity wrapper. +2. Store the answer in a virtual file `output.txt`. +3. Write the virtual file to the real filesystem. +""" + +import argparse +import os +from pathlib import Path + +from langchain_community.utilities import Perplexity +from langchain_core.prompts import ChatPromptTemplate +from langchain_core.output_parsers import StrOutputParser + +# Configure Perplexity API key via environment variable +# Users should set PERPLEXITY_API_KEY in their environment + +PERPLEXITY_API_KEY = os.getenv("PERPLEXITY_API_KEY") +if not PERPLEXITY_API_KEY: + raise RuntimeError("PERPLEXITY_API_KEY environment variable not set") + +# Create a simple chain: prompt -> Perplexity -> output parser +prompt = ChatPromptTemplate.from_messages([ + ("system", "You are a helpful assistant that answers questions based on web search."), + ("human", "{question}") +]) + +llm = Perplexity(api_key=PERPLEXITY_API_KEY, temperature=0.7) +chain = prompt | llm | StrOutputParser() + + +def search_and_write(query: str, output_path: Path) -> None: + """Search the web for *query* and write the answer to *output_path*. + + The function creates a virtual file in memory and then writes it to disk. + """ + print(f"Searching for: {query}") + answer = chain.invoke({"question": query}) + # Virtual file content + virtual_file_content = f"Query: {query}\n\nAnswer:\n{answer}\n" + # Write to real filesystem + output_path.write_text(virtual_file_content, encoding="utf-8") + print(f"Result written to {output_path.resolve()}") + + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description="Deep agent search tool") + parser.add_argument("query", type=str, help="Search query") + parser.add_argument( + "--output", + type=str, + default="output.txt", + help="Path to write the result file (default: output.txt)", + ) + args = parser.parse_args() + + output_path = Path(args.output) + search_and_write(args.query, output_path)