-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathclient.py
executable file
·276 lines (241 loc) · 8.81 KB
/
client.py
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/usr/bin/python
# Copyright 2024, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of NVIDIA CORPORATION nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import argparse
import json
import sys
import client_utils
import numpy as np
import tritonclient.grpc as grpcclient
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"-v",
"--verbose",
action="store_true",
required=False,
default=False,
help="Enable verbose output",
)
parser.add_argument(
"-u", "--url", type=str, required=False, help="Inference server URL."
)
parser.add_argument("-p", "--prompt", type=str, required=True, help="Input prompt.")
parser.add_argument(
"--model-name",
type=str,
required=False,
default="ensemble",
choices=["ensemble", "tensorrt_llm_bls"],
help="Name of the Triton model to send request to",
)
parser.add_argument(
"-S",
"--streaming",
action="store_true",
required=False,
default=False,
help="Enable streaming mode. Default is False.",
)
parser.add_argument(
"-b",
"--beam-width",
required=False,
type=int,
default=1,
help="Beam width value",
)
parser.add_argument(
"--temperature",
type=float,
required=False,
default=1.0,
help="temperature value",
)
parser.add_argument(
"--repetition-penalty",
type=float,
required=False,
default=None,
help="The repetition penalty value",
)
parser.add_argument(
"--presence-penalty",
type=float,
required=False,
default=None,
help="The presence penalty value",
)
parser.add_argument(
"--frequency-penalty",
type=float,
required=False,
default=None,
help="The frequency penalty value",
)
parser.add_argument(
"-o",
"--output-len",
type=int,
default=100,
required=False,
help="Specify output length",
)
parser.add_argument(
"--request-id",
type=str,
default="",
required=False,
help="The request_id for the stop request",
)
parser.add_argument("--stop-words", nargs="+", default=[], help="The stop words")
parser.add_argument("--bad-words", nargs="+", default=[], help="The bad words")
parser.add_argument(
"--embedding-bias-words", nargs="+", default=[], help="The biased words"
)
parser.add_argument(
"--embedding-bias-weights",
nargs="+",
default=[],
help="The biased words weights",
)
parser.add_argument(
"--overwrite-output-text",
action="store_true",
required=False,
default=False,
help="In streaming mode, overwrite previously received output text instead of appending to it",
)
parser.add_argument(
"--return-context-logits",
action="store_true",
required=False,
default=False,
help="Return context logits, the engine must be built with gather_context_logits or gather_all_token_logits",
)
parser.add_argument(
"--return-generation-logits",
action="store_true",
required=False,
default=False,
help="Return generation logits, the engine must be built with gather_ generation_logits or gather_all_token_logits",
)
parser.add_argument(
"--end-id", type=int, required=False, help="The token id for end token."
)
parser.add_argument(
"--pad-id", type=int, required=False, help="The token id for pad token."
)
FLAGS = parser.parse_args()
if FLAGS.url is None:
FLAGS.url = "localhost:8001"
embedding_bias_words = (
FLAGS.embedding_bias_words if FLAGS.embedding_bias_words else None
)
embedding_bias_weights = (
FLAGS.embedding_bias_weights if FLAGS.embedding_bias_weights else None
)
try:
client = grpcclient.InferenceServerClient(url=FLAGS.url)
except Exception as e:
print("client creation failed: " + str(e))
sys.exit(1)
return_context_logits_data = None
if FLAGS.return_context_logits:
return_context_logits_data = np.array(
[[FLAGS.return_context_logits]], dtype=bool
)
return_generation_logits_data = None
if FLAGS.return_generation_logits:
return_generation_logits_data = np.array(
[[FLAGS.return_generation_logits]], dtype=bool
)
prompt = client_utils.process_prompt(FLAGS.prompt)
functions = client_utils.MyFunctions()
while True:
output_text = client_utils.run_inference(
client,
prompt,
FLAGS.output_len,
FLAGS.request_id,
FLAGS.repetition_penalty,
FLAGS.presence_penalty,
FLAGS.frequency_penalty,
FLAGS.temperature,
FLAGS.stop_words,
FLAGS.bad_words,
embedding_bias_words,
embedding_bias_weights,
FLAGS.model_name,
FLAGS.streaming,
FLAGS.beam_width,
FLAGS.overwrite_output_text,
return_context_logits_data,
return_generation_logits_data,
FLAGS.end_id,
FLAGS.pad_id,
FLAGS.verbose,
)
try:
response = json.loads(output_text)
except ValueError:
print("\n[ERROR] LLM responded with invalid JSON format!")
break
# Repeat the loop until `final_answer` tool is called, which indicates
# that the full response is ready and llm does not require any
# additional information. Additionally, if the loop has taken more
# than 50 steps, the script ends.
if response["tool"] == "final_answer" or response["step"] == "50":
if response["tool"] == "final_answer":
final_response = response["arguments"]["final_response"]
print("\n\n+++++++++++++++++++++++++++++++++++++")
print(f"RESPONSE: {final_response}")
print("+++++++++++++++++++++++++++++++++++++\n\n")
elif response["step"] == "50":
print("\n\n+++++++++++++++++++++++++++++++++++++")
print(f"Reached maximum number of function calls available.")
print("+++++++++++++++++++++++++++++++++++++\n\n")
break
# Extract tool's name and arguments from the response
function_name = response["tool"]
function_args = response["arguments"]
function_to_call = getattr(functions, function_name)
# Execute function call and store results in `function_response`
function_response = function_to_call(*function_args.values())
if FLAGS.verbose:
print("=====================================")
print(f"Executing function: {function_name}({function_args}) ")
print(f"Function response: {str(function_response)}")
print("=====================================")
# Update prompt with the generated function call and results of that
# call.
results_dict = f'{{"name": "{function_name}", "content": {function_response}}}'
prompt += str(
output_text
+ "<|im_end|>\n<tool_response>"
+ str(results_dict)
+ "</tool_response>\n<|im_start|>assistant"
)