|
| 1 | +# Lab 21: Custom Tracing |
| 2 | + |
| 3 | +In this lab, we will see how we can create traces for specific workflows. In this example, we will create a trace for the "Review Job Description" workflow. |
| 4 | + |
| 5 | +## High Level Overview |
| 6 | + |
| 7 | +1. First we have to integrate langchain with braintrust. Open `ai.py` and follow the steps in the documentation - https://www.braintrust.dev/docs/integrations/sdk-integrations/langchain |
| 8 | +1. Now any time we invoke a chain, it will create a trace on braintrust |
| 9 | +1. Start the app (`fastapi dev main.py`) and go to the new job page. Add a job title and description and click `Review`. This will cause three chains to be invoked. You can use the data below to test |
| 10 | + |
| 11 | +``` |
| 12 | +We’re seeking a Forward Deployed Engineer. We want someone with 3+ years of software engineering experience with production systems. They should be rockstar programmers and problem solvers. They should have experience in a customer-facing technical role with a background in systems integration or professional services |
| 13 | +``` |
| 14 | + |
| 15 | +1. Go to Braintrust UI and you should see all the three traces there |
| 16 | + |
| 17 | +By default, every chain invokation is a separate trace. It would be better if the entire review job description flow would be a single trace. Let us configure that |
| 18 | + |
| 19 | +1. Import the `traced` decorator from the `braintrust` package |
| 20 | +1. Apply `@traced(name='Review Job Description')` decorator to the `review_application` function |
| 21 | +1. Now go back to the new job page and review the job description again. |
| 22 | +1. Go to the braintrust UI and this time you should see a single trace which contains all the three chain invokation as spans within it |
| 23 | + |
| 24 | +## Hints |
| 25 | + |
| 26 | +### How do I configure langchain with braintrust? |
| 27 | + |
| 28 | +<details> |
| 29 | +<summary>Answer</summary> |
| 30 | + |
| 31 | +Update `requirements.txt` then `pip install` |
| 32 | + |
| 33 | +``` |
| 34 | +braintrust-langchain==0.1.5 |
| 35 | +``` |
| 36 | + |
| 37 | +Then open `ai.py` and add this code |
| 38 | + |
| 39 | +```python |
| 40 | +from braintrust import init_logger, traced |
| 41 | +from braintrust_langchain import BraintrustCallbackHandler, set_global_handler |
| 42 | + |
| 43 | +init_logger(project="Prodapt", api_key=settings.BRAINTRUST_API_KEY) |
| 44 | +set_global_handler(BraintrustCallbackHandler()) |
| 45 | +``` |
| 46 | +</details> |
| 47 | + |
| 48 | +### How do I create a customised trace? |
| 49 | + |
| 50 | +<details> |
| 51 | +<summary>Answer</summary> |
| 52 | + |
| 53 | +Put the `@traced` decorator on the function |
| 54 | + |
| 55 | +```python |
| 56 | +@traced(name="Review Job Description") |
| 57 | +def review_application(job_description: str) -> ReviewedApplication: |
| 58 | +``` |
| 59 | +</details> |
0 commit comments