diff --git a/ambient/agent.py b/ambient/agent.py index 0113473..743774d 100644 --- a/ambient/agent.py +++ b/ambient/agent.py @@ -354,8 +354,13 @@ async def run_agent( print(f"Turn {turn}") turn_trajectory: dict = {"turn": turn, "messages": list(messages)} + # Agent (orchestrator) endpoint may differ from the vision endpoint: + # vision tools use llm_base_url (e.g. local vLLM); the agent uses agent_base_url + # (e.g. OpenRouter/deepseek) when set, falling back to the llm_* pair. + _agent_base = settings.agent_base_url or settings.llm_base_url + _agent_key = settings.agent_api_key or settings.llm_api_key resp = await _send_request( - settings.llm_base_url, settings.llm_api_key, model, messages + _agent_base, _agent_key, model, messages ) if "content" not in resp: print(f"No content in response: {resp}") diff --git a/ambient/llm.py b/ambient/llm.py index 68e356a..ebd2d5c 100644 --- a/ambient/llm.py +++ b/ambient/llm.py @@ -28,6 +28,22 @@ def video_to_data_url(path: str, mime="video/mp4") -> str: return f"data:{mime};base64,{b64}" +def _clip_to_frame_urls(mp4: str, fps: float = 0.5, cap: int = 28, dim: int = 768) -> list: + """Sample chronological frames from a clip mp4 as base64 image data-URLs. + Used for image-only vision models (e.g. qwen) that reject video_url input.""" + import subprocess, tempfile, glob as _glob + d = tempfile.mkdtemp(prefix="clipfr_") + subprocess.run( + ["ffmpeg", "-nostdin", "-loglevel", "error", "-i", mp4, "-vf", + f"fps={fps},scale='if(gt(iw,ih),{dim},-2)':'if(gt(iw,ih),-2,{dim})'", "-q:v", "4", + os.path.join(d, "f%04d.jpg")], check=False) + out = [] + for p in sorted(_glob.glob(os.path.join(d, "*.jpg")))[:cap]: + with open(p, "rb") as f: + out.append("data:image/jpeg;base64," + base64.b64encode(f.read()).decode()) + return out + + def construct_payload(clips: Optional[List[Clip]] = None, frames: Optional[List[Frame]] = None): payload = [] if clips is None and frames is None: @@ -38,28 +54,26 @@ def construct_payload(clips: Optional[List[Clip]] = None, frames: Optional[List[ is_clip_url = clip.clip_url is not None is_clip_file_path = clip.clip_file_path is not None and os.path.exists(clip.clip_file_path) - if is_clip_url: - url = clip.clip_url - elif is_clip_file_path: - url = video_to_data_url(clip.clip_file_path) - else: - raise ValueError(f"Clip {clip.id} has no valid url or file path") - - timestamp = f"Timestamp: {clip.start_time} seconds to {clip.end_time} seconds" - - payload.extend( - [ - { - "type": "text", - "text": f"Clip ID: {clip.id}\n{timestamp}", - }, - { - "type": "video_url", - "video_url": {"url": url}, - }, - ] - ) + image_only = "gemini" not in (settings.llm_model or "").lower() + + if is_clip_file_path and image_only: + # image-only vision model (e.g. qwen3.6-27b): send sampled frames, not video_url + payload.append({"type": "text", + "text": f"Clip ID: {clip.id}\n{timestamp}\n(chronological sampled frames)"}) + for u in _clip_to_frame_urls(clip.clip_file_path): + payload.append({"type": "image_url", "image_url": {"url": u}}) + else: + if is_clip_url: + url = clip.clip_url + elif is_clip_file_path: + url = video_to_data_url(clip.clip_file_path) + else: + raise ValueError(f"Clip {clip.id} has no valid url or file path") + payload.extend([ + {"type": "text", "text": f"Clip ID: {clip.id}\n{timestamp}"}, + {"type": "video_url", "video_url": {"url": url}}, + ]) if frames is not None: start_time = frames[0].timestamp