
    D_iW                    2   S r SSKJr  SSKrSSKrSSKJrJr  SSKJrJ	r	J
r
  SSKJr  SSKJr  SSKJr  SS	KJr  \(       a  SS
KJr  SSKJr   " S S\5      r " S S\
5      r " S S\5      r " S S\
5      r " S S\
SS9r\" SS9 " S S\5      5       rg)zBase classes for indexing.    )annotationsN)ABCabstractmethod)TYPE_CHECKINGAny	TypedDict)override)beta)BaseRetriever)run_in_executor)Sequence)Documentc                     \ rS rSrSr    SS jr\SS j5       r\SS j5       r\SS j5       r	\SS j5       r
\SSS	.       SS
 jj5       r\SSS	.       SS jj5       r\SS j5       r\SS j5       r\SSSSS.         SS jj5       r\SSSSS.         SS jj5       r\SS j5       r\SS j5       rSrg)RecordManager   aO  Abstract base class representing the interface for a record manager.

The record manager abstraction is used by the langchain indexing API.

The record manager keeps track of which documents have been
written into a `VectorStore` and when they were written.

The indexing API computes hashes for each document and stores the hash
together with the write time and the source id in the record manager.

On subsequent indexing runs, the indexing API can check the record manager
to determine which documents have already been indexed and which have not.

This allows the indexing API to avoid re-indexing documents that have
already been indexed, and to only index new documents.

The main benefit of this abstraction is that it works across many vectorstores.
To be supported, a `VectorStore` needs to only support the ability to add and
delete documents by ID. Using the record manager, the indexing API will
be able to delete outdated documents and avoid redundant indexing of documents
that have already been indexed.

The main constraints of this abstraction are:

1. It relies on the time-stamps to determine which documents have been
    indexed and which have not. This means that the time-stamps must be
    monotonically increasing. The timestamp should be the timestamp
    as measured by the server to minimize issues.
2. The record manager is currently implemented separately from the
    vectorstore, which means that the overall system becomes distributed
    and may create issues with consistency. For example, writing to
    record manager succeeds, but corresponding writing to `VectorStore` fails.
c                    Xl         g)z[Initialize the record manager.

Args:
    namespace: The namespace for the record manager.
N	namespace)selfr   s     V/home/james-whalen/.local/lib/python3.13/site-packages/langchain_core/indexing/base.py__init__RecordManager.__init__9   s	     #    c                    g)z2Create the database schema for the record manager.N r   s    r   create_schemaRecordManager.create_schemaD       r   c                   #    g7f)zAAsynchronously create the database schema for the record manager.Nr   r   s    r   acreate_schemaRecordManager.acreate_schemaH           c                    g)a  Get the current server time as a high resolution timestamp!

It's important to get this from the server to ensure a monotonic clock,
otherwise there may be data loss when cleaning up old documents!

Returns:
    The current server time as a float timestamp.
Nr   r   s    r   get_timeRecordManager.get_timeL   r   r   c                   #    g7f)a  Asynchronously get the current server time as a high resolution timestamp.

It's important to get this from the server to ensure a monotonic clock,
otherwise there may be data loss when cleaning up old documents!

Returns:
    The current server time as a float timestamp.
Nr   r   s    r   	aget_timeRecordManager.aget_timeW   r#   r$   N	group_idstime_at_leastc                   g)a  Upsert records into the database.

Args:
    keys: A list of record keys to upsert.
    group_ids: A list of group IDs corresponding to the keys.
    time_at_least: Optional timestamp. Implementation can use this
        to optionally verify that the timestamp IS at least this time
        in the system that stores the data.

        e.g., use to validate that the time in the postgres database
        is equal to or larger than the given timestamp, if not
        raise an error.

        This is meant to help prevent time-drift issues since
        time may not be monotonically increasing!

Raises:
    ValueError: If the length of keys doesn't match the length of group_ids.
Nr   r   keysr,   r-   s       r   updateRecordManager.updateb   r   r   c                  #    g7f)a  Asynchronously upsert records into the database.

Args:
    keys: A list of record keys to upsert.
    group_ids: A list of group IDs corresponding to the keys.
    time_at_least: Optional timestamp. Implementation can use this
        to optionally verify that the timestamp IS at least this time
        in the system that stores the data.

        e.g., use to validate that the time in the postgres database
        is equal to or larger than the given timestamp, if not
        raise an error.

        This is meant to help prevent time-drift issues since
        time may not be monotonically increasing!

Raises:
    ValueError: If the length of keys doesn't match the length of group_ids.
Nr   r/   s       r   aupdateRecordManager.aupdate~   r#   r$   c                    g)Check if the provided keys exist in the database.

Args:
    keys: A list of keys to check.

Returns:
    A list of boolean values indicating the existence of each key.
Nr   r   r0   s     r   existsRecordManager.exists   r   r   c                   #    g7f)zAsynchronously check if the provided keys exist in the database.

Args:
    keys: A list of keys to check.

Returns:
    A list of boolean values indicating the existence of each key.
Nr   r8   s     r   aexistsRecordManager.aexists   r#   r$   beforeafterr,   limitc                   g)ao  List records in the database based on the provided filters.

Args:
    before: Filter to list records updated before this time.
    after: Filter to list records updated after this time.
    group_ids: Filter to list records with specific group IDs.
    limit: optional limit on the number of records to return.

Returns:
    A list of keys for the matching records.
Nr   r   r?   r@   r,   rA   s        r   	list_keysRecordManager.list_keys   r   r   c                  #    g7f)a~  Asynchronously list records in the database based on the provided filters.

Args:
    before: Filter to list records updated before this time.
    after: Filter to list records updated after this time.
    group_ids: Filter to list records with specific group IDs.
    limit: optional limit on the number of records to return.

Returns:
    A list of keys for the matching records.
Nr   rC   s        r   
alist_keysRecordManager.alist_keys   r#   r$   c                    gzWDelete specified records from the database.

Args:
    keys: A list of keys to delete.
Nr   r8   s     r   delete_keysRecordManager.delete_keys   r   r   c                   #    g7f)zfAsynchronously delete specified records from the database.

Args:
    keys: A list of keys to delete.
Nr   r8   s     r   adelete_keysRecordManager.adelete_keys   r#   r$   r   r   strreturnNonerR   rS   rR   floatr0   Sequence[str]r,   zSequence[str | None] | Noner-   float | NonerR   rS   r0   rX   rR   z
list[bool]
r?   rY   r@   rY   r,   zSequence[str] | NonerA   z
int | NonerR   	list[str]r0   rX   rR   rS   )__name__
__module____qualname____firstlineno____doc__r   r   r   r!   r&   r)   r1   r4   r9   r<   rD   rG   rK   rN   __static_attributes__r   r   r   r   r      s    D	#	# 
	# A A P P     
 26&* /	
 $ 
 6 
 26&* /	
 $ 
 6       $"*.   	
 (  
 (   $"*.   	
 (  
 (    r   r   c                  *    \ rS rSr% S\S'   S\S'   Srg)_Record   z
str | Nonegroup_idrV   
updated_atr   N)r^   r_   r`   ra   __annotations__rc   r   r   r   re   re      s    r   re   c                  ,  ^  \ rS rSrSrSU 4S jjrSS jrSS jr\SS j5       r	\SS j5       r
SSS	.       SS
 jjrSSS	.       SS jjrSS jrSS jrSSSSS.         SS jjrSSSSS.         SS jjrSS jrSS jrSrU =r$ )InMemoryRecordManager   z1An in-memory record manager for testing purposes.c                >   > [         TU ]  U5        0 U l        Xl        g)zeInitialize the in-memory record manager.

Args:
    namespace: The namespace for the record manager.
N)superr   recordsr   )r   r   	__class__s     r   r   InMemoryRecordManager.__init__   s      	# ,."r   c                    gzJIn-memory schema creation is simply ensuring the structure is initialized.Nr   r   s    r   r   #InMemoryRecordManager.create_schema   r   r   c                   #    g7frs   r   r   s    r   r!   $InMemoryRecordManager.acreate_schema  r#   r$   c                ,    [         R                   " 5       $ N)timer   s    r   r&   InMemoryRecordManager.get_time  s    yy{r   c                *   #    U R                  5       $ 7frx   )r&   r   s    r   r)   InMemoryRecordManager.aget_time	  s     }}   Nr+   c               *   U(       a%  [        U5      [        U5      :w  a  Sn[        U5      e[        U5       HX  u  pVU(       a  X%   OSnU(       a   X0R                  5       :  a  Sn[        U5      eXpR                  5       S.U R                  U'   MZ     g)a  Upsert records into the database.

Args:
    keys: A list of record keys to upsert.
    group_ids: A list of group IDs corresponding to the keys.

    time_at_least: Optional timestamp. Implementation can use this
        to optionally verify that the timestamp IS at least this time
        in the system that stores.
        E.g., use to validate that the time in the postgres database
        is equal to or larger than the given timestamp, if not
        raise an error.
        This is meant to help prevent time-drift issues since
        time may not be monotonically increasing!

Raises:
    ValueError: If the length of keys doesn't match the length of group
        ids.
    ValueError: If time_at_least is in the future.
z-Length of keys must match length of group_idsNz!time_at_least must be in the past)rg   rh   )len
ValueError	enumerater&   ro   )r   r0   r,   r-   msgindexkeyrg   s           r   r1   InMemoryRecordManager.update  su    6 Tc)n4ACS/!#D/JE+4y'$H!@9 o%-5]]_ UDLL *r   c               ,   #    U R                  XUS9  g7f)aQ  Async upsert records into the database.

Args:
    keys: A list of record keys to upsert.
    group_ids: A list of group IDs corresponding to the keys.

    time_at_least: Optional timestamp. Implementation can use this
        to optionally verify that the timestamp IS at least this time
        in the system that stores.
        E.g., use to validate that the time in the postgres database
        is equal to or larger than the given timestamp, if not
        raise an error.
        This is meant to help prevent time-drift issues since
        time may not be monotonically increasing!
r+   N)r1   r/   s       r   r4   InMemoryRecordManager.aupdate2  s     , 	D]K   c                J    U Vs/ s H  o"U R                   ;   PM     sn$ s  snf )r7   ro   r   r0   r   s      r   r9   InMemoryRecordManager.existsJ  s#     044tt||#t444s    c                ,   #    U R                  U5      $ 7f)zAsync check if the provided keys exist in the database.

Args:
    keys: A list of keys to check.

Returns:
    A list of boolean values indicating the existence of each key.
)r9   r8   s     r   r<   InMemoryRecordManager.aexistsU  s      {{4  r   r>   c                   / nU R                   R                  5        HL  u  pgU(       a  US   U:  a  M  U(       a  US   U::  a  M)  U(       a  US   U;  a  M;  UR                  U5        MN     U(       a  USU $ U$ )as  List records in the database based on the provided filters.

Args:
    before: Filter to list records updated before this time.

    after: Filter to list records updated after this time.

    group_ids: Filter to list records with specific group IDs.

    limit: optional limit on the number of records to return.


Returns:
    A list of keys for the matching records.
rh   rg   N)ro   itemsappend)r   r?   r@   r,   rA   resultr   datas           r   rD   InMemoryRecordManager.list_keys`  sz    . ++-IC$|,6l+u4T*-Y>MM# . &5>!r   c               *   #    U R                  XX4S9$ 7f)ay  Async list records in the database based on the provided filters.

Args:
    before: Filter to list records updated before this time.

    after: Filter to list records updated after this time.

    group_ids: Filter to list records with specific group IDs.

    limit: optional limit on the number of records to return.


Returns:
    A list of keys for the matching records.
r>   )rD   rC   s        r   rG    InMemoryRecordManager.alist_keys  s"     . ~~)  
 	
r}   c                R    U H!  nX R                   ;   d  M  U R                   U	 M#     grJ   r   r   s      r   rK   !InMemoryRecordManager.delete_keys  s$     Cll"LL% r   c                .   #    U R                  U5        g7f)z]Async delete specified records from the database.

Args:
    keys: A list of keys to delete.
N)rK   r8   s     r   rN   "InMemoryRecordManager.adelete_keys  s      	s   )r   ro   rP   rT   rU   rW   rZ   r[   r]   )r^   r_   r`   ra   rb   r   r   r!   r	   r&   r)   r1   r4   r9   r<   rD   rG   rK   rN   rc   __classcell__)rp   s   @r   rk   rk      sT   ;
#YY     26&*#V#V /	#V
 $#V 
#VR 26&*LL /	L
 $L 
L0	5	!  $"*. " " 	"
 (" " 
"N  $"*. 
 
 	

 (
 
 

6& r   rk   c                  0    \ rS rSr% SrS\S'    S\S'   Srg)UpsertResponsei  a  A generic response for upsert operations.

The upsert response will be used by abstractions that implement an upsert
operation for content that can be upserted by ID.

Upsert APIs that accept inputs with IDs and generate IDs internally
will return a response that includes the IDs that succeeded and the IDs
that failed.

If there are no failures, the failed list will be empty, and the order
of the IDs in the succeeded list will match the order of the input documents.

If there are failures, the response becomes ill defined, and a user of the API
cannot determine which generated ID corresponds to which input document.

It is recommended for users explicitly attach the IDs to the items being
indexed to avoid this issue.
r\   	succeededfailedr   Nr^   r_   r`   ra   rb   ri   rc   r   r   r   r   r     s    & 1'r   r   c                  H    \ rS rSr% SrS\S'    S\S'    S\S'    S\S'   S	rg
)DeleteResponsei  zA generic response for delete operation.

The fields in this response are optional and whether the `VectorStore`
returns them or not is up to the implementation.
intnum_deletedrX   r   r   
num_failedr   Nr   r   r   r   r   r     s8        O8r   r   F)totalz6Added in 0.2.29. The abstraction is subject to change.)messagec                      \ rS rSrSr\R                  SS j5       r      SS jr\R                  SSS jj5       r	 S     SS jjr
\R                        SS j5       r      SS	 jrS
rg)DocumentIndexi  a  A document retriever that supports indexing operations.

This indexing interface is designed to be a generic abstraction for storing and
querying documents that has an ID and metadata associated with it.

The interface is designed to be agnostic to the underlying implementation of the
indexing system.

The interface is designed to support the following operations:

1. Storing document in the index.
2. Fetching document by ID.
3. Searching for document using a query.
c                   g)a  Upsert documents into the index.

The upsert functionality should utilize the ID field of the content object
if it is provided. If the ID is not provided, the upsert method is free
to generate an ID for the content.

When an ID is specified and the content already exists in the `VectorStore`,
the upsert method should update the content with the new data. If the content
does not exist, the upsert method should add the item to the `VectorStore`.

Args:
    items: Sequence of documents to add to the `VectorStore`.
    **kwargs: Additional keyword arguments.

Returns:
    A response object that contains the list of IDs that were
    successfully added or updated in the `VectorStore` and the list of IDs that
    failed to be added or updated.
Nr   r   r   kwargss      r   upsertDocumentIndex.upsert  r   r   c               N   #    [        SU R                  U40 UD6I Sh  vN $  N7f)a  Add or update documents in the `VectorStore`. Async version of `upsert`.

The upsert functionality should utilize the ID field of the item
if it is provided. If the ID is not provided, the upsert method is free
to generate an ID for the item.

When an ID is specified and the item already exists in the `VectorStore`,
the upsert method should update the item with the new data. If the item
does not exist, the upsert method should add the item to the `VectorStore`.

Args:
    items: Sequence of documents to add to the `VectorStore`.
    **kwargs: Additional keyword arguments.

Returns:
    A response object that contains the list of IDs that were
    successfully added or updated in the `VectorStore` and the list of IDs that
    failed to be added or updated.
N)r   r   r   s      r   aupsertDocumentIndex.aupsert  s5     , %KK
 	
 
 	
 
   %#%Nc                    g)a  Delete by IDs or other criteria.

Calling delete without any input parameters should raise a ValueError!

Args:
    ids: List of IDs to delete.
    **kwargs: Additional keyword arguments. This is up to the implementation.
        For example, can include an option to delete the entire index,
        or else issue a non-blocking delete etc.

Returns:
    A response object that contains the list of IDs that were
    successfully deleted and the list of IDs that failed to be deleted.
Nr   r   idsr   s      r   deleteDocumentIndex.delete4  r   r   c                N   #    [        SU R                  U40 UD6I Sh  vN $  N7f)a  Delete by IDs or other criteria. Async variant.

Calling adelete without any input parameters should raise a ValueError!

Args:
    ids: List of IDs to delete.
    **kwargs: Additional keyword arguments. This is up to the implementation.
        For example, can include an option to delete the entire index.

Returns:
    A response object that contains the list of IDs that were
    successfully deleted and the list of IDs that failed to be deleted.
N)r   r   r   s      r   adeleteDocumentIndex.adeleteE  s5       %KK
 	
 
 	
 
r   c                   ga%  Get documents by id.

Fewer documents may be returned than requested if some IDs are not found or
if there are duplicated IDs.

Users should not assume that the order of the returned documents matches
the order of the input IDs. Instead, users should rely on the ID field of the
returned documents.

This method should **NOT** raise exceptions if no documents are found for
some IDs.

Args:
    ids: List of IDs to get.
    **kwargs: Additional keyword arguments. These are up to the implementation.

Returns:
    List of documents that were found.
Nr   r   s      r   getDocumentIndex.get\  r   r   c               N   #    [        SU R                  U40 UD6I Sh  vN $  N7fr   )r   r   r   s      r   agetDocumentIndex.agetw  s5     2 %HH
 	
 
 	
 
r   r   )r   zSequence[Document]r   r   rR   r   rx   )r   zlist[str] | Noner   r   rR   r   )r   rX   r   r   rR   zlist[Document])r^   r_   r`   ra   rb   abcr   r   r   r   r   r   r   rc   r   r   r   r   r     s     	 *
'
69
	
: 	 " '+
#
69
	
. 	 	
 
 4

 	

 

r   r   )rb   
__future__r   r   ry   r   r   typingr   r   r   typing_extensionsr	   langchain_core._apir
   langchain_core.retrieversr   langchain_core.runnablesr   collections.abcr   langchain_core.documentsr   r   re   rk   r   r   r   r   r   r   <module>r      s      " 
  # 0 0 & $ 3 4(1RC Rji 
M D(Y (4!9Ye !9H FGd
M d
 Hd
r   