Files
zeekay 1b367e2b62 chore: rebase the tree onto upstream master
The tree was a debranded snapshot of upstream taken around 2026-02-20, and
upstream has moved about five months since. This replaces it with upstream
master, mechanically re-derived: bin/debrand does the rename, and the
pipeline and squashed migrations are restored from the shipped tree.

Not a merge. The previous import replaced the tree wholesale in a single
commit, so there is no shared content history to merge against. Reproducing
the rename on the new tree is what actually converges.

What survives: .hanzo/workflows, the Dockerfiles and bin/docker* entrypoints,
compose files, LLM.md, and insights/migrations. The squashed initial
migration is load-bearing — the live database records it by name, so
upstream's ~918 individual migrations cannot come back.

Nine products are dropped as unreferenced. Twenty-one more that we do not run
are kept anyway because the core app still imports them; unpicking those is a
refactor and does not belong in a rebase.

This base does not build yet, and the reasons are not in the rename:
upstream now wants Python 3.13.13 against our 3.12.12 and Django 5.2 against
our 4.2, and the dependency manifests still name upstream packages that we
republish under our own names. Those are decisions, not renames.
2026-08-01 15:44:40 -07:00

86 lines
2.5 KiB
Python

"""Gemini client utilities for surveys."""
import uuid
from typing import TypeVar
from django.conf import settings
import structlog
import hanzo_insights
from google.genai.types import GenerateContentConfig
from hanzo_insights.ai.gemini import genai
from pydantic import BaseModel
from rest_framework import exceptions
from insights.event_usage import groups
logger = structlog.get_logger(__name__)
T = TypeVar("T", bound=BaseModel)
def create_gemini_client():
if settings.DEBUG and hanzo_insights.disabled:
hanzo_insights.disabled = False
if not hanzo_insights.host:
hanzo_insights.host = settings.SITE_URL
insights_client = hanzo_insights.default_client
if not insights_client:
logger.warning("Insights default_client not available, AI observability will not be tracked")
return genai.Client(
api_key=settings.GEMINI_API_KEY,
insights_client=insights_client,
)
def generate_structured_output(
*,
model: str,
system_prompt: str,
user_prompt: str,
response_schema: type[T],
insights_properties: dict | None = None,
team_id: int | None = None,
distinct_id: str | None = None,
billable: bool = False,
) -> tuple[T, str]:
client = create_gemini_client()
config = GenerateContentConfig(
system_instruction=system_prompt,
response_mime_type="application/json",
response_json_schema=response_schema.model_json_schema(),
)
trace_id = str(uuid.uuid4())
properties = {**(insights_properties or {})}
insights_groups = {"project": str(team_id)} if team_id else {}
if billable and team_id is not None:
properties["team_id"] = team_id
properties["$ai_billable"] = True
insights_groups.update(groups())
try:
response = client.models.generate_content(
model=model,
contents=user_prompt,
config=config,
insights_distinct_id=distinct_id or "",
insights_trace_id=trace_id,
insights_properties=properties,
insights_groups=insights_groups,
)
if not response.text:
raise exceptions.ValidationError("Gemini returned empty response")
return response_schema.model_validate_json(response.text), trace_id
except exceptions.ValidationError:
raise
except Exception:
logger.exception("Gemini API call failed", model=model, properties=properties)
raise exceptions.APIException("Failed to generate response")