feat: name agents and add custom tracing spans for the research/writing phases
Agents were showing up as "Unnamed Agent" in rig's built-in gen_ai.* spans. Naming them via .name(...) fixes that, and splitting the two phases into #[tracing::instrument]-annotated functions wraps rig's per-turn chat/execute_tool spans in a parent span per phase, making the trace tree legible instead of a flat stream of chat calls.
This commit is contained in:
parent
9212914282
commit
2f24dc1b50
1 changed files with 19 additions and 3 deletions
|
|
@ -44,8 +44,20 @@ pub(crate) async fn start() -> anyhow::Result<()> {
|
|||
async fn research(topic: &str) -> anyhow::Result<String> {
|
||||
let client = ollama::Client::new(Nothing)?;
|
||||
|
||||
let findings = gather_findings(&client, topic).await?;
|
||||
let report = write_report(&client, topic, &findings).await?;
|
||||
|
||||
Ok(report)
|
||||
}
|
||||
|
||||
/// Wraps the tool-calling research loop in its own span so it's visible as a
|
||||
/// single unit in traces, distinct from the writing phase and nesting rig's
|
||||
/// own per-turn `chat`/`execute_tool` spans underneath it.
|
||||
#[tracing::instrument(skip(client), fields(gen_ai.agent.name = "researcher"))]
|
||||
async fn gather_findings(client: &ollama::Client, topic: &str) -> anyhow::Result<String> {
|
||||
let researcher = client
|
||||
.agent(RESEARCHER_MODEL)
|
||||
.name("researcher")
|
||||
.preamble(
|
||||
"You are a meticulous research assistant. Use the search_web and fetch_page tools to \
|
||||
investigate the user's topic: run several searches with varied phrasing, fetch the \
|
||||
|
|
@ -59,8 +71,6 @@ async fn research(topic: &str) -> anyhow::Result<String> {
|
|||
.tool(FetchPage)
|
||||
.build();
|
||||
|
||||
tracing::info!(topic, "starting research phase");
|
||||
|
||||
let findings = researcher
|
||||
.runner(topic)
|
||||
.max_turns(MAX_RESEARCH_TURNS)
|
||||
|
|
@ -68,10 +78,16 @@ async fn research(topic: &str) -> anyhow::Result<String> {
|
|||
.await?
|
||||
.output;
|
||||
|
||||
tracing::info!(findings = %findings, "research phase complete, starting writing phase");
|
||||
tracing::info!(findings = %findings, "research phase complete");
|
||||
|
||||
Ok(findings)
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(client, findings), fields(gen_ai.agent.name = "writer"))]
|
||||
async fn write_report(client: &ollama::Client, topic: &str, findings: &str) -> anyhow::Result<String> {
|
||||
let writer = client
|
||||
.agent(WRITER_MODEL)
|
||||
.name("writer")
|
||||
.preamble(
|
||||
"You turn raw research notes into a clear, well-organized report for the reader. \
|
||||
Structure the report with headings, cite source URLs inline next to the claims they \
|
||||
|
|
|
|||
Loading…
Reference in a new issue