Add LLM token
This commit is contained in:
@@ -1,4 +1,8 @@
|
||||
"""Provide service-layer logic for deepseek client."""
|
||||
"""Provide service-layer logic for deepseek client.
|
||||
|
||||
P0-0: ``chat()`` now accepts an optional ``tools`` list and parses ``tool_calls``
|
||||
from the model response so that callers can dispatch tool invocations.
|
||||
"""
|
||||
|
||||
import time
|
||||
from typing import List, Dict, Optional
|
||||
@@ -6,6 +10,7 @@ from loguru import logger
|
||||
import httpx
|
||||
|
||||
from .base_client import BaseLLMClient, LLMResponse, LLMConfig, LLMProvider
|
||||
from .tool_types import Tool, ToolCall
|
||||
# Keep provider-specific behavior explicit so debugging stays straightforward.
|
||||
|
||||
|
||||
@@ -46,13 +51,20 @@ class DeepSeekClient(BaseLLMClient):
|
||||
messages: List[Dict[str, str]],
|
||||
max_tokens: Optional[int] = None,
|
||||
temperature: Optional[float] = None,
|
||||
tools: Optional[List[Tool]] = None,
|
||||
**kwargs
|
||||
) -> LLMResponse:
|
||||
"""Handle chat for the Deep Seek Client instance."""
|
||||
"""Handle chat for the Deep Seek Client instance.
|
||||
|
||||
When ``tools`` is provided the request includes the tool definitions and
|
||||
``tool_choice="auto"``; any tool_calls returned by the model are parsed
|
||||
into ``LLMResponse.tool_calls``.
|
||||
"""
|
||||
import json
|
||||
start_time = time.time()
|
||||
|
||||
try:
|
||||
payload = {
|
||||
payload: Dict = {
|
||||
"model": self.config.model,
|
||||
"messages": messages,
|
||||
"max_tokens": max_tokens or self.config.max_tokens,
|
||||
@@ -61,6 +73,11 @@ class DeepSeekClient(BaseLLMClient):
|
||||
"stream": False
|
||||
}
|
||||
|
||||
# P0-0: inject tool definitions when provided.
|
||||
if tools:
|
||||
payload["tools"] = [t.to_openai_format() for t in tools]
|
||||
payload["tool_choice"] = "auto"
|
||||
|
||||
response = self._client.post("/chat/completions", json=payload)
|
||||
response.raise_for_status()
|
||||
|
||||
@@ -71,12 +88,24 @@ class DeepSeekClient(BaseLLMClient):
|
||||
choices = data.get("choices", [{}])
|
||||
message = choices[0].get("message", {})
|
||||
|
||||
# P0-0: parse tool_calls returned by the model.
|
||||
raw_tool_calls = message.get("tool_calls") or []
|
||||
parsed_tool_calls: List[ToolCall] = []
|
||||
for tc in raw_tool_calls:
|
||||
fn = tc.get("function", {})
|
||||
try:
|
||||
args = json.loads(fn.get("arguments", "{}"))
|
||||
except json.JSONDecodeError:
|
||||
args = {}
|
||||
parsed_tool_calls.append(ToolCall(id=tc.get("id", ""), name=fn.get("name", ""), arguments=args))
|
||||
|
||||
return LLMResponse(
|
||||
content=message.get("content", ""),
|
||||
content=message.get("content", "") or "",
|
||||
model=data.get("model", self.config.model),
|
||||
usage=data.get("usage", {}),
|
||||
finish_reason=choices[0].get("finish_reason", "stop"),
|
||||
latency_ms=latency_ms
|
||||
latency_ms=latency_ms,
|
||||
tool_calls=parsed_tool_calls,
|
||||
)
|
||||
|
||||
except httpx.HTTPStatusError as e:
|
||||
|
||||
Reference in New Issue
Block a user