6c1a69af0d
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
110 lines
3.0 KiB
Python
110 lines
3.0 KiB
Python
"""
|
|
使用硅基流动API的示例
|
|
"""
|
|
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
sys.path.insert(0, str(Path(__file__).parent.parent))
|
|
|
|
from src.utils.llm_client import LLMClient, LLMProvider
|
|
from src.utils.config import load_config
|
|
from src.extraction.ner import NERExtractor
|
|
|
|
|
|
def example_basic_usage():
|
|
"""基础使用示例"""
|
|
print("示例1: 基础API调用")
|
|
print("-" * 60)
|
|
|
|
config = load_config()
|
|
client = LLMClient(
|
|
provider=LLMProvider.SILICONFLOW,
|
|
model="Qwen/Qwen2.5-72B-Instruct", # 请根据实际情况修改
|
|
config=config
|
|
)
|
|
|
|
messages = [
|
|
{"role": "system", "content": "你是一个法规文本分析专家。"},
|
|
{"role": "user", "content": "请分析以下文本中的实体:城市更新需要遵循国土空间规划的要求。"}
|
|
]
|
|
|
|
response = client.chat(messages)
|
|
print(f"响应: {response}\n")
|
|
|
|
|
|
def example_ner_with_siliconflow():
|
|
"""使用硅基流动进行实体识别"""
|
|
print("示例2: 使用硅基流动进行实体识别")
|
|
print("-" * 60)
|
|
|
|
config = load_config()
|
|
|
|
# 注意:NERExtractor默认使用Qwen,我们可以修改为使用硅基流动
|
|
# 方法1: 直接创建NER提取器并替换LLM客户端
|
|
ner_extractor = NERExtractor(model="Qwen/Qwen2.5-72B-Instruct", config=config)
|
|
|
|
# 替换为硅基流动客户端
|
|
ner_extractor.llm_client = LLMClient(
|
|
provider=LLMProvider.SILICONFLOW,
|
|
model="Qwen/Qwen2.5-72B-Instruct", # 请根据实际情况修改
|
|
config=config
|
|
)
|
|
|
|
text = "城市更新应当遵循国土空间规划,坚持生态优先、绿色发展原则。"
|
|
entities = ner_extractor.extract(text)
|
|
|
|
print(f"输入文本: {text}")
|
|
print(f"识别到的实体: {entities}\n")
|
|
|
|
|
|
def example_multi_provider_comparison():
|
|
"""多提供商对比示例"""
|
|
print("示例3: 对比不同LLM提供商的响应")
|
|
print("-" * 60)
|
|
|
|
config = load_config()
|
|
messages = [
|
|
{"role": "user", "content": "什么是知识图谱?用一句话回答。"}
|
|
]
|
|
|
|
providers = [
|
|
(LLMProvider.SILICONFLOW, "Qwen/Qwen2.5-72B-Instruct"),
|
|
# 可以添加其他提供商进行对比
|
|
]
|
|
|
|
for provider, model in providers:
|
|
try:
|
|
client = LLMClient(provider=provider, model=model, config=config)
|
|
response = client.chat(messages)
|
|
print(f"{provider.value}: {response[:100]}...")
|
|
except Exception as e:
|
|
print(f"{provider.value}: 调用失败 - {e}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
print("=" * 60)
|
|
print("硅基流动API使用示例")
|
|
print("=" * 60)
|
|
print()
|
|
|
|
# 运行示例
|
|
try:
|
|
example_basic_usage()
|
|
except Exception as e:
|
|
print(f"示例1执行失败: {e}\n")
|
|
|
|
try:
|
|
example_ner_with_siliconflow()
|
|
except Exception as e:
|
|
print(f"示例2执行失败: {e}\n")
|
|
|
|
try:
|
|
example_multi_provider_comparison()
|
|
except Exception as e:
|
|
print(f"示例3执行失败: {e}\n")
|
|
|
|
|
|
|
|
|