Spaces:
Running
Running
maxiaolong03
commited on
Commit
·
1eb802e
1
Parent(s):
8037955
add files
Browse files- app.py +130 -32
- bot_requests.py +88 -77
app.py
CHANGED
|
@@ -102,14 +102,25 @@ def get_args() -> argparse.Namespace:
|
|
| 102 |
"""
|
| 103 |
parser = ArgumentParser(description="ERNIE models web chat demo.")
|
| 104 |
|
| 105 |
-
parser.add_argument(
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
parser.add_argument(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
parser.add_argument(
|
| 110 |
"--model_map",
|
| 111 |
type=str,
|
| 112 |
-
default=
|
| 113 |
help="""JSON string defining model name to endpoint mappings.
|
| 114 |
Required Format:
|
| 115 |
{"ERNIE-4.5-VL": "http://localhost:port/v1"}
|
|
@@ -128,10 +139,22 @@ def get_args() -> argparse.Namespace:
|
|
| 128 |
help="Web Search Service URL.",
|
| 129 |
)
|
| 130 |
parser.add_argument(
|
| 131 |
-
"--qianfan_api_key",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
)
|
| 133 |
parser.add_argument(
|
| 134 |
-
"--
|
| 135 |
)
|
| 136 |
|
| 137 |
args = parser.parse_args()
|
|
@@ -158,7 +181,9 @@ class GradioEvents:
|
|
| 158 |
"""
|
| 159 |
|
| 160 |
@staticmethod
|
| 161 |
-
def get_history_conversation(
|
|
|
|
|
|
|
| 162 |
"""
|
| 163 |
Constructs complete conversation history from stored components including text messages,
|
| 164 |
attached files and images. Processes each dialogue turn by combining the raw query/response
|
|
@@ -185,7 +210,12 @@ class GradioEvents:
|
|
| 185 |
if idx in image_history:
|
| 186 |
content = []
|
| 187 |
for image_url in image_history[idx]:
|
| 188 |
-
content.append(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
content.append({"type": "text", "text": query_h})
|
| 190 |
conversation.append({"role": "user", "content": content})
|
| 191 |
else:
|
|
@@ -194,7 +224,9 @@ class GradioEvents:
|
|
| 194 |
return conversation, conversation_str
|
| 195 |
|
| 196 |
@staticmethod
|
| 197 |
-
def get_search_query(
|
|
|
|
|
|
|
| 198 |
"""
|
| 199 |
Processes conversation history to generate search queries by sending the conversation context
|
| 200 |
to the model and parsing its JSON response. Handles model output validation and extracts
|
|
@@ -262,9 +294,12 @@ class GradioEvents:
|
|
| 262 |
for file_url in files_url:
|
| 263 |
extionsion = "." + file_url.split(".")[-1]
|
| 264 |
if extionsion in TEXT_FILE_TYPE and (
|
| 265 |
-
len(file_history) == 0
|
|
|
|
| 266 |
):
|
| 267 |
-
file_history[diologue_turn] = file_history.get(diologue_turn, []) + [
|
|
|
|
|
|
|
| 268 |
file_name = file_url.split("/")[-1]
|
| 269 |
file_contents_words = bot_client.cut_chinese_english(file_contents)
|
| 270 |
|
|
@@ -275,14 +310,25 @@ class GradioEvents:
|
|
| 275 |
+ f"用户上传\n{file_name}\n{GradioEvents.get_file_text(file_url)}\n"
|
| 276 |
)
|
| 277 |
file_content_words = bot_client.cut_chinese_english(file_content)
|
| 278 |
-
max_char = min(
|
|
|
|
|
|
|
|
|
|
| 279 |
file_content_words = file_content_words[:max_char]
|
| 280 |
file_contents += "".join(file_content_words) + "\n"
|
| 281 |
elif extionsion in IMAGE_FILE_TYPE and (
|
| 282 |
-
len(image_history) == 0
|
|
|
|
| 283 |
):
|
| 284 |
-
image_history[diologue_turn] = image_history.get(diologue_turn, []) + [
|
| 285 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 286 |
return input_content, file_contents, ref_file_num
|
| 287 |
|
| 288 |
@staticmethod
|
|
@@ -328,31 +374,50 @@ class GradioEvents:
|
|
| 328 |
search_info_res = {}
|
| 329 |
if search_state:
|
| 330 |
search_info_message = SEARCH_INFO_PROMPT.format(
|
| 331 |
-
date=datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
|
|
|
|
|
|
| 332 |
)
|
| 333 |
search_conversation = [{"role": "user", "content": search_info_message}]
|
| 334 |
-
search_info_res = GradioEvents.get_search_query(
|
|
|
|
|
|
|
| 335 |
if search_info_res is None:
|
| 336 |
search_info_res = {"is_search": True, "query_list": [query]}
|
| 337 |
|
| 338 |
# Process files
|
| 339 |
diologue_turn = len(task_history)
|
| 340 |
-
if search_info_res.get("is_search", False) and search_info_res.get(
|
|
|
|
|
|
|
| 341 |
max_file_char = max_ref_char // 2
|
| 342 |
else:
|
| 343 |
max_file_char = max_ref_char
|
| 344 |
input_content, file_contents, ref_file_num = GradioEvents.process_files(
|
| 345 |
-
diologue_turn,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 346 |
)
|
| 347 |
|
| 348 |
# Step 2: If a search is needed, obtain the corresponding query results
|
| 349 |
-
if search_info_res.get("is_search", False) and search_info_res.get(
|
|
|
|
|
|
|
| 350 |
yield {"type": "search_result", "content": "🧐 努力搜索中... ✨"}
|
| 351 |
search_result = bot_client.get_web_search_res(search_info_res["query_list"])
|
| 352 |
|
| 353 |
-
max_search_result_char = max_ref_char - len(
|
|
|
|
|
|
|
| 354 |
complete_search_result = await GradioEvents.get_complete_search_content(
|
| 355 |
-
ref_file_num,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 356 |
)
|
| 357 |
complete_ref = file_contents + complete_search_result
|
| 358 |
|
|
@@ -559,7 +624,15 @@ class GradioEvents:
|
|
| 559 |
GradioEvents.gc()
|
| 560 |
|
| 561 |
reset_result = namedtuple(
|
| 562 |
-
"reset_result",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 563 |
)
|
| 564 |
return reset_result(
|
| 565 |
[], # clear chatbot
|
|
@@ -757,7 +830,10 @@ class GradioEvents:
|
|
| 757 |
search_res_words = search_res_words[:max_char]
|
| 758 |
item_text = "".join(search_res_words)
|
| 759 |
|
| 760 |
-
results.append(
|
|
|
|
|
|
|
|
|
|
| 761 |
|
| 762 |
return "".join(results)
|
| 763 |
|
|
@@ -819,15 +895,21 @@ def launch_demo(args: argparse.Namespace, bot_client: BotClient):
|
|
| 819 |
<a href="https://yiyan.baidu.com/blog/publication/">Technical Report</a></center>"""
|
| 820 |
)
|
| 821 |
|
| 822 |
-
chatbot = gr.Chatbot(
|
|
|
|
|
|
|
| 823 |
|
| 824 |
-
search_result = gr.Textbox(
|
|
|
|
|
|
|
| 825 |
|
| 826 |
with gr.Row():
|
| 827 |
search_check = gr.Checkbox(label="🌐 Search the web(联网搜索)")
|
| 828 |
|
| 829 |
with gr.Row():
|
| 830 |
-
query = gr.Textbox(
|
|
|
|
|
|
|
| 831 |
file_btn = gr.File(
|
| 832 |
label="File upload (Accepted formats: PNG, JPEG, JPG, PDF, TXT, MD, DOC, DOCX)",
|
| 833 |
scale=4,
|
|
@@ -847,10 +929,16 @@ def launch_demo(args: argparse.Namespace, bot_client: BotClient):
|
|
| 847 |
model_name = gr.State(next(iter(args.model_map.keys())))
|
| 848 |
max_crawler_threads = gr.State(args.max_crawler_threads)
|
| 849 |
|
| 850 |
-
search_check.change(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 851 |
|
| 852 |
predict_with_clients = partial(GradioEvents.predict, bot_client=bot_client)
|
| 853 |
-
regenerate_with_clients = partial(
|
|
|
|
|
|
|
| 854 |
query.submit(
|
| 855 |
predict_with_clients,
|
| 856 |
inputs=[
|
|
@@ -887,7 +975,14 @@ def launch_demo(args: argparse.Namespace, bot_client: BotClient):
|
|
| 887 |
submit_btn.click(GradioEvents.reset_user_input, [], [query])
|
| 888 |
empty_btn.click(
|
| 889 |
GradioEvents.reset_state,
|
| 890 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 891 |
show_progress=True,
|
| 892 |
)
|
| 893 |
regen_btn.click(
|
|
@@ -906,7 +1001,10 @@ def launch_demo(args: argparse.Namespace, bot_client: BotClient):
|
|
| 906 |
show_progress=True,
|
| 907 |
)
|
| 908 |
|
| 909 |
-
demo.queue(
|
|
|
|
|
|
|
|
|
|
| 910 |
|
| 911 |
|
| 912 |
def main():
|
|
|
|
| 102 |
"""
|
| 103 |
parser = ArgumentParser(description="ERNIE models web chat demo.")
|
| 104 |
|
| 105 |
+
parser.add_argument(
|
| 106 |
+
"--server-port", type=int, default=7860, help="Demo server port."
|
| 107 |
+
)
|
| 108 |
+
parser.add_argument(
|
| 109 |
+
"--server-name", type=str, default="0.0.0.0", help="Demo server name."
|
| 110 |
+
)
|
| 111 |
+
parser.add_argument(
|
| 112 |
+
"--max_char",
|
| 113 |
+
type=int,
|
| 114 |
+
default=20000,
|
| 115 |
+
help="Maximum character limit for messages.",
|
| 116 |
+
)
|
| 117 |
+
parser.add_argument(
|
| 118 |
+
"--max_retry_num", type=int, default=3, help="Maximum retry number for request."
|
| 119 |
+
)
|
| 120 |
parser.add_argument(
|
| 121 |
"--model_map",
|
| 122 |
type=str,
|
| 123 |
+
default='{"ernie-4.5-turbo-vl-preview": "https://qianfan.baidubce.com/v2"}',
|
| 124 |
help="""JSON string defining model name to endpoint mappings.
|
| 125 |
Required Format:
|
| 126 |
{"ERNIE-4.5-VL": "http://localhost:port/v1"}
|
|
|
|
| 139 |
help="Web Search Service URL.",
|
| 140 |
)
|
| 141 |
parser.add_argument(
|
| 142 |
+
"--qianfan_api_key",
|
| 143 |
+
type=str,
|
| 144 |
+
default=os.environ.get('API_SEARCH_KEY'),
|
| 145 |
+
help="Web Search Service API key.",
|
| 146 |
+
)
|
| 147 |
+
parser.add_argument(
|
| 148 |
+
"--max_crawler_threads",
|
| 149 |
+
type=int,
|
| 150 |
+
default=10,
|
| 151 |
+
help="The maximum number of concurrent crawler threads.",
|
| 152 |
+
)
|
| 153 |
+
parser.add_argument(
|
| 154 |
+
"--concurrency_limit", type=int, default=10, help="Default concurrency limit."
|
| 155 |
)
|
| 156 |
parser.add_argument(
|
| 157 |
+
"--max_queue_size", type=int, default=50, help="Maximum queue size for request."
|
| 158 |
)
|
| 159 |
|
| 160 |
args = parser.parse_args()
|
|
|
|
| 181 |
"""
|
| 182 |
|
| 183 |
@staticmethod
|
| 184 |
+
def get_history_conversation(
|
| 185 |
+
task_history: list, image_history: dict, file_history: dict
|
| 186 |
+
) -> tuple:
|
| 187 |
"""
|
| 188 |
Constructs complete conversation history from stored components including text messages,
|
| 189 |
attached files and images. Processes each dialogue turn by combining the raw query/response
|
|
|
|
| 210 |
if idx in image_history:
|
| 211 |
content = []
|
| 212 |
for image_url in image_history[idx]:
|
| 213 |
+
content.append(
|
| 214 |
+
{
|
| 215 |
+
"type": "image_url",
|
| 216 |
+
"image_url": {"url": GradioEvents.get_image_url(image_url)},
|
| 217 |
+
}
|
| 218 |
+
)
|
| 219 |
content.append({"type": "text", "text": query_h})
|
| 220 |
conversation.append({"role": "user", "content": content})
|
| 221 |
else:
|
|
|
|
| 224 |
return conversation, conversation_str
|
| 225 |
|
| 226 |
@staticmethod
|
| 227 |
+
def get_search_query(
|
| 228 |
+
conversation: list, model_name: str, bot_client: BotClient
|
| 229 |
+
) -> list:
|
| 230 |
"""
|
| 231 |
Processes conversation history to generate search queries by sending the conversation context
|
| 232 |
to the model and parsing its JSON response. Handles model output validation and extracts
|
|
|
|
| 294 |
for file_url in files_url:
|
| 295 |
extionsion = "." + file_url.split(".")[-1]
|
| 296 |
if extionsion in TEXT_FILE_TYPE and (
|
| 297 |
+
len(file_history) == 0
|
| 298 |
+
or file_url not in list(file_history.values())[-1]
|
| 299 |
):
|
| 300 |
+
file_history[diologue_turn] = file_history.get(diologue_turn, []) + [
|
| 301 |
+
file_url
|
| 302 |
+
]
|
| 303 |
file_name = file_url.split("/")[-1]
|
| 304 |
file_contents_words = bot_client.cut_chinese_english(file_contents)
|
| 305 |
|
|
|
|
| 310 |
+ f"用户上传\n{file_name}\n{GradioEvents.get_file_text(file_url)}\n"
|
| 311 |
)
|
| 312 |
file_content_words = bot_client.cut_chinese_english(file_content)
|
| 313 |
+
max_char = min(
|
| 314 |
+
len(file_content_words),
|
| 315 |
+
max_file_char - len(file_contents_words),
|
| 316 |
+
)
|
| 317 |
file_content_words = file_content_words[:max_char]
|
| 318 |
file_contents += "".join(file_content_words) + "\n"
|
| 319 |
elif extionsion in IMAGE_FILE_TYPE and (
|
| 320 |
+
len(image_history) == 0
|
| 321 |
+
or file_url not in list(image_history.values())[-1]
|
| 322 |
):
|
| 323 |
+
image_history[diologue_turn] = image_history.get(diologue_turn, []) + [
|
| 324 |
+
file_url
|
| 325 |
+
]
|
| 326 |
+
input_content.append(
|
| 327 |
+
{
|
| 328 |
+
"type": "image_url",
|
| 329 |
+
"image_url": {"url": GradioEvents.get_image_url(file_url)},
|
| 330 |
+
}
|
| 331 |
+
)
|
| 332 |
return input_content, file_contents, ref_file_num
|
| 333 |
|
| 334 |
@staticmethod
|
|
|
|
| 374 |
search_info_res = {}
|
| 375 |
if search_state:
|
| 376 |
search_info_message = SEARCH_INFO_PROMPT.format(
|
| 377 |
+
date=datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
| 378 |
+
context=conversation_str,
|
| 379 |
+
query=query,
|
| 380 |
)
|
| 381 |
search_conversation = [{"role": "user", "content": search_info_message}]
|
| 382 |
+
search_info_res = GradioEvents.get_search_query(
|
| 383 |
+
search_conversation, model_name, bot_client
|
| 384 |
+
)
|
| 385 |
if search_info_res is None:
|
| 386 |
search_info_res = {"is_search": True, "query_list": [query]}
|
| 387 |
|
| 388 |
# Process files
|
| 389 |
diologue_turn = len(task_history)
|
| 390 |
+
if search_info_res.get("is_search", False) and search_info_res.get(
|
| 391 |
+
"query_list", []
|
| 392 |
+
):
|
| 393 |
max_file_char = max_ref_char // 2
|
| 394 |
else:
|
| 395 |
max_file_char = max_ref_char
|
| 396 |
input_content, file_contents, ref_file_num = GradioEvents.process_files(
|
| 397 |
+
diologue_turn,
|
| 398 |
+
files_url,
|
| 399 |
+
file_history,
|
| 400 |
+
image_history,
|
| 401 |
+
bot_client,
|
| 402 |
+
max_file_char,
|
| 403 |
)
|
| 404 |
|
| 405 |
# Step 2: If a search is needed, obtain the corresponding query results
|
| 406 |
+
if search_info_res.get("is_search", False) and search_info_res.get(
|
| 407 |
+
"query_list", []
|
| 408 |
+
):
|
| 409 |
yield {"type": "search_result", "content": "🧐 努力搜索中... ✨"}
|
| 410 |
search_result = bot_client.get_web_search_res(search_info_res["query_list"])
|
| 411 |
|
| 412 |
+
max_search_result_char = max_ref_char - len(
|
| 413 |
+
bot_client.cut_chinese_english(file_contents)
|
| 414 |
+
)
|
| 415 |
complete_search_result = await GradioEvents.get_complete_search_content(
|
| 416 |
+
ref_file_num,
|
| 417 |
+
search_result,
|
| 418 |
+
max_crawler_threads,
|
| 419 |
+
bot_client,
|
| 420 |
+
max_search_result_char,
|
| 421 |
)
|
| 422 |
complete_ref = file_contents + complete_search_result
|
| 423 |
|
|
|
|
| 624 |
GradioEvents.gc()
|
| 625 |
|
| 626 |
reset_result = namedtuple(
|
| 627 |
+
"reset_result",
|
| 628 |
+
[
|
| 629 |
+
"chatbot",
|
| 630 |
+
"task_history",
|
| 631 |
+
"image_history",
|
| 632 |
+
"file_history",
|
| 633 |
+
"file_btn",
|
| 634 |
+
"search_result",
|
| 635 |
+
],
|
| 636 |
)
|
| 637 |
return reset_result(
|
| 638 |
[], # clear chatbot
|
|
|
|
| 830 |
search_res_words = search_res_words[:max_char]
|
| 831 |
item_text = "".join(search_res_words)
|
| 832 |
|
| 833 |
+
results.append(
|
| 834 |
+
f"\n参考资料[{len(results) + 1 + ref_file_num}]:\n"
|
| 835 |
+
+ f"资料来源:素材检索\n{item_text}\n"
|
| 836 |
+
)
|
| 837 |
|
| 838 |
return "".join(results)
|
| 839 |
|
|
|
|
| 895 |
<a href="https://yiyan.baidu.com/blog/publication/">Technical Report</a></center>"""
|
| 896 |
)
|
| 897 |
|
| 898 |
+
chatbot = gr.Chatbot(
|
| 899 |
+
label="ERNIE", elem_classes="control-height", type="messages"
|
| 900 |
+
)
|
| 901 |
|
| 902 |
+
search_result = gr.Textbox(
|
| 903 |
+
label="Search Result", lines=10, max_lines=10, visible=False
|
| 904 |
+
)
|
| 905 |
|
| 906 |
with gr.Row():
|
| 907 |
search_check = gr.Checkbox(label="🌐 Search the web(联网搜索)")
|
| 908 |
|
| 909 |
with gr.Row():
|
| 910 |
+
query = gr.Textbox(
|
| 911 |
+
label="Input", lines=1, scale=6, elem_classes="input-textbox"
|
| 912 |
+
)
|
| 913 |
file_btn = gr.File(
|
| 914 |
label="File upload (Accepted formats: PNG, JPEG, JPG, PDF, TXT, MD, DOC, DOCX)",
|
| 915 |
scale=4,
|
|
|
|
| 929 |
model_name = gr.State(next(iter(args.model_map.keys())))
|
| 930 |
max_crawler_threads = gr.State(args.max_crawler_threads)
|
| 931 |
|
| 932 |
+
search_check.change(
|
| 933 |
+
fn=GradioEvents.search_toggle_state,
|
| 934 |
+
inputs=search_check,
|
| 935 |
+
outputs=search_result,
|
| 936 |
+
)
|
| 937 |
|
| 938 |
predict_with_clients = partial(GradioEvents.predict, bot_client=bot_client)
|
| 939 |
+
regenerate_with_clients = partial(
|
| 940 |
+
GradioEvents.regenerate, bot_client=bot_client
|
| 941 |
+
)
|
| 942 |
query.submit(
|
| 943 |
predict_with_clients,
|
| 944 |
inputs=[
|
|
|
|
| 975 |
submit_btn.click(GradioEvents.reset_user_input, [], [query])
|
| 976 |
empty_btn.click(
|
| 977 |
GradioEvents.reset_state,
|
| 978 |
+
outputs=[
|
| 979 |
+
chatbot,
|
| 980 |
+
task_history,
|
| 981 |
+
image_history,
|
| 982 |
+
file_history,
|
| 983 |
+
file_btn,
|
| 984 |
+
search_result,
|
| 985 |
+
],
|
| 986 |
show_progress=True,
|
| 987 |
)
|
| 988 |
regen_btn.click(
|
|
|
|
| 1001 |
show_progress=True,
|
| 1002 |
)
|
| 1003 |
|
| 1004 |
+
demo.queue(
|
| 1005 |
+
default_concurrency_limit=args.concurrency_limit, max_size=args.max_queue_size
|
| 1006 |
+
)
|
| 1007 |
+
demo.launch(server_port=args.server_port, server_name=args.server_name)
|
| 1008 |
|
| 1009 |
|
| 1010 |
def main():
|
bot_requests.py
CHANGED
|
@@ -16,20 +16,22 @@
|
|
| 16 |
|
| 17 |
import os
|
| 18 |
import argparse
|
|
|
|
| 19 |
import logging
|
| 20 |
import traceback
|
| 21 |
-
|
| 22 |
import jieba
|
|
|
|
| 23 |
from openai import OpenAI
|
| 24 |
|
| 25 |
-
import requests
|
| 26 |
|
| 27 |
-
class BotClient
|
| 28 |
"""Client for interacting with various AI models."""
|
|
|
|
| 29 |
def __init__(self, args: argparse.Namespace):
|
| 30 |
"""
|
| 31 |
-
Initializes the BotClient instance by configuring essential parameters from command line arguments
|
| 32 |
-
including retry limits, character constraints, model endpoints and API credentials while setting up
|
| 33 |
default values for missing arguments to ensure robust operation.
|
| 34 |
|
| 35 |
Args:
|
|
@@ -37,25 +39,29 @@ class BotClient(object):
|
|
| 37 |
Uses getattr() to safely retrieve values with fallback defaults.
|
| 38 |
"""
|
| 39 |
self.logger = logging.getLogger(__name__)
|
| 40 |
-
|
| 41 |
-
self.max_retry_num = getattr(args, 'max_retry_num', 3)
|
| 42 |
-
self.max_char = getattr(args, 'max_char', 8000)
|
| 43 |
|
| 44 |
-
self.
|
|
|
|
|
|
|
|
|
|
| 45 |
self.api_key = os.environ.get('API_KEY')
|
| 46 |
|
| 47 |
-
self.embedding_service_url = getattr(
|
| 48 |
-
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
self.web_search_service_url = getattr(
|
| 51 |
-
|
|
|
|
|
|
|
| 52 |
|
| 53 |
self.qianfan_api_key = os.environ.get('API_SEARCH_KEY')
|
| 54 |
|
| 55 |
def call_back(self, host_url: str, req_data: dict) -> dict:
|
| 56 |
"""
|
| 57 |
-
Executes an HTTP request to the specified endpoint using the OpenAI client, handles the response
|
| 58 |
-
conversion to a compatible dictionary format, and manages any exceptions that may occur during
|
| 59 |
the request process while logging errors appropriately.
|
| 60 |
|
| 61 |
Args:
|
|
@@ -68,20 +74,18 @@ class BotClient(object):
|
|
| 68 |
"""
|
| 69 |
try:
|
| 70 |
client = OpenAI(base_url=host_url, api_key=self.api_key)
|
| 71 |
-
response = client.chat.completions.create(
|
| 72 |
-
|
| 73 |
-
)
|
| 74 |
-
|
| 75 |
# Convert OpenAI response to compatible format
|
| 76 |
return response.model_dump()
|
| 77 |
|
| 78 |
except Exception as e:
|
| 79 |
-
self.logger.error("Stream request failed: {}"
|
| 80 |
raise
|
| 81 |
|
| 82 |
def call_back_stream(self, host_url: str, req_data: dict) -> dict:
|
| 83 |
"""
|
| 84 |
-
Makes a streaming HTTP request to the specified host URL using the OpenAI client and yields response chunks
|
| 85 |
in real-time while handling any exceptions that may occur during the streaming process.
|
| 86 |
|
| 87 |
Args:
|
|
@@ -100,25 +104,25 @@ class BotClient(object):
|
|
| 100 |
for chunk in response:
|
| 101 |
if not chunk.choices:
|
| 102 |
continue
|
| 103 |
-
|
| 104 |
# Convert OpenAI response to compatible format
|
| 105 |
yield chunk.model_dump()
|
| 106 |
|
| 107 |
except Exception as e:
|
| 108 |
-
self.logger.error("Stream request failed: {}"
|
| 109 |
raise
|
| 110 |
|
| 111 |
def process(
|
| 112 |
-
self,
|
| 113 |
-
model_name: str,
|
| 114 |
-
req_data: dict,
|
| 115 |
-
max_tokens: int=2048,
|
| 116 |
-
temperature: float=1.0,
|
| 117 |
-
top_p: float=0.7
|
| 118 |
) -> dict:
|
| 119 |
"""
|
| 120 |
-
Handles chat completion requests by mapping the model name to its endpoint, preparing request parameters
|
| 121 |
-
including token limits and sampling settings, truncating messages to fit character limits, making API calls
|
| 122 |
with built-in retry mechanism, and logging the full request/response cycle for debugging purposes.
|
| 123 |
|
| 124 |
Args:
|
|
@@ -140,7 +144,7 @@ class BotClient(object):
|
|
| 140 |
req_data["messages"] = self.truncate_messages(req_data["messages"])
|
| 141 |
for _ in range(self.max_retry_num):
|
| 142 |
try:
|
| 143 |
-
self.logger.info("[MODEL] {}"
|
| 144 |
self.logger.info("[req_data]====>")
|
| 145 |
self.logger.info(json.dumps(req_data, ensure_ascii=False))
|
| 146 |
res = self.call_back(model_url, req_data)
|
|
@@ -153,15 +157,16 @@ class BotClient(object):
|
|
| 153 |
res = {}
|
| 154 |
if len(res) != 0 and "error" not in res:
|
| 155 |
break
|
| 156 |
-
|
| 157 |
return res
|
| 158 |
|
| 159 |
def process_stream(
|
| 160 |
-
self,
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
|
|
|
| 165 |
) -> dict:
|
| 166 |
"""
|
| 167 |
Processes streaming requests by mapping the model name to its endpoint, configuring request parameters,
|
|
@@ -184,29 +189,30 @@ class BotClient(object):
|
|
| 184 |
req_data["temperature"] = temperature
|
| 185 |
req_data["top_p"] = top_p
|
| 186 |
req_data["messages"] = self.truncate_messages(req_data["messages"])
|
| 187 |
-
|
| 188 |
last_error = None
|
| 189 |
for _ in range(self.max_retry_num):
|
| 190 |
try:
|
| 191 |
-
self.logger.info("[MODEL] {}"
|
| 192 |
self.logger.info("[req_data]====>")
|
| 193 |
self.logger.info(json.dumps(req_data, ensure_ascii=False))
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
yield chunk
|
| 197 |
return
|
| 198 |
-
|
| 199 |
except Exception as e:
|
| 200 |
last_error = e
|
| 201 |
-
self.logger.error(
|
| 202 |
-
|
|
|
|
|
|
|
| 203 |
self.logger.error("All retry attempts failed for stream request")
|
| 204 |
yield {"error": str(last_error)}
|
| 205 |
|
| 206 |
def cut_chinese_english(self, text: str) -> list:
|
| 207 |
"""
|
| 208 |
-
Segments mixed Chinese and English text into individual components using Jieba for Chinese words
|
| 209 |
-
while preserving English words as whole units, with special handling for Unicode character ranges
|
| 210 |
to distinguish between the two languages.
|
| 211 |
|
| 212 |
Args:
|
|
@@ -219,7 +225,9 @@ class BotClient(object):
|
|
| 219 |
en_ch_words = []
|
| 220 |
|
| 221 |
for word in words:
|
| 222 |
-
if word.isalpha() and not any(
|
|
|
|
|
|
|
| 223 |
en_ch_words.append(word)
|
| 224 |
else:
|
| 225 |
en_ch_words.extend(list(word))
|
|
@@ -239,10 +247,10 @@ class BotClient(object):
|
|
| 239 |
"""
|
| 240 |
if not messages:
|
| 241 |
return messages
|
| 242 |
-
|
| 243 |
processed = []
|
| 244 |
total_units = 0
|
| 245 |
-
|
| 246 |
for msg in messages:
|
| 247 |
# Handle two different content formats
|
| 248 |
if isinstance(msg["content"], str):
|
|
@@ -251,31 +259,33 @@ class BotClient(object):
|
|
| 251 |
text_content = msg["content"][1]["text"]
|
| 252 |
else:
|
| 253 |
text_content = ""
|
| 254 |
-
|
| 255 |
# Calculate unit count after tokenization
|
| 256 |
units = self.cut_chinese_english(text_content)
|
| 257 |
unit_count = len(units)
|
| 258 |
-
|
| 259 |
-
processed.append(
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
|
|
|
|
|
|
| 266 |
total_units += unit_count
|
| 267 |
-
|
| 268 |
if total_units <= self.max_char:
|
| 269 |
return messages
|
| 270 |
-
|
| 271 |
# Number of units to remove
|
| 272 |
to_remove = total_units - self.max_char
|
| 273 |
-
|
| 274 |
# 1. Truncate historical messages
|
| 275 |
for i in range(len(processed) - 1, 1):
|
| 276 |
if to_remove <= 0:
|
| 277 |
break
|
| 278 |
-
|
| 279 |
# current = processed[i]
|
| 280 |
if processed[i]["unit_count"] <= to_remove:
|
| 281 |
processed[i]["text_content"] = ""
|
|
@@ -293,7 +303,7 @@ class BotClient(object):
|
|
| 293 |
elif isinstance(processed[i]["original_content"], list):
|
| 294 |
processed[i]["original_content"][1]["text"] = new_text
|
| 295 |
to_remove = 0
|
| 296 |
-
|
| 297 |
# 2. Truncate system message
|
| 298 |
if to_remove > 0:
|
| 299 |
system_msg = processed[0]
|
|
@@ -313,7 +323,7 @@ class BotClient(object):
|
|
| 313 |
elif isinstance(processed[0]["original_content"], list):
|
| 314 |
processed[0]["original_content"][1]["text"] = new_text
|
| 315 |
to_remove = 0
|
| 316 |
-
|
| 317 |
# 3. Truncate last message
|
| 318 |
if to_remove > 0 and len(processed) > 1:
|
| 319 |
last_msg = processed[-1]
|
|
@@ -331,15 +341,12 @@ class BotClient(object):
|
|
| 331 |
last_msg["original_content"] = ""
|
| 332 |
elif isinstance(last_msg["original_content"], list):
|
| 333 |
last_msg["original_content"][1]["text"] = ""
|
| 334 |
-
|
| 335 |
result = []
|
| 336 |
for msg in processed:
|
| 337 |
if msg["text_content"]:
|
| 338 |
-
result.append({
|
| 339 |
-
|
| 340 |
-
"content": msg["original_content"]
|
| 341 |
-
})
|
| 342 |
-
|
| 343 |
return result
|
| 344 |
|
| 345 |
def embed_fn(self, text: str) -> list:
|
|
@@ -352,7 +359,9 @@ class BotClient(object):
|
|
| 352 |
Returns:
|
| 353 |
list: A list of floats representing the embedding.
|
| 354 |
"""
|
| 355 |
-
client = OpenAI(
|
|
|
|
|
|
|
| 356 |
response = client.embeddings.create(input=[text], model=self.embedding_model)
|
| 357 |
return response.data[0].embedding
|
| 358 |
|
|
@@ -368,7 +377,7 @@ class BotClient(object):
|
|
| 368 |
"""
|
| 369 |
headers = {
|
| 370 |
"Authorization": "Bearer " + self.qianfan_api_key,
|
| 371 |
-
"Content-Type": "application/json"
|
| 372 |
}
|
| 373 |
|
| 374 |
results = []
|
|
@@ -376,9 +385,11 @@ class BotClient(object):
|
|
| 376 |
for query in query_list:
|
| 377 |
payload = {
|
| 378 |
"messages": [{"role": "user", "content": query}],
|
| 379 |
-
"resource_type_filter": [{"type": "web", "top_k": top_k}]
|
| 380 |
}
|
| 381 |
-
response = requests.post(
|
|
|
|
|
|
|
| 382 |
|
| 383 |
if response.status_code == 200:
|
| 384 |
response = response.json()
|
|
@@ -387,4 +398,4 @@ class BotClient(object):
|
|
| 387 |
else:
|
| 388 |
self.logger.info(f"请求失败,状态码: {response.status_code}")
|
| 389 |
self.logger.info(response.text)
|
| 390 |
-
return results
|
|
|
|
| 16 |
|
| 17 |
import os
|
| 18 |
import argparse
|
| 19 |
+
import json
|
| 20 |
import logging
|
| 21 |
import traceback
|
| 22 |
+
|
| 23 |
import jieba
|
| 24 |
+
import requests
|
| 25 |
from openai import OpenAI
|
| 26 |
|
|
|
|
| 27 |
|
| 28 |
+
class BotClient:
|
| 29 |
"""Client for interacting with various AI models."""
|
| 30 |
+
|
| 31 |
def __init__(self, args: argparse.Namespace):
|
| 32 |
"""
|
| 33 |
+
Initializes the BotClient instance by configuring essential parameters from command line arguments
|
| 34 |
+
including retry limits, character constraints, model endpoints and API credentials while setting up
|
| 35 |
default values for missing arguments to ensure robust operation.
|
| 36 |
|
| 37 |
Args:
|
|
|
|
| 39 |
Uses getattr() to safely retrieve values with fallback defaults.
|
| 40 |
"""
|
| 41 |
self.logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
+
self.max_retry_num = getattr(args, "max_retry_num", 3)
|
| 44 |
+
self.max_char = getattr(args, "max_char", 8000)
|
| 45 |
+
|
| 46 |
+
self.model_map = getattr(args, "model_map", {})
|
| 47 |
self.api_key = os.environ.get('API_KEY')
|
| 48 |
|
| 49 |
+
self.embedding_service_url = getattr(
|
| 50 |
+
args, "embedding_service_url", "embedding_service_url"
|
| 51 |
+
)
|
| 52 |
+
self.embedding_model = getattr(args, "embedding_model", "embedding_model")
|
| 53 |
|
| 54 |
+
self.web_search_service_url = getattr(
|
| 55 |
+
args, "web_search_service_url", "web_search_service_url"
|
| 56 |
+
)
|
| 57 |
+
self.max_search_results_num = getattr(args, "max_search_results_num", 15)
|
| 58 |
|
| 59 |
self.qianfan_api_key = os.environ.get('API_SEARCH_KEY')
|
| 60 |
|
| 61 |
def call_back(self, host_url: str, req_data: dict) -> dict:
|
| 62 |
"""
|
| 63 |
+
Executes an HTTP request to the specified endpoint using the OpenAI client, handles the response
|
| 64 |
+
conversion to a compatible dictionary format, and manages any exceptions that may occur during
|
| 65 |
the request process while logging errors appropriately.
|
| 66 |
|
| 67 |
Args:
|
|
|
|
| 74 |
"""
|
| 75 |
try:
|
| 76 |
client = OpenAI(base_url=host_url, api_key=self.api_key)
|
| 77 |
+
response = client.chat.completions.create(**req_data)
|
| 78 |
+
|
|
|
|
|
|
|
| 79 |
# Convert OpenAI response to compatible format
|
| 80 |
return response.model_dump()
|
| 81 |
|
| 82 |
except Exception as e:
|
| 83 |
+
self.logger.error(f"Stream request failed: {e}")
|
| 84 |
raise
|
| 85 |
|
| 86 |
def call_back_stream(self, host_url: str, req_data: dict) -> dict:
|
| 87 |
"""
|
| 88 |
+
Makes a streaming HTTP request to the specified host URL using the OpenAI client and yields response chunks
|
| 89 |
in real-time while handling any exceptions that may occur during the streaming process.
|
| 90 |
|
| 91 |
Args:
|
|
|
|
| 104 |
for chunk in response:
|
| 105 |
if not chunk.choices:
|
| 106 |
continue
|
| 107 |
+
|
| 108 |
# Convert OpenAI response to compatible format
|
| 109 |
yield chunk.model_dump()
|
| 110 |
|
| 111 |
except Exception as e:
|
| 112 |
+
self.logger.error(f"Stream request failed: {e}")
|
| 113 |
raise
|
| 114 |
|
| 115 |
def process(
|
| 116 |
+
self,
|
| 117 |
+
model_name: str,
|
| 118 |
+
req_data: dict,
|
| 119 |
+
max_tokens: int = 2048,
|
| 120 |
+
temperature: float = 1.0,
|
| 121 |
+
top_p: float = 0.7,
|
| 122 |
) -> dict:
|
| 123 |
"""
|
| 124 |
+
Handles chat completion requests by mapping the model name to its endpoint, preparing request parameters
|
| 125 |
+
including token limits and sampling settings, truncating messages to fit character limits, making API calls
|
| 126 |
with built-in retry mechanism, and logging the full request/response cycle for debugging purposes.
|
| 127 |
|
| 128 |
Args:
|
|
|
|
| 144 |
req_data["messages"] = self.truncate_messages(req_data["messages"])
|
| 145 |
for _ in range(self.max_retry_num):
|
| 146 |
try:
|
| 147 |
+
self.logger.info(f"[MODEL] {model_url}")
|
| 148 |
self.logger.info("[req_data]====>")
|
| 149 |
self.logger.info(json.dumps(req_data, ensure_ascii=False))
|
| 150 |
res = self.call_back(model_url, req_data)
|
|
|
|
| 157 |
res = {}
|
| 158 |
if len(res) != 0 and "error" not in res:
|
| 159 |
break
|
| 160 |
+
|
| 161 |
return res
|
| 162 |
|
| 163 |
def process_stream(
|
| 164 |
+
self,
|
| 165 |
+
model_name: str,
|
| 166 |
+
req_data: dict,
|
| 167 |
+
max_tokens: int = 2048,
|
| 168 |
+
temperature: float = 1.0,
|
| 169 |
+
top_p: float = 0.7,
|
| 170 |
) -> dict:
|
| 171 |
"""
|
| 172 |
Processes streaming requests by mapping the model name to its endpoint, configuring request parameters,
|
|
|
|
| 189 |
req_data["temperature"] = temperature
|
| 190 |
req_data["top_p"] = top_p
|
| 191 |
req_data["messages"] = self.truncate_messages(req_data["messages"])
|
| 192 |
+
|
| 193 |
last_error = None
|
| 194 |
for _ in range(self.max_retry_num):
|
| 195 |
try:
|
| 196 |
+
self.logger.info(f"[MODEL] {model_url}")
|
| 197 |
self.logger.info("[req_data]====>")
|
| 198 |
self.logger.info(json.dumps(req_data, ensure_ascii=False))
|
| 199 |
+
|
| 200 |
+
yield from self.call_back_stream(model_url, req_data)
|
|
|
|
| 201 |
return
|
| 202 |
+
|
| 203 |
except Exception as e:
|
| 204 |
last_error = e
|
| 205 |
+
self.logger.error(
|
| 206 |
+
f"Stream request failed (attempt {_ + 1}/{self.max_retry_num}): {e}"
|
| 207 |
+
)
|
| 208 |
+
|
| 209 |
self.logger.error("All retry attempts failed for stream request")
|
| 210 |
yield {"error": str(last_error)}
|
| 211 |
|
| 212 |
def cut_chinese_english(self, text: str) -> list:
|
| 213 |
"""
|
| 214 |
+
Segments mixed Chinese and English text into individual components using Jieba for Chinese words
|
| 215 |
+
while preserving English words as whole units, with special handling for Unicode character ranges
|
| 216 |
to distinguish between the two languages.
|
| 217 |
|
| 218 |
Args:
|
|
|
|
| 225 |
en_ch_words = []
|
| 226 |
|
| 227 |
for word in words:
|
| 228 |
+
if word.isalpha() and not any(
|
| 229 |
+
"\u4e00" <= char <= "\u9fff" for char in word
|
| 230 |
+
):
|
| 231 |
en_ch_words.append(word)
|
| 232 |
else:
|
| 233 |
en_ch_words.extend(list(word))
|
|
|
|
| 247 |
"""
|
| 248 |
if not messages:
|
| 249 |
return messages
|
| 250 |
+
|
| 251 |
processed = []
|
| 252 |
total_units = 0
|
| 253 |
+
|
| 254 |
for msg in messages:
|
| 255 |
# Handle two different content formats
|
| 256 |
if isinstance(msg["content"], str):
|
|
|
|
| 259 |
text_content = msg["content"][1]["text"]
|
| 260 |
else:
|
| 261 |
text_content = ""
|
| 262 |
+
|
| 263 |
# Calculate unit count after tokenization
|
| 264 |
units = self.cut_chinese_english(text_content)
|
| 265 |
unit_count = len(units)
|
| 266 |
+
|
| 267 |
+
processed.append(
|
| 268 |
+
{
|
| 269 |
+
"role": msg["role"],
|
| 270 |
+
"original_content": msg["content"], # Preserve original content
|
| 271 |
+
"text_content": text_content, # Extracted plain text
|
| 272 |
+
"units": units,
|
| 273 |
+
"unit_count": unit_count,
|
| 274 |
+
}
|
| 275 |
+
)
|
| 276 |
total_units += unit_count
|
| 277 |
+
|
| 278 |
if total_units <= self.max_char:
|
| 279 |
return messages
|
| 280 |
+
|
| 281 |
# Number of units to remove
|
| 282 |
to_remove = total_units - self.max_char
|
| 283 |
+
|
| 284 |
# 1. Truncate historical messages
|
| 285 |
for i in range(len(processed) - 1, 1):
|
| 286 |
if to_remove <= 0:
|
| 287 |
break
|
| 288 |
+
|
| 289 |
# current = processed[i]
|
| 290 |
if processed[i]["unit_count"] <= to_remove:
|
| 291 |
processed[i]["text_content"] = ""
|
|
|
|
| 303 |
elif isinstance(processed[i]["original_content"], list):
|
| 304 |
processed[i]["original_content"][1]["text"] = new_text
|
| 305 |
to_remove = 0
|
| 306 |
+
|
| 307 |
# 2. Truncate system message
|
| 308 |
if to_remove > 0:
|
| 309 |
system_msg = processed[0]
|
|
|
|
| 323 |
elif isinstance(processed[0]["original_content"], list):
|
| 324 |
processed[0]["original_content"][1]["text"] = new_text
|
| 325 |
to_remove = 0
|
| 326 |
+
|
| 327 |
# 3. Truncate last message
|
| 328 |
if to_remove > 0 and len(processed) > 1:
|
| 329 |
last_msg = processed[-1]
|
|
|
|
| 341 |
last_msg["original_content"] = ""
|
| 342 |
elif isinstance(last_msg["original_content"], list):
|
| 343 |
last_msg["original_content"][1]["text"] = ""
|
| 344 |
+
|
| 345 |
result = []
|
| 346 |
for msg in processed:
|
| 347 |
if msg["text_content"]:
|
| 348 |
+
result.append({"role": msg["role"], "content": msg["original_content"]})
|
| 349 |
+
|
|
|
|
|
|
|
|
|
|
| 350 |
return result
|
| 351 |
|
| 352 |
def embed_fn(self, text: str) -> list:
|
|
|
|
| 359 |
Returns:
|
| 360 |
list: A list of floats representing the embedding.
|
| 361 |
"""
|
| 362 |
+
client = OpenAI(
|
| 363 |
+
base_url=self.embedding_service_url, api_key=self.qianfan_api_key
|
| 364 |
+
)
|
| 365 |
response = client.embeddings.create(input=[text], model=self.embedding_model)
|
| 366 |
return response.data[0].embedding
|
| 367 |
|
|
|
|
| 377 |
"""
|
| 378 |
headers = {
|
| 379 |
"Authorization": "Bearer " + self.qianfan_api_key,
|
| 380 |
+
"Content-Type": "application/json",
|
| 381 |
}
|
| 382 |
|
| 383 |
results = []
|
|
|
|
| 385 |
for query in query_list:
|
| 386 |
payload = {
|
| 387 |
"messages": [{"role": "user", "content": query}],
|
| 388 |
+
"resource_type_filter": [{"type": "web", "top_k": top_k}],
|
| 389 |
}
|
| 390 |
+
response = requests.post(
|
| 391 |
+
self.web_search_service_url, headers=headers, json=payload
|
| 392 |
+
)
|
| 393 |
|
| 394 |
if response.status_code == 200:
|
| 395 |
response = response.json()
|
|
|
|
| 398 |
else:
|
| 399 |
self.logger.info(f"请求失败,状态码: {response.status_code}")
|
| 400 |
self.logger.info(response.text)
|
| 401 |
+
return results
|