360 lines
9.4 KiB
Python
360 lines
9.4 KiB
Python
|
|
"""
|
|||
|
|
AI 模块使用示例
|
|||
|
|
|
|||
|
|
演示如何使用 AI 模块进行文本分类
|
|||
|
|
"""
|
|||
|
|
|
|||
|
|
import sys
|
|||
|
|
import os
|
|||
|
|
|
|||
|
|
# 添加项目根目录到 Python 路径
|
|||
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|||
|
|
|
|||
|
|
from src.core.ai import (
|
|||
|
|
CategoryType,
|
|||
|
|
ClassificationResult,
|
|||
|
|
AIClassifier,
|
|||
|
|
classify_text,
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
|
|||
|
|
def example_classify_with_openai():
|
|||
|
|
"""
|
|||
|
|
示例 1: 使用 OpenAI 进行分类
|
|||
|
|
|
|||
|
|
注意:需要安装 openai 库并配置 API key
|
|||
|
|
pip install openai
|
|||
|
|
"""
|
|||
|
|
print("=" * 60)
|
|||
|
|
print("示例 1: 使用 OpenAI 进行分类")
|
|||
|
|
print("=" * 60)
|
|||
|
|
print()
|
|||
|
|
|
|||
|
|
# 创建 OpenAI 客户端
|
|||
|
|
client = AIClassifier.create_client(
|
|||
|
|
provider="openai",
|
|||
|
|
api_key="your-openai-api-key", # 替换为实际的 API key
|
|||
|
|
model="gpt-4o-mini",
|
|||
|
|
temperature=0.7,
|
|||
|
|
max_tokens=2000,
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
# 测试文本
|
|||
|
|
test_text = """
|
|||
|
|
今天要完成的任务:
|
|||
|
|
1. 完成项目文档
|
|||
|
|
2. 修复 Bug #123
|
|||
|
|
3. 参加团队会议
|
|||
|
|
4. 代码审查
|
|||
|
|
"""
|
|||
|
|
|
|||
|
|
print(f"输入文本:\n{test_text}\n")
|
|||
|
|
|
|||
|
|
# 进行分类
|
|||
|
|
try:
|
|||
|
|
result = client.classify(test_text)
|
|||
|
|
|
|||
|
|
print("分类结果:")
|
|||
|
|
print(f" 分类: {result.category}")
|
|||
|
|
print(f" 置信度: {result.confidence:.2f}")
|
|||
|
|
print(f" 标题: {result.title}")
|
|||
|
|
print(f" 标签: {', '.join(result.tags)}")
|
|||
|
|
print(f" 理由: {result.reasoning}")
|
|||
|
|
print(f"\n生成的 Markdown 内容:")
|
|||
|
|
print(result.content)
|
|||
|
|
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f"分类失败: {e}")
|
|||
|
|
print("提示:请确保已安装 openai 库并配置正确的 API key")
|
|||
|
|
|
|||
|
|
|
|||
|
|
def example_classify_with_claude():
|
|||
|
|
"""
|
|||
|
|
示例 2: 使用 Claude 进行分类
|
|||
|
|
|
|||
|
|
注意:需要安装 anthropic 库并配置 API key
|
|||
|
|
pip install anthropic
|
|||
|
|
"""
|
|||
|
|
print("\n" + "=" * 60)
|
|||
|
|
print("示例 2: 使用 Claude 进行分类")
|
|||
|
|
print("=" * 60)
|
|||
|
|
print()
|
|||
|
|
|
|||
|
|
# 创建 Claude 客户端
|
|||
|
|
client = AIClassifier.create_client(
|
|||
|
|
provider="anthropic",
|
|||
|
|
api_key="your-anthropic-api-key", # 替换为实际的 API key
|
|||
|
|
model="claude-3-5-sonnet-20241022",
|
|||
|
|
temperature=0.7,
|
|||
|
|
max_tokens=2000,
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
# 测试文本 - 笔记类型
|
|||
|
|
test_text = """
|
|||
|
|
Python 装饰器是一种强大的功能,它允许在不修改原函数代码的情况下增强函数功能。
|
|||
|
|
|
|||
|
|
基本语法:
|
|||
|
|
@decorator_name
|
|||
|
|
def function():
|
|||
|
|
pass
|
|||
|
|
|
|||
|
|
常见用途:
|
|||
|
|
- 日志记录
|
|||
|
|
- 性能测试
|
|||
|
|
- 权限验证
|
|||
|
|
- 缓存
|
|||
|
|
"""
|
|||
|
|
|
|||
|
|
print(f"输入文本:\n{test_text}\n")
|
|||
|
|
|
|||
|
|
# 进行分类
|
|||
|
|
try:
|
|||
|
|
result = client.classify(test_text)
|
|||
|
|
|
|||
|
|
print("分类结果:")
|
|||
|
|
print(f" 分类: {result.category}")
|
|||
|
|
print(f" 置信度: {result.confidence:.2f}")
|
|||
|
|
print(f" 标题: {result.title}")
|
|||
|
|
print(f" 标签: {', '.join(result.tags)}")
|
|||
|
|
print(f" 理由: {result.reasoning}")
|
|||
|
|
print(f"\n生成的 Markdown 内容:")
|
|||
|
|
print(result.content)
|
|||
|
|
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f"分类失败: {e}")
|
|||
|
|
print("提示:请确保已安装 anthropic 库并配置正确的 API key")
|
|||
|
|
|
|||
|
|
|
|||
|
|
def example_classify_with_qwen():
|
|||
|
|
"""
|
|||
|
|
示例 3: 使用通义千问进行分类
|
|||
|
|
|
|||
|
|
注意:需要阿里云 API key
|
|||
|
|
"""
|
|||
|
|
print("\n" + "=" * 60)
|
|||
|
|
print("示例 3: 使用通义千问进行分类")
|
|||
|
|
print("=" * 60)
|
|||
|
|
print()
|
|||
|
|
|
|||
|
|
# 创建通义千问客户端
|
|||
|
|
client = AIClassifier.create_client(
|
|||
|
|
provider="qwen",
|
|||
|
|
api_key="your-qwen-api-key", # 替换为实际的 API key
|
|||
|
|
model="qwen-turbo",
|
|||
|
|
temperature=0.7,
|
|||
|
|
max_tokens=2000,
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
# 测试文本 - 灵感类型
|
|||
|
|
test_text = """
|
|||
|
|
突然想到一个产品创意:做一个智能截图管理工具!
|
|||
|
|
|
|||
|
|
核心功能:
|
|||
|
|
- 自动 OCR 识别截图文字
|
|||
|
|
- AI 智能分类整理
|
|||
|
|
- 自动生成待办事项
|
|||
|
|
- 云端同步多设备
|
|||
|
|
|
|||
|
|
技术栈:
|
|||
|
|
- Python + PyQt6 桌面应用
|
|||
|
|
- PaddleOCR 本地识别
|
|||
|
|
- OpenAI/Claude AI 分类
|
|||
|
|
- SQLite 本地存储
|
|||
|
|
"""
|
|||
|
|
|
|||
|
|
print(f"输入文本:\n{test_text}\n")
|
|||
|
|
|
|||
|
|
# 进行分类
|
|||
|
|
try:
|
|||
|
|
result = client.classify(test_text)
|
|||
|
|
|
|||
|
|
print("分类结果:")
|
|||
|
|
print(f" 分类: {result.category}")
|
|||
|
|
print(f" 置信度: {result.confidence:.2f}")
|
|||
|
|
print(f" 标题: {result.title}")
|
|||
|
|
print(f" 标签: {', '.join(result.tags)}")
|
|||
|
|
print(f" 理由: {result.reasoning}")
|
|||
|
|
print(f"\n生成的 Markdown 内容:")
|
|||
|
|
print(result.content)
|
|||
|
|
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f"分类失败: {e}")
|
|||
|
|
print("提示:请确保已配置正确的通义千问 API key")
|
|||
|
|
|
|||
|
|
|
|||
|
|
def example_classify_with_ollama():
|
|||
|
|
"""
|
|||
|
|
示例 4: 使用本地 Ollama 进行分类
|
|||
|
|
|
|||
|
|
注意:需要先安装并运行 Ollama
|
|||
|
|
https://ollama.ai/
|
|||
|
|
"""
|
|||
|
|
print("\n" + "=" * 60)
|
|||
|
|
print("示例 4: 使用本地 Ollama 进行分类")
|
|||
|
|
print("=" * 60)
|
|||
|
|
print()
|
|||
|
|
|
|||
|
|
# 创建 Ollama 客户端
|
|||
|
|
client = AIClassifier.create_client(
|
|||
|
|
provider="ollama",
|
|||
|
|
api_key="", # Ollama 不需要 API key
|
|||
|
|
model="llama3.2", # 或其他已下载的模型
|
|||
|
|
temperature=0.7,
|
|||
|
|
max_tokens=2000,
|
|||
|
|
timeout=120, # 本地模型可能需要更长时间
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
# 测试文本 - 搞笑类型
|
|||
|
|
test_text = """
|
|||
|
|
程序员最讨厌的四件事:
|
|||
|
|
1. 写注释
|
|||
|
|
2. 写文档
|
|||
|
|
3. 别人不写注释
|
|||
|
|
4. 别人不写文档
|
|||
|
|
|
|||
|
|
为什么程序员总是分不清万圣节和圣诞节?
|
|||
|
|
因为 Oct 31 == Dec 25
|
|||
|
|
"""
|
|||
|
|
|
|||
|
|
print(f"输入文本:\n{test_text}\n")
|
|||
|
|
|
|||
|
|
# 进行分类
|
|||
|
|
try:
|
|||
|
|
result = client.classify(test_text)
|
|||
|
|
|
|||
|
|
print("分类结果:")
|
|||
|
|
print(f" 分类: {result.category}")
|
|||
|
|
print(f" 置信度: {result.confidence:.2f}")
|
|||
|
|
print(f" 标题: {result.title}")
|
|||
|
|
print(f" 标签: {', '.join(result.tags)}")
|
|||
|
|
print(f" 理由: {result.reasoning}")
|
|||
|
|
print(f"\n生成的 Markdown 内容:")
|
|||
|
|
print(result.content)
|
|||
|
|
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f"分类失败: {e}")
|
|||
|
|
print("提示:请确保已安装并运行 Ollama 服务")
|
|||
|
|
|
|||
|
|
|
|||
|
|
def example_classify_with_config():
|
|||
|
|
"""
|
|||
|
|
示例 5: 使用配置文件进行分类
|
|||
|
|
|
|||
|
|
从配置文件读取 AI 配置
|
|||
|
|
"""
|
|||
|
|
print("\n" + "=" * 60)
|
|||
|
|
print("示例 5: 使用配置文件进行分类")
|
|||
|
|
print("=" * 60)
|
|||
|
|
print()
|
|||
|
|
|
|||
|
|
try:
|
|||
|
|
from src.config.settings import get_settings
|
|||
|
|
|
|||
|
|
# 加载配置
|
|||
|
|
settings = get_settings()
|
|||
|
|
|
|||
|
|
# 检查配置
|
|||
|
|
print("当前 AI 配置:")
|
|||
|
|
print(f" 提供商: {settings.ai.provider}")
|
|||
|
|
print(f" 模型: {settings.ai.model}")
|
|||
|
|
print(f" 温度: {settings.ai.temperature}")
|
|||
|
|
print(f" 最大 tokens: {settings.ai.max_tokens}")
|
|||
|
|
print()
|
|||
|
|
|
|||
|
|
# 测试文本
|
|||
|
|
test_text = """
|
|||
|
|
API 接口文档
|
|||
|
|
|
|||
|
|
GET /api/users
|
|||
|
|
获取用户列表
|
|||
|
|
|
|||
|
|
参数:
|
|||
|
|
- page: 页码(默认 1)
|
|||
|
|
- limit: 每页数量(默认 20)
|
|||
|
|
|
|||
|
|
返回:
|
|||
|
|
{
|
|||
|
|
"users": [...],
|
|||
|
|
"total": 100,
|
|||
|
|
"page": 1
|
|||
|
|
}
|
|||
|
|
"""
|
|||
|
|
|
|||
|
|
print(f"输入文本:\n{test_text}\n")
|
|||
|
|
|
|||
|
|
# 使用配置进行分类
|
|||
|
|
result = classify_text(test_text, settings.ai)
|
|||
|
|
|
|||
|
|
print("分类结果:")
|
|||
|
|
print(f" 分类: {result.category}")
|
|||
|
|
print(f" 置信度: {result.confidence:.2f}")
|
|||
|
|
print(f" 标题: {result.title}")
|
|||
|
|
print(f" 标签: {', '.join(result.tags)}")
|
|||
|
|
print(f" 理由: {result.reasoning}")
|
|||
|
|
print(f"\n生成的 Markdown 内容:")
|
|||
|
|
print(result.content)
|
|||
|
|
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f"分类失败: {e}")
|
|||
|
|
print("提示:请确保已在配置文件中正确设置 AI 配置")
|
|||
|
|
|
|||
|
|
|
|||
|
|
def example_batch_classification():
|
|||
|
|
"""
|
|||
|
|
示例 6: 批量分类多个文本
|
|||
|
|
"""
|
|||
|
|
print("\n" + "=" * 60)
|
|||
|
|
print("示例 6: 批量分类多个文本")
|
|||
|
|
print("=" * 60)
|
|||
|
|
print()
|
|||
|
|
|
|||
|
|
# 测试文本列表
|
|||
|
|
test_texts = [
|
|||
|
|
("今天要完成:写代码、测试、部署", "待办类型"),
|
|||
|
|
("Python 列表推导式的用法示例", "笔记类型"),
|
|||
|
|
("产品创意:做一个 AI 写作助手", "灵感类型"),
|
|||
|
|
("API 接口:POST /api/create", "参考资料"),
|
|||
|
|
("程序员的 10 个搞笑瞬间", "搞笑类型"),
|
|||
|
|
]
|
|||
|
|
|
|||
|
|
print("批量分类结果:\n")
|
|||
|
|
|
|||
|
|
for text, description in test_texts:
|
|||
|
|
print(f"文本: {description}")
|
|||
|
|
print(f"内容: {text[:40]}...")
|
|||
|
|
print(f"预期分类: {description.split('类型')[0]}")
|
|||
|
|
print(f"实际分类: 需要调用 AI 服务")
|
|||
|
|
print()
|
|||
|
|
|
|||
|
|
|
|||
|
|
def main():
|
|||
|
|
"""主函数"""
|
|||
|
|
print("\n")
|
|||
|
|
print("╔" + "═" * 58 + "╗")
|
|||
|
|
print("║" + " " * 15 + "AI 模块使用示例" + " " * 15 + "║")
|
|||
|
|
print("╚" + "═" * 58 + "╝")
|
|||
|
|
print()
|
|||
|
|
|
|||
|
|
# 运行示例(注释掉需要 API key 的示例)
|
|||
|
|
# example_classify_with_openai()
|
|||
|
|
# example_classify_with_claude()
|
|||
|
|
# example_classify_with_qwen()
|
|||
|
|
# example_classify_with_ollama()
|
|||
|
|
# example_classify_with_config()
|
|||
|
|
example_batch_classification()
|
|||
|
|
|
|||
|
|
print("\n" + "=" * 60)
|
|||
|
|
print("提示")
|
|||
|
|
print("=" * 60)
|
|||
|
|
print()
|
|||
|
|
print("1. 取消注释上面的示例函数来运行特定示例")
|
|||
|
|
print("2. 替换 'your-api-key' 为实际的 API 密钥")
|
|||
|
|
print("3. 确保已安装所需的依赖库:")
|
|||
|
|
print(" pip install openai anthropic")
|
|||
|
|
print("4. Ollama 需要单独安装和运行")
|
|||
|
|
print()
|
|||
|
|
|
|||
|
|
|
|||
|
|
if __name__ == "__main__":
|
|||
|
|
main()
|