CoolFace
Apppublic

Dwild/ChuanhuChatGPT

sourceHugging Facegpl-3.0updated 3y agoView on Hugging Face
0likes
llama_func.cpython-39.pyc66 linesDownload Raw Back to __pycache__
1a

2�,dj�@s�ddlZddlZddlmZddlmZmZmZmZmZddl	Z	ddl3Z4ddlmZddlTddl
Tdd�Zdd	�Zd5d�Zddd�Zdd�ZdS)�N)�download_loader)�Document�LLMPredictor�PromptHelper�QuestionAnswerPrompt�RefinePrompt)�tqdm)�*c	Cs|dd�|D�}|jdd�d�t��}|D]H}t|d��*}|�d�}rT|�|�q:Wd�q*1sh0Yq*|��S)NcSsg|]6}|j�qS�)�name)�.0�xr7r8�K/Users/treediagram/Documents/Developer/ChuanhuChatGPT/modules/llama_func.py�9<listcomp>�z"get_index_name.<locals>.<listcomp>cSstj�|�S)N)�os�path�basename)r
r10r11r�<lambda>rz get_index_name.<locals>.<lambda>)�key�rbi )�sort�hashlib�md5�open�read�update�	hexdigest)�file_src�12file_pathsZmd5_hash�	file_path�f�chunkr13r14r�get_index_names,r#cCs8g}t|�dkr4|�t|dd���|dd�}q|S)Nri�)�len�appendr)�text�blocksr15r16r�block_splits17r(cCsg}t�d�t�d|���|D�]�}|j}tj�|�}tj�|�d}t�d|���|dk�rt�d�z8ddlm	}dd	l18m}|d19�dd�}|||�j
}	Wn`d
}	t|d��6}20t�|21�}t|j�D]}|	|��7}	q�Wd�n1s�0YYn0|	}
n�|dk�rDt�d�td�}|�}|j|d�dj
}
n�|dk�rzt�d�td�}|�}|j|d�dj
}
n`|dk�r�t�d�t|�}
nBt�d�t|ddd��}|��}
Wd�n1�s�0Yt|
�}|t|�g7}q"t�d�|S)NzLoading documents...z22file_src: �zloading file: z.pdfzLoading PDF...r)�	parse_pdf)�advance_docs�pdf�23two_columnF�rz.docxzLoading Word...�24DocxReader)�filez.epubzLoading EPUB...�25EpubReaderz.xlsxzLoading Excel...zLoading text file...�rzutf-8)�encodingzDocuments loaded.)�logging�debugrrrr�splitext�infoZmodules.pdf_funcr*�modules.configr+�getr&r�PyPDF2�	PdfReaderr�pages�extract_textr�	load_data�excel_to_stringr�	add_spacer)r�	documentsr0�filepath�filename�	file_typer*r+r-ZpdftextZ26pdfFileObjZ	pdfReader�page�text_rawr/�loaderr1r!r&r27r28r�
get_documents&sT293031323323435363738394041(42rH����X� c43Cs�ddlm}ddlm}	m}44|tjd<|dkr2dn|}|dkrBdn|}|dkrRdn|}t|d|d�d	�}t||||d45|d�}t	|�}
tj46�d|
�d
��r�t�
d�|	�d|
�d
��Sz�t|�}t�
d�t��.|47j|||d�}|	j||d�}Wd�n1�s0Yt�d�tjddd�|�d|
�d
��t�d�|WSt�y�}z"t�d|�t|�WYd}~dSd}~00dS)Nr)�48ChatOpenAI)�GPTSimpleVectorIndex�ServiceContext�OPENAI_API_KEYr.rMzgpt-3.5-turbo-0301)�49model_name�openai_api_key)�llmrL)�max_input_size�50num_output�max_chunk_overlap�embedding_limit�chunk_size_limit�	separatorz./index/z.jsonu0找到了缓存的索引文件,加载中……u构建索引中……)�
llm_predictor�
prompt_helperrY)�service_contextu索引构建完成!z./indexT)�exist_oku索引已保存至本地!u索引构建失败!)Zlangchain.chat_modelsrN�llama_indexrOrPr�environrrr#r�existsr4r7�load_from_diskrH�retrieve_proxy�
from_defaults�from_documentsr5�makedirs�save_to_disk�	Exception�error�print)�api_keyrrUZnum_outputsrWrYrXrZrNrOrPr[r\�51index_namerAr]�index�er52r53r�construct_indexVs>545556�5758�&5960rocCs4ddddddd�}|��D]\}}|�||�}q|S)Nu, u。 u? u! u: u; )u,u。u?u!u:u;)�items�replace)r&ZpunctuationsZcn_puncZen_puncr61r62rr@�sr@)rIrJrKrLNrM)rr4r_rrrrrr�coloramar:r�modules.presets�
modules.utilsr#r(rHror@r63r64r65r�<module>s&3�66/