Observability
Debugging

Debugging chains and tools

In this guide, we will show you how to use Log10 to debug LLM chains and tools developed with the magentic framework. The program we will use is a simple tool that fetches stock data using the Yahoo Finance API and compares the data for different two stocks tickers.

Setup

First, install the required packages using pip. We will use the log10-io, magentic, openai, and yfinance packages in this example.

$ pip install log10-io magentic openai yfinance

NOTE Setup OPENAI_API_TOKEN and LOG10_URL, LOG10_TOKEN and LOG10_ORG_ID environment variables according to your log10 organization, and follow the api key instructions to get the values and ids.

Save script

Then, save the following to a file named get_stock.py:

from magentic import prompt_chain
from log10.load import log10
import openai
 
log10(openai)
 
import yfinance as yf
 
def get_recent_stock_data(ticker) -> str:
    stock = yf.Ticker(ticker)
    data = stock.history(period="5d")
    return data.to_string()
 
@prompt_chain(
    "Get the recent stock data for {ticker1} and compare it to {ticker2}.",
    functions=[get_recent_stock_data],
)
def get_stock(ticker1: str, ticker2: str) -> str: ...
 
 
print(get_stock("AAPL", "MSFT"))

Run script

and run it with:

$ python get_stock.py

After this, you should see an analysis of the two stocks be printed to the console, and can look something like this:

Here's recent stock data for both AAPL (Apple Inc.) and MSFT (Microsoft Corporation):

### Apple Inc. (AAPL):
- **April 22, 2024:** Opened at $165.52, High $167.26, Low $164.77, Closed at $165.84, with 48,116,400 shares traded.
- **April 23, 2024:** Opened at $165.35, High $167.05, Low $164.92, Closed at $166.90, with 49,537,800 shares traded.
- **April 24, 2024:** Opened at $166.54, High $169.30, Low $166.21, Closed at $169.02, with 48,251,800 shares traded.
- **April 25, 2024:** Opened at $169.53, High $170.61, Low $168.15, Closed at $169.89, with 50,558,300 shares traded.
- **April 26, 2024:** Opened at $169.88, High $171.34, Low $169.18, Closed at $169.30, with 44,525,100 shares traded.

### Microsoft Corporation (MSFT):
- **April 22, 2024:** Opened at $400.08, High $402.85, Low $395.75, Closed at $400.96, with 20,286,900 shares traded.
- **April 23, 2024:** Opened at $404.24, High $408.20, Low $403.06, Closed at $407.57, with 15,734,500 shares traded.
- **April 24, 2024:** Opened at $409.56, High $412.47, Low $406.78, Closed at $409.06, with 15,065,300 shares traded.
- **April 25, 2024:** Opened at $394.03, High $399.89, Low $388.03, Closed at $399.04, with 40,586,500 shares traded.
- **April 26, 2024:** Opened at $412.17, High $413.00, Low $405.76, Closed at $406.32, with 29,553,600 shares traded.

### Comparison:
- **Price Movement:** Both Apple and Microsoft display relative stability with minor daily fluctuations. Apple's prices remained relatively consistent through the covered dates, whereas Microsoft showed larger swings on April 25th, likely related to specific market events or company announcements.
- **Volume:** Apple consistently had a higher trading volume as compared to Microsoft, suggesting potentially greater liquidity or investor interest in Apple stock over the observed period.
- **Closing Prices:** On the most recent date, Apple closed at $169.30 while Microsoft closed at $406.32, reflecting the different scales of their stock prices but reasonably stable closings through the week for both companies.

This data provides a snapshot of the trading behavior and stock performance of both tech giants over the specified period.

Inspect using log10 dashboard

Using the log10 dashboard, you should be able to see 3 logs in the chain.

Chain log

By clicking one of the logs, and you can use the session tab to navigate between the logs in that session.

Chain log

And by clicking the tools tab, you can see which tools were available to the LLM at the time.

Tools