
    <i.                         S SK J r Jr  S SKJr  S SKJrJr  S SKJr  S SK	J
r
  S SKJrJr   S SKJrJrJrJrJrJr   " S
 S5      rg	! \ a!  r0 r0 r\" 5       rS1r\" 5       r0 r S	rCN-S	rCff = f)    )copydeepcopy)Path)TypeAny)	BaseModel)model_json_schema)	FieldPathconvert_paths)DEFSCACHE_STR_PATHRECURSIVE_REFSEXCLUDED_RECURSIVE_REFSINCLUDED_RECURSIVE_REFSNAME_RECURSIVE_REF_MAPPINGFilterNc                   &   \ rS rSrSrSr1 SkrSS jr  SS\\	\
4   \\\	\
4      -  S	\	S-  S
\S-  S\\	\
4   \\\	\
4      -  4S jjr   SS\\	\
4   \\\	\
4      -  S\	S\S
\S-  S\\	   4
S jjrS\\   SS4S jr\4S\\	-  SS4S jjrSrg)ModelSchemaParser   a  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                    [        [        5      U l        [        [        5      U l        [        [        5      U l        [        [        5      U l	        [        [        5      U l        [        R                  " 5        VVs0 s H  u  pX_M	     snnU l        U R                  R                  5        VVs0 s H  u  p4U[        U5      _M     snnU l        0 U l        g s  snnf s  snnf )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        [/home/james-whalen/.local/lib/python3.13/site-packages/qdrant_client/embed/schema_parser.py__init__ModelSchemaParser.__init__J   s    GOPT~
,4^,D),^)<256M2N%256M2N% 8==?;
?TQAD?;
' =AKK<M<M<O7
<OLEE=''<O7
 :<&;
7
s   =C1C schemaparent	seen_refsc           
         U(       a  UOSnU(       a  UO	[        5       n[        U[        5      (       GaI  SU;   a  US   nUR                  S5      S   nXPR                  ;   a  U R                  U   $ XR:X  d  XS;   a+  U R
                  R                  U5        XR                  U'   U$ UR                  U5        U R                  U R                  U   U[        U5      S9$ 0 nSU;   aB  UR                  5        H,  u  pxUS:X  a  U R                  X[        U5      S9Xg'   M(  XU'   M.     U$ UR                  5        H?  u  px[        U[        5      (       a  SU;   a  UOUn	U R                  X[        U5      S9Xg'   MA     U$ [        U[        5      (       a*  U V
s/ s H  n
U R                  X[        U5      S9PM     sn
$ U$ s  sn
f )zReplace 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   ModelSchemaParser._replace_refs[   s     "t!*I	fd##!&>"..-b1<<<99'BB$(<((,,W5>D227;!Mg&))JJw'4	? *   Gv%"LLNDAL(%)%7%7#$tI &8 &
 &'
 + N #LLNDA&0D&9&9la>OU[J!%!3!3 tI "4 "GJ + N%% #"D ""$i"Y" 
 Ms   #F?current_pathafter_propertiesc                    / nUb  UO	[        5       nUR                  S5      n[        U5      [        [        U5      5      :w  a  U$ [        U[        5      (       d  U$ SU;   a&  US   U R
                  ;   a  UR                  U5        U$ UR                  5        GH?  u  pUS:X  a  M  US:X  a{  U	R                  S5      S   n
U R                  U
   n	X;   a  M>  XR                  ;   a  MO  XR                  ;   a1  UR                  U
5        XR                  UR                  S5      S   '   U(       a  U(       a  U SU 3nOUnOUn[        U	[        5      (       a%  UR                  U R                  XUS:H  US95        M  [        U	[        5      (       d  M  U	 H>  n[        U[        5      (       d  M  UR                  U R                  UUUS:H  US95        M@     GMB     [!        [        U5      5      $ )	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-   rA   rB   r/   document_pathsseen_recursive_refspartskeyvalue
model_namenew_pathr?   s                r*   rK   &ModelSchemaParser._find_document_paths   s   ( %'+4+@ice""3'u:SZ(!!&$''!!fD4O4O!O!!,/!! ,,.JCg~f}"[[-b1


:.4 "?"?? "6"66 (++J7S]33L4F4Fs4KB4OP".q6H"H'%&&%%--)<H[ . 
 E4((!D!$--&-- 55 $ ( #| 3*=	 6  "M )b c.)**    r(   c                    UR                   nX R                  ;   a  g[        U5      nUR                  S0 5      R	                  5        H$  u  pEX@R
                  ;  d  M  XPR
                  U'   M&     SU;   aD  SU;   a  SUS   0OSUS   0nU R                  U5      nU R                  U5      U R                  U'   O/ U R                  U'   U R                   H~  nXR                  ;   d  XR                  ;   a  M#  U R                  U R
                  U   5      (       a  U R                  R                  U5        Mc  U R                  R                  U5        M     U R                  R	                  5        VVs0 s H  u  pEXPR                  ;  d  M  XE_M     snnU l        U R                  R	                  5        VV	s0 s H  u  pU[        U	5      _M     sn	nU l        gs  snnf s  sn	nf )a  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
NrF   r1   r4   )__name__r   r	   getr!   r   r9   rK   r   r   r    r8   r"   r   r#   )
r%   r(   rR   r-   r&   r'   raw_refsrefsrefr)   s
             r*   parse_modelModelSchemaParser.parse_model   s    ^^
$"5)JJw+113DA

" !

1 4 f V# ("F<$89 
 %%h/D&*&?&?&EDKK
#&(DKK
#''C333s>[>[7[((C99--11#6--11#6 ( 77==?+
?555 AD?+
' LP;;K\K\K^_K^<55-"66K^_+
 `s   4GG9G!output_pathc                    [        US5       nUR                  SU R                   S35        UR                  SU R                   S35        UR                  S[	        U R
                  5       S35        UR                  S[	        U R                  5       S35        UR                  S[	        U R                  5       S35        UR                  SU R                   S35        S	S	S	5        g	! , (       d  f       g	= f)
zxPersist 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   rL   r   r    r   r"   )r%   r^   fs      r*   _persist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,,
C:)r   r   r   r    r$   r   r"   r#   )r   N)NN) FN)rW   
__module____qualname____firstlineno____doc__
CACHE_PATHrH   r+   r6   strr   r:   r   r9   boolrK   r   r   r\   r   re   __static_attributes__ rU   r*   r   r      s3   'R (JE<( " $	<S#Xd38n!55< d
< :	<
 
c3h$tCH~.	.<B !& $S+S#Xd38n!55S+ S+ 	S+
 :S+ 
cS+j1`i 1`T 1`f 2< YD3J Y Y YrU   r   )r   r   pathlibr   typingr   r   pydanticr   qdrant_client._pydantic_compatr	   qdrant_client.embed.utilsr
   r   %qdrant_client.embed._inspection_cacher   r   r   r   r   r   ImportErrorer   r   rp   rU   r*   <module>ry      sm        < >$ "TY TY  $DNUN'j!e!#$s   A A.A))A.