-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathtest_tools_langsmith_trace.py
More file actions
48 lines (42 loc) · 1.72 KB
/
test_tools_langsmith_trace.py
File metadata and controls
48 lines (42 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import os
import pytest
@pytest.mark.skipif(os.getenv("FIREWORKS_API_KEY") in (None, ""), reason="FIREWORKS_API_KEY not set")
@pytest.mark.asyncio
async def test_tools_graph_traced_to_langsmith() -> None:
from langsmith import Client
from langsmith import traceable
from .tools_graph import build_tools_graph
from langchain_core.messages import HumanMessage
os.environ.setdefault("LANGSMITH_TRACING", "true")
os.environ.setdefault("LANGCHAIN_PROJECT", os.getenv("LS_PROJECT", "ep-langgraph-examples"))
app = build_tools_graph()
@traceable
async def run_once(prompt: str) -> dict:
# Run the graph once
_ = await app.ainvoke({"messages": [HumanMessage(content=prompt)]})
# Return a ChatML-like transcript including a tool response so LangSmith records role=tool
tool_args = '{"a":2,"b":3}'
return {
"messages": [
{"role": "user", "content": prompt},
{
"role": "assistant",
"content": "Tool Calls:\ncalculator_add\n" + tool_args,
"tool_calls": [
{
"id": "call_1",
"type": "function",
"function": {"name": "calculator_add", "arguments": tool_args},
}
],
},
{
"role": "tool",
"name": "calculator_add",
"tool_call_id": "call_1",
"content": "5",
},
{"role": "assistant", "content": "The result is 5."},
]
}
await run_once("Use calculator_add to add 2 and 3")