CoolFace
Apppublic

Programmer140/Hackathon

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes
api.cpython-313.pyc39 linesDownload Raw Back to __pycache__
12��Li/��F�SSKJrJr SSKJr SSKJr SSKrSSKr\RR\RR\RR\
555 SSKJr \"5 SSKJrJr SSKJrJr \"SS	S39r\R-\/SQSS
/S
/S9 "SS\5r"SS\5r\R3S5S5r\R7S5S5r\R7S5S5r\R=S\S9S\4Sj5r\R=S5S5r \R7S5S 5r!\R7S!5S"5r"\#S#:Xa=SSK$r$SSKr\%"\RLR7S$S%55r'\$RP"\S&\'S'9 gg)(�)�FastAPI�
HTTPException)�CORSMiddleware)�	BaseModelN)�validate_config)�query_qdrant�format_response)�ingest_local_docs�create_collectionzAI Assistant Backendz'Backend API for Docusaurus AI Assistant)�title�description)zhttp://localhost:3000zhttp://localhost:5173zhttp://127.0.0.1:5173zhttp://localhost:3001zhttp://127.0.0.1:8000zhttp://localhost:8000zhttps://*.vercel.appT�*)�
allow_origins�allow_credentials�
allow_methods�
allow_headersc� �\rSrSr%\\S'Srg)�ChatRequest�&�query�N��__name__�4__module__�__qualname__�__firstlineno__�str�__annotations__�__static_attributes__r��:C:\Users\DELL\OneDrive\Desktop\New folder\Hackathon\api.pyrr&s���Jr rc� �\rSrSr%\\S'Srg)�ChatResponse�)�responserNrrr r!r#r#)s���Mr r#�startupc��\# �[S5 [5 [S5 SSKJn SSKJnJnJn U"XSS9nURUS9n[US	5(aURO([U[5(aURS	S5OSnUS:Xa([S55 SSKJn U"5n[SUS
35 g[SUS35 g![an	[SU	35 Sn	A	gSn	A	ff=f![a+n	SSKn6U7R#S[%U	535 Sn	A	gSn	A	ff=f7f)z*Ensure Qdrant collection exists on startupz$Ensuring Qdrant collection exists...zQdrant collection ensured.r��QdrantClient��8QDRANT_URL�QDRANT_API_KEY�COLLECTION_NAMEF��url�api_key�prefer_grpc��collection_name�points_countz3Collection is empty, starting document ingestion...�r9z&Document ingestion completed. Indexed z documents.zCollection already has z points, skipping ingestion.z'Error checking or ingesting documents: Nz)Error ensuring Qdrant collection exists: )�printr�
qdrant_clientr)�configr+r,r-�get_collection�hasattrr4�10isinstance�dict�getr11�	Exception�logging�errorr)r)r+r,r-r7�collection_info�point_countr12�documents_indexed�er?s           r!�
startup_eventrE,sN���L�
�4�5���
�*�+�	/�F�F�$��Y^�_�
�	A�+�:�:�?�:�[�O�:A�/�Sa�:b�:b�/�6�6�S]�^m�os�St�St�hw�h{�h{�}K�MN�iO�z{�K��a���K�L�?�$5�$7�!��>�?P�>Q�Q\�]�^��/��}�<X�Y�Z���	A��;�A�3�?�@�@��	A���L���
�
�A�#�a�&��J�K�K��L�sd�D,�7C4�BC�>D,�?C�D,�13C1�C,�'C4�+D,�,C1�1C4�414D)�>!D$�D,�$D)�)D,�/c�15�SS0$)N�messagez AI Assistant Backend is running!rrr r!�	read_rootrINs���9�:�:r z/healthc��SSSS.$)N�ok�backendF)�status�service�errorsrrr r!�health_checkrPRs������r z/chat)�response_model�requestc��R# �URnU(d16[SSS9e[U5n[X5n[US179$![a<nSSKnURS[
U535 [SS[
U53S9eSnAff=f![a<nSSKnURS[
U535 [SS	[
U53S9eSnAff=f![a e[a<nSSKnURS[
U535 [SS[
U53S9eSnAff=f7f)
Ni�zQuery is required��status_code�detailrzError querying Qdrant: ��z)Unable to connect to the knowledge base: zError formatting response: zError generating response: )r%z#Unexpected error in chat endpoint: zAn unexpected error occurred: )	rrrr>r?r@rr	r#)rRr�search_resultsrDr?r%s      r!�
chat_endpointrYZs>���_��
�
����C�8K�L�L�	n�)�%�0�N�	`�&�u�=�H��X�.�.���	n���M�M�3�C��F�8�<�=��C�:c�dg�hi�dj�ck�8l�m�m��		n���	`���M�M�7��A��x�@�A��C�:U�VY�Z[�V\�U]�8^�_�_��	`����
��_���
�
�;�C��F�8�D�E���6T�UX�YZ�U[�T\�4]�^�^��_�sg�D'�C�A�B�C�D'�18B�7B�B�C�19C�7C�C�C�D$�(7D�D$�$D'z
/admin/ingestc���# �SSKJn U"5nSSUS.$![a<nSSKnURS[	U535 [SS[	U53S	9eSnAff=f7f)20Nrr5�successzDocuments ingested successfully)rMrHrCzError during manual ingestion: rWzError during ingestion: rT)r21r>r?r@rr)r22rCrDr?s    r!�admin_ingestr\sn���Y�7�-�/��$�0Q�hy�z�z���Y���
�
�7��A��x�@�A���6N�s�ST�v�h�4W�X�X��Y�s$�A ��A �23A�7A�A�A z
/admin/statusc��# �SSKJn SSKJnJnJn U"XSS9nUR
US9n[US5(aURO([U[5(aURSS5OSnSUS	S24.$![anSSS[U5S.sSnA$SnAff=f![an[S
S[U53S9eSnAff=f7f)Nrr(r*Fr.r2r4T�healthy)�collection_existsrBrM�missing)r_rBrMr@rWzError checking status: rT)r7r)r8r+r,r-r9r:r4r;r<r=r>rr)r)r+r,r-r7rArBrDs        r!�admin_statusra�s���X�.�F�F�$��Y^�_�
�	�+�:�:�?�:�[�O�:A�/�Sa�:b�:b�/�6�6�S]�^m�os�St�St�hw�h{�h{�}K�MN�iO�z{�K�%)�*�#��
��25�	�%*� �#��Q��	�
��	���X���6M�c�RS�f�X�4V�W�W��X�sX�C�B�AA7�6C�726B�B�B�B�C�B�B�27C�(B>�>C�Cz/debug/docsc��D# �SSKnURRURRURR[55S5nURRU5(a!UR"U5nS[U5US.$SS/S.$7f)zADebug endpoint to check if docs directory exists and its contentsrN�docsT)�docs_exists�28file_count�filesF)�os�path�join�dirname�__file__�exists�listdir�len)rg�	docs_pathrfs   r!�29debug_docsrp�s����������R�W�W�_�_�R�W�W�_�_�X�-F�G��P�I�	�w�w�~�~�i� � ��3031�9�%����e�*��32�	33�!���34�	35�s�BB �__main__�PORTi@z0.0.0.0)�host�port))�fastapirr�fastapi.middleware.corsr�pydanticr�sysrgrh�appendrj�abspathrk�
secure_configr�chat_openairr	r36r�app�add_middlewarerr#�on_eventrEr=rIrP�postrYr\rarpr�uvicorn�int�environrt�runrr r!�<module>r�s���*�2��37�	����������������� 9�:�;�*���5�B��*�8a�b���������%��%���"�)���9�����i��L��L�B�����;��;������������'�,��/� _�� _�0� _�H���/��Y��Y������X��X�8�����38��39�&�z���
��r�z�z�~�~�f�d�+�,�D��K�K��)�$�/�	r