
    7hi                     j    d dl mZ d dlmZ d dlmZ d dl mZ d dlZd dlZ	de
de
de
fd	Zdd
Z	 ddZy)    )
ChatOpenAI)PromptTemplate)Milvus)OpenAIEmbeddingsNcollection_namedb_nameuric                 D    t        dd      }t        || ||dd      }|S )Nztext-embedding-3-largez:sk-agrota-v7nqf1DdSh1EYdGJic5ST3BlbkFJr5HoyVHSYxf462AMV6hs)modelopenai_api_keyzroot:Milvus)r   r	   token)embedding_functionr   connection_args)r   r   )r   r   r	   
embeddingsconexions        6/opt/lhia/agrota/backend/python/app/filtro_vectores.pymilvus_busquedar   	   s5    !&SJ
 %'$+C-PH
 O    c                 j   g }fd}t         j                  j                  |      5 }| D 	ci c]  }	|j                  ||	      |	 }
}	t         j                  j	                  |
      D ]&  }|j                         }|s|j                  |       ( 	 d d d        |S c c}	w # 1 sw Y   |S xY w)Nc                     	 t        |       j                        }|D cg c]  }|d   dkD  s| }}|r|S 	 y c c}w # t        $ r}t        d|  d|        Y d }~y d }~ww xY wN)k   g333333?u   Error procesando la colección z: r   similarity_search_with_score	Exceptionprint		coleccioncoleccion_docsdocresultados_filtradoser   r   preguntar	   s	        r   procesar_coleccionz0procesar_colecciones.<locals>.procesar_coleccion       	F,7C**8q*9  4B#RCSVc\C#R #R#++ $  $S  	F3I;bDEE	F)   "A ??A A 	A(A##A(max_workers)
concurrentfuturesThreadPoolExecutorsubmitas_completedresultappend)coleccionesr   r	   r$   r   r)   lista_coleccionesr%   executorr   futurosfuturo	resultados    ````        r   procesar_coleccionesr7      s    
 
			.	.;	.	G 48 )
 OO.	:IE
 
 !((55g> 	4FI!((3	44 
4 s"   B(B#6B(B(#B((B2c                    g }fd}t         j                  j                  |      5 }| D 	ci c]  }	|j                  ||	      |	 }
}	t         j                  j	                  |
      D ]>  }|j                         }|s|j                  |       t        dt        |       d       @ 	 d d d        |S c c}	w # 1 sw Y   |S xY w)Nc                     	 t        |       j                        }|D cg c]  }|d   dkD  s| }}|r|S 	 y c c}w # t        $ r}t        d|  d|        Y d }~y d }~ww xY wr   r   r   s	        r   procesar_coleccion_codigoz>procesar_colecciones_codigo.<locals>.procesar_coleccion_codigo9   r&   r'   r(   zProcesadas z colecciones.)	r*   r+   r,   r-   r.   r/   r0   r   len)r1   r   r	   r$   r   r)   r2   r:   r3   r   r4   r5   r6   s    ````        r   procesar_colecciones_codigor<   4   s     
 
			.	.;	.	G 	K8 )
 OO5yA9L
 
 !((55g> 	KFI!((3C(9$:#;=IJ		K	K 
	K s"   C B;6C +C ;C  C
)d   )langchain_openair   langchain_core.prompts.chatr   langchain_milvusr   r   
statisticsconcurrent.futuresr*   strr   r7   r<    r   r   <module>rE      s@    ' 6 # -  S 3 S < 9<r   