schemas.py 8.2 KB
"""JSON Schema definitions for registering the recommendation tools."""

import copy


ALGORITHM_NAMES = [
    'swing_cpp',
    'swing',
    'session_w2v',
    'deepwalk',
    'content_name',
    'content_pic',
    'item_behavior',
]

DIMENSION_NAMES = [
    'platform',
    'client_platform',
    'country_code',
    'customer_type',
    'supplier',
    'category_level1',
    'category_level2',
    'category_level3',
    'category_level4',
]

LIST_TYPE_NAMES = ['hot', 'cart', 'new', 'global']


SEED_ITEMS_SCHEMA = {
    'type': 'array',
    'minItems': 1,
    'maxItems': 20,
    'items': {
        'type': 'object',
        'additionalProperties': False,
        'properties': {
            'item_id': {
                'description': '商品 SKU ID,建议以字符串传递。',
                'oneOf': [{'type': 'string'}, {'type': 'integer'}],
            },
            'weight': {
                'type': 'number',
                'exclusiveMinimum': 0,
                'maximum': 10,
                'description': '偏好强度;省略时按 behavior 推导。',
            },
            'behavior': {
                'type': 'string',
                'enum': [
                    'click', 'view', 'addToPool', 'addToCart',
                    'contactFactory', 'purchase',
                ],
            },
            'reason': {
                'type': 'string',
                'maxLength': 200,
                'description': '画像中选择该种子商品的简短依据。',
            },
        },
        'required': ['item_id'],
    },
}


CONTEXT_SCHEMA = {
    'type': 'object',
    'additionalProperties': False,
    'properties': {
        'platform': {'type': 'string'},
        'client_platform': {'type': 'string'},
        'country_code': {
            'oneOf': [{'type': 'string'}, {'type': 'integer'}],
        },
        'customer_type': {'type': 'string'},
    },
    'description': '当前请求上下文;组合维度会由工具自动规划。',
}


INTERESTS_SCHEMA = {
    'type': 'array',
    'maxItems': 30,
    'items': {
        'type': 'object',
        'additionalProperties': False,
        'properties': {
            'dimension': {'type': 'string', 'enum': DIMENSION_NAMES},
            'value': {
                'description': '内部维度值;名称应先通过维度解析工具转换。',
                'oneOf': [{'type': 'string'}, {'type': 'integer'}],
            },
            'weight': {
                'type': 'number',
                'exclusiveMinimum': 0,
                'maximum': 10,
                'default': 1,
            },
            'reason': {'type': 'string', 'maxLength': 200},
        },
        'required': ['dimension', 'value'],
    },
}


def _common_recall_properties():
    return {
        'top_k': {
            'type': 'integer', 'minimum': 1, 'maximum': 100, 'default': 50,
        },
        'exclude_item_ids': {
            'type': 'array',
            'maxItems': 500,
            'items': {'oneOf': [{'type': 'string'}, {'type': 'integer'}]},
            'description': '已曝光、已购买、不可售等需排除的商品 ID。',
        },
    }


def _similar_properties():
    properties = _common_recall_properties()
    properties.update({
        'seed_items': copy.deepcopy(SEED_ITEMS_SCHEMA),
        'algorithms': {
            'type': 'array',
            'uniqueItems': True,
            'items': {'type': 'string', 'enum': ALGORITHM_NAMES},
        },
        'per_seed_k': {
            'type': 'integer', 'minimum': 1, 'maximum': 200, 'default': 50,
        },
    })
    return properties


def _dimension_properties():
    properties = _common_recall_properties()
    properties.update({
        'context': copy.deepcopy(CONTEXT_SCHEMA),
        'interests': copy.deepcopy(INTERESTS_SCHEMA),
        'list_types': {
            'type': 'array',
            'minItems': 1,
            'uniqueItems': True,
            'items': {'type': 'string', 'enum': LIST_TYPE_NAMES},
        },
        'per_source_k': {
            'type': 'integer', 'minimum': 1, 'maximum': 500, 'default': 100,
        },
    })
    return properties


def get_tool_definitions():
    """Return framework-neutral function tool definitions."""
    hybrid_properties = _similar_properties()
    hybrid_properties.update(_dimension_properties())
    hybrid_properties.update({
        'collaborative_weight': {
            'type': 'number', 'minimum': 0, 'maximum': 1, 'default': 0.65,
        },
        'dimension_weight': {
            'type': 'number', 'minimum': 0, 'maximum': 1, 'default': 0.35,
        },
    })
    hybrid_properties['seed_items']['minItems'] = 0

    return [
        {
            'type': 'function',
            'function': {
                'name': 'recommendation_capabilities',
                'description': (
                    '查看可用协同算法、画像维度、索引语义和推荐工具调用顺序。'
                    '首次接入或不确定参数时调用。'
                ),
                'parameters': {
                    'type': 'object', 'properties': {},
                    'additionalProperties': False,
                },
            },
        },
        {
            'type': 'function',
            'function': {
                'name': 'resolve_recommendation_dimension',
                'description': (
                    '把分类、供应商、平台等自然语言名称解析为离线索引使用的内部维度值。'
                    '只返回候选映射,不做商品召回。'
                ),
                'parameters': {
                    'type': 'object',
                    'additionalProperties': False,
                    'properties': {
                        'dimension': {'type': 'string', 'enum': DIMENSION_NAMES},
                        'query': {'type': 'string', 'minLength': 1, 'maxLength': 200},
                        'limit': {
                            'type': 'integer', 'minimum': 1, 'maximum': 20,
                            'default': 10,
                        },
                    },
                    'required': ['dimension', 'query'],
                },
            },
        },
        {
            'type': 'function',
            'function': {
                'name': 'recall_similar_products',
                'description': (
                    '基于用户明确感兴趣的种子商品,从 Swing、序列、图和内容索引中'
                    '召回相似商品,并返回可追溯的多算法融合证据。'
                ),
                'parameters': {
                    'type': 'object',
                    'additionalProperties': False,
                    'properties': _similar_properties(),
                    'required': ['seed_items'],
                },
            },
        },
        {
            'type': 'function',
            'function': {
                'name': 'recall_products_by_dimensions',
                'description': (
                    '根据平台、国家、客户类型、分类、供应商等画像维度召回热门、'
                    '高加购、新品和全局商品;自动使用组合维度并回退到单维度。'
                ),
                'parameters': {
                    'type': 'object',
                    'additionalProperties': False,
                    'properties': _dimension_properties(),
                    'anyOf': [{'required': ['context']}, {'required': ['interests']}],
                },
            },
        },
        {
            'type': 'function',
            'function': {
                'name': 'build_recommendation_candidates',
                'description': (
                    '为智能导购生成高召回候选池:融合种子商品协同召回与画像维度召回,'
                    '去重、排除已知商品,并保留供 LLM 挑选的来源证据。'
                ),
                'parameters': {
                    'type': 'object',
                    'additionalProperties': False,
                    'properties': hybrid_properties,
                    'anyOf': [
                        {'required': ['seed_items']},
                        {'required': ['context']},
                        {'required': ['interests']},
                    ],
                },
            },
        },
    ]