-
Notifications
You must be signed in to change notification settings - Fork 0
/
tool_call.flow.yaml
255 lines (242 loc) · 10.1 KB
/
tool_call.flow.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
name: tool_call
type: serial
description: Manage projects by calling tools.
input:
schema:
input:
type: object
required:
- messages
properties:
messages:
type: array
description: A list of messages comprising the conversation so far.
items:
type: object
required:
- role
- content
properties:
role:
type: string
description: The role of the messages author.
enum:
- system
- user
- assistant
default: user
content:
type: string
description: The contents of the user message.
output:
type: object
properties:
content:
type: string
description: The assistant's reply.
tasks:
- name: chat
type: call
input:
loader: llmflow
task: llm_azure_openai
input:
api_version: 2023-07-01-preview
deployment_name: YOUR_DEPLOYMENT_NAME
messages: ${input.messages}
resource_name: YOUR_RESOURCE_NAME
tools:
- name: create_project
description: Create a project with the given name and visibility.
parameters:
type: object
required:
- name
properties:
name:
type: string
description: The project name.
visibility:
type: string
description: The project visibility
enum:
- public
- private
- name: delete_project
description: Delete a project
parameters:
type: object
required:
- name
properties:
name:
type: string
description: The project name to delete.
- name: need_to_call_tool
type: decision
input:
expression: ${str(bool(chat.tool_calls))}
cases:
"True":
name: switch
type: serial
input:
tasks:
- name: extract_tool
type: code
input:
code: |-
def _(env):
call = env.chat.tool_calls[0].function
return dict(
name=call.name,
arguments=jsondecode(call.arguments),
)
- name: validate_tool
type: code
input:
code: |-
# Here we use a Code task just for demonstration purposes.
# Typically, you should use an HTTP task to access the service provided by the tool vendor.
def _(env):
tool = env.extract_tool
args = tool.arguments
arg_name = args.get('name', '') if args else ''
arg_visibility = args.get('visibility', '') if args else ''
if tool.name == 'create_project':
if not arg_name.isalnum():
return 'Please provide the name.'
if arg_visibility not in ('public', 'private'):
return 'Please provide the visibility (public or private).'
elif tool.name == 'delete_project':
if not arg_name.isalnum():
return 'Please provide the name.'
else:
return 'Sorry, operation not supported!'
return ''
- name: is_valid
type: decision
input:
expression: ${str(validate_tool.result == '')}
cases:
"True":
name: switch
type: serial
input:
tasks:
- name: has_confirm_message
type: decision
input:
expression: ${str(len(input.messages) > 1 and
input.messages[-2].content.endswith('Are you
sure?'))}
cases:
"True":
name: switch
type: serial
input:
tasks:
- name: analyze_prompt
type: template
input:
args:
query: ${input.messages[-1]}
template: |-
Based on the input, determine whether it's positive, negative or other.
Example 1
Input: Okay
Output: positive
Example 2
Input: Forget it
Output: negative
Example 3
Input: Create a project
Output: other
Example 4
Input: Change the name, please
Output: other
Example 5
Input: No problem
Output: positive
Example 6
Input: What did you say?
Output: other
Current conversation
Input: {{$.query}}
Output:
- name: analyze_llm
type: call
input:
loader: llmflow
task: llm_azure_openai
input:
api_version: 2023-07-01-preview
deployment_name: YOUR_DEPLOYMENT_NAME
messages:
- role: user
content: ${analyze_prompt.result}
resource_name: YOUR_RESOURCE_NAME
- name: user_sentiment
type: decision
input:
expression: ${analyze_llm.content}
cases:
positive:
name: switch
type: serial
input:
tasks:
- name: call_tool
type: code
input:
code: |-
# Here we use a Code task just for demonstration purposes.
# Typically, you should use an HTTP task to access the service provided by the tool vendor.
def _(env):
tool = env.extract_tool.result
op_name = 'created' if tool.name == 'create_project' else 'deleted'
proj_name = tool.arguments.name
return 'The project `%s` has been %s for you!' % (proj_name, op_name)
- name: finish
type: terminate
input:
output:
content: ${call_tool.result}
negative:
name: cancel
type: terminate
input:
output:
content: Operation canceled. Let me know if you need any further assistance.
- name: build_confirm_message
type: code
input:
code: |-
def _(env):
tool = env.extract_tool
args = dict(
name=tool.name.removesuffix('_project'),
arguments=jsonencode(tool.arguments),
)
return '''About to {name} a project for you, with the following arguments:
```json
{arguments}
```
Are you sure?'''.format(**args)
- name: ask_user_to_confirm
type: terminate
input:
output:
content: ${build_confirm_message.result}
"False":
name: ask_user_to_correct
type: terminate
input:
output:
content: ${validate_tool.result}
"False":
name: reply
type: terminate
input:
output:
content: ${chat.content}