|
29 | 29 |
|
30 | 30 | from google.protobuf.message import Message as GrpcMessage
|
31 | 31 | from google.protobuf.empty_pb2 import Empty as GrpcEmpty
|
| 32 | +from google.protobuf.any_pb2 import Any as GrpcAny |
32 | 33 |
|
33 | 34 | import grpc.aio # type: ignore
|
34 | 35 | from grpc.aio import ( # type: ignore
|
|
75 | 76 | InvokeMethodRequest,
|
76 | 77 | BindingRequest,
|
77 | 78 | TransactionalStateOperation,
|
| 79 | + ConversationInput, |
78 | 80 | )
|
79 | 81 | from dapr.clients.grpc._response import (
|
80 | 82 | BindingResponse,
|
| 83 | + ConversationResponse, |
| 84 | + ConversationResult, |
81 | 85 | DaprResponse,
|
82 | 86 | GetSecretResponse,
|
83 | 87 | GetBulkSecretResponse,
|
@@ -1711,6 +1715,62 @@ async def purge_workflow(self, instance_id: str, workflow_component: str) -> Dap
|
1711 | 1715 | except grpc.aio.AioRpcError as err:
|
1712 | 1716 | raise DaprInternalError(err.details())
|
1713 | 1717 |
|
| 1718 | + async def converse_alpha1( |
| 1719 | + self, |
| 1720 | + name: str, |
| 1721 | + inputs: List[ConversationInput], |
| 1722 | + *, |
| 1723 | + context_id: Optional[str] = None, |
| 1724 | + parameters: Optional[Dict[str, GrpcAny]] = None, |
| 1725 | + metadata: Optional[Dict[str, str]] = None, |
| 1726 | + scrub_pii: Optional[bool] = None, |
| 1727 | + temperature: Optional[float] = None, |
| 1728 | + ) -> ConversationResponse: |
| 1729 | + """Invoke an LLM using the conversation API (Alpha). |
| 1730 | +
|
| 1731 | + Args: |
| 1732 | + name: Name of the LLM component to invoke |
| 1733 | + inputs: List of conversation inputs |
| 1734 | + context_id: Optional ID for continuing an existing chat |
| 1735 | + parameters: Optional custom parameters for the request |
| 1736 | + metadata: Optional metadata for the component |
| 1737 | + scrub_pii: Optional flag to scrub PII from inputs and outputs |
| 1738 | + temperature: Optional temperature setting for the LLM to optimize for creativity or predictability |
| 1739 | +
|
| 1740 | + Returns: |
| 1741 | + ConversationResponse containing the conversation results |
| 1742 | +
|
| 1743 | + Raises: |
| 1744 | + DaprGrpcError: If the Dapr runtime returns an error |
| 1745 | + """ |
| 1746 | + inputs_pb = [ |
| 1747 | + api_v1.ConversationInput(content=inp.content, role=inp.role, scrubPII=inp.scrub_pii) |
| 1748 | + for inp in inputs |
| 1749 | + ] |
| 1750 | + |
| 1751 | + request = api_v1.ConversationRequest( |
| 1752 | + name=name, |
| 1753 | + inputs=inputs_pb, |
| 1754 | + contextID=context_id, |
| 1755 | + parameters=parameters or {}, |
| 1756 | + metadata=metadata or {}, |
| 1757 | + scrubPII=scrub_pii, |
| 1758 | + temperature=temperature, |
| 1759 | + ) |
| 1760 | + |
| 1761 | + try: |
| 1762 | + response = await self._stub.ConverseAlpha1(request) |
| 1763 | + |
| 1764 | + outputs = [ |
| 1765 | + ConversationResult(result=output.result, parameters=output.parameters) |
| 1766 | + for output in response.outputs |
| 1767 | + ] |
| 1768 | + |
| 1769 | + return ConversationResponse(context_id=response.contextID, outputs=outputs) |
| 1770 | + |
| 1771 | + except grpc.aio.AioRpcError as err: |
| 1772 | + raise DaprGrpcError(err) from err |
| 1773 | + |
1714 | 1774 | async def wait(self, timeout_s: float):
|
1715 | 1775 | """Waits for sidecar to be available within the timeout.
|
1716 | 1776 |
|
|
0 commit comments