LLM Integration
Relevant source files
The following files were used as context for generating this wiki page:The Greenline application integrates Large Language Models (LLMs) to provide users with AI-powered financial insights. This is achieved through a direct client-side integration with the OpenAI API. The primary goal is to enhance the user's understanding of stock tickers by offering conversational AI capabilities and automated analysis of significant market events. These features align with the project's aim to provide "market insights powered by machine learning".
The core functionalities are encapsulated in two main React components: TickerChat for interactive conversations and StockSummary for generating narrative summaries of historical price movements. Both features are accessible on the individual stock quote page.
Sources: client/src/components/Hero/index.tsx:180-185, client/src/app/quote/[ticker]/page.tsx:13,14,121,122
Core Dependency
The LLM integration relies on the official openai Node.js library. The client-side application includes this as a direct dependency, enabling it to make API calls to OpenAI services from the browser. The configuration dangerouslyAllowBrowser: true is used to facilitate this client-side execution.
| Dependency | Version | Purpose |
|---|---|---|
openai | ^4.72.0 | Official library for OpenAI API access. |
Sources: client/package.json:18, client/src/components/TickerChat/index.tsx:64, client/src/components/StockSummary/index.tsx:82
Feature Implementations
TickerChat: Conversational AI
The TickerChat component provides a chat interface allowing users to have a conversation with an AI persona representing a specific stock ticker. This feature is designed to give users a novel way to request and receive information about a stock.
Sources: client/src/components/TickerChat/index.tsx
Architecture and Flow
The component manages a conversation history in its state. When a user sends a message, it is appended to the history, and a request is streamed to the OpenAI API. The AI's response is then streamed back to the UI in real-time and added to the conversation history upon completion.
This diagram illustrates the sequence of events when a user interacts with the TickerChat component.
LLM Configuration
The chat functionality is configured with a specific model and a system prompt to define the AI's persona.
| Parameter | Value | Description - |
|---|---|---|
model | gpt-4o-mini | The specific OpenAI model used for generating chat responses. - |
system prompt | You are the stock ticker {ticker}. You will respond as if you are the ticker itself, providing insights, information, and relevant advice... | Defines the AI's persona and instructions. It is dynamically populated with the current stock ticker. - |
stream | true | Enables receiving the response as a stream of tokens for a real-time typing effect. - |
Sources: client/src/components/TickerChat/index.tsx:24-29, 67-72
StockSummary: Automated Event Analysis
The StockSummary component automatically identifies significant historical price changes for a given ticker and uses an LLM to generate a concise, human-readable explanation for each event.
Sources: client/src/components/StockSummary/index.tsx
Architecture and Flow
The process begins by fetching 6 months of daily stock data. The component then iterates through the data to find days with a price change greater than 2%. For each of these "significant changes," a request is made to the OpenAI API to generate a descriptive reason for the price movement.
This flowchart shows the process of generating the critical summary timeline.
LLM Configuration
This feature uses a different model and prompt structure, tailored for analysis rather than conversation.
| Parameter | Value | Description - |
|---|---|---|
model | gpt-3.5-turbo | The OpenAI model used for generating analytical descriptions. - |
prompt | Analyze the following stock event: - Date: {date} - Price: {price} - Change: {change}%... | A structured prompt providing the model with the context of a specific price change to analyze. - |
max_tokens | 100 | Limits the response length to ensure the description is concise. - |
Sources: client/src/components/StockSummary/index.tsx:84-93
Related Machine Learning Features
Stock Price Forecasting
While not a direct LLM integration, the StockChart component includes a "Forecast" feature that demonstrates the project's use of predictive models. This feature is available for '1D' and '5D' time windows.
When activated, it sends the historical chart data to a local backend service at http://localhost:8000/forecast/. This service, whose implementation is not present in the client-side code, returns a set of forecasted prices and confidence intervals, which are then plotted on the chart. This aligns with the "Price Predictions" capability mentioned on the application's hero page.
Sources: client/src/components/StockChart/index.tsx:117-140, client/src/components/Hero/index.tsx:182
Conclusion
The LLM integration in Greenline is a client-side implementation that leverages the openai library to provide two distinct, value-added features. The TickerChat offers an engaging, conversational method for users to query stock information, while the StockSummary provides automated, AI-generated narratives for significant price movements. Together, these features significantly enhance the analytical capabilities of the platform by translating raw financial data into accessible, human-readable insights.