Run one direct LLM call and return an AgentResult.
(self, question: str)
| 78 | super().__init__(llm, server_paths) |
| 79 | |
| 80 | async def run(self, question: str) -> AgentResult: |
| 81 | """Run one direct LLM call and return an AgentResult.""" |
| 82 | with agent_run_span( |
| 83 | "direct-llm-agent", |
| 84 | model=self._llm.model_id, |
| 85 | question=question, |
| 86 | ) as span: |
| 87 | started = datetime.now(timezone.utc).isoformat() |
| 88 | run_started = time.perf_counter() |
| 89 | |
| 90 | prompt = f"""{_DIRECT_LLM_SYSTEM_PROMPT} |
| 91 | |
| 92 | User task: |
| 93 | {question} |
| 94 | |
| 95 | Final answer:""" |
| 96 | |
| 97 | call_started = time.perf_counter() |
| 98 | result = self._llm.generate_with_usage(prompt, temperature=0.0) |
| 99 | duration_ms = (time.perf_counter() - call_started) * 1000 |
| 100 | total_duration_ms = (time.perf_counter() - run_started) * 1000 |
| 101 | |
| 102 | answer = _clean_final_answer(result.text) |
| 103 | |
| 104 | trajectory = Trajectory( |
| 105 | started_at=started, |
| 106 | turns=[ |
| 107 | TurnRecord( |
| 108 | index=0, |
| 109 | text=answer, |
| 110 | tool_calls=[], |
| 111 | input_tokens=result.input_tokens, |
| 112 | output_tokens=result.output_tokens, |
| 113 | duration_ms=duration_ms, |
| 114 | ) |
| 115 | ], |
| 116 | ) |
| 117 | |
| 118 | span.set_attribute("agent.answer.length", len(answer)) |
| 119 | span.set_attribute("agent.duration_ms", total_duration_ms) |
| 120 | span.set_attribute("agent.llm_time_ms", duration_ms) |
| 121 | span.set_attribute("gen_ai.usage.input_tokens", result.input_tokens) |
| 122 | span.set_attribute("gen_ai.usage.output_tokens", result.output_tokens) |
| 123 | span.set_attribute("agent.tool_calls", 0) |
| 124 | |
| 125 | persist_trajectory( |
| 126 | runner_name="direct-llm-agent", |
| 127 | model=self._llm.model_id, |
| 128 | question=question, |
| 129 | answer=answer, |
| 130 | trajectory=trajectory, |
| 131 | ) |
| 132 | |
| 133 | return AgentResult( |
| 134 | question=question, |
| 135 | answer=answer, |
| 136 | trajectory=trajectory, |
| 137 | ) |