Improve agent reliability and token efficiency
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import asyncio
|
||||
import json
|
||||
import logging
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
from typing import Any
|
||||
@@ -7,6 +8,9 @@ from typing import Any
|
||||
from .ai import AIClientError
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class OllamaError(AIClientError):
|
||||
pass
|
||||
|
||||
@@ -52,6 +56,26 @@ class OllamaClient:
|
||||
payload["format"] = "json"
|
||||
|
||||
data = self._request_json("POST", "/api/chat", payload, timeout=180)
|
||||
prompt_tokens = data.get("prompt_eval_count")
|
||||
completion_tokens = data.get("eval_count")
|
||||
logger.info(
|
||||
"AI usage provider=ollama model=%s input_tokens=%s "
|
||||
"output_tokens=%s total_tokens=%s prompt_eval_ms=%.1f "
|
||||
"generation_ms=%.1f total_ms=%.1f load_ms=%.1f",
|
||||
model,
|
||||
prompt_tokens,
|
||||
completion_tokens,
|
||||
(
|
||||
prompt_tokens + completion_tokens
|
||||
if isinstance(prompt_tokens, int)
|
||||
and isinstance(completion_tokens, int)
|
||||
else None
|
||||
),
|
||||
self._duration_ms(data.get("prompt_eval_duration")),
|
||||
self._duration_ms(data.get("eval_duration")),
|
||||
self._duration_ms(data.get("total_duration")),
|
||||
self._duration_ms(data.get("load_duration")),
|
||||
)
|
||||
content = data.get("message", {}).get("content")
|
||||
if isinstance(content, str) and content.strip():
|
||||
return content.strip()
|
||||
@@ -62,6 +86,10 @@ class OllamaClient:
|
||||
|
||||
raise OllamaError("Ollama returned an empty response.")
|
||||
|
||||
@staticmethod
|
||||
def _duration_ms(value: Any) -> float:
|
||||
return value / 1_000_000 if isinstance(value, int | float) else 0.0
|
||||
|
||||
def _request_json(
|
||||
self,
|
||||
method: str,
|
||||
|
||||
Reference in New Issue
Block a user