MCPcopy Create free account
hub / github.com/crewAIInc/crewAI / _query

Function _query

scripts/jev_evaluation_runner.py:410–470  ·  view source on GitHub ↗
(
    record: RouteRecord | GuardrailRecord,
    client: SystemOneClient,
    name: str,
    question: dict[str, Any],
    model: str | None,
)

Source from the content-addressed store, hash-verified

408
409
410def _query(
411 record: RouteRecord | GuardrailRecord,
412 client: SystemOneClient,
413 name: str,
414 question: dict[str, Any],
415 model: str | None,
416) -> RouteRecord | GuardrailRecord:
417 start = time.perf_counter()
418 try:
419 response = client.system_one(record.state, {name: question}, model=model)
420 except Exception as e:
421 return record.model_copy(update={"error": f"{type(e).__name__}: {e}"})
422 latency_ms = (time.perf_counter() - start) * 1000
423
424 usage = getattr(response, "usage", None)
425 response_model = getattr(response, "model", None)
426 metadata = DecisionMetadata(
427 model=response_model if isinstance(response_model, str) else None,
428 latency_ms=latency_ms,
429 )
430 if usage is not None:
431 try:
432 metadata.usage = TokenUsage.model_validate(usage, from_attributes=True)
433 except ValidationError as e:
434 first = e.errors()[0]
435 location = ".".join(str(part) for part in first["loc"]) or "usage"
436 return record.model_copy(
437 update={
438 "error": f"invalid usage: {location}: {first['msg']}",
439 "metadata": metadata,
440 }
441 )
442 answers = getattr(response, "answers", None)
443 answer = answers.get(name) if isinstance(answers, Mapping) else None
444 if answer is None:
445 return record.model_copy(
446 update={"error": f"no answer returned for {name!r}", "metadata": metadata}
447 )
448 answer_type = getattr(answer, "type", None)
449 if answer_type is not None and answer_type != question["type"]:
450 return record.model_copy(
451 update={
452 "error": f"expected a {question['type']!r} answer for {name!r},"
453 f" got {answer_type!r}",
454 "metadata": metadata,
455 }
456 )
457 if isinstance(record, RouteRecord):
458 probabilities = getattr(answer, "probabilities", None)
459 if probabilities is None:
460 probabilities = {}
461 elif isinstance(probabilities, Mapping):
462 probabilities = dict(probabilities)
463 decision = {
464 "choice": getattr(answer, "choice", None),
465 "confidence": getattr(answer, "confidence", None),
466 "probabilities": probabilities,
467 }

Callers 1

collect_live_decisionsFunction · 0.85

Calls 5

DecisionMetadataClass · 0.85
joinMethod · 0.80
system_oneMethod · 0.45
errorsMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected