Stock X Stock is an AI investment agent powered by Perplexity's Sonar Pro and Sonar (yes both :) LLMs that compares any pair of 10,000+ stocks based on a top 10 investment factor and gives an investment recommendation. The comparison is based on Sonar Pro's real-time Internet search capability to get and interpret the latest financial data using its advanced LLM engine. Also, a summary of the whole analysis and an optional report are generated using Sonar. The user can also enter their own custom investment factors to do the Stock X Stock comparison, such as comparing AI strategy or latest analyst sentiment about Trump tariff policy impact.
All prompts used by the app are stored in 15 prompt files. This allows prompt tweaks without touching the code. I crafted the prompts to follow best practices provided by Perplexity:
- https://docs.perplexity.ai/guides/prompt-guide#best-practices-for-prompting-web-search-models and
- https://docs.perplexity.ai/guides/prompt-guide#web-search-model-pitfalls-to-avoid
For example check out the prompt for earnings performance analysis with its specific, contextual questions and focus on search: https://github.com/williamcheung/stockxstock/blob/master/prompts/prompt1.txt
Also in the main prompt, the model is instructed to include the sources it used as links in its response: https://github.com/williamcheung/stockxstock/blob/master/prompts/main_prompt.txt#L12 This enables the user to deep dive on the whys behind Sonar Pro's answers and confirm they're based on the most up-to-date data available.
For prompts not requiring the latest data, Sonar is used instead of Sonar Pro. These are the prompts for recommending one of two stocks based on the analysis done on each by Sonar Pro, summarizing the recommendation, and generating a report:
- prompts/recommend_prompt.txt
- prompts/summarize_prompt.txt
- prompts/gen_report_prompt.txt
To optimize performance, API requests to the model to analyze each of the 2 stocks being compared are sent in parallel, not sequentially: https://github.com/williamcheung/stockxstock/blob/master/gradio_ui.py#L88
For each step of the recommendation workflow, (a) analysis, (b) comparison, (c) summarization, as soon as the results for one step is returned by the model, those results are shown in the UI without waiting for all 3 steps to be complete:
- https://github.com/williamcheung/stockxstock/blob/master/gradio_ui.py#L98
- https://github.com/williamcheung/stockxstock/blob/master/gradio_ui.py#L111
- https://github.com/williamcheung/stockxstock/blob/master/gradio_ui.py#L123
And for handling API rate limit errors, the Tenacity library is used to retry seamlessly with exponential backoff: https://github.com/williamcheung/stockxstock/blob/master/llm.py#L54
Finally to give the user notification their investment recommendation is ready, once the workflow is complete, the summary is read aloud to them :)
Built With
- gradio
- langchain
- perplexity
- python-3.12
- sonar
- sonar-pro
- tenacity

Log in or sign up for Devpost to join the conversation.