Skip to content

Commit 76fa44c

Browse files
authored
Merge branch 'main' into fix-notify-when-stream-ready
2 parents 810b53c + 40b5734 commit 76fa44c

10 files changed

Lines changed: 657 additions & 80 deletions

File tree

dapr/clients/grpc/client.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@
2525

2626
import grpc # type: ignore
2727
from google.protobuf.any_pb2 import Any as GrpcAny
28+
from google.protobuf.duration_pb2 import Duration as GrpcDuration
2829
from google.protobuf.empty_pb2 import Empty as GrpcEmpty
2930
from google.protobuf.message import Message as GrpcMessage
31+
from google.protobuf.struct_pb2 import Struct as GrpcStruct
3032
from grpc import ( # type: ignore
3133
RpcError,
3234
StreamStreamClientInterceptor,
@@ -1787,6 +1789,8 @@ def converse_alpha2(
17871789
temperature: Optional[float] = None,
17881790
tools: Optional[List[conversation.ConversationTools]] = None,
17891791
tool_choice: Optional[str] = None,
1792+
response_format: Optional[GrpcStruct] = None,
1793+
prompt_cache_retention: Optional[GrpcDuration] = None,
17901794
) -> conversation.ConversationResponseAlpha2:
17911795
"""Invoke an LLM using the conversation API (Alpha2) with tool calling support.
17921796
@@ -1800,6 +1804,8 @@ def converse_alpha2(
18001804
temperature: Optional temperature setting for the LLM to optimize for creativity or predictability
18011805
tools: Optional list of tools available for the LLM to call
18021806
tool_choice: Optional control over which tools can be called ('none', 'auto', 'required', or specific tool name)
1807+
response_format: Optional response format (google.protobuf.struct_pb2.Struct, ex: json_schema for structured output)
1808+
prompt_cache_retention: Optional retention for prompt cache (google.protobuf.duration_pb2.Duration)
18031809
18041810
Returns:
18051811
ConversationResponseAlpha2 containing the conversation results with choices and tool calls
@@ -1856,6 +1862,10 @@ def converse_alpha2(
18561862
request.temperature = temperature
18571863
if tool_choice is not None:
18581864
request.tool_choice = tool_choice
1865+
if response_format is not None and hasattr(request, 'response_format'):
1866+
request.response_format.CopyFrom(response_format)
1867+
if prompt_cache_retention is not None and hasattr(request, 'prompt_cache_retention'):
1868+
request.prompt_cache_retention.CopyFrom(prompt_cache_retention)
18591869

18601870
try:
18611871
response, call = self.retry_policy.run_rpc(self._stub.ConverseAlpha2.with_call, request)

dapr/clients/grpc/conversation.py

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,11 +338,46 @@ class ConversationResultAlpha2Choices:
338338
message: ConversationResultAlpha2Message
339339

340340

341+
@dataclass
342+
class ConversationResultAlpha2CompletionUsageCompletionTokensDetails:
343+
"""Breakdown of tokens used in the completion."""
344+
345+
accepted_prediction_tokens: int = 0
346+
audio_tokens: int = 0
347+
reasoning_tokens: int = 0
348+
rejected_prediction_tokens: int = 0
349+
350+
351+
@dataclass
352+
class ConversationResultAlpha2CompletionUsagePromptTokensDetails:
353+
"""Breakdown of tokens used in the prompt."""
354+
355+
audio_tokens: int = 0
356+
cached_tokens: int = 0
357+
358+
359+
@dataclass
360+
class ConversationResultAlpha2CompletionUsage:
361+
"""Token usage for one Alpha2 conversation result."""
362+
363+
completion_tokens: int = 0
364+
prompt_tokens: int = 0
365+
total_tokens: int = 0
366+
completion_tokens_details: Optional[
367+
ConversationResultAlpha2CompletionUsageCompletionTokensDetails
368+
] = None
369+
prompt_tokens_details: Optional[ConversationResultAlpha2CompletionUsagePromptTokensDetails] = (
370+
None
371+
)
372+
373+
341374
@dataclass
342375
class ConversationResultAlpha2:
343376
"""One of the outputs in Alpha2 response from conversation input."""
344377

345378
choices: List[ConversationResultAlpha2Choices] = field(default_factory=list)
379+
model: Optional[str] = None
380+
usage: Optional[ConversationResultAlpha2CompletionUsage] = None
346381

347382

348383
@dataclass
@@ -657,5 +692,38 @@ def _get_outputs_from_grpc_response(
657692
)
658693
)
659694

660-
outputs.append(ConversationResultAlpha2(choices=choices))
695+
model: Optional[str] = None
696+
usage: Optional[ConversationResultAlpha2CompletionUsage] = None
697+
if hasattr(output, 'model') and getattr(output, 'model', None):
698+
model = output.model
699+
if hasattr(output, 'usage') and output.usage:
700+
u = output.usage
701+
completion_details: Optional[
702+
ConversationResultAlpha2CompletionUsageCompletionTokensDetails
703+
] = None
704+
prompt_details: Optional[ConversationResultAlpha2CompletionUsagePromptTokensDetails] = (
705+
None
706+
)
707+
if hasattr(u, 'completion_tokens_details') and u.completion_tokens_details:
708+
cd = u.completion_tokens_details
709+
completion_details = ConversationResultAlpha2CompletionUsageCompletionTokensDetails(
710+
accepted_prediction_tokens=getattr(cd, 'accepted_prediction_tokens', 0) or 0,
711+
audio_tokens=getattr(cd, 'audio_tokens', 0) or 0,
712+
reasoning_tokens=getattr(cd, 'reasoning_tokens', 0) or 0,
713+
rejected_prediction_tokens=getattr(cd, 'rejected_prediction_tokens', 0) or 0,
714+
)
715+
if hasattr(u, 'prompt_tokens_details') and u.prompt_tokens_details:
716+
pd = u.prompt_tokens_details
717+
prompt_details = ConversationResultAlpha2CompletionUsagePromptTokensDetails(
718+
audio_tokens=getattr(pd, 'audio_tokens', 0) or 0,
719+
cached_tokens=getattr(pd, 'cached_tokens', 0) or 0,
720+
)
721+
usage = ConversationResultAlpha2CompletionUsage(
722+
completion_tokens=getattr(u, 'completion_tokens', 0) or 0,
723+
prompt_tokens=getattr(u, 'prompt_tokens', 0) or 0,
724+
total_tokens=getattr(u, 'total_tokens', 0) or 0,
725+
completion_tokens_details=completion_details,
726+
prompt_tokens_details=prompt_details,
727+
)
728+
outputs.append(ConversationResultAlpha2(choices=choices, model=model, usage=usage))
661729
return outputs

0 commit comments

Comments
 (0)