speech
Loading...
Searching...
No Matches
llmClientExample.py
1#!/usr/bin/env python3
2
3
7
8import argparse
9import yarp
10
11parser = argparse.ArgumentParser(description='LLM client example')
12parser.add_argument('--remote', default='/llm_nws/rpc:i', type=str, help='remote port')
13
14args = parser.parse_args()
15
16yarp.Network.init()
17
18if not yarp.Network.checkNetwork():
19 print('Please start a YARP name server first')
20 raise SystemExit
21
22options = yarp.Property()
23options.put('device', 'LLM_nwc_yarp')
24options.put('local', '/llmClientExample/client')
25options.put('remote', args.remote)
26
27device = yarp.PolyDriver(options)
28
29if not device.isValid():
30 print('Remote device not available')
31 raise SystemExit
32
33llm = device.viewILLM()
34
35if not llm:
36 print('Unable to get ILLM interface')
37 raise SystemExit
38
39if not llm.setPrompt('You are a helpful mathematician.'):
40 print('Failed to set prompt')
41 raise SystemExit
42
43prompt = yarp.SVector(1)
44
45if not llm.readPrompt(prompt):
46 print('Failed to read prompt')
47 raise SystemExit
48else:
49 print(f'Prompt is: {prompt[0]}')
50
51answer = yarp.LLM_Message()
52
53question = 'What is 2 + 2?';
54print(f'Asking: {question}')
55
56if not llm.ask(question, answer):
57 print('Failed to ask question')
58 raise SystemExit
59
60print('Answer:')
61print(f'* type: {answer.type}')
62print(f'* content: {answer.content}')
63print(f'* parameters: {list(answer.parameters)}')
64print(f'* arguments: {list(answer.arguments)}')
65
66print('Deleting conversation')
67
68if not llm.refreshConversation():
69 print('Failed to refresh conversation')
70 raise SystemExit
71
72question = 'What is 3 + 3?'
73
74print(f'Asking: {question}')
75
76if not llm.ask(question, answer):
77 print('Failed to ask question')
78 raise SystemExit
79
80print('Answer:')
81print(f'* type: {answer.type}')
82print(f'* content: {answer.content}')
83print(f'* parameters: {list(answer.parameters)}')
84print(f'* arguments: {list(answer.arguments)}')
85
86conversation = yarp.LLMVector()
87
88if not llm.getConversation(conversation):
89 print('Failed to get conversation')
90 raise SystemExit
91else:
92 print('Conversation:')
93
94 for msg in conversation:
95 print(msg.toString_c())
96
97print('Deleting conversation and prompt')
98
99if not llm.deleteConversation():
100 print('Failed to delete conversation')
101 raise SystemExit
102
103if not llm.setPrompt('You are the worst mathematician ever, you always provide wrong answers.'):
104 print('Failed to set prompt')
105 raise SystemExit
106
107if not llm.readPrompt(prompt):
108 print('Failed to read prompt')
109 raise SystemExit
110else:
111 print(f'Prompt is: {prompt[0]}')
112
113question = 'What is 2 + 2?'
114
115print(f'Asking: {question}')
116
117if not llm.ask(question, answer):
118 print('Failed to ask question')
119 raise SystemExit
120
121print('Answer:')
122print(f'* type: {answer.type}')
123print(f'* content: {answer.content}')
124print(f'* parameters: {list(answer.parameters)}')
125print(f'* arguments: {list(answer.arguments)}')
126
127device.close()