Skip to content

Commit decdf6a

Browse files
committed
Add traces
1 parent 7d6e8c3 commit decdf6a

3 files changed

Lines changed: 40 additions & 12 deletions

File tree

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,19 @@
1919
4. Publish the hr_agent prompts to langfuse prompt registry
2020

2121
```shell
22-
uv run main.py prompts --publish hr_agent
22+
uv run langfuse_experiment prompts --publish hr_agent
2323
```
2424

2525
6. Before you can runs evals, you need to publish the eval dataset to langfuse dataset registry
2626

2727
```shell
28-
uv run main.py datasets --publish ci
28+
uv run langfuse_experiment datasets --publish ci
2929
```
3030

3131
5. Chat with the agent
3232

3333
```
34-
uv run main.py chat
34+
uv run langfuse_experiment chat
3535
```
3636

3737
![Screenshot](./docs/Screenshot.png)
@@ -42,8 +42,8 @@
4242
- [x] Prompt gemma, falcon from langchain
4343
- [x] Setup chat memory
4444
- [x] Get the system prompt from SaaS langfuse
45-
- [ ] Setup offline Evals to run on the CI/CD pipeline
46-
- [ ] Setup traces
45+
- [x] Setup offline Evals to run on the CI/CD pipeline
46+
- [x] Setup traces
4747
- [ ] Generate a generic hr handbook
4848
- [ ] Use LlamaIndex to do rag over the handbook.
4949
- [ ] Add testing for rag (ragas?)

src/langfuse_experiment/graph.py

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,50 @@
1+
from langfuse import Langfuse, propagate_attributes
12
from dataclasses import dataclass
23
from langfuse import Langfuse
34
from langchain.chat_models import BaseChatModel
45
from langgraph.runtime import Runtime
56
from langgraph.graph import END, START, MessagesState, StateGraph
67
from langgraph.checkpoint.memory import InMemorySaver
78
from langchain_ollama import ChatOllama
9+
from langchain_core.runnables import RunnableConfig
810

911

1012
@dataclass
1113
class ContextSchema:
12-
prompt_registry: Langfuse
14+
langfuse: Langfuse
1315
llm: BaseChatModel
1416

1517

16-
def call_model(state: MessagesState, runtime: Runtime[ContextSchema]):
17-
prompt_client = runtime.context.prompt_registry.get_prompt("hr_agent")
18-
system_prompt = prompt_client.compile()
19-
ai_msg = runtime.context.llm.invoke([system_prompt] + state["messages"])
20-
return {"messages": [ai_msg]}
18+
def call_model(
19+
state: MessagesState, runtime: Runtime[ContextSchema], config: RunnableConfig
20+
):
21+
langfuse = runtime.context.langfuse
22+
llm = runtime.context.llm
23+
thread_id = config["configurable"]["thread_id"]
24+
25+
with propagate_attributes(session_id=thread_id):
26+
27+
with langfuse.start_as_current_observation(
28+
as_type="span", name="call-model"
29+
) as root_span:
30+
31+
prompt_client = langfuse.get_prompt("hr_agent")
32+
system_prompt = prompt_client.compile()
33+
messages = [system_prompt] + state["messages"]
34+
35+
with root_span.start_as_current_observation(
36+
as_type="generation",
37+
name="generate-response",
38+
model=runtime.context.llm.model,
39+
input=messages,
40+
prompt=prompt_client,
41+
model_parameters={"temperature": llm.temperature},
42+
) as gen:
43+
ai_msg = llm.invoke(messages)
44+
45+
gen.update(output=ai_msg, usage_details=ai_msg.usage_metadata)
46+
47+
return {"messages": [ai_msg]}
2148

2249

2350
def build_model():

src/langfuse_experiment/main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import sys
22
import argparse
3+
from uuid import uuid4
34
from dotenv import load_dotenv
45
from langfuse import get_client
56
from langchain_core.runnables import RunnableConfig
@@ -13,7 +14,7 @@ def chat(langfuse):
1314

1415
graph = build_graph()
1516

16-
config: RunnableConfig = {"configurable": {"thread_id": "1"}}
17+
config: RunnableConfig = {"configurable": {"thread_id": str(uuid4())}}
1718

1819
while True:
1920
user_input = input("Human: ")

0 commit comments

Comments
 (0)