1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
| from langchain.agents import create_agent import json import os
extended_product_schema = { "type": "object", "description": "Extended product information with additional details", "properties": { "name": {"type": "string", "description": "产品名称"}, "catalog_number": {"type": "string", "description": "规格货号"}, "description": {"type": "string", "description": "产品描述"}, "validity_period": {"type": "string", "description": "有效期"}, "storage_temperature": {"type": "string", "description": "保存温度"}, "category": {"type": "string", "description": "产品类别"}, "brand": {"type": "string", "description": "品牌"}, "precautions": { "type": "array", "items": {"type": "string"}, "description": "注意事项列表" }, "specifications": { "type": "array", "items": { "type": "object", "properties": { "spec_name": {"type": "string", "description": "规格名称"}, "spec_value": {"type": "string", "description": "规格值"}, "unit": {"type": "string", "description": "单位"} }, "required": ["spec_name", "spec_value"] }, "description": "技术规格列表" } }, "required": [] }
os.environ["OPENAI_API_BASE"] = "https://{HOST}/v1" os.environ["OPENAI_API_KEY"] = "sk-XXX"
extended_agent = create_agent( model="openai:XXXX", tools=[], response_format=extended_product_schema )
def extract_product_info_json(text: str) -> dict: """从产品描述中提取扩展信息""" try: result = extended_agent.invoke({ "messages": [{"role": "user", "content": f""" 请从以下产品说明书中提取结构化信息:
{text}
请提取以下信息: 1. 产品名称 2. 规格货号(产品编号) 3. 产品描述 4. 有效期 5. 保存温度 6. 产品类别 7. 品牌 8. 注意事项列表 9. 技术规格列表
请严格按照JSON Schema的结构返回结果。 """}] }) return result["structured_response"] except Exception as e: print(f"扩展产品信息提取失败: {e}") return { "name": "提取失败", "catalog_number": "未知", "description": "无法提取产品描述", "validity_period": "未知", "storage_temperature": "未知", "category": "未知", "brand": "未知", "precautions": ["提取过程中出现错误"], "specifications": [] }
sample_text = """ ## 达优
## Mouse IL-22 Precoated ELISA Kit
Cat#:1212202/1212203
ELISA 试剂盒 说明书
本试剂盒仅供科研使用,请勿用于诊断 使用前请仔细阅读说明书并检查试剂盒组分 ## Mouse IL-22
## Precoated ELISA Kit
ELISA试剂盒
## | 检测原理
达优ELISA 试剂盒采用双抗体夹心酶联免疫吸附检测技术。特异性的抗体包 被到酶标板上,标准品或样本加入孔中,样本中含有的目标物质被固定化的抗体 捕获,加入检测抗体,形成抗体-抗原-抗体的复合体,加入酶和底物,在酶的催 化下底物发生反应,且反应的颜色与目标物质的含量成正比,加入终止液后,在 酶标仪中测定反应的吸光值。
## | 预期用途
定量检测血清、血浆、缓冲液或细胞培养液中的IL-22含量。
## | 保存条件
未开封试剂盒在2℃~8℃可稳定保存12个月。 """
try: result = extract_product_info_json(sample_text) print("产品信息提取结果:") print(json.dumps(result, ensure_ascii=False, indent=2)) except Exception as e: print(f"提取失败:{e}")
|