Add OpenClaw skills, platform kit, and template docs
Made-with: Cursor
This commit is contained in:
91
llm-manager/scripts/engines/qianwen.py
Normal file
91
llm-manager/scripts/engines/qianwen.py
Normal file
@@ -0,0 +1,91 @@
|
||||
"""
|
||||
通义千问网页版驱动引擎(tongyi.aliyun.com/qianwen)。
|
||||
|
||||
选择器说明(如页面改版需更新):
|
||||
- 输入框:#search-input 或 textarea[class*="input"](textarea)
|
||||
- 发送按钮:button[class*="send"] 或 [aria-label="发送"]
|
||||
- 停止生成:button[class*="stop"] 或 [aria-label="停止"]
|
||||
- 复制按钮:最后一条回复下的 [class*="copy"] 或 [aria-label="复制"]
|
||||
"""
|
||||
import asyncio
|
||||
from .base import BaseEngine
|
||||
|
||||
|
||||
class QianwenEngine(BaseEngine):
|
||||
async def generate(self, prompt: str) -> str:
|
||||
await self.page.goto("https://tongyi.aliyun.com/qianwen/")
|
||||
await self.page.wait_for_load_state("networkidle")
|
||||
|
||||
# 登录检测
|
||||
input_selectors = [
|
||||
"#search-input",
|
||||
"textarea[class*='input']",
|
||||
"div[class*='input'][contenteditable='true']",
|
||||
]
|
||||
editor = None
|
||||
for sel in input_selectors:
|
||||
try:
|
||||
loc = self.page.locator(sel).first
|
||||
await loc.wait_for(state="visible", timeout=4000)
|
||||
editor = loc
|
||||
break
|
||||
except Exception:
|
||||
continue
|
||||
if editor is None:
|
||||
return "ERROR:REQUIRE_LOGIN"
|
||||
|
||||
# 输入提示词
|
||||
await editor.click()
|
||||
await self.page.keyboard.insert_text(prompt)
|
||||
await asyncio.sleep(0.5)
|
||||
|
||||
# 发送
|
||||
sent = False
|
||||
for sel in (
|
||||
'button[class*="send"]',
|
||||
'[aria-label="发送"]',
|
||||
'button[type="submit"]',
|
||||
):
|
||||
try:
|
||||
btn = self.page.locator(sel).first
|
||||
if await btn.is_visible(timeout=1000):
|
||||
await btn.click()
|
||||
sent = True
|
||||
break
|
||||
except Exception:
|
||||
continue
|
||||
if not sent:
|
||||
await self.page.keyboard.press("Enter")
|
||||
|
||||
print("💡 [llm-manager/qianwen] 已发送提示词,等待通义千问生成响应...")
|
||||
await asyncio.sleep(2)
|
||||
|
||||
# 等待生成完毕
|
||||
stop_selectors = ['button[class*="stop"]', '[aria-label="停止"]', '[aria-label="停止生成"]']
|
||||
deadline = asyncio.get_event_loop().time() + 150
|
||||
while asyncio.get_event_loop().time() < deadline:
|
||||
stop_visible = False
|
||||
for sel in stop_selectors:
|
||||
try:
|
||||
if await self.page.locator(sel).first.is_visible(timeout=300):
|
||||
stop_visible = True
|
||||
break
|
||||
except Exception:
|
||||
pass
|
||||
if not stop_visible:
|
||||
break
|
||||
await asyncio.sleep(2)
|
||||
|
||||
await asyncio.sleep(2)
|
||||
|
||||
# 取结果
|
||||
try:
|
||||
copy_btn = self.page.locator(
|
||||
'[aria-label="复制"], [class*="copy-btn"], button:has(svg[class*="copy"])'
|
||||
).last
|
||||
await copy_btn.click()
|
||||
await asyncio.sleep(0.5)
|
||||
result = await self.page.evaluate("navigator.clipboard.readText()")
|
||||
return result.strip()
|
||||
except Exception as e:
|
||||
return f"ERROR:抓取通义千问内容时异常:{e}"
|
||||
Reference in New Issue
Block a user