mirror of
https://github.com/cdryzun/tg_bot_collections.git
synced 2025-11-03 16:16:45 +08:00
feat: enhance web search functionality with system prompt and improved result formatting
Signed-off-by: Frost Ming <me@frostming.com>
This commit is contained in:
@ -27,6 +27,11 @@ client = settings.openai_client
|
|||||||
|
|
||||||
# Web search / tool-calling configuration
|
# Web search / tool-calling configuration
|
||||||
WEB_SEARCH_TOOL_NAME = "web_search"
|
WEB_SEARCH_TOOL_NAME = "web_search"
|
||||||
|
WEB_SEARCH_SYSTEM_PROMPT = {
|
||||||
|
"role": "system",
|
||||||
|
"content": "You are a helpful assistant that uses the Ollama Cloud Web Search API to fetch recent information "
|
||||||
|
"from the public internet when needed. Always cite your sources using the format [number](URL) in your responses.\n\n",
|
||||||
|
}
|
||||||
OLLAMA_WEB_SEARCH_URL = "https://ollama.com/api/web_search"
|
OLLAMA_WEB_SEARCH_URL = "https://ollama.com/api/web_search"
|
||||||
WEB_SEARCH_TOOL = {
|
WEB_SEARCH_TOOL = {
|
||||||
"type": "function",
|
"type": "function",
|
||||||
@ -95,7 +100,7 @@ def _format_web_search_results(payload: dict[str, Any]) -> str:
|
|||||||
snippet = snippet[:397].rstrip() + "..."
|
snippet = snippet[:397].rstrip() + "..."
|
||||||
entry = f"[{idx}] {title}"
|
entry = f"[{idx}] {title}"
|
||||||
if url:
|
if url:
|
||||||
entry = f"{entry}\n{url}"
|
entry = f"{entry}\nURL: {url}"
|
||||||
if snippet:
|
if snippet:
|
||||||
entry = f"{entry}\n{snippet}"
|
entry = f"{entry}\n{snippet}"
|
||||||
formatted.append(entry)
|
formatted.append(entry)
|
||||||
@ -168,6 +173,7 @@ def _accumulate_tool_call_deltas(
|
|||||||
|
|
||||||
def _finalize_tool_calls(buffer: dict[int, dict[str, Any]]) -> list[dict[str, Any]]:
|
def _finalize_tool_calls(buffer: dict[int, dict[str, Any]]) -> list[dict[str, Any]]:
|
||||||
tool_calls: list[dict[str, Any]] = []
|
tool_calls: list[dict[str, Any]] = []
|
||||||
|
|
||||||
for idx in sorted(buffer):
|
for idx in sorted(buffer):
|
||||||
entry = buffer[idx]
|
entry = buffer[idx]
|
||||||
function_name = entry.get("function", {}).get("name")
|
function_name = entry.get("function", {}).get("name")
|
||||||
@ -242,6 +248,8 @@ def _stream_chatgpt_pro_response(
|
|||||||
tools = _available_tools()
|
tools = _available_tools()
|
||||||
tool_loops_remaining = MAX_TOOL_ITERATIONS if tools else 0
|
tool_loops_remaining = MAX_TOOL_ITERATIONS if tools else 0
|
||||||
final_response = ""
|
final_response = ""
|
||||||
|
if tools:
|
||||||
|
conversation.insert(0, WEB_SEARCH_SYSTEM_PROMPT)
|
||||||
while True:
|
while True:
|
||||||
request_payload: dict[str, Any] = {
|
request_payload: dict[str, Any] = {
|
||||||
"messages": conversation,
|
"messages": conversation,
|
||||||
@ -249,7 +257,7 @@ def _stream_chatgpt_pro_response(
|
|||||||
"stream": True,
|
"stream": True,
|
||||||
}
|
}
|
||||||
if tools:
|
if tools:
|
||||||
request_payload["tools"] = tools
|
request_payload.update(tools=tools, tool_choice="auto")
|
||||||
|
|
||||||
stream = client.chat.completions.create(**request_payload)
|
stream = client.chat.completions.create(**request_payload)
|
||||||
buffer = ""
|
buffer = ""
|
||||||
@ -409,7 +417,7 @@ def chatgpt_pro_handler(message: Message, bot: TeleBot) -> None:
|
|||||||
player_message = player_message[2:]
|
player_message = player_message[2:]
|
||||||
|
|
||||||
try:
|
try:
|
||||||
reply_text = _stream_chatgpt_pro_response(player_message, reply_id, who, bot)
|
reply_text = _stream_chatgpt_pro_response(player_message[:], reply_id, who, bot)
|
||||||
player_message.append(
|
player_message.append(
|
||||||
{
|
{
|
||||||
"role": "assistant",
|
"role": "assistant",
|
||||||
|
|||||||
Reference in New Issue
Block a user