
    Fdc"                       d dl mZ d dlZd dlZd dlmZ d dlZd dlm	Z	 d dl
mZ d dlmZ d dlmZ d dlmZmZmZ d	d
lmZmZ ed         Zej         G d d                      Zej         G d d                      ZdS )    )annotationsN)Literal)Config)Message)count_string_tokens)logger)chunk_content
split_textsummarize_text   )	Embeddingget_embedding)webpage	text_file	code_fileagent_historyc                      e Zd ZU dZded<   ded<   ded<   ded<   ded	<   d
ed<   ded<   d,d-dZei ddfd.d            Zed/d            Zed0d            Z	ed1d#            Z
e	 d,d2d&            Zd3d4d)Zd5d+ZdS )6
MemoryItemz:Memory object containing raw content as well as embeddingsstrraw_contentsummaryz	list[str]chunkschunk_summariesr   	e_summaryzlist[Embedding]e_chunksdictmetadataNquerye_queryEmbedding | Nonec                :    t                               | ||          S N)MemoryItemRelevanceof)selfr   r   s      UC:\Users\Administrator\Downloads\Auto-GPT-master\autogpt\memory\vector\memory_item.pyrelevance_forzMemoryItem.relevance_for    s    "%%dE7;;;    textsource_typeMemoryDocTypeconfigr   how_to_summarize
str | Nonequestion_for_summaryc           	        t          j        dd d|  dd d           d |dk    rt          | j                  nt	          | j                  D             }t          j        dt          |          z              d fd|D             D             }t          j        d	t          |          z              t          |          }t          |          d
k    r|d         n*t          d	                    |                    d         }	t          j        d|	z              t          |	          }
||d<   t          | |	|||
||          S )NzMemorizing text:
z --------------------------------
c                    g | ]\  }}|S  r3   ).0chunk_s      r&   
<listcomp>z(MemoryItem.from_text.<locals>.<listcomp>.   s,     
 
 
q 
 
 
r(   r   zChunks: c                    g | ]\  }}|S r3   r3   )r4   r   r6   s      r&   r7   z(MemoryItem.from_text.<locals>.<listcomp>8   s,     
 
 
 
 
 
r(   c                6    g | ]}t          |           S )instructionquestion)r   )r4   
text_chunkr,   r-   r/   s     r&   r7   z(MemoryItem.from_text.<locals>.<listcomp>:   sF          01	    r(   zChunk summaries: r   r   z

r:   zTotal summary: r*   )r   )r   debugr
   embedding_modelr	   r   r   lenr   joinr   )r)   r*   r,   r   r-   r/   r   r   r   r   r   s     ` ``     r&   	from_textzMemoryItem.from_text#   s    	F&FFDFFFFFFGGG
 
 +-- 4!7@@@"4)?@@
 
 
 	Z#f++-...
 
      #)  
 
 
 	(3+?+??@@@ 00 6{{a AO,,,-   	 	 	&0111 "'622	"-
 
 
 	
r(   contentpathc                @    t                               | d|d|i          S )Nr   locationr   rB   )rC   rD   r,   s      r&   from_text_filezMemoryItem.from_text_filec   s!    ##G[&:tBTUUUr(   c                >    t                               | dd|i          S )Nr   rF   rG   )rC   rD   s     r&   from_code_filezMemoryItem.from_code_fileg   s!     ##G[:t:LMMMr(   
ai_messager   result_messagec                ,   | j         dk    rt          d| j                    |j                            d          r|j        nd}|j                            d          r|j        nd}d| j         d| d| }t                              |d	d
          S )N	assistantzInvalid role on 'ai_message': CommandNonezHuman feedbackzAssistant Reply: z


Result: z

Human Feedback: r   zif possible, also make clear the link between the command in the assistant's response and the command result. Do not mention the human feedback if there is none)r)   r*   r-   )role
ValueErrorrC   
startswithr   rB   )rK   rL   result
user_inputmemory_contents        r&   from_ai_actionzMemoryItem.from_ai_actionl   s    
 ?k))OjoOOPPP %00;;N"" 	 %001ABBN"" 	,
 2 , ,, ,  *	, , 	 ##' @ $ 
 
 	
r(   urlr<   c                D    t                               | d|d|i|          S )Nr   rF   )r)   r*   r,   r   r/   rG   )rC   rX   r,   r<   s       r&   from_webpagezMemoryItem.from_webpage   s5     ##! #&!) $ 
 
 	
r(   Freturnc           
         |r&t          | j        t                      j                  }d|r| dnd t	          | j                   dt          j        | j        d           d| j	         d| j         d	
S )
Nz2
=============== MemoryItem ===============
Size: z tokens in  z chunks
Metadata:    )indentz,
---------------- SUMMARY -----------------
z,
------------------ RAW -------------------
z,
==========================================
)
r   r   r   r?   r@   r   jsondumpsr   r   )r%   calculate_lengthtoken_lengths      r&   dumpzMemoryItem.dump   s     	. &((": L	'7?,####R	ADT]ASAS	 	 :dmA...	 	
 	 	 	 	 	 		r(   otherc                   | j         |j         k    oS| j        |j        k    oB| j        |j        k    o1t          j        t          | j        t          j                  r| j        n$t          j        | j        t          j	                  t          |j        t          j                  r|j        n$t          j        |j        t          j	                            ot          j        t          | j
        d         t          j                  r| j
        nd | j
        D             t          |j
        d         t          j                  r|j
        nd |j
        D                       S )Ndtyper   c                N    g | ]"}t          j        |t           j                   #S rg   nparrayfloat32r4   cs     r&   r7   z%MemoryItem.__eq__.<locals>.<listcomp>   s)    KKKbhq
333KKKr(   c                N    g | ]"}t          j        |t           j                   #S rj   rk   ro   s     r&   r7   z%MemoryItem.__eq__.<locals>.<listcomp>   s)    LLLbhq
333LLLr(   )r   r   r   rl   array_equal
isinstancer   ndarrayrm   rn   r   )r%   re   s     r&   __eq__zMemoryItem.__eq__   s5    11 u|+$(==
 dnbj99@XdnBJ???eorz::AXeoRZ@@@  dmA.
;;LKKT]KKKenQ/<<MLLU^LLL 	
r(   r"   )r   r   r   r    )r)   r   r*   r+   r,   r   r   r   r-   r.   r/   r.   )rC   r   rD   r   r,   r   )rC   r   rD   r   )rK   r   rL   r   )rC   r   rX   r   r,   r   r<   r.   )F)r[   r   )re   r   )__name__
__module____qualname____doc____annotations__r'   staticmethodrB   rH   rJ   rW   rZ   rd   ru   r3   r(   r&   r   r      sq        DDLLLNNN< < < < < 
 '++/=
 =
 =
 =
 \=
~ V V V \V N N N \N 
 
 
 \
> GK	
 	
 	
 	
 \	
     
 
 
 
 
 
r(   r   c                      e Zd ZU dZded<   ded<   ded<   ded	<   e	 ddd            Zedd            Zedd            Z	edd            Z
d Zd
S )r#   z
    Class that encapsulates memory relevance search functionality and data.
    Instances contain a MemoryItem and its relevance scores for a given query.
    r   memory_itemr   	for_queryfloatsummary_relevance_scorezlist[float]chunk_relevance_scoresNr   r    r[   c                    |pt          |          }t                              | |          \  }}}t          || ||          S )N)r~   r}   r   r   )r   r#   calculate_scores)r}   r~   r   r6   srscrss         r&   r$   zMemoryItemRelevance.of   sT     5]955)::;PP3"#$'#&	
 
 
 	
r(   memory
compare_tor    tuple[float, float, list[float]]c                "   t          j        | j        |          }t          j        | j        |          }t	          j        d|            t	          j        d|            |g|}t	          j        d|            t          |          ||fS )a0  
        Calculates similarity between given embedding and all embeddings of the memory

        Returns:
            float: the aggregate (max) relevance score of the memory
            float: the relevance score of the memory summary
            list: the relevance scores of the memory chunks
        zRelevance of summary: zRelevance of chunks: zRelevance scores: )rl   dotr   r   r   r>   max)r   r   r   r   relevance_scoress        r&   r   z$MemoryItemRelevance.calculate_scores   s     #%&)9:"F"F!#!D!DG.EGGHHHE-CEEFFF3M6LM<*:<<===#$$&=?UUUr(   c                :    t          | j        g| j                  S )zDThe aggregate relevance score of the memory item for the given query)r   r   r   r%   s    r&   scorezMemoryItemRelevance.score   s      D0O43NOPPPr(   tuple[str, float]c                r    t          j        | j                  }| j        j        |         | j        |         fS )zJThe most relevant chunk of the memory item + its score for the given query)rl   argmaxr   r}   r   )r%   i_relmaxs     r&   most_relevant_chunkz'MemoryItemRelevance.most_relevant_chunk   s4     9T899&x0$2Mh2WWWr(   c                >    | j         j         d| j         d| j         S )Nz (z) )r}   r   r   r   r   s    r&   __str__zMemoryItemRelevance.__str__   s9    ' - -4+G - -*- -	
r(   r"   )r}   r   r~   r   r   r    r[   r#   )r   r   r   r   r[   r   )r[   r   )r[   r   )rv   rw   rx   ry   rz   r{   r$   r   propertyr   r   r   r3   r(   r&   r#   r#      s          
 NNN""""''''MQ

 

 

 

 \

 V V V \V( Q Q Q XQ X X X XX

 
 
 
 
r(   r#   )
__future__r   dataclassesr`   typingr   numpyrl   autogpt.configr   autogpt.llmr   autogpt.llm.utilsr   autogpt.logsr   autogpt.processing.textr	   r
   r   utilsr   r   r+   	dataclassr   r#   r3   r(   r&   <module>r      sV   " " " " " "                ! ! ! ! ! !       1 1 1 1 1 1       M M M M M M M M M M + + + + + + + +LM j
 j
 j
 j
 j
 j
 j
 j
Z <
 <
 <
 <
 <
 <
 <
 <
 <
 <
r(   