
    'XhF.                         d dl m Z mZ d dlmZ d dlmZmZmZmZ d dl	m
Z
 d dlmZ d dlmZmZ 	 d dlmZmZmZmZmZmZ  G d
 d      Zy	# e$ r!Zi Zi Z e       ZdhZ e       Zi ZY d	Z[,d	Z[ww xY w)    )copydeepcopy)Path)TypeUnionAnyOptional)	BaseModel)model_json_schema)	FieldPathconvert_paths)DEFSCACHE_STR_PATHRECURSIVE_REFSEXCLUDED_RECURSIVE_REFSINCLUDED_RECURSIVE_REFSNAME_RECURSIVE_REF_MAPPINGFilterNc                      e Zd ZdZdZh dZddZ	 	 ddeee	e
f   eee	e
f      f   dee	   d	ee   deee	e
f   eee	e
f      f   fd
Z	 	 	 ddeee	e
f   eee	e
f      f   de	ded	ee   dee	   f
dZdee   ddfdZefdeee	f   ddfdZy)ModelSchemaParsera  Model schema parser. Parses json schemas to retrieve paths to objects requiring inference.

    The parser is stateful, it accumulates the results of parsing in its internal structures.

    Attributes:
        _defs: definitions extracted from json schemas
        _recursive_refs: set of recursive refs found in the processed schemas, e.g.:
            {"Filter", "Prefetch"}
        _excluded_recursive_refs: predefined time-consuming recursive refs which don't have inference objects, e.g.:
            {"Filter"}
        _included_recursive_refs: set of recursive refs which have inference objects, e.g.:
            {"Prefetch"}
        _cache: cache of string paths for models containing objects for inference, e.g.:
            {"Prefetch": ['prefetch.query', 'prefetch.query.context.negative', ...]}
        path_cache: cache of FieldPath objects for models containing objects for inference, e.g.:
            {
                 "Prefetch": [
                     FieldPath(
                         current="prefetch",
                         tail=[
                             FieldPath(
                                 current="query",
                                 tail=[
                                     FieldPath(
                                         current="recommend",
                                         tail=[
                                             FieldPath(current="negative", tail=None),
                                             FieldPath(current="positive", tail=None),
                                         ],
                                     ),
                                     ...,
                                 ],
                             ),
                         ],
                     )
                 ]
            }
        name_recursive_ref_mapping: mapping of model field names to ref names, e.g.:
            {"prefetch": "Prefetch"}
    z_inspection_cache.py>   ImageDocumentInferenceObjectreturnNc                    t        t              | _        t        t              | _        t        t              | _        t        t              | _	        t        t              | _        t        j                         D ci c]  \  }}||
 c}}| _        | j                  j                         D ci c]  \  }}|t        |       c}}| _        i | _        y c c}}w c c}}w )N)r   r   _defsr   _cachesetr   _recursive_refsr   _excluded_recursive_refsr   _included_recursive_refsr   itemsname_recursive_ref_mappingr   
path_cache_processed_recursive_defs)selfkvmodelpathss        K/RAG/venv/lib/python3.12/site-packages/qdrant_client/embed/schema_parser.py__init__zModelSchemaParser.__init__J   s    MUVZ^
,4^,D),^)<256M2N%256M2N% 8==?;
?TQAqD?;
' =AKK<M<M<O7
<OLE5E=''<O7
 :<&;
7
s   <C/Cschemaparent	seen_refsc           
      ^   |r|nd}|r|n	t               }t        |t              rGd|v r|d   }|j                  d      d   }|| j                  v r| j                  |   S ||k(  s||v r,| j
                  j                  |       || j                  |<   |S |j                  |       | j                  | j                  |   |t        |            S i }d|v rE|j                         D ]0  \  }}|dk(  r!| j                  ||t        |            ||<   ,|||<   2 |S |j                         D ]=  \  }}t        |t              rd|v r|n|}	| j                  ||	t        |            ||<   ? |S t        |t              r+|D 
cg c]  }
| j                  |
|t        |            ! c}
S |S c c}
w )a  Replace refs in schema with their definitions

        Args:
            schema: schema to parse
            parent: previous level key
            seen_refs: set of seen refs to spot recursive paths

        Returns:
            schema with replaced refs
        N$ref/)r.   r/   
properties)r-   r.   r/   )r   
isinstancedictsplitr%   r   add_replace_refsr   r   r"   list)r&   r-   r.   r/   ref_pathdef_keyschemesr'   r(   
parent_keyitems              r+   r9   zModelSchemaParser._replace_refs[   s     "t!*I	fd#!&>"..-b1d<<<99'BBf$9(<((,,W5>DD227;!Mg&))JJw'4	? *   Gv%"LLNDAqL(%)%7%7#$VtI &8 &
 &'
 + N #LLNDAq&0D&9la>OU[J!%!3!3 tI "4 "GAJ + N% #"D ""$vi"Y" 
 Ms   $F*current_pathafter_propertiesc           
         g }||n	t               }|j                  d      }t        |      t        t        |            k7  r|S t        |t              s|S d|v r$|d   | j
                  v r|j                  |       |S |j                         D ]!  \  }}	|dk(  r|dk(  rw|	j                  d      d   }
| j                  |
   }	|
|v r:|
| j                  v rI|
| j                  v r2|j                  |
       |
| j                  |j                  d      d   <   |r|r| d| }n|}n|}t        |	t              r(|j                  | j                  |	||dk(  |             t        |	t              s|	D ]:  }t        |t              s|j                  | j                  |||dk(  |             < $ t!        t        |            S )	a  Read a schema and find paths to objects requiring inference

        Populates model fields names to ref names mapping

        Args:
            schema: schema to parse
            current_path: current path in the schema
            after_properties: flag indicating if the current path is after "properties" key
            seen_refs: set of seen refs to spot recursive paths

        Returns:
            List of string dot separated paths to objects requiring inference
        .title$defsr1   r2   r3   r4   )r/   )r   r7   lenr5   r6   INFERENCE_OBJECT_NAMESappendr"   r   r    r   r8   r#   extend_find_document_pathsr:   sorted)r&   r-   r@   rA   r/   document_pathsseen_recursive_refspartskeyvalue
model_namenew_pathr?   s                r+   rJ   z&ModelSchemaParser._find_document_paths   s   ( %'+4+@ice""3'u:SZ(!!&$'!!fD4O4O!O!!,/!! ,,.JCg~f}"[[-b1


:.!44 $"?"?? $"6"66 (++J7S]D33L4F4Fs4KB4OP".q6H"H'%&%%--x)<H[ . 
 E4(!D!$-&-- 55 $ ( #| 3*=	 6  "M )b c.)**    r)   c                    |j                   }|| j                  v ryt        |      }|j                  di       j	                         D ]#  \  }}|| j
                  vs|| j
                  |<   % d|v rBd|v rd|d   ind|d   i}| j                  |      }| j                  |      | j                  |<   ng | j                  |<   | j                  D ]t  }|| j                  v s|| j                  v r | j                  | j
                  |         r| j                  j                  |       Z| j                  j                  |       v | j                  j	                         D ci c]  \  }}|| j                  vr|| c}}| _        | j                  j	                         D 	ci c]  \  }}	|t        |	       c}	}| _        yc c}}w c c}	}w )aI  Parse model schema to retrieve paths to objects requiring inference.

        Checks model json schema, extracts definitions and finds paths to objects requiring inference.
        No parsing happens if model has already been processed.

        Args:
            model: model to parse

        Returns:
            None
        NrE   r1   r4   )__name__r   r   getr"   r   r9   rJ   r   r    r!   r8   r#   r   r$   )
r&   r)   rQ   r-   r'   r(   raw_refsrefsrefr*   s
             r+   parse_modelzModelSchemaParser.parse_model   s    ^^
$"5)JJw+113DAq

" !

1 4 f V# ("F<$89 
 %%h/D&*&?&?&EDKK
#&(DKK
#''Cd333sd>[>[7[((C9--11#6--11#6 ( 77==?+
?1555 qD?+
' LP;;K\K\K^_K^<5%5-"66K^_+
 `s   "G#G	output_pathc                    t        |d      5 }|j                  d| j                   d       |j                  d| j                   d       |j                  dt	        | j
                         d       |j                  dt	        | j                         d       |j                  dt	        | j                         d       |j                  d| j                   d       d	d	d	       y	# 1 sw Y   y	xY w)
zPersist the parser state to a file

        Args:
            output_path: path to the file to save the parser state

        Returns:
            None
        wzCACHE_STR_PATH = 
zDEFS = zRECURSIVE_REFS = zINCLUDED_RECURSIVE_REFS = zEXCLUDED_RECURSIVE_REFS = zNAME_RECURSIVE_REF_MAPPING = N)	openwriter   r   rK   r   r!   r    r#   )r&   r[   fs      r+   _persistzModelSchemaParser._persist!  s     +s#qGG'}B78GGgdjj\,-GG't/C/C(D'ERHIGG08U8U1V0WWYZ[GG08U8U1V0WWYZ[GG3D4S4S3TTVWX $##s   CC,,C5)r   N)NN) FN)rU   
__module____qualname____doc__
CACHE_PATHrG   r,   r   r6   strr   r:   r	   r   r9   boolrJ   r   r
   rZ   r   rb    rS   r+   r   r      sB   'R (JE<( !%#'	<d38nd4S>&::;< < C=	<
 
tCH~tDcN33	4<B !&#'S+d38nd4S>&::;S+ S+ 	S+
 C=S+ 
cS+j1`i 1`T 1`f 8B YE$)$4 Yd YrS   r   )r   r   pathlibr   typingr   r   r   r	   pydanticr
   qdrant_client._pydantic_compatr   qdrant_client.embed.utilsr   r   %qdrant_client.embed._inspection_cacher   r   r   r   r   r   ImportErrorer   r   rj   rS   r+   <module>rs      sp      - -  < >$ "TY TY  $DNUN'j!e!#$s   A A1A,,A1