|
@@ -5,7 +5,12 @@ class QwenClient:
|
|
|
def __init__(self):
|
|
|
self.api_key = "sk-1022fe8e15ff4e0e9abc20541b281165"
|
|
|
|
|
|
- def chat(self, model="qwen3-max", system_prompt="You are a helpful assistant.", user_prompt=""):
|
|
|
+ def chat(
|
|
|
+ self,
|
|
|
+ model="qwen3-max",
|
|
|
+ system_prompt="You are a helpful assistant.",
|
|
|
+ user_prompt="",
|
|
|
+ ):
|
|
|
"""
|
|
|
普通聊天,不使用搜索功能
|
|
|
|
|
@@ -27,7 +32,7 @@ class QwenClient:
|
|
|
api_key=self.api_key,
|
|
|
model=model,
|
|
|
messages=messages,
|
|
|
- result_format="message"
|
|
|
+ result_format="message",
|
|
|
)
|
|
|
|
|
|
if response.status_code != 200:
|
|
@@ -38,7 +43,13 @@ class QwenClient:
|
|
|
except Exception as e:
|
|
|
raise Exception(f"QwenClient chat失败: {str(e)}")
|
|
|
|
|
|
- def search_and_chat(self, model="qwen3-max", system_prompt="You are a helpful assistant.", user_prompt="", search_strategy="max"):
|
|
|
+ def search_and_chat(
|
|
|
+ self,
|
|
|
+ model="qwen3-max",
|
|
|
+ system_prompt="You are a helpful assistant.",
|
|
|
+ user_prompt="",
|
|
|
+ search_strategy="max",
|
|
|
+ ):
|
|
|
"""
|
|
|
搜索并聊天
|
|
|
|
|
@@ -65,9 +76,9 @@ class QwenClient:
|
|
|
search_options={
|
|
|
"forced_search": True,
|
|
|
"enable_source": True,
|
|
|
- "search_strategy": search_strategy
|
|
|
+ "search_strategy": search_strategy,
|
|
|
},
|
|
|
- result_format="message"
|
|
|
+ result_format="message",
|
|
|
)
|
|
|
|
|
|
if response.status_code != 200:
|
|
@@ -76,13 +87,10 @@ class QwenClient:
|
|
|
content = response["output"]["choices"][0]["message"]["content"]
|
|
|
search_results = []
|
|
|
|
|
|
- if hasattr(response.output, 'search_info') and response.output.search_info:
|
|
|
+ if hasattr(response.output, "search_info") and response.output.search_info:
|
|
|
search_results = response.output.search_info.get("search_results", [])
|
|
|
|
|
|
- return {
|
|
|
- "content": content,
|
|
|
- "search_results": search_results
|
|
|
- }
|
|
|
+ return {"content": content, "search_results": search_results}
|
|
|
|
|
|
except Exception as e:
|
|
|
raise Exception(f"QwenClient search_and_chat失败: {str(e)}")
|
|
@@ -93,7 +101,6 @@ if __name__ == "__main__":
|
|
|
|
|
|
# 测试
|
|
|
try:
|
|
|
-
|
|
|
# result = client.chat(user_prompt="hello")
|
|
|
# print(result)
|
|
|
|
|
@@ -124,17 +131,18 @@ if __name__ == "__main__":
|
|
|
"页面操作路径": "完整的页面操作路径"
|
|
|
}"""
|
|
|
|
|
|
-
|
|
|
# user_prompt = "请搜索 白瓜AI 官网"
|
|
|
|
|
|
- result = client.search_and_chat(user_prompt=user_prompt, search_strategy="agent")
|
|
|
+ result = client.search_and_chat(
|
|
|
+ user_prompt=user_prompt, search_strategy="agent"
|
|
|
+ )
|
|
|
|
|
|
- print("="*20 + "搜索结果" + "="*20)
|
|
|
+ print("=" * 20 + "搜索结果" + "=" * 20)
|
|
|
for web in result["search_results"]:
|
|
|
print(f"[{web['index']}]: [{web['title']}]({web['url']})")
|
|
|
|
|
|
- print("="*20 + "回复内容" + "="*20)
|
|
|
+ print("=" * 20 + "回复内容" + "=" * 20)
|
|
|
print(result["content"])
|
|
|
|
|
|
except Exception as e:
|
|
|
- print(f"错误: {e}")
|
|
|
+ print(f"错误: {e}")
|