refactor(ai_client): _send_llama_native_result() returns Result[str]
This commit is contained in:
+27
-24
@@ -2536,36 +2536,39 @@ def ollama_chat(
|
||||
resp = requests.post(f"{base_url}/api/chat", json=payload, timeout=120)
|
||||
return resp.json()
|
||||
|
||||
def _send_llama_native(md_content: str, user_message: str, base_dir: str,
|
||||
def _send_llama_native_result(md_content: str, user_message: str, base_dir: str,
|
||||
file_items: list[dict[str, Any]] | None = None,
|
||||
discussion_history: str = "",
|
||||
stream: bool = False,
|
||||
pre_tool_callback: Optional[Callable[[str, str, Optional[Callable[[str], str]]], Optional[str]]] = None,
|
||||
qa_callback: Optional[Callable[[str], str]] = None,
|
||||
stream_callback: Optional[Callable[[str], None]] = None,
|
||||
patch_callback: Optional[Callable[[str, str], Optional[str]]] = None) -> str:
|
||||
base_url = _llama_base_url.replace("/v1", "")
|
||||
with _llama_history_lock:
|
||||
if discussion_history and not _llama_history:
|
||||
_llama_history.append({"role": "user", "content": f"[DISCUSSION HISTORY]\n\n{discussion_history}\n\n---\n\n{user_message}"})
|
||||
else:
|
||||
_llama_history.append({"role": "user", "content": user_message})
|
||||
messages: list[dict[str, Any]] = [{"role": "system", "content": f"{_get_combined_system_prompt()}\n\n<context>\n{md_content}\n</context>"}]
|
||||
messages.extend(_llama_history)
|
||||
images: list[str] = []
|
||||
if file_items:
|
||||
for fi in file_items:
|
||||
if fi.get("is_image") and fi.get("base64_data"):
|
||||
images.append(fi["base64_data"])
|
||||
response = ollama_chat(_model, messages, images=images, base_url=base_url)
|
||||
text = response.get("message", {}).get("content", "")
|
||||
thinking = response.get("message", {}).get("thinking", "")
|
||||
with _llama_history_lock:
|
||||
msg: dict[str, Any] = {"role": "assistant", "content": text or None}
|
||||
if thinking:
|
||||
msg["thinking"] = thinking
|
||||
_llama_history.append(msg)
|
||||
return (f"<thinking>\n{thinking}\n</thinking>\n" if thinking else "") + text
|
||||
patch_callback: Optional[Callable[[str, str], Optional[str]]] = None) -> Result[str]:
|
||||
try:
|
||||
base_url = _llama_base_url.replace("/v1", "")
|
||||
with _llama_history_lock:
|
||||
if discussion_history and not _llama_history:
|
||||
_llama_history.append({"role": "user", "content": f"[DISCUSSION HISTORY]\n\n{discussion_history}\n\n---\n\n{user_message}"})
|
||||
else:
|
||||
_llama_history.append({"role": "user", "content": user_message})
|
||||
messages: list[dict[str, Any]] = [{"role": "system", "content": f"{_get_combined_system_prompt()}\n\n<context>\n{md_content}\n</context>"}]
|
||||
messages.extend(_llama_history)
|
||||
images: list[str] = []
|
||||
if file_items:
|
||||
for fi in file_items:
|
||||
if fi.get("is_image") and fi.get("base64_data"):
|
||||
images.append(fi["base64_data"])
|
||||
response = ollama_chat(_model, messages, images=images, base_url=base_url)
|
||||
text = response.get("message", {}).get("content", "")
|
||||
thinking = response.get("message", {}).get("thinking", "")
|
||||
with _llama_history_lock:
|
||||
msg: dict[str, Any] = {"role": "assistant", "content": text or None}
|
||||
if thinking:
|
||||
msg["thinking"] = thinking
|
||||
_llama_history.append(msg)
|
||||
return Result(data=(f"<thinking>\n{thinking}\n</thinking>\n" if thinking else "") + text)
|
||||
except Exception as exc:
|
||||
return Result(data="", errors=[ErrorInfo(kind=ErrorKind.INTERNAL, message=str(exc), source="ai_client.llama_native", original=exc)])
|
||||
def _list_llama_models() -> list[str]:
|
||||
from src.vendor_capabilities import list_models_for_vendor
|
||||
return list_models_for_vendor("llama")
|
||||
|
||||
Reference in New Issue
Block a user