Skip to content

ranking

duallens_analytics.ranking

Score and rank companies by combining financial + AI initiative insights.

The module builds a composite prompt that feeds both quantitative financial data and qualitative AI-initiative text to the LLM, asking it to assign a 1–100 score per company and produce a ranked list with justifications.

RANKING_SYSTEM = 'You are a financial and strategic investment assistant.\nYou will receive two types of information about organisations:\n\n1. **Financial Data** – quantitative performance metrics such as Market Cap, P/E Ratio, Dividend Yield, Beta, and Total Revenue.\n2. **AI Initiatives** – qualitative strategic information about AI adoption, innovation, and technology-driven readiness.\n\nYour task:\n1. Analyse each organisation across both dimensions.\n2. Assign a composite score (1-100) to each organisation based on financial strength *and* AI readiness.\n3. Rank the organisations from best to worst investment potential.\n4. Provide a clear 2-3 sentence justification for each organisation.\n\nFormat your response as a numbered list:\n1. TICKER – Score: XX/100\n Justification: …\n' module-attribute

System prompt for the composite ranking evaluator.

RANKING_USER = 'Using the information below, rank these organisations for investment potential.\n\n### Financial Data\n{financial_data}\n\n### AI Initiatives\n{ai_initiatives}\n' module-attribute

User-message template with {financial_data} and {ai_initiatives} placeholders.

rank_companies(df, vectorstore, settings)

Prompt the LLM to score and rank companies for investment potential.

The prompt merges a stringified financial-metrics DataFrame with up to 80 AI-initiative document passages retrieved from the vector store.

Parameters:

Name Type Description Default
df DataFrame

Financial-metrics DataFrame (index = ticker symbols).

required
vectorstore Chroma

ChromaDB store containing AI-initiative documents.

required
settings Settings

Application settings forwarded to :func:~duallens_analytics.rag.get_llm.

required

Returns:

Type Description
str

The raw Markdown-formatted ranking text generated by the LLM.

Source code in src/duallens_analytics/ranking.py
def rank_companies(
    df: pd.DataFrame,
    vectorstore: Chroma,
    settings: Settings,
) -> str:
    """Prompt the LLM to score and rank companies for investment potential.

    The prompt merges a stringified financial-metrics DataFrame with up
    to 80 AI-initiative document passages retrieved from the vector
    store.

    Args:
        df: Financial-metrics DataFrame (index = ticker symbols).
        vectorstore: ChromaDB store containing AI-initiative documents.
        settings: Application settings forwarded to
            :func:`~duallens_analytics.rag.get_llm`.

    Returns:
        The raw Markdown-formatted ranking text generated by the LLM.
    """
    logger.info("Ranking companies: %s", list(df.index))
    ai_docs = vectorstore.get()["documents"]
    logger.debug("Total AI docs in store: %d (using first 80)", len(ai_docs))
    # Truncate to avoid token limits; take first 80 docs
    ai_text = "\n---\n".join(ai_docs[:80])

    prompt = (
        f"[INST]{RANKING_SYSTEM}\n"
        f"user: {RANKING_USER.format(financial_data=df.to_string(), ai_initiatives=ai_text)}"
        f"[/INST]"
    )
    llm = get_llm(settings)
    logger.debug("Invoking LLM for ranking")
    resp = llm.invoke(prompt)
    logger.info("Ranking response generated (%d chars)", len(resp.content))
    return resp.content