Financial Chat

repository·main·Indexed 42 days ago

https://github.com/wshobson/financial-chat

An agentic financial analysis application version 0.1.0 that leverages LangChain, LangGraph, and OpenBB to provide stock insights, technical analysis, and sentiment tracking. It features a Streamlit UI and a FastAPI server with a /chat endpoint exposed via LangServe.

Tokens
1.4K
Snippets
9
Records
11
Agent score
4%

What's inside Financial Chat

  1. Understand the project structure

    main

    The project is organized as follows:

    • app/: The core application directory.
      • chains/: Contains LangChain agent logic and prompt templates.
      • features/: Contains feature-specific implementations like technical analysis and charting.
      • tools/: Contains custom tools used by agents for data retrieval and analysis.
      • ui.py: The entry point for the Streamlit UI.
      • server.py: The entry point for the FastAPI server.
    • Dockerfile: Instructions for building the Docker image.
    • pyproject.toml: Dependency management via Poetry.
  2. Run the FastAPI server

    main

    To start the backend API server, use uvicorn to serve the app instance located in app/server.py.

    Once running, you can access:

    • Swagger Docs: http://0.0.0.0:8080/docs to explore and test the API.
    • Playground: http://0.0.0.0:8080/chat/playground (provided by LangServe) to test chat interactions.
    uvicorn app.server:app --host 0.0.0.0 --port 8080
  3. Deploy financial-chat using Docker

    main

    You can containerize the application using the provided Dockerfile.

    Build the image:

    docker build -t financial-chat .

    Run the container: Pass your .env file to the container and map port 8080 to ensure the API is accessible.

    docker run -p 8080:8080 --env-file .env financial-chat
    docker build -t financial-chat .
    docker run -p 8080:8080 --env-file .env financial-chat
  4. Configure environment variables for financial-chat

    main

    The application requires several API keys to function. You can set these in your shell or create an .env file at the project root. The following environment variables are required:

    • OPENAI_API_KEY: OpenAI API key for LLM capabilities.
    • OPENBB_TOKEN: OpenBB platform token for financial data.
    • TIINGO_API_KEY: Tiingo API key.
    • IMGUR_CLIENT_ID: Imgur client ID (used for image handling).
    • IMGUR_CLIENT_SECRET: Imgur client secret.
    • FMP_API_KEY: Financial Modeling Prep API key.
    • INTRINIO_API_KEY: Intrinio API key.
    export OPENAI_API_KEY=<your-api-key>
    export OPENBB_TOKEN=<your-openbb-token>
    export TIINGO_API_KEY=<your-tiingo-api-key>
    export IMGUR_CLIENT_ID=<your-imgur-client-id>
    export IMGUR_CLIENT_SECRET=<your-imgur-client-secret>
    export FMP_API_KEY=<fmp-api-key>
    export INTRINIO_API_KEY=<intrinio-api-key>
  5. Configure environment variables for OpenBB and Financial APIs

    main

    The server requires several environment variables to authenticate with financial data providers via OpenBB. Ensure the following are set in your environment or .env file:

    • OPENBB_TOKEN: Used for OpenBB account login.
    • TIINGO_API_KEY: For Tiingo data access.
    • FMP_API_KEY: For Financial Modeling Prep data access.
    • INTRINIO_API_KEY: For Intrinio data access.
    • FRED_API_KEY: For Federal Reserve Economic Data access.
  6. Use the Financial Chat API via LangServe

    main

    The Financial Chat service exposes an agentic workflow through a FastAPI server using LangServe. The primary interaction point is the /chat endpoint, which accepts a conversation history and returns an agent response.

    To interact with the API, you must provide an AgentInput object containing a list of HumanMessage and AIMessage objects representing the conversation state. The server returns an AgentOutput object containing the output field.

    # Example conceptual interaction with the /chat endpoint
    from langchain_core.messages import HumanMessage, AIMessage
    
    # The input shape required by the /chat route
    input_data = {
        "messages": [
            HumanMessage(content="What is the current price of AAPL?"),
            AIMessage(content="The current price of AAPL is $180.")
        ]
    }
    
    # The expected response shape
    # {"output": ...}
  7. AgentInput schema

    main

    The AgentInput class defines the required input structure for the chat agent. It expects a list of LangChain messages to maintain conversation context.

    class AgentInput(BaseModel):
        messages: List[Union[HumanMessage, AIMessage]] = Field(
            ...,
            description="The chat messages representing the current conversation.",
            extra={"widget": {"type": "chat", "input": "messages"}},
        )