#!/usr/bin/env python3
"""
直接测试 Pydantic 模型字段修改
"""
import sys
import os
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))

from llama_index.vector_stores.qdrant import QdrantVectorStore
import pydantic

print("=== 直接修改 Pydantic 模型字段 ===")

# 1. 查看原始字段定义
print("1. 原始字段定义:")
field = QdrantVectorStore.model_fields['path']
print(f"   field: {field}")
print(f"   annotation: {field.annotation}")
print(f"   is_required(): {field.is_required()}")

# 2. 尝试直接修改字段
print("\n2. 尝试修改字段定义...")
try:
    # 创建一个新的 FieldInfo，使其不再是必需字段
    from pydantic.fields import FieldInfo
    
    # 获取原始字段信息
    original_field = QdrantVectorStore.model_fields['path']
    
    # 创建一个新的 FieldInfo，设置默认值为 None
    new_field = FieldInfo(
        annotation=original_field.annotation,
        default=None,  # 设置默认值
        default_factory=None,
        title=original_field.title,
        description=original_field.description,
        examples=original_field.examples,
        json_schema_extra=original_field.json_schema_extra,
    )
    
    # 替换字段
    QdrantVectorStore.model_fields['path'] = new_field
    
    print("✅ 成功修改字段定义")
    print(f"   新字段 is_required(): {new_field.is_required()}")
    
except Exception as e:
    print(f"❌ 修改字段失败: {e}")
    import traceback
    traceback.print_exc()

# 3. 测试修改后的效果
print("\n3. 测试修改后的效果...")
try:
    from unittest.mock import Mock
    mock_client = Mock()
    
    # 尝试创建实例
    store = QdrantVectorStore(
        collection_name="test_collection",
        client=mock_client,
        enable_hybrid=False
    )
    
    print("✅ 成功创建 QdrantVectorStore 实例！")
    print(f"   path 字段值: {store.path}")
    
except Exception as e:
    print(f"❌ 创建实例失败: {e}")
    import traceback
    traceback.print_exc()