|
23 | 23 |
|
24 | 24 | from dapr.aio.clients.grpc.client import DaprGrpcClientAsync
|
25 | 25 | from dapr.aio.clients import DaprClient
|
26 |
| -from dapr.clients.exceptions import DaprGrpcError |
| 26 | +from dapr.clients.exceptions import DaprGrpcError, DaprInternalError |
27 | 27 | from dapr.common.pubsub.subscription import StreamInactiveError
|
28 | 28 | from dapr.proto import common_v1
|
29 | 29 | from .fake_dapr_server import FakeDaprSidecar
|
30 | 30 | from dapr.conf import settings
|
31 | 31 | from dapr.clients.grpc._helpers import to_bytes
|
32 |
| -from dapr.clients.grpc._request import TransactionalStateOperation |
| 32 | +from dapr.clients.grpc._request import TransactionalStateOperation, ConversationInput |
33 | 33 | from dapr.clients.grpc._state import StateOptions, Consistency, Concurrency, StateItem
|
34 | 34 | from dapr.clients.grpc._crypto import EncryptOptions, DecryptOptions
|
35 | 35 | from dapr.clients.grpc._response import (
|
@@ -1113,6 +1113,58 @@ async def test_decrypt_file_data_read_chunks(self):
|
1113 | 1113 | self.assertEqual(await resp.read(5), b'hello')
|
1114 | 1114 | self.assertEqual(await resp.read(5), b' dapr')
|
1115 | 1115 |
|
| 1116 | + async def test_converse_alpha1_basic(self): |
| 1117 | + dapr = DaprGrpcClientAsync(f'{self.scheme}localhost:{self.grpc_port}') |
| 1118 | + |
| 1119 | + inputs = [ConversationInput(message="Hello", role="user"), |
| 1120 | + ConversationInput(message="How are you?", role="user")] |
| 1121 | + |
| 1122 | + response = await dapr.converse_alpha1(name="test-llm", inputs=inputs) |
| 1123 | + |
| 1124 | + # Check response structure |
| 1125 | + self.assertIsNotNone(response) |
| 1126 | + self.assertEqual(len(response.outputs), 2) |
| 1127 | + self.assertEqual(response.outputs[0].result, "Response to: Hello") |
| 1128 | + self.assertEqual(response.outputs[1].result, "Response to: How are you?") |
| 1129 | + await dapr.close() |
| 1130 | + |
| 1131 | + async def test_converse_alpha1_with_options(self): |
| 1132 | + dapr = DaprGrpcClientAsync(f'{self.scheme}localhost:{self.grpc_port}') |
| 1133 | + |
| 1134 | + inputs = [ConversationInput(message="Hello", role="user", scrub_pii=True)] |
| 1135 | + |
| 1136 | + response = await dapr.converse_alpha1(name="test-llm", inputs=inputs, context_id="chat-123", |
| 1137 | + temperature=0.7, scrub_pii=True, metadata={"key": "value"}) |
| 1138 | + |
| 1139 | + self.assertIsNotNone(response) |
| 1140 | + self.assertEqual(len(response.outputs), 1) |
| 1141 | + self.assertEqual(response.outputs[0].result, "Response to: Hello") |
| 1142 | + await dapr.close() |
| 1143 | + |
| 1144 | + async def test_converse_alpha1_error_handling(self): |
| 1145 | + dapr = DaprGrpcClientAsync(f'{self.scheme}localhost:{self.grpc_port}') |
| 1146 | + |
| 1147 | + # Setup server to raise an exception |
| 1148 | + self._fake_dapr_server.raise_exception_on_next_call( |
| 1149 | + status_pb2.Status(code=code_pb2.INVALID_ARGUMENT, message="Invalid argument")) |
| 1150 | + |
| 1151 | + inputs = [ConversationInput(message="Hello", role="user")] |
| 1152 | + |
| 1153 | + with self.assertRaises(DaprInternalError) as context: |
| 1154 | + await dapr.converse_alpha1(name="test-llm", inputs=inputs) |
| 1155 | + self.assertTrue("Invalid argument" in str(context.exception)) |
| 1156 | + await dapr.close() |
| 1157 | + |
| 1158 | + async def test_converse_alpha1_empty_inputs(self): |
| 1159 | + dapr = DaprGrpcClientAsync(f'{self.scheme}localhost:{self.grpc_port}') |
| 1160 | + |
| 1161 | + # Test with empty inputs list |
| 1162 | + response = await dapr.converse_alpha1(name="test-llm", inputs=[]) |
| 1163 | + |
| 1164 | + self.assertIsNotNone(response) |
| 1165 | + self.assertEqual(len(response.outputs), 0) |
| 1166 | + await dapr.close() |
| 1167 | + |
1116 | 1168 |
|
1117 | 1169 | if __name__ == '__main__':
|
1118 | 1170 | unittest.main()
|
0 commit comments