|
|
@@ -1,11 +1,14 @@
|
|
|
from __future__ import annotations
|
|
|
|
|
|
+import copy
|
|
|
import os
|
|
|
+from pathlib import Path
|
|
|
from typing import Any, Mapping
|
|
|
|
|
|
import httpx
|
|
|
|
|
|
from content_agent.errors import ContentAgentError, ErrorCode
|
|
|
+from content_agent.integrations.query_prompt_config import DEFAULT_PROFILE, load_profile
|
|
|
from content_agent.interfaces import QueryVariantClient, QueryVariantResult
|
|
|
|
|
|
DEFAULT_OPENROUTER_BASE_URL = "https://openrouter.ai/api/v1"
|
|
|
@@ -44,12 +47,14 @@ class OpenRouterQueryVariantClient:
|
|
|
base_url: str = DEFAULT_OPENROUTER_BASE_URL,
|
|
|
timeout_seconds: float = DEFAULT_QUERY_TIMEOUT_SECONDS,
|
|
|
prompt_version: str = DEFAULT_QUERY_PROMPT_VERSION,
|
|
|
+ profile: dict[str, Any] | None = None,
|
|
|
) -> None:
|
|
|
self.api_key = api_key
|
|
|
self.model = model
|
|
|
self.base_url = base_url.rstrip("/")
|
|
|
self.timeout_seconds = timeout_seconds
|
|
|
- self.prompt_version = prompt_version
|
|
|
+ self.profile = copy.deepcopy(profile or DEFAULT_PROFILE)
|
|
|
+ self.prompt_version = str(self.profile.get("prompt_version") or prompt_version)
|
|
|
|
|
|
def generate_variant(
|
|
|
self,
|
|
|
@@ -66,9 +71,9 @@ class OpenRouterQueryVariantClient:
|
|
|
},
|
|
|
json={
|
|
|
"model": self.model,
|
|
|
- "messages": _messages(seed_term, evidence_context),
|
|
|
- "temperature": 0.4,
|
|
|
- "max_tokens": 64,
|
|
|
+ "messages": _render_messages(self.profile, seed_term, evidence_context),
|
|
|
+ "temperature": self.profile["temperature"],
|
|
|
+ "max_tokens": self.profile["max_tokens"],
|
|
|
},
|
|
|
timeout=self.timeout_seconds,
|
|
|
)
|
|
|
@@ -105,6 +110,10 @@ class OpenRouterQueryVariantClient:
|
|
|
|
|
|
def query_variant_client_from_env(
|
|
|
env: Mapping[str, str] | None = None,
|
|
|
+ *,
|
|
|
+ platform: str = "douyin",
|
|
|
+ strategy_version: str = "V1",
|
|
|
+ root_dir: Path | str = Path("."),
|
|
|
) -> QueryVariantClient:
|
|
|
source = os.environ if env is None else env
|
|
|
api_key = _env_value(source, "OPENROUTER_API_KEY") or _env_value(
|
|
|
@@ -113,10 +122,7 @@ def query_variant_client_from_env(
|
|
|
)
|
|
|
model = _env_value(source, "CONTENT_AGENT_QUERY_LLM_MODEL") or _env_value(source, "MODEL")
|
|
|
base_url = _env_value(source, "OPENROUTER_BASE_URL") or DEFAULT_OPENROUTER_BASE_URL
|
|
|
- prompt_version = (
|
|
|
- _env_value(source, "CONTENT_AGENT_QUERY_LLM_PROMPT_VERSION")
|
|
|
- or DEFAULT_QUERY_PROMPT_VERSION
|
|
|
- )
|
|
|
+ prompt_version = _env_value(source, "CONTENT_AGENT_QUERY_LLM_PROMPT_VERSION") or DEFAULT_QUERY_PROMPT_VERSION
|
|
|
timeout_seconds = _float_env(
|
|
|
source,
|
|
|
"CONTENT_AGENT_QUERY_LLM_TIMEOUT_SECONDS",
|
|
|
@@ -140,29 +146,29 @@ def query_variant_client_from_env(
|
|
|
base_url=base_url,
|
|
|
timeout_seconds=timeout_seconds,
|
|
|
prompt_version=prompt_version,
|
|
|
+ profile=load_profile(platform, strategy_version, root_dir=root_dir),
|
|
|
)
|
|
|
|
|
|
|
|
|
def _messages(seed_term: str, evidence_context: dict[str, Any]) -> list[dict[str, str]]:
|
|
|
+ return _render_messages(DEFAULT_PROFILE, seed_term, evidence_context)
|
|
|
+
|
|
|
+
|
|
|
+def _render_messages(
|
|
|
+ profile: dict[str, Any],
|
|
|
+ seed_term: str,
|
|
|
+ evidence_context: dict[str, Any],
|
|
|
+) -> list[dict[str, str]]:
|
|
|
return [
|
|
|
{
|
|
|
"role": "system",
|
|
|
- "content": (
|
|
|
- "You generate one concise Chinese short-video search query. "
|
|
|
- "Return exactly one plain query string. Do not return JSON, "
|
|
|
- "lists, quotes, explanations, or multiple lines."
|
|
|
- ),
|
|
|
+ "content": str(profile["system"]),
|
|
|
},
|
|
|
{
|
|
|
"role": "user",
|
|
|
- "content": (
|
|
|
- "Seed term:\n"
|
|
|
- f"{seed_term}\n\n"
|
|
|
- "Evidence context:\n"
|
|
|
- f"{evidence_context}\n\n"
|
|
|
- "Create one adjacent search phrase that stays faithful to the evidence. "
|
|
|
- "Avoid any phrase listed in existing_search_queries."
|
|
|
- ),
|
|
|
+ "content": str(profile["user"])
|
|
|
+ .replace("{seed_term}", seed_term)
|
|
|
+ .replace("{evidence_context}", str(evidence_context)),
|
|
|
},
|
|
|
]
|
|
|
|