|
10 | 10 | # See the License for the specific language governing permissions and |
11 | 11 | # limitations under the License. |
12 | 12 | # ------------------------------------------------------------ |
13 | | -import sys |
14 | 13 | from dapr.clients import DaprClient |
15 | 14 | from dapr.clients.grpc.conversation import ConversationInputAlpha2, ConversationMessage, ConversationMessageContent, ConversationMessageOfUser |
16 | 15 |
|
17 | | -try: |
18 | | - with DaprClient() as d: |
19 | | - text_input = "What is dapr?" |
20 | | - provider_component = "ollama" |
| 16 | +with DaprClient() as d: |
| 17 | + text_input = "What is dapr?" |
| 18 | + provider_component = "echo" |
21 | 19 |
|
22 | | - inputs = [ |
23 | | - ConversationInputAlpha2(messages=[ConversationMessage(of_user=ConversationMessageOfUser(content=[ConversationMessageContent(text=text_input)]))], |
24 | | - scrub_pii=True), |
25 | | - ] |
| 20 | + inputs = [ |
| 21 | + ConversationInputAlpha2(messages=[ConversationMessage(of_user=ConversationMessageOfUser(content=[ConversationMessageContent(text=text_input)]))], |
| 22 | + scrub_pii=True), |
| 23 | + ] |
26 | 24 |
|
27 | | - print(f'Input sent: {text_input}', flush=True) |
| 25 | + print(f'Input sent: {text_input}') |
28 | 26 |
|
29 | | - response = d.converse_alpha2(name=provider_component, inputs=inputs, temperature=0.7) |
30 | | - |
31 | | - if response and hasattr(response, 'outputs') and response.outputs: |
32 | | - for output in response.outputs: |
33 | | - if hasattr(output, 'model') and output.model: |
34 | | - print(f'Model: {output.model}', flush=True) |
35 | | - |
36 | | - if hasattr(output, 'usage') and output.usage: |
37 | | - usage = output.usage |
38 | | - prompt_tokens = getattr(usage, 'prompt_tokens', None) or getattr(usage, 'promptTokens', None) |
39 | | - completion_tokens = getattr(usage, 'completion_tokens', None) or getattr(usage, 'completionTokens', None) |
40 | | - total_tokens = getattr(usage, 'total_tokens', None) or getattr(usage, 'totalTokens', None) |
41 | | - print(f'Usage: prompt_tokens={prompt_tokens} completion_tokens={completion_tokens} total_tokens={total_tokens}', flush=True) |
42 | | - |
43 | | - if output and hasattr(output, 'choices') and output.choices and len(output.choices) > 0: |
44 | | - choice = output.choices[0] |
45 | | - if choice and hasattr(choice, 'message') and choice.message: |
46 | | - message = choice.message |
47 | | - content = getattr(message, 'content', None) |
48 | | - if content: |
49 | | - print(f'Output response: {content}', flush=True) |
50 | | - else: |
51 | | - print(f'Output response: {message}', flush=True) |
52 | | - else: |
53 | | - print(f'Output response: {choice}', flush=True) |
54 | | - else: |
55 | | - print('No choices in output', flush=True) |
56 | | - print(f'Output response: {output}', flush=True) |
57 | | - else: |
58 | | - print('No outputs found in response', flush=True) |
59 | | - print(f'Response data: {response}', flush=True) |
60 | | -except Exception as e: |
61 | | - print(f'Error: {e}', file=sys.stderr, flush=True) |
62 | | - sys.exit(1) |
| 27 | + response = d.converse_alpha2(name=provider_component, inputs=inputs, temperature=0.7, context_id='chat-123') |
| 28 | + |
| 29 | + for output in response.outputs: |
| 30 | + print(f'Output response: {output.choices[0].message.content}') |
0 commit comments