女生學電子商務專業(yè)好嗎seo推廣排名公司
GraphRAG的介紹
目前微軟已經(jīng)開源了GraphRAG的完整項目代碼。對于某一些LLM的下游任務則可以使用GraphRAG去增強自己業(yè)務的RAG的表現(xiàn)。項目給出了兩種使用方式:
- 在打包好的項目狀態(tài)下運行,可進行嘗試使用。
- 在源碼基礎上運行,適合為了下游任務的微調時使用。
如果需要利用Ollama部署本地大模型的可以參考我的另一篇博客
以下在通過自身的實踐之后的給出對這兩種方式的使用教程,如果還有什么問題在評論區(qū)交流。
一、在源碼基礎上運行(便于后續(xù)修改)
1. 準備環(huán)境(在終端運行)
(1)創(chuàng)建虛擬環(huán)境(已安裝好anaconda),此處建議使用python3.11:
conda create -n GraphRAG python=3.11
conda activate GraphRAG
2. 下載源碼并進入目錄
git clone https://github.com/microsoft/graphrag.git
cd graphrag
3. 下載依賴并初始化項目
(1)安裝poetry資源包管理工具及相關依賴:
pip install poetry
poetry install
(2)初始化
poetry run poe index --init --root .
正確運行后,此處會在graphrag目錄下生成output、prompts、.env、settings.yaml文件
4. 下載并將待檢索的文檔document放入./input/目錄下
mkdir ./input
curl https://www.xxx.com/xxx.txt > ./input/book.txt #示例,可以替換為任何的txt文件
5.修改相關配置文件
(1)修改.env文件(默認是隱藏的)中的api_key
vi .env #進入.env文件,并修改為自己的api_key
修改后是全局配置,后續(xù)不需要再次修改了
(2)修改settings.yaml文件,修改其中的使用的llm模型和對應的api_base
提前說明,因為GraphRAG需要多次調用大模型和Embedding,默認使用的是openai的GPT-4,花費及其昂貴(
土豪當我沒說,配置也不需要改),建議大家可以使用其他模型或國產(chǎn)大模型的api
我這里使用的是agicto提供的APIkey(主要是新用戶注冊可以免費獲取到10塊錢的調用額度,白嫖還是挺爽的)。我在這里主要就修改了API地址和調用模型的名稱,修改完成后的settings文件完整內(nèi)容如下:
(代碼行后有標記的為需要修改的地方),如果用的是agicto則則不用修改settings.yaml
encoding_model: cl100k_base
skip_workflows: []
llm:api_key: ${GRAPHRAG_API_KEY}type: openai_chat # or azure_openai_chatmodel: deepseek-chat #修改model_supports_json: false # recommended if this is available for your model.api_base: https://api.agicto.cn/v1 #修改# max_tokens: 4000# request_timeout: 180.0# api_version: 2024-02-15-preview# organization: <organization_id># deployment_name: <azure_model_deployment_name># tokens_per_minute: 150_000 # set a leaky bucket throttle# requests_per_minute: 10_000 # set a leaky bucket throttle# max_retries: 10# max_retry_wait: 10.0# sleep_on_rate_limit_recommendation: true # whether to sleep when azure suggests wait-times# concurrent_requests: 25 # the number of parallel inflight requests that may be madeparallelization:stagger: 0.3# num_threads: 50 # the number of threads to use for parallel processingasync_mode: threaded # or asyncioembeddings:## parallelization: override the global parallelization settings for embeddingsasync_mode: threaded # or asynciollm:api_key: ${GRAPHRAG_API_KEY}type: openai_embedding # or azure_openai_embeddingmodel: text-embedding-3-small #修改api_base: https://api.agicto.cn/v1 #修改# api_base: https://<instance>.openai.azure.com# api_version: 2024-02-15-preview# organization: <organization_id># deployment_name: <azure_model_deployment_name># tokens_per_minute: 150_000 # set a leaky bucket throttle# requests_per_minute: 10_000 # set a leaky bucket throttle# max_retries: 10# max_retry_wait: 10.0# sleep_on_rate_limit_recommendation: true # whether to sleep when azure suggests wait-times# concurrent_requests: 25 # the number of parallel inflight requests that may be made# batch_size: 16 # the number of documents to send in a single request# batch_max_tokens: 8191 # the maximum number of tokens to send in a single request# target: required # or optionalchunks:size: 300overlap: 100group_by_columns: [id] # by default, we don't allow chunks to cross documentsinput:type: file # or blobfile_type: text # or csvbase_dir: "input"file_encoding: utf-8file_pattern: ".*\\.txt$"cache:type: file # or blobbase_dir: "cache"# connection_string: <azure_blob_storage_connection_string># container_name: <azure_blob_storage_container_name>storage:type: file # or blobbase_dir: "output/${timestamp}/artifacts"# connection_string: <azure_blob_storage_connection_string># container_name: <azure_blob_storage_container_name>reporting:type: file # or console, blobbase_dir: "output/${timestamp}/reports"# connection_string: <azure_blob_storage_connection_string># container_name: <azure_blob_storage_container_name>entity_extraction:## llm: override the global llm settings for this task## parallelization: override the global parallelization settings for this task## async_mode: override the global async_mode settings for this taskprompt: "prompts/entity_extraction.txt"entity_types: [organization,person,geo,event]max_gleanings: 0summarize_descriptions:## llm: override the global llm settings for this task## parallelization: override the global parallelization settings for this task## async_mode: override the global async_mode settings for this taskprompt: "prompts/summarize_descriptions.txt"max_length: 500claim_extraction:## llm: override the global llm settings for this task## parallelization: override the global parallelization settings for this task## async_mode: override the global async_mode settings for this task# enabled: trueprompt: "prompts/claim_extraction.txt"description: "Any claims or facts that could be relevant to information discovery."max_gleanings: 0community_report:## llm: override the global llm settings for this task## parallelization: override the global parallelization settings for this task## async_mode: override the global async_mode settings for this taskprompt: "prompts/community_report.txt"max_length: 2000max_input_length: 8000cluster_graph:max_cluster_size: 10embed_graph:enabled: false # if true, will generate node2vec embeddings for nodes# num_walks: 10# walk_length: 40# window_size: 2# iterations: 3# random_seed: 597832umap:enabled: false # if true, will generate UMAP embeddings for nodessnapshots:graphml: falseraw_entities: falsetop_level_nodes: falselocal_search:# text_unit_prop: 0.5# community_prop: 0.1# conversation_history_max_turns: 5# top_k_mapped_entities: 10# top_k_relationships: 10# max_tokens: 12000global_search:# max_tokens: 12000# data_max_tokens: 12000# map_max_tokens: 1000# reduce_max_tokens: 2000# concurrency: 32
6.構建GraphRAG的索引(耗時較長,取決于document的長度)
poetry run poe index --root .
成功后如下:
? GraphRAG Indexer
├── Loading Input (InputFileType.text) - 1 files loaded (0 filtered) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100% 0:00:00 0:00:00
├── create_base_text_units
├── create_base_extracted_entities
├── create_summarized_entities
├── create_base_entity_graph
├── create_final_entities
├── create_final_nodes
├── create_final_communities
├── join_text_units_to_entity_ids
├── create_final_relationships
├── join_text_units_to_relationship_ids
├── create_final_community_reports
├── create_final_text_units
├── create_base_documents
└── create_final_documents
🚀 All workflows completed successfully.
7.進行查詢
此處GraphRAG提供了兩種查詢方式
1)全局查詢 :更側重全文理解
poetry run poe query --root . --method global "本文主要講了什么"
運行成功后可以看到輸出結果
2)局部查詢:更側重細節(jié)
poetry run poe query --root . --method local "本文主要講了什么"
運行成功后可以看到輸出結果
8. 總結
上述過程為已經(jīng)驗證過的,如果報錯可以檢查是否正確配置api_key及api_base
二、在python包的基礎上進行(快速嘗試)
1. 環(huán)境安裝
pip install graphrag
2. 初始化項目
創(chuàng)建一個臨時的文件夾graphrag,用于存在運行時數(shù)據(jù)
mkdir ./graphrag/input
curl https://www.xxx.com/xxx.txt > ./myTest/input/book.txt // 這里是示例代碼,根據(jù)實際情況放入自己要測試的txt文本即可。
cd ./graphrag
python -m graphrag.index --init
3. 配置相關文件(可參考上述的配置文件過程)
4. 執(zhí)行并構建圖索引
python -m graphrag.index
5.進行查詢
1)全局查詢
python -m graphrag.query --root ../myTest --method global "這篇文章主要講述了什么內(nèi)容?"
2)局部查詢
python -m graphrag.query --root ../myTest --method local "這篇文章主要講述了什么內(nèi)容?"
總結
通過以上兩種方式,我們已經(jīng)嘗試了利用源碼和python資源包進行配置GraphRAG的方式。大家可以按照自己的需求嘗試以上兩種方法。如果還有問題,歡迎在評論區(qū)討論!