refactor(ai_client): _send_llama_result() returns Result[str]
This commit is contained in:
+35
-32
@@ -2471,45 +2471,48 @@ def _ensure_llama_client() -> Any:
|
|||||||
_llama_client = openai.OpenAI(api_key=_llama_api_key, base_url=_llama_base_url)
|
_llama_client = openai.OpenAI(api_key=_llama_api_key, base_url=_llama_base_url)
|
||||||
return _llama_client
|
return _llama_client
|
||||||
|
|
||||||
def _send_llama(md_content: str, user_message: str, base_dir: str,
|
def _send_llama_result(md_content: str, user_message: str, base_dir: str,
|
||||||
file_items: list[dict[str, Any]] | None = None,
|
file_items: list[dict[str, Any]] | None = None,
|
||||||
discussion_history: str = "",
|
discussion_history: str = "",
|
||||||
stream: bool = False,
|
stream: bool = False,
|
||||||
pre_tool_callback: Optional[Callable[[str, str, Optional[Callable[[str], str]]], Optional[str]]] = None,
|
pre_tool_callback: Optional[Callable[[str, str, Optional[Callable[[str], str]]], Optional[str]]] = None,
|
||||||
qa_callback: Optional[Callable[[str], str]] = None,
|
qa_callback: Optional[Callable[[str], str]] = None,
|
||||||
stream_callback: Optional[Callable[[str], None]] = None,
|
stream_callback: Optional[Callable[[str], None]] = None,
|
||||||
patch_callback: Optional[Callable[[str, str], Optional[str]]] = None) -> str:
|
patch_callback: Optional[Callable[[str, str], Optional[str]]] = None) -> Result[str]:
|
||||||
if "localhost" in _llama_base_url or "127.0.0.1" in _llama_base_url:
|
from src.openai_compatible import OpenAICompatibleRequest, _classify_openai_compatible_error
|
||||||
return _send_llama_native(md_content, user_message, base_dir, file_items, discussion_history, stream, pre_tool_callback, qa_callback, stream_callback, patch_callback)
|
try:
|
||||||
from src.openai_compatible import OpenAICompatibleRequest
|
if "localhost" in _llama_base_url or "127.0.0.1" in _llama_base_url:
|
||||||
client = _ensure_llama_client()
|
return _send_llama_native(md_content, user_message, base_dir, file_items, discussion_history, stream, pre_tool_callback, qa_callback, stream_callback, patch_callback)
|
||||||
tools: list[dict[str, Any]] | None = _get_deepseek_tools() or None
|
client = _ensure_llama_client()
|
||||||
with _llama_history_lock:
|
tools: list[dict[str, Any]] | None = _get_deepseek_tools() or None
|
||||||
user_content = user_message
|
|
||||||
if file_items:
|
|
||||||
for fi in file_items:
|
|
||||||
if fi.get("is_image") and fi.get("base64_data"):
|
|
||||||
user_content = f"[IMAGE: {fi.get('path', 'attachment')}]\n{user_content}"
|
|
||||||
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_content})
|
|
||||||
def _build_llama_request(_round_idx: int) -> OpenAICompatibleRequest:
|
|
||||||
with _llama_history_lock:
|
with _llama_history_lock:
|
||||||
messages: list[dict[str, Any]] = [{"role": "system", "content": f"{_get_combined_system_prompt()}\n\n<context>\n{md_content}\n</context>"}]
|
user_content = user_message
|
||||||
messages.extend(_llama_history)
|
if file_items:
|
||||||
return OpenAICompatibleRequest(
|
for fi in file_items:
|
||||||
messages=messages, model=_model, temperature=_temperature, top_p=_top_p,
|
if fi.get("is_image") and fi.get("base64_data"):
|
||||||
max_tokens=_max_tokens, stream=stream, stream_callback=stream_callback,
|
user_content = f"[IMAGE: {fi.get('path', 'attachment')}]\n{user_content}"
|
||||||
tools=tools, tool_choice="auto" if tools else "auto",
|
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}"})
|
||||||
caps = get_capabilities("llama", _model)
|
else:
|
||||||
return run_with_tool_loop(
|
_llama_history.append({"role": "user", "content": user_content})
|
||||||
client, _build_llama_request, capabilities=caps,
|
def _build_llama_request(_round_idx: int) -> OpenAICompatibleRequest:
|
||||||
pre_tool_callback=pre_tool_callback, qa_callback=qa_callback, stream_callback=stream_callback,
|
with _llama_history_lock:
|
||||||
patch_callback=patch_callback, base_dir=base_dir, vendor_name="llama",
|
messages: list[dict[str, Any]] = [{"role": "system", "content": f"{_get_combined_system_prompt()}\n\n<context>\n{md_content}\n</context>"}]
|
||||||
history_lock=_llama_history_lock, history=_llama_history,
|
messages.extend(_llama_history)
|
||||||
)
|
return OpenAICompatibleRequest(
|
||||||
|
messages=messages, model=_model, temperature=_temperature, top_p=_top_p,
|
||||||
|
max_tokens=_max_tokens, stream=stream, stream_callback=stream_callback,
|
||||||
|
tools=tools, tool_choice="auto" if tools else "auto",
|
||||||
|
)
|
||||||
|
caps = get_capabilities("llama", _model)
|
||||||
|
return Result(data=run_with_tool_loop(
|
||||||
|
client, _build_llama_request, capabilities=caps,
|
||||||
|
pre_tool_callback=pre_tool_callback, qa_callback=qa_callback, stream_callback=stream_callback,
|
||||||
|
patch_callback=patch_callback, base_dir=base_dir, vendor_name="llama",
|
||||||
|
history_lock=_llama_history_lock, history=_llama_history,
|
||||||
|
))
|
||||||
|
except Exception as exc:
|
||||||
|
return Result(data="", errors=[_classify_openai_compatible_error(exc, source="ai_client.llama")])
|
||||||
|
|
||||||
OLLAMA_DEFAULT_BASE_URL: str = "http://localhost:11434"
|
OLLAMA_DEFAULT_BASE_URL: str = "http://localhost:11434"
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user