You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
83 lines
2.7 KiB
83 lines
2.7 KiB
"""
|
|
测试URL返回功能
|
|
"""
|
|
|
|
import asyncio
|
|
import json
|
|
import re
|
|
from ai_agent import create_agent
|
|
|
|
|
|
async def test_url_return():
|
|
"""测试灵感中是否包含URL"""
|
|
|
|
print("=" * 80)
|
|
print("测试URL返回功能")
|
|
print("=" * 80)
|
|
|
|
# 创建Agent
|
|
agent = create_agent()
|
|
|
|
# 测试查询
|
|
query = "我想做一些校园相关的短视频"
|
|
|
|
print(f"\n用户查询: {query}\n")
|
|
print("正在生成灵感...\n")
|
|
|
|
# 运行Agent
|
|
result = await agent.run(
|
|
user_input=query,
|
|
system_prompt_file="prompts/agent_prompt.md",
|
|
max_iterations=15
|
|
)
|
|
|
|
if result["success"]:
|
|
print("✓ 执行成功\n")
|
|
|
|
# 提取JSON
|
|
json_match = re.search(r'```json\s*(.*?)\s*```', result["final_answer"], re.DOTALL)
|
|
if json_match:
|
|
try:
|
|
data = json.loads(json_match.group(1))
|
|
inspirations = data.get('inspirations', [])
|
|
|
|
print(f"✓ 生成了 {len(inspirations)} 个灵感\n")
|
|
|
|
# 检查每个灵感是否包含URL
|
|
url_count = 0
|
|
for i, inspiration in enumerate(inspirations, 1):
|
|
has_url = 'url' in inspiration and inspiration['url']
|
|
url_valid = has_url and inspiration['url'] != 'N/A' and 'http' in inspiration['url']
|
|
|
|
print(f"灵感 {i}:")
|
|
print(f" - 标题: {inspiration.get('title', 'N/A')}")
|
|
print(f" - 作者: {inspiration.get('reference_author', 'N/A')}")
|
|
print(f" - URL: {inspiration.get('url', 'N/A')}")
|
|
|
|
if url_valid:
|
|
print(f" ✓ URL有效")
|
|
url_count += 1
|
|
else:
|
|
print(f" ✗ URL缺失或无效")
|
|
print()
|
|
|
|
print("=" * 80)
|
|
print(f"总结: {url_count}/{len(inspirations)} 个灵感包含有效的URL")
|
|
print("=" * 80)
|
|
|
|
if url_count == len(inspirations):
|
|
print("\n✓ 所有灵感都包含有效的URL!")
|
|
else:
|
|
print(f"\n✗ 有 {len(inspirations) - url_count} 个灵感缺少有效的URL")
|
|
|
|
except json.JSONDecodeError as e:
|
|
print(f"\n✗ JSON解析失败: {e}")
|
|
else:
|
|
print("\n✗ 未找到JSON格式的输出")
|
|
|
|
else:
|
|
print(f"✗ 执行失败: {result.get('error')}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(test_url_return())
|
|
|