Spaces:
Running
Running
maxiaolong03
commited on
Commit
·
1b0a1e0
1
Parent(s):
e6723e9
add files
Browse files- app.py +109 -24
- bot_requests.py +88 -77
app.py
CHANGED
|
@@ -99,10 +99,21 @@ def get_args() -> argparse.Namespace:
|
|
| 99 |
"""
|
| 100 |
parser = ArgumentParser(description="ERNIE models web chat demo.")
|
| 101 |
|
| 102 |
-
parser.add_argument(
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
parser.add_argument(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
parser.add_argument(
|
| 107 |
"--model_map",
|
| 108 |
type=str,
|
|
@@ -124,9 +135,23 @@ def get_args() -> argparse.Namespace:
|
|
| 124 |
default="https://qianfan.baidubce.com/v2/ai_search/chat/completions",
|
| 125 |
help="Web Search Service URL.",
|
| 126 |
)
|
| 127 |
-
parser.add_argument("--qianfan_api_key", type=str, default=os.environ.get("API_SEARCH_KEY"), help="QianFan API Key.")
|
| 128 |
parser.add_argument(
|
| 129 |
-
"--
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
)
|
| 131 |
|
| 132 |
args = parser.parse_args()
|
|
@@ -170,7 +195,9 @@ class GradioEvents:
|
|
| 170 |
return conversation, conversation_str
|
| 171 |
|
| 172 |
@staticmethod
|
| 173 |
-
def get_search_query(
|
|
|
|
|
|
|
| 174 |
"""
|
| 175 |
Determines if a web search is needed by analyzing conversation context.
|
| 176 |
Processes model response to extract structured search decision and queries.
|
|
@@ -224,21 +251,29 @@ class GradioEvents:
|
|
| 224 |
Yields:
|
| 225 |
dict: A dictionary containing the event type and its corresponding content.
|
| 226 |
"""
|
| 227 |
-
conversation, conversation_str = GradioEvents.get_history_conversation(
|
|
|
|
|
|
|
| 228 |
|
| 229 |
# Step 1: Determine whether a search is needed and obtain the corresponding query list
|
| 230 |
search_info_res = {}
|
| 231 |
if search_state:
|
| 232 |
search_info_message = SEARCH_INFO_PROMPT.format(
|
| 233 |
-
date=datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
|
|
|
|
|
|
| 234 |
)
|
| 235 |
search_conversation = [{"role": "user", "content": search_info_message}]
|
| 236 |
-
search_info_res = GradioEvents.get_search_query(
|
|
|
|
|
|
|
| 237 |
if search_info_res is None:
|
| 238 |
search_info_res = {"is_search": True, "query_list": [query]}
|
| 239 |
|
| 240 |
# Step 2: If a search is needed, obtain the corresponding query results
|
| 241 |
-
if search_info_res.get("is_search", False) and search_info_res.get(
|
|
|
|
|
|
|
| 242 |
yield {"type": "search_result", "content": "🧐 努力搜索中... ✨"}
|
| 243 |
search_result = bot_client.get_web_search_res(search_info_res["query_list"])
|
| 244 |
|
|
@@ -367,7 +402,13 @@ class GradioEvents:
|
|
| 367 |
chatbot.pop(-1)
|
| 368 |
|
| 369 |
async for chunk, search_result in GradioEvents.predict(
|
| 370 |
-
item[0],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 371 |
):
|
| 372 |
yield chunk, search_result
|
| 373 |
|
|
@@ -434,7 +475,10 @@ class GradioEvents:
|
|
| 434 |
|
| 435 |
@staticmethod
|
| 436 |
async def get_complete_search_content(
|
| 437 |
-
search_results: list,
|
|
|
|
|
|
|
|
|
|
| 438 |
) -> str:
|
| 439 |
"""
|
| 440 |
Combines and formats multiple search results into a single string.
|
|
@@ -528,14 +572,22 @@ def launch_demo(args: argparse.Namespace, bot_client: BotClient):
|
|
| 528 |
<a href="https://yiyan.baidu.com/blog/publication/">Technical Report</a></center>"""
|
| 529 |
)
|
| 530 |
|
| 531 |
-
chatbot = gr.Chatbot(
|
|
|
|
|
|
|
| 532 |
|
| 533 |
-
search_result = gr.Textbox(
|
|
|
|
|
|
|
| 534 |
|
| 535 |
-
search_check = gr.Checkbox(
|
|
|
|
|
|
|
| 536 |
|
| 537 |
with gr.Row():
|
| 538 |
-
query = gr.Textbox(
|
|
|
|
|
|
|
| 539 |
|
| 540 |
with gr.Row():
|
| 541 |
empty_btn = gr.Button("🧹 Clear History(清除历史)")
|
|
@@ -546,33 +598,66 @@ def launch_demo(args: argparse.Namespace, bot_client: BotClient):
|
|
| 546 |
model_name = gr.State(next(iter(args.model_map.keys())))
|
| 547 |
max_crawler_threads = gr.State(args.max_crawler_threads)
|
| 548 |
|
| 549 |
-
search_check.change(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 550 |
|
| 551 |
predict_with_clients = partial(GradioEvents.predict, bot_client=bot_client)
|
| 552 |
-
regenerate_with_clients = partial(
|
|
|
|
|
|
|
| 553 |
query.submit(
|
| 554 |
predict_with_clients,
|
| 555 |
-
inputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 556 |
outputs=[chatbot, search_result],
|
| 557 |
show_progress=True,
|
| 558 |
)
|
| 559 |
query.submit(GradioEvents.reset_user_input, [], [query])
|
| 560 |
submit_btn.click(
|
| 561 |
predict_with_clients,
|
| 562 |
-
inputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 563 |
outputs=[chatbot, search_result],
|
| 564 |
show_progress=True,
|
| 565 |
)
|
| 566 |
submit_btn.click(GradioEvents.reset_user_input, [], [query])
|
| 567 |
-
empty_btn.click(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 568 |
regen_btn.click(
|
| 569 |
regenerate_with_clients,
|
| 570 |
-
inputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 571 |
outputs=[chatbot, search_result],
|
| 572 |
show_progress=True,
|
| 573 |
)
|
| 574 |
|
| 575 |
-
demo.queue(
|
|
|
|
|
|
|
|
|
|
| 576 |
|
| 577 |
|
| 578 |
def main():
|
|
|
|
| 99 |
"""
|
| 100 |
parser = ArgumentParser(description="ERNIE models web chat demo.")
|
| 101 |
|
| 102 |
+
parser.add_argument(
|
| 103 |
+
"--server-port", type=int, default=7860, help="Demo server port."
|
| 104 |
+
)
|
| 105 |
+
parser.add_argument(
|
| 106 |
+
"--server-name", type=str, default="0.0.0.0", help="Demo server name."
|
| 107 |
+
)
|
| 108 |
+
parser.add_argument(
|
| 109 |
+
"--max_char",
|
| 110 |
+
type=int,
|
| 111 |
+
default=20000,
|
| 112 |
+
help="Maximum character limit for messages.",
|
| 113 |
+
)
|
| 114 |
+
parser.add_argument(
|
| 115 |
+
"--max_retry_num", type=int, default=3, help="Maximum retry number for request."
|
| 116 |
+
)
|
| 117 |
parser.add_argument(
|
| 118 |
"--model_map",
|
| 119 |
type=str,
|
|
|
|
| 135 |
default="https://qianfan.baidubce.com/v2/ai_search/chat/completions",
|
| 136 |
help="Web Search Service URL.",
|
| 137 |
)
|
|
|
|
| 138 |
parser.add_argument(
|
| 139 |
+
"--qianfan_api_key",
|
| 140 |
+
type=str,
|
| 141 |
+
default=os.environ.get("API_SEARCH_KEY"),
|
| 142 |
+
help="QianFan API Key.",
|
| 143 |
+
)
|
| 144 |
+
parser.add_argument(
|
| 145 |
+
"--max_crawler_threads",
|
| 146 |
+
type=int,
|
| 147 |
+
default=10,
|
| 148 |
+
help="The maximum number of concurrent crawler threads.",
|
| 149 |
+
)
|
| 150 |
+
parser.add_argument(
|
| 151 |
+
"--concurrency_limit", type=int, default=10, help="Default concurrency limit."
|
| 152 |
+
)
|
| 153 |
+
parser.add_argument(
|
| 154 |
+
"--max_queue_size", type=int, default=50, help="Maximum queue size for request."
|
| 155 |
)
|
| 156 |
|
| 157 |
args = parser.parse_args()
|
|
|
|
| 195 |
return conversation, conversation_str
|
| 196 |
|
| 197 |
@staticmethod
|
| 198 |
+
def get_search_query(
|
| 199 |
+
conversation: list, model_name: str, bot_client: BotClient
|
| 200 |
+
) -> dict:
|
| 201 |
"""
|
| 202 |
Determines if a web search is needed by analyzing conversation context.
|
| 203 |
Processes model response to extract structured search decision and queries.
|
|
|
|
| 251 |
Yields:
|
| 252 |
dict: A dictionary containing the event type and its corresponding content.
|
| 253 |
"""
|
| 254 |
+
conversation, conversation_str = GradioEvents.get_history_conversation(
|
| 255 |
+
task_history
|
| 256 |
+
)
|
| 257 |
|
| 258 |
# Step 1: Determine whether a search is needed and obtain the corresponding query list
|
| 259 |
search_info_res = {}
|
| 260 |
if search_state:
|
| 261 |
search_info_message = SEARCH_INFO_PROMPT.format(
|
| 262 |
+
date=datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
| 263 |
+
context=conversation_str,
|
| 264 |
+
query=query,
|
| 265 |
)
|
| 266 |
search_conversation = [{"role": "user", "content": search_info_message}]
|
| 267 |
+
search_info_res = GradioEvents.get_search_query(
|
| 268 |
+
search_conversation, model_name, bot_client
|
| 269 |
+
)
|
| 270 |
if search_info_res is None:
|
| 271 |
search_info_res = {"is_search": True, "query_list": [query]}
|
| 272 |
|
| 273 |
# Step 2: If a search is needed, obtain the corresponding query results
|
| 274 |
+
if search_info_res.get("is_search", False) and search_info_res.get(
|
| 275 |
+
"query_list", []
|
| 276 |
+
):
|
| 277 |
yield {"type": "search_result", "content": "🧐 努力搜索中... ✨"}
|
| 278 |
search_result = bot_client.get_web_search_res(search_info_res["query_list"])
|
| 279 |
|
|
|
|
| 402 |
chatbot.pop(-1)
|
| 403 |
|
| 404 |
async for chunk, search_result in GradioEvents.predict(
|
| 405 |
+
item[0],
|
| 406 |
+
chatbot,
|
| 407 |
+
task_history,
|
| 408 |
+
model,
|
| 409 |
+
search_state,
|
| 410 |
+
max_crawler_threads,
|
| 411 |
+
bot_client,
|
| 412 |
):
|
| 413 |
yield chunk, search_result
|
| 414 |
|
|
|
|
| 475 |
|
| 476 |
@staticmethod
|
| 477 |
async def get_complete_search_content(
|
| 478 |
+
search_results: list,
|
| 479 |
+
max_crawler_threads: int,
|
| 480 |
+
bot_client: BotClient,
|
| 481 |
+
max_search_results_char: int = 18000,
|
| 482 |
) -> str:
|
| 483 |
"""
|
| 484 |
Combines and formats multiple search results into a single string.
|
|
|
|
| 572 |
<a href="https://yiyan.baidu.com/blog/publication/">Technical Report</a></center>"""
|
| 573 |
)
|
| 574 |
|
| 575 |
+
chatbot = gr.Chatbot(
|
| 576 |
+
label="ERNIE", elem_classes="control-height", type="messages"
|
| 577 |
+
)
|
| 578 |
|
| 579 |
+
search_result = gr.Textbox(
|
| 580 |
+
label="Search Result", lines=10, max_lines=10, visible=True
|
| 581 |
+
)
|
| 582 |
|
| 583 |
+
search_check = gr.Checkbox(
|
| 584 |
+
label="🌐 Search the web(联网搜索)", value=True, interactive=True
|
| 585 |
+
)
|
| 586 |
|
| 587 |
with gr.Row():
|
| 588 |
+
query = gr.Textbox(
|
| 589 |
+
label="Input", lines=1, scale=6, elem_classes="input-textbox"
|
| 590 |
+
)
|
| 591 |
|
| 592 |
with gr.Row():
|
| 593 |
empty_btn = gr.Button("🧹 Clear History(清除历史)")
|
|
|
|
| 598 |
model_name = gr.State(next(iter(args.model_map.keys())))
|
| 599 |
max_crawler_threads = gr.State(args.max_crawler_threads)
|
| 600 |
|
| 601 |
+
search_check.change(
|
| 602 |
+
fn=GradioEvents.search_toggle_state,
|
| 603 |
+
inputs=search_check,
|
| 604 |
+
outputs=search_result,
|
| 605 |
+
)
|
| 606 |
|
| 607 |
predict_with_clients = partial(GradioEvents.predict, bot_client=bot_client)
|
| 608 |
+
regenerate_with_clients = partial(
|
| 609 |
+
GradioEvents.regenerate, bot_client=bot_client
|
| 610 |
+
)
|
| 611 |
query.submit(
|
| 612 |
predict_with_clients,
|
| 613 |
+
inputs=[
|
| 614 |
+
query,
|
| 615 |
+
chatbot,
|
| 616 |
+
task_history,
|
| 617 |
+
model_name,
|
| 618 |
+
search_check,
|
| 619 |
+
max_crawler_threads,
|
| 620 |
+
],
|
| 621 |
outputs=[chatbot, search_result],
|
| 622 |
show_progress=True,
|
| 623 |
)
|
| 624 |
query.submit(GradioEvents.reset_user_input, [], [query])
|
| 625 |
submit_btn.click(
|
| 626 |
predict_with_clients,
|
| 627 |
+
inputs=[
|
| 628 |
+
query,
|
| 629 |
+
chatbot,
|
| 630 |
+
task_history,
|
| 631 |
+
model_name,
|
| 632 |
+
search_check,
|
| 633 |
+
max_crawler_threads,
|
| 634 |
+
],
|
| 635 |
outputs=[chatbot, search_result],
|
| 636 |
show_progress=True,
|
| 637 |
)
|
| 638 |
submit_btn.click(GradioEvents.reset_user_input, [], [query])
|
| 639 |
+
empty_btn.click(
|
| 640 |
+
GradioEvents.reset_state,
|
| 641 |
+
outputs=[chatbot, task_history, search_result],
|
| 642 |
+
show_progress=True,
|
| 643 |
+
)
|
| 644 |
regen_btn.click(
|
| 645 |
regenerate_with_clients,
|
| 646 |
+
inputs=[
|
| 647 |
+
chatbot,
|
| 648 |
+
task_history,
|
| 649 |
+
model_name,
|
| 650 |
+
search_check,
|
| 651 |
+
max_crawler_threads,
|
| 652 |
+
],
|
| 653 |
outputs=[chatbot, search_result],
|
| 654 |
show_progress=True,
|
| 655 |
)
|
| 656 |
|
| 657 |
+
demo.queue(
|
| 658 |
+
default_concurrency_limit=args.concurrency_limit, max_size=args.max_queue_size
|
| 659 |
+
)
|
| 660 |
+
demo.launch(server_port=args.server_port, server_name=args.server_name)
|
| 661 |
|
| 662 |
|
| 663 |
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
|