Skip to content

Commit 491ba18

Browse files
committed
Instructions for lab 21
1 parent bee1696 commit 491ba18

File tree

3 files changed

+66
-1
lines changed

3 files changed

+66
-1
lines changed

ai.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@
77
from langchain_core.prompts import ChatPromptTemplate
88
from langchain_core.output_parsers import PydanticOutputParser
99
from langchain_qdrant import QdrantVectorStore
10+
from braintrust import init_logger, traced
11+
from braintrust_langchain import BraintrustCallbackHandler, set_global_handler
1012
from qdrant_client import QdrantClient
1113
from qdrant_client.http.models import Distance, VectorParams
1214

1315
from config import settings
1416

1517
client = OpenAI(api_key = settings.OPENAI_API_KEY)
18+
init_logger(project="Prodapt", api_key=settings.BRAINTRUST_API_KEY)
19+
set_global_handler(BraintrustCallbackHandler())
1620

1721
resume_eval_prompt = """
1822
You are an expert hiring screener. Given the candidate resume text and a job description, evaluate candidate's fit.
@@ -197,6 +201,7 @@ class JDRewriteOutput(BaseModel):
197201
Return only the final text.
198202
"""
199203

204+
@traced(name="Review Job Description")
200205
def review_application(job_description: str) -> ReviewedApplication:
201206
llm = ChatOpenAI(model="gpt-5.1", temperature=0, api_key=settings.OPENAI_API_KEY)
202207

labs/21-custom-traces.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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>

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ langchain==1.1.0 # Langchain framework
2222
langchain-openai==1.1.0 # Langchain Open AI interface
2323
langchain-qdrant==1.1.0 # Langchain with Qdrant vector DB
2424
openai-agents==0.6.2 # OpenAI Agent SDK
25-
braintrust[openai-agents]==0.3.12 # Braintrust observability
25+
braintrust[openai-agents]==0.3.12 # Braintrust observability
26+
braintrust-langchain==0.1.5 # Langchain integration with braintrust

0 commit comments

Comments
 (0)