中国城市建设网网站现在用什么工具做网站好

张小明 2025/12/26 16:45:07
中国城市建设网网站,现在用什么工具做网站好,网站首页设计怎么做,洛阳万悦网站建设LightRAG 是个开源的 RAG 框架#xff0c;专门用来快速搭建模块化的检索增强生成管道。这个项目在 GitHub 上热度不低#xff0c;我们今天来看看他到底怎么用基础安装与环境配置LightRAG 的安装过程很简单#xff0c;几行命令就能搞定#xff1a;pip install lightra…LightRAG 是个开源的 RAG 框架专门用来快速搭建模块化的检索增强生成管道。这个项目在 GitHub 上热度不低我们今天来看看他到底怎么用基础安装与环境配置LightRAG 的安装过程很简单几行命令就能搞定pip install lightrag-hku[api] cp env.example .env # ---这个有很多参数 非常丰富 lightrag-server官方提供的 UI 界面做得还算不错不过测试时基本没用上因为更关注的是代码层面的实现。环境搭好之后可以先跑一下官方提供的示例代码(摘自 readme)import os import asyncio from lightrag import LightRAG, QueryParam from lightrag.llm.openai import gpt_4o_mini_complete, gpt_4o_complete, openai_embed from lightrag.kg.shared_storage import initialize_pipeline_status from lightrag.utils import setup_logger setup_logger(lightrag, levelINFO) WORKING_DIR ./rag_storage if not os.path.exists(WORKING_DIR): os.mkdir(WORKING_DIR) async def initialize_rag(): rag LightRAG( working_dirWORKING_DIR, embedding_funcopenai_embed, llm_model_funcgpt_4o_mini_complete, ) # IMPORTANT: Both initialization calls are required! await rag.initialize_storages() # Initialize storage backends await initialize_pipeline_status() # Initialize processing pipeline return rag async def main(): try: # Initialize RAG instance rag await initialize_rag() await rag.ainsert(Your text) # Perform hybrid search mode hybrid print( await rag.aquery( What are the top themes in this story?, paramQueryParam(modemode) ) ) except Exception as e: print(fAn error occurred: {e}) finally: if rag: await rag.finalize_storages() if __name__ __main__: asyncio.run(main())官方示例里还有个基于 Gemini 的版本看着比较简单就选了这个来测试# pip install -q -U google-genai to use gemini as a client import os import numpy as np from google import genai from google.genai import types from dotenv import load_dotenv from lightrag.utils import EmbeddingFunc from lightrag import LightRAG, QueryParam from sentence_transformers import SentenceTransformer from lightrag.kg.shared_storage import initialize_pipeline_status import asyncio import nest_asyncio # Apply nest_asyncio to solve event loop issues nest_asyncio.apply() load_dotenv() gemini_api_key os.getenv(GEMINI_API_KEY) WORKING_DIR ./dickens if os.path.exists(WORKING_DIR): import shutil shutil.rmtree(WORKING_DIR) os.mkdir(WORKING_DIR) async def llm_model_func( prompt, system_promptNone, history_messages[], keyword_extractionFalse, **kwargs ) - str: # 1. Initialize the GenAI Client with your Gemini API Key client genai.Client(api_keygemini_api_key) # 2. Combine prompts: system prompt, history, and user prompt if history_messages is None: history_messages [] combined_prompt if system_prompt: combined_prompt f{system_prompt}\n for msg in history_messages: # Each msg is expected to be a dict: {role: ..., content: ...} combined_prompt f{msg[role]}: {msg[content]}\n # Finally, add the new user prompt combined_prompt fuser: {prompt} # 3. Call the Gemini model response client.models.generate_content( modelgemini-1.5-flash, contents[combined_prompt], configtypes.GenerateContentConfig(max_output_tokens500, temperature0.1), ) # 4. Return the response text return response.text async def embedding_func(texts: list[str]) - np.ndarray: model SentenceTransformer(all-MiniLM-L6-v2) embeddings model.encode(texts, convert_to_numpyTrue) return embeddings async def initialize_rag(): rag LightRAG( working_dirWORKING_DIR, llm_model_funcllm_model_func, embedding_funcEmbeddingFunc( embedding_dim384, max_token_size8192, funcembedding_func, ), ) await rag.initialize_storages() await initialize_pipeline_status() return rag def main(): # Initialize RAG instance rag asyncio.run(initialize_rag()) file_path story.txt with open(file_path, r) as file: text file.read() rag.insert(text) response rag.query( queryWhat is the main theme of the story?, paramQueryParam(modehybrid, top_k5, response_typesingle line), ) print(response) if __name__ __main__: main().env配置文件的参数非常丰富需要根据实际使用的工具链做适配。为了调用本地模型我这里用Ollama 做了相应调整。### This is sample file of .env ########################### ### Server Configuration ########################### HOST0.0.0.0 PORT9621 WEBUI_TITLEMy Graph KB WEBUI_DESCRIPTIONSimple and Fast Graph Based RAG System # WORKERS2 ### gunicorn worker timeout(as default LLM request timeout if LLM_TIMEOUT is not set) # TIMEOUT150 # CORS_ORIGINShttp://localhost:3000,http://localhost:8080 ### Optional SSL Configuration # SSLtrue # SSL_CERTFILE/path/to/cert.pem # SSL_KEYFILE/path/to/key.pem ### Directory Configuration (defaults to current working directory) ### Default value is ./inputs and ./rag_storage # INPUT_DIRabsolute_path_for_doc_input_dir # WORKING_DIRabsolute_path_for_working_dir ### Tiktoken cache directory (Store cached files in this folder for offline deployment) # TIKTOKEN_CACHE_DIR/app/data/tiktoken ### Ollama Emulating Model and Tag # OLLAMA_EMULATING_MODEL_NAMElightrag OLLAMA_EMULATING_MODEL_TAGlatest ### Max nodes return from graph retrieval in webui # MAX_GRAPH_NODES1000 ### Logging level # LOG_LEVELINFO # VERBOSEFalse # LOG_MAX_BYTES10485760 # LOG_BACKUP_COUNT5 ### Logfile location (defaults to current working directory) # LOG_DIR/path/to/log/directory ##################################### ### Login and API-Key Configuration ##################################### # AUTH_ACCOUNTSadmin:admin123,user1:pass456 # TOKEN_SECRETYour-Key-For-LightRAG-API-Server # TOKEN_EXPIRE_HOURS48 # GUEST_TOKEN_EXPIRE_HOURS24 # JWT_ALGORITHMHS256 ### API-Key to access LightRAG Server API # LIGHTRAG_API_KEYyour-secure-api-key-here # WHITELIST_PATHS/health,/api/* ###################################################################################### ### Query Configuration ### ### How to control the context length sent to LLM: ### MAX_ENTITY_TOKENS MAX_RELATION_TOKENS MAX_TOTAL_TOKENS ### Chunk_Tokens MAX_TOTAL_TOKENS - Actual_Entity_Tokens - Actual_Relation_Tokens ###################################################################################### # LLM response cache for query (Not valid for streaming response) ENABLE_LLM_CACHEtrue # COSINE_THRESHOLD0.2 ### Number of entities or relations retrieved from KG # TOP_K40 ### Maximum number or chunks for naive vector search # CHUNK_TOP_K20 ### control the actual entities send to LLM # MAX_ENTITY_TOKENS6000 ### control the actual relations send to LLM # MAX_RELATION_TOKENS8000 ### control the maximum tokens send to LLM (include entities, relations and chunks) # MAX_TOTAL_TOKENS30000 ### chunk selection strategies ### VECTOR: Pick KG chunks by vector similarity, delivered chunks to the LLM aligning more closely with naive retrieval ### WEIGHT: Pick KG chunks by entity and chunk weight, delivered more solely KG related chunks to the LLM ### If reranking is enabled, the impact of chunk selection strategies will be diminished. # KG_CHUNK_PICK_METHODVECTOR ######################################################### ### Reranking configuration ### RERANK_BINDING type: null, cohere, jina, aliyun ### For rerank model deployed by vLLM use cohere binding ######################################################### RERANK_BINDINGnull ### Enable rerank by default in query params when RERANK_BINDING is not null # RERANK_BY_DEFAULTTrue ### rerank score chunk filter(set to 0.0 to keep all chunks, 0.6 or above if LLM is not strong enough) # MIN_RERANK_SCORE0.0 ### For local deployment with vLLM # RERANK_MODELBAAI/bge-reranker-v2-m3 # RERANK_BINDING_HOSThttp://localhost:8000/v1/rerank # RERANK_BINDING_API_KEYyour_rerank_api_key_here ### Default value for Cohere AI # RERANK_MODELrerank-v3.5 # RERANK_BINDING_HOSThttps://api.cohere.com/v2/rerank # RERANK_BINDING_API_KEYyour_rerank_api_key_here ### Default value for Jina AI # RERANK_MODELjina-reranker-v2-base-multilingual # RERANK_BINDING_HOSThttps://api.jina.ai/v1/rerank # RERANK_BINDING_API_KEYyour_rerank_api_key_here ### Default value for Aliyun # RERANK_MODELgte-rerank-v2 # RERANK_BINDING_HOSThttps://dashscope.aliyuncs.com/api/v1/services/rerank/text-rerank/text-rerank # RERANK_BINDING_API_KEYyour_rerank_api_key_here ######################################## ### Document processing configuration ######################################## ENABLE_LLM_CACHE_FOR_EXTRACTtrue ### Document processing output language: English, Chinese, French, German ... SUMMARY_LANGUAGEEnglish ### Entity types that the LLM will attempt to recognize # ENTITY_TYPES[Person, Creature, Organization, Location, Event, Concept, Method, Content, Data, Artifact, NaturalObject] ### Chunk size for document splitting, 500~1500 is recommended # CHUNK_SIZE1200 # CHUNK_OVERLAP_SIZE100 ### Number of summary segments or tokens to trigger LLM summary on entity/relation merge (at least 3 is recommended) # FORCE_LLM_SUMMARY_ON_MERGE8 ### Max description token size to trigger LLM summary # SUMMARY_MAX_TOKENS 1200 ### Recommended LLM summary output length in tokens # SUMMARY_LENGTH_RECOMMENDED_600 ### Maximum context size sent to LLM for description summary # SUMMARY_CONTEXT_SIZE12000 ### control the maximum chunk_ids stored in vector and graph db # MAX_SOURCE_IDS_PER_ENTITY300 # MAX_SOURCE_IDS_PER_RELATION300 ### control chunk_ids limitation method: FIFO, KEEP ### FIFO: First in first out ### KEEP: Keep oldest (less merge action and faster) # SOURCE_IDS_LIMIT_METHODFIFO # Maximum number of file paths stored in entity/relation file_path field (For displayed only, does not affect query performance) # MAX_FILE_PATHS100 ### maximum number of related chunks per source entity or relation ### The chunk picker uses this value to determine the total number of chunks selected from KG(knowledge graph) ### Higher values increase re-ranking time # RELATED_CHUNK_NUMBER5 ############################### ### Concurrency Configuration ############################### ### Max concurrency requests of LLM (for both query and document processing) MAX_ASYNC4 ### Number of parallel processing documents(between 2~10, MAX_ASYNC/3 is recommended) MAX_PARALLEL_INSERT2 ### Max concurrency requests for Embedding # EMBEDDING_FUNC_MAX_ASYNC8 ### Num of chunks send to Embedding in single request # EMBEDDING_BATCH_NUM10 ########################################################### ### LLM Configuration ### LLM_BINDING type: openai, ollama, lollms, azure_openai, aws_bedrock ########################################################### ### LLM request timeout setting for all llm (0 means no timeout for Ollma) # LLM_TIMEOUT180 LLM_BINDINGollama LLM_MODELgranite4:latest LLM_BINDING_HOSThttp://localhost:11434 [#LLM](#LLM)_BINDING_API_KEYyour_api_key ### Optional for Azure # AZURE_OPENAI_API_VERSION2024-08-01-preview # AZURE_OPENAI_DEPLOYMENTgpt-4o ### Openrouter example # LLM_MODELgoogle/gemini-2.5-flash # LLM_BINDING_HOSThttps://openrouter.ai/api/v1 # LLM_BINDING_API_KEYyour_api_key # LLM_BINDINGopenai ### OpenAI Compatible API Specific Parameters ### Increased temperature values may mitigate infinite inference loops in certain LLM, such as Qwen3-30B. # OPENAI_LLM_TEMPERATURE0.9 ### Set the max_tokens to mitigate endless output of some LLM (less than LLM_TIMEOUT * llm_output_tokens/second, i.e. 9000 180s * 50 tokens/s) ### Typically, max_tokens does not include prompt content, though some models, such as Gemini Models, are exceptions ### For vLLM/SGLang deployed models, or most of OpenAI compatible API provider # OPENAI_LLM_MAX_TOKENS9000 ### For OpenAI o1-mini or newer modles [#OPENAI](#OPENAI)_LLM_MAX_COMPLETION_TOKENS9000 #### OpenAIs new API utilizes max_completion_tokens instead of max_tokens # OPENAI_LLM_MAX_COMPLETION_TOKENS9000 ### use the following command to see all support options for OpenAI, azure_openai or OpenRouter ### lightrag-server --llm-binding openai --help ### OpenAI Specific Parameters # OPENAI_LLM_REASONING_EFFORTminimal ### OpenRouter Specific Parameters # OPENAI_LLM_EXTRA_BODY{reasoning: {enabled: false}} ### Qwen3 Specific Parameters deploy by vLLM # OPENAI_LLM_EXTRA_BODY{chat_template_kwargs: {enable_thinking: false}} ### use the following command to see all support options for Ollama LLM ### If LightRAG deployed in Docker uses host.docker.internal instead of localhost in LLM_BINDING_HOST ### lightrag-server --llm-binding ollama --help ### Ollama Server Specific Parameters ### OLLAMA_LLM_NUM_CTX must be provided, and should at least larger than MAX_TOTAL_TOKENS 2000 OLLAMA_LLM_NUM_CTX32768 ### Set the max_output_tokens to mitigate endless output of some LLM (less than LLM_TIMEOUT * llm_output_tokens/second, i.e. 9000 180s * 50 tokens/s) # OLLAMA_LLM_NUM_PREDICT9000 ### Stop sequences for Ollama LLM # OLLAMA_LLM_STOP[/s, |EOT|] ### Bedrock Specific Parameters # BEDROCK_LLM_TEMPERATURE1.0 #################################################################################### ### Embedding Configuration (Should not be changed after the first file processed) ### EMBEDDING_BINDING: ollama, openai, azure_openai, jina, lollms, aws_bedrock #################################################################################### # EMBEDDING_TIMEOUT30 EMBEDDING_BINDINGollama EMBEDDING_MODELgranite-embedding:latest EMBEDDING_DIM1024 EMBEDDING_BINDING_API_KEYyour_api_key # If LightRAG deployed in Docker uses host.docker.internal instead of localhost EMBEDDING_BINDING_HOSThttp://localhost:11434 ### OpenAI compatible (VoyageAI embedding openai compatible) # EMBEDDING_BINDINGopenai # EMBEDDING_MODELtext-embedding-3-large # EMBEDDING_DIM3072 # EMBEDDING_BINDING_HOSThttps://api.openai.com/v1 # EMBEDDING_BINDING_API_KEYyour_api_key ### Optional for Azure # AZURE_EMBEDDING_DEPLOYMENTtext-embedding-3-large # AZURE_EMBEDDING_API_VERSION2023-05-15 # AZURE_EMBEDDING_ENDPOINTyour_endpoint # AZURE_EMBEDDING_API_KEYyour_api_key ### Jina AI Embedding # EMBEDDING_BINDINGjina # EMBEDDING_BINDING_HOSThttps://api.jina.ai/v1/embeddings # EMBEDDING_MODELjina-embeddings-v4 # EMBEDDING_DIM2048 # EMBEDDING_BINDING_API_KEYyour_api_key ### Optional for Ollama embedding OLLAMA_EMBEDDING_NUM_CTX8192 ### use the following command to see all support options for Ollama embedding ### lightrag-server --embedding-binding ollama --help #################################################################### ### WORKSPACE sets workspace name for all storage types ### for the purpose of isolating data from LightRAG instances. ### Valid workspace name constraints: a-z, A-Z, 0-9, and _ #################################################################### # WORKSPACEspace1 ############################ ### Data storage selection ############################ ### Default storage (Recommended for small scale deployment) # LIGHTRAG_KV_STORAGEJsonKVStorage # LIGHTRAG_DOC_STATUS_STORAGEJsonDocStatusStorage # LIGHTRAG_GRAPH_STORAGENetworkXStorage # LIGHTRAG_VECTOR_STORAGENanoVectorDBStorage ### Redis Storage (Recommended for production deployment) # LIGHTRAG_KV_STORAGERedisKVStorage # LIGHTRAG_DOC_STATUS_STORAGERedisDocStatusStorage ### Vector Storage (Recommended for production deployment) # LIGHTRAG_VECTOR_STORAGEMilvusVectorDBStorage # LIGHTRAG_VECTOR_STORAGEQdrantVectorDBStorage # LIGHTRAG_VECTOR_STORAGEFaissVectorDBStorage ### Graph Storage (Recommended for production deployment) # LIGHTRAG_GRAPH_STORAGENeo4JStorage # LIGHTRAG_GRAPH_STORAGEMemgraphStorage ### PostgreSQL # LIGHTRAG_KV_STORAGEPGKVStorage # LIGHTRAG_DOC_STATUS_STORAGEPGDocStatusStorage # LIGHTRAG_GRAPH_STORAGEPGGraphStorage # LIGHTRAG_VECTOR_STORAGEPGVectorStorage ### MongoDB (Vector storage only available on Atlas Cloud) # LIGHTRAG_KV_STORAGEMongoKVStorage # LIGHTRAG_DOC_STATUS_STORAGEMongoDocStatusStorage # LIGHTRAG_GRAPH_STORAGEMongoGraphStorage # LIGHTRAG_VECTOR_STORAGEMongoVectorDBStorage ### PostgreSQL Configuration POSTGRES_HOSTlocalhost POSTGRES_PORT5432 POSTGRES_USERyour_username POSTGRES_PASSWORDyour_password POSTGRES_DATABASEyour_database POSTGRES_MAX_CONNECTIONS12 # POSTGRES_WORKSPACEforced_workspace_name ### PostgreSQL Vector Storage Configuration ### Vector storage type: HNSW, IVFFlat POSTGRES_VECTOR_INDEX_TYPEHNSW POSTGRES_HNSW_M16 POSTGRES_HNSW_EF200 POSTGRES_IVFFLAT_LISTS100 ### PostgreSQL Connection Retry Configuration (Network Robustness) ### Number of retry attempts (1-10, default: 3) ### Initial retry backoff in seconds (0.1-5.0, default: 0.5) ### Maximum retry backoff in seconds (backoff-60.0, default: 5.0) ### Connection pool close timeout in seconds (1.0-30.0, default: 5.0) # POSTGRES_CONNECTION_RETRIES3 # POSTGRES_CONNECTION_RETRY_BACKOFF0.5 # POSTGRES_CONNECTION_RETRY_BACKOFF_MAX5.0 # POSTGRES_POOL_CLOSE_TIMEOUT5.0 ### PostgreSQL SSL Configuration (Optional) # POSTGRES_SSL_MODErequire # POSTGRES_SSL_CERT/path/to/client-cert.pem # POSTGRES_SSL_KEY/path/to/client-key.pem # POSTGRES_SSL_ROOT_CERT/path/to/ca-cert.pem # POSTGRES_SSL_CRL/path/to/crl.pem ### PostgreSQL Server Settings (for Supabase Supavisor) # Use this to pass extra options to the PostgreSQL connection string. # For Supabase, you might need to set it like this: # POSTGRES_SERVER_SETTINGSoptionsreference%3D[project-ref] # Default is 100 set to 0 to disable # POSTGRES_STATEMENT_CACHE_SIZE100 ### Neo4j Configuration NEO4J_URIneo4js://xxxxxxxx.databases.neo4j.io NEO4J_USERNAMEneo4j NEO4J_PASSWORDyour_password NEO4J_DATABASEneo4j NEO4J_MAX_CONNECTION_POOL_SIZE100 NEO4J_CONNECTION_TIMEOUT30 NEO4J_CONNECTION_ACQUISITION_TIMEOUT30 NEO4J_MAX_TRANSACTION_RETRY_TIME30 NEO4J_MAX_CONNECTION_LIFETIME300 NEO4J_LIVENESS_CHECK_TIMEOUT30 NEO4J_KEEP_ALIVEtrue # NEO4J_WORKSPACEforced_workspace_name ### MongoDB Configuration MONGO_URImongodb://root:rootlocalhost:27017/ [#MONGO](#MONGO)_URImongodbsrv://xxxx MONGO_DATABASELightRAG # MONGODB_WORKSPACEforced_workspace_name ### Milvus Configuration MILVUS_URIhttp://localhost:19530 MILVUS_DB_NAMElightrag # MILVUS_USERroot # MILVUS_PASSWORDyour_password # MILVUS_TOKENyour_token # MILVUS_WORKSPACEforced_workspace_name ### Qdrant QDRANT_URLhttp://localhost:6333 # QDRANT_API_KEYyour-api-key # QDRANT_WORKSPACEforced_workspace_name ### Redis REDIS_URIredis://localhost:6379 REDIS_SOCKET_TIMEOUT30 REDIS_CONNECT_TIMEOUT10 REDIS_MAX_CONNECTIONS100 REDIS_RETRY_ATTEMPTS3 # REDIS_WORKSPACEforced_workspace_name ### Memgraph Configuration MEMGRAPH_URIbolt://localhost:7687 MEMGRAPH_USERNAME MEMGRAPH_PASSWORD MEMGRAPH_DATABASEmemgraph # MEMGRAPH_WORKSPACEforced_workspace_name参考前面的 Gemini 示例写了下面的代码包含了一些硬编码文本的测试代码# 准备环境 python3 -m venv venv source venv/bin/activate pip install --upgrade pip pip install lightrag-hku[api] pip install ollamaimport os import asyncio from functools import partial from datetime import datetime from lightrag import LightRAG, QueryParam try: from ollama import AsyncClient except ImportError: print(Warning: The ollama Python package is required. Please run: pip install ollama) class AsyncClient: def __init__(self, host): pass async def chat(self, **kwargs): raise NotImplementedError(ollama package not installed.) from lightrag.llm.ollama import ollama_embed from lightrag.utils import setup_logger, EmbeddingFunc from lightrag.kg.shared_storage import initialize_pipeline_status OLLAMA_BASE_URL http://localhost:11434 LLM_MODEL granite4:latest EMBEDDING_MODEL granite-embedding:latest WORKING_DIR ./rag_storage_ollama EMBEDDING_DIMENSION 384 OUTPUT_DIR ./output setup_logger(lightrag, levelINFO) if not os.path.exists(WORKING_DIR): os.mkdir(WORKING_DIR) if not os.path.exists(OUTPUT_DIR): os.mkdir(OUTPUT_DIR) async def custom_ollama_llm_complete(prompt: str, system_prompt: str None, **kwargs): A custom function that handles the Ollama client initialization and model/base_url parameters that are injected via functools.partial, while robustly filtering out unwanted internal keywords. model kwargs.pop(model) base_url kwargs.pop(base_url) client AsyncClient(hostbase_url) messages [] if system_prompt: messages.append({role: system, content: system_prompt}) messages.append({role: user, content: prompt}) keys_to_filter { host, hashing_kv, llm_model_name, history_messages, keyword_extraction, enable_cot, is_system_prompt_only, prompt_config } cleaned_kwargs {k: v for k, v in kwargs.items() if k not in keys_to_filter} try: response await client.chat( modelmodel, messagesmessages, **cleaned_kwargs ) return response[message][content] except Exception as e: raise e async def initialize_rag(): Initializes the LightRAG instance using standard Ollama configuration. configured_ollama_complete partial( custom_ollama_llm_complete, modelLLM_MODEL, base_urlOLLAMA_BASE_URL, ) configured_ollama_embed partial( ollama_embed, embed_modelEMBEDDING_MODEL, base_urlOLLAMA_BASE_URL ) wrapped_embedding_func EmbeddingFunc( embedding_dimEMBEDDING_DIMENSION, funcconfigured_ollama_embed, ) rag LightRAG( working_dirWORKING_DIR, llm_model_funcconfigured_ollama_complete, embedding_funcwrapped_embedding_func, ) await rag.initialize_storages() await initialize_pipeline_status() return rag async def main(): rag None query How does RAG solve the problem of LLM hallucination and what are its main use cases? try: print(Checking if required Ollama models are pulled...) # the knowledge source sample_text The concept of Retrieval-Augmented Generation (RAG) is a critical development in the field of large language models (LLMs). It addresses the hallucination problem by grounding LLM responses in external, verified knowledge sources. Instead of relying solely on the LLMs static training data, RAG first retrieves relevant documents from a knowledge base (often a vector store) and then feeds these documents, alongside the users query, to the LLM for generation. This two-step process significantly improves the accuracy, relevance, and transparency of the generated output. Popular applications include enterprise search, customer support, and domain-specific QA systems. print(f--- 1. Initializing RAG with Ollama Models ---) rag await initialize_rag() print(f\n--- 2. Inserting Sample Text ({len(sample_text.split())} words) ---) await rag.ainsert(sample_text) print(Insertion complete. Data is ready for retrieval.) mode hybrid print(f\n--- 3. Querying the RAG System (Mode: {mode}) ---) print(fQuery: {query}) rag_result await rag.aquery( query, paramQueryParam(modemode) ) response_text None if hasattr(rag_result, get_response_text): response_text rag_result.get_response_text() elif isinstance(rag_result, str): response_text rag_result print(\n *50) print(FINAL RAG RESPONSE) print(*50) output_content # Prepare string for file output if response_text and not str(response_text).strip().startswith(Error:): print(response_text) output_content f# RAG Query Result\n\n output_content f## Query\n\n {query}\n\n output_content f## LLM/Cache Response\n\n{response_text}\n\n print(\n *50) print(\n--- Context Retrieved (Sources) ---) output_content f## Retrieved Context (Sources)\n\n if not isinstance(rag_result, str) and rag_result.retriever_output and rag_result.retriever_output.docs: for i, doc in enumerate(rag_result.retriever_output.docs): source_text doc.text print(fSource {i1}: {source_text[:100]}...) output_content f### Source {i1}\n\n output_content ftext\n{source_text}\n\n else: print(No context documents were retrieved (or result was a cache hit string).) output_content No context documents were retrieved (or result was a cache hit string).\n else: error_message LLM failed to generate a response (Check Ollama logs for details). print(error_message) output_content f# RAG Query Result\n\n## Error\n\n{error_message}\n\n if response_text: print(f\nError String from LightRAG: {response_text}) output_content f**Error Detail:** {response_text}\n timestamp datetime.now().strftime(%Y%m%d_%H%M%S) filename frag_query_output_{timestamp}.md output_filepath os.path.join(OUTPUT_DIR, filename) with open(output_filepath, w, encodingutf-8) as f: f.write(output_content) print(f\n--- Output Written to File ---) print(fSuccessfully wrote output to: {output_filepath}) except Exception as e: if str object has no attribute retriever_output in str(e): print(\n--- ERROR BYPASS: Detected Cache Hit String Result ---) print(The response was successfully retrieved from the cache and written to the output file.) else: # For all other (real) exceptions, print the detailed error block print(\n *50) print(AN ERROR OCCURRED DURING RAG PROCESS) print(*50) print(fError: {e}) print(fPlease ensure Ollama is running and accessible at {OLLAMA_BASE_URL}, and the models {LLM_MODEL} and {EMBEDDING_MODEL} are pulled locally.) print(fTo pull: ollama pull {LLM_MODEL} and ollama pull {EMBEDDING_MODEL}) print(*50 \n) finally: if rag: print(\n--- Finalizing storages ---) await rag.finalize_storages() if __name__ __main__: asyncio.run(main())虽然.env文件里有参数可以配置 input 文件夹路径但测试时直接在代码里写死了路径。运行后的输出包括控制台日志和 markdown 格式的结果文件结果太长我就不贴了。接下来测试了更实际的场景准备了几份 markdown 格式的文档其他格式应该也支持但没测用这些文档构建了自己的 RAG 系统继续用 Ollama 和 Granite 模型来验证效果这次的代码就没那么硬编码了。.env文件提供了 input 文件夹的配置选项不过这里还是用的硬编码方式import os import asyncio from functools import partial from datetime import datetime from lightrag import LightRAG, QueryParam import glob try: from ollama import AsyncClient except ImportError: print(Warning: The ollama Python package is required. Please run: pip install ollama) class AsyncClient: def __init__(self, host): pass async def chat(self, **kwargs): raise NotImplementedError(ollama package not installed.) from lightrag.llm.ollama import ollama_embed from lightrag.utils import setup_logger, EmbeddingFunc from lightrag.kg.shared_storage import initialize_pipeline_status OLLAMA_BASE_URL http://localhost:11434 LLM_MODEL granite4:latest EMBEDDING_MODEL granite-embedding:latest WORKING_DIR ./rag_storage_ollama EMBEDDING_DIMENSION 384 DOCUMENTS_DIR ./documents # Directory to read source files from OUTPUT_DIR ./output # Directory to write RAG results to setup_logger(lightrag, levelINFO) if not os.path.exists(WORKING_DIR): os.mkdir(WORKING_DIR) print(fCreated working directory: {WORKING_DIR}) if not os.path.exists(OUTPUT_DIR): os.mkdir(OUTPUT_DIR) print(fCreated output directory: {OUTPUT_DIR}) if not os.path.exists(DOCUMENTS_DIR): os.mkdir(DOCUMENTS_DIR) print(fCreated documents directory: {DOCUMENTS_DIR}) async def custom_ollama_llm_complete(prompt: str, system_prompt: str None, **kwargs): A custom function that handles the Ollama client initialization and model/base_url parameters that are injected via functools.partial, while robustly filtering out unwanted internal keywords. model kwargs.pop(model) base_url kwargs.pop(base_url) client AsyncClient(hostbase_url) messages [] if system_prompt: messages.append({role: system, content: system_prompt}) messages.append({role: user, content: prompt}) keys_to_filter { host, hashing_kv, llm_model_name, history_messages, keyword_extraction, enable_cot, is_system_prompt_only, prompt_config } cleaned_kwargs {k: v for k, v in kwargs.items() if k not in keys_to_filter} try: response await client.chat( modelmodel, messagesmessages, **cleaned_kwargs ) return response[message][content] except Exception as e: raise e async def initialize_rag(): Initializes the LightRAG instance using standard Ollama configuration. configured_ollama_complete partial( custom_ollama_llm_complete, modelLLM_MODEL, base_urlOLLAMA_BASE_URL, ) configured_ollama_embed partial( ollama_embed, embed_modelEMBEDDING_MODEL, base_urlOLLAMA_BASE_URL ) wrapped_embedding_func EmbeddingFunc( embedding_dimEMBEDDING_DIMENSION, funcconfigured_ollama_embed, ) rag LightRAG( working_dirWORKING_DIR, llm_model_funcconfigured_ollama_complete, embedding_funcwrapped_embedding_func, ) await rag.initialize_storages() await initialize_pipeline_status() return rag async def load_and_insert_documents(rag: LightRAG): Reads files from the DOCUMENTS_DIR and inserts their content into the RAG system. Fixed to use a more compatible method for document insertion. file_paths glob.glob(os.path.join(DOCUMENTS_DIR, *.[mM][dD])) \ glob.glob(os.path.join(DOCUMENTS_DIR, *.[tT][xX][tT])) if not file_paths: print(\n--- WARNING: No documents found in ./documents directory. ---) print(Please add some Markdown (.md) or Text (.txt) files to populate the knowledge base.) return False print(f\n--- 2. Inserting Documents ({len(file_paths)} file(s) found) ---) insertion_succeeded 0 for file_path in file_paths: filename os.path.basename(file_path) try: with open(file_path, r, encodingutf-8) as f: content f.read() await rag.ainsert(content, doc_meta{doc_id: filename}) print(f Successfully inserted: {filename} ({len(content.split())} words)) insertion_succeeded 1 except TypeError as te: if doc_id in str(te) or doc_meta in str(te): print(f FAILED (Type Error): {filename}. Attempting insertion without metadata to check compatibility.) try: await rag.ainsert(content) print(f Successfully inserted (no metadata): {filename}) insertion_succeeded 1 except Exception as e: print(f FAILED (General Error): {filename} - {e}) else: print(f FAILED to read or insert {filename} (Type Error): {te}) except Exception as e: print(f FAILED to read or insert {filename} (General Error): {e}) if insertion_succeeded 0: print(Insertion complete, but no documents were successfully inserted. Please check LightRAG documentation for the correct argument name for source IDs.) return False print(Insertion complete. Data is ready for retrieval.) return True async def main(): rag None query Describe Quantum-Safe cryptography? try: print(Checking if required Ollama models are pulled...) print(f--- 1. Initializing RAG with Ollama Models ---) rag await initialize_rag() documents_inserted await load_and_insert_documents(rag) if not documents_inserted: return mode hybrid print(f\n--- 3. Querying the RAG System (Mode: {mode}) ---) print(fQuery: {query}) rag_result await rag.aquery( query, paramQueryParam(modemode) ) response_text None if hasattr(rag_result, get_response_text): response_text rag_result.get_response_text() elif isinstance(rag_result, str): response_text rag_result print(\n *50) print(FINAL RAG RESPONSE) print(*50) output_content # Prepare string for file output if response_text and not str(response_text).strip().startswith(Error:): print(response_text) output_content f# RAG Query Result\n\n output_content f## Query\n\n {query}\n\n output_content f## LLM/Cache Response\n\n{response_text}\n\n print(\n *50) print(\n--- Context Retrieved (Sources) ---) output_content f## Retrieved Context (Sources)\n\n if not isinstance(rag_result, str) and rag_result.retriever_output and rag_result.retriever_output.docs: unique_sources set() for i, doc in enumerate(rag_result.retriever_output.docs): source_text doc.text source_id doc.doc_id if hasattr(doc, doc_id) and doc.doc_id else ( doc.doc_meta.get(doc_id) if hasattr(doc, doc_meta) and isinstance(doc.doc_meta, dict) else Unknown Source ) unique_sources.add(source_id) print(fSource {i1} (File: {source_id}): {source_text[:100]}...) output_content f### Source {i1} (File: {source_id})\n\n output_content ftext\n{source_text}\n\n print(f\nAnswer Grounded in: {, .join(sorted(list(unique_sources)))}) else: print(No context documents were retrieved (or result was a cache hit string).) output_content No context documents were retrieved (or result was a cache hit string).\n else: error_message LLM failed to generate a response (Check Ollama logs for details). print(error_message) output_content f# RAG Query Result\n\n## Error\n\n{error_message}\n\n if response_text: print(f\nError String from LightRAG: {response_text}) output_content f**Error Detail:** {response_text}\n timestamp datetime.now().strftime(%Y%m%d_%H%M%S) filename frag_query_output_{timestamp}.md output_filepath os.path.join(OUTPUT_DIR, filename) with open(output_filepath, w, encodingutf-8) as f: f.write(output_content) print(f\n--- Output Written to File ---) print(fSuccessfully wrote output to: {output_filepath}) except Exception as e: if str object has no attribute retriever_output in str(e): print(\n--- ERROR BYPASS: Detected Cache Hit String Result ---) print(The response was successfully retrieved from the cache and written to the output file.) else: print(\n *50) print(AN ERROR OCCURRED DURING RAG PROCESS) print(*50) print(fError: {e}) print(fPlease ensure Ollama is running and accessible at {OLLAMA_BASE_URL}, and the models {LLM_MODEL} and {EMBEDDING_MODEL} are pulled locally.) print(fTo pull: ollama pull {LLM_MODEL} and ollama pull {EMBEDDING_MODEL}) print(*50 \n) finally: if rag: print(\n--- Finalizing storages ---) await rag.finalize_storages() if __name__ __main__: asyncio.run(main())实际的查询输出示例RAG Query Result ## Query Describe Quantum-Safe cryptography? ## LLM/Cache Response ### What is Quantum-Safe Cryptography? Quantum-safe cryptography, also known as post‑quantum cryptography (PQC), refers to cryptographic algorithms and protocols designed to remain secure even against attacks by a sufficiently powerful quantum computer. Traditional public-key cryptosystems like RSA, ECC, Diffie‑Hellman, and elliptic curve variants are vulnerable to Shor’s algorithm, which could efficiently factor large integers and compute discrete logarithms—tasks that form the basis of these cryptographic schemes.知识图谱生成LightRAG 自带知识图谱生成功能这点比较实用。为了把生成的图谱文件展示出来我还用用 Streamlit 写了个简单的可视化应用。代码比较粗糙后续可以继续优化。import streamlit as st import networkx as nx import matplotlib.pyplot as plt import io import itertools from io import StringIO def visualize_graph(G: nx.Graph, layout_name: str, layout_params: dict): Generates a Matplotlib plot of the NetworkX graph with custom styling. node_labels nx.get_node_attributes(G, label) if not node_labels: node_labels nx.get_node_attributes(G, text) edge_labels nx.get_edge_attributes(G, text) node_types nx.get_node_attributes(G, type) type_color_map { Entity: [#1f78b4](#1f78b4), # Blue Chunk: [#b2df8a](#b2df8a), # Light Green Relation: [#33a02c](#33a02c), # Dark Green Unknown: [#a6cee3](#a6cee3) # Light Blue } node_colors [type_color_map.get(node_types.get(node, Unknown), type_color_map[Unknown]) for node in G.nodes()] if layout_name Spring Layout: pos nx.spring_layout(G, **layout_params) elif layout_name Circular Layout: pos nx.circular_layout(G) elif layout_name Spectral Layout: pos nx.spectral_layout(G) elif layout_name Kamada-Kawai Layout: pos nx.kamada_kawai_layout(G) else: pos nx.spring_layout(G, **layout_params) fig, ax plt.subplots(figsize(16, 10)) nx.draw_networkx_nodes( G, pos, node_size2500, node_colornode_colors, alpha0.9 ) # Draw edges nx.draw_networkx_edges( G, pos, axax, edge_colorgray, styledashed, arrowstyle-, arrowsize25 ) nx.draw_networkx_labels( G, pos, labelsnode_labels, font_size11, font_colorblack, font_weightbold, ) nx.draw_networkx_edge_labels( G, pos, edge_labelsedge_labels, font_colorred, font_size9, bbox{boxstyle: round,pad0.4, fc: white, alpha: 0.7, ec: none} ) ax.set_title(fVisualized Graph: {G.number_of_nodes()} Nodes, {G.number_of_edges()} Edges, fontsize16) plt.axis(off) plt.tight_layout() st.pyplot(fig) def app(): st.set_page_config(layoutwide, page_titleGraphML Viewer) st.title(GraphML Visualization App) st.markdown(A tool to visualize GraphML (e.g., LightRAG) outputs using NetworkX and Streamlit.) st.sidebar.header(Data Upload Layout Controls) uploaded_file st.sidebar.file_uploader( Upload your .graphml file, type[graphml] ) graph_data None if uploaded_file is not None: try: graph_data uploaded_file.read().decode(utf-8) st.sidebar.success(File uploaded successfully! Graph loading...) except Exception as e: st.sidebar.error(fError reading file: {e}) graph_data None else: st.info(Please upload a GraphML file in the sidebar to visualize your knowledge graph.) st.sidebar.subheader(Layout Algorithm) layout_name st.sidebar.selectbox( Choose Graph Layout:, (Spring Layout, Kamada-Kawai Layout, Circular Layout, Spectral Layout) ) layout_params {} if layout_name Spring Layout: st.sidebar.caption(Fine-tune the Spring Layout forces:) k_val st.sidebar.slider(k (Node Spacing), 0.01, 1.0, 0.15, 0.01) iters st.sidebar.slider(Iterations, 10, 100, 50, 10) layout_params {k: k_val, iterations: iters} if graph_data: try: G nx.read_graphml(StringIO(graph_data)) st.header(Knowledge Graph Visualization) st.write(fGraph loaded: {G.number_of_nodes()} Nodes, {G.number_of_edges()} Edges) visualize_graph(G, layout_name, layout_params) except Exception as e: st.error(fAn error occurred while processing the graph: {e}) st.code(fError details: {e}) st.warning(Please check if the GraphML file is correctly formatted and contains valid data.) if __name__ __main__: app()运行以后你就能看到上面的结果总结LightRAG 在构建 RAG 系统方面提供了相对完整的解决方案。相比传统的纯向量检索它的核心特点是引入了知识图谱能把非结构化文本组织成实体-关系网络这种混合检索策略语义向量图谱关系确实能让 LLM 获得更丰富的上下文信息。项目文档写得很详细并且包含了大量工具和服务的集成方案。整个框架的设计比较模块化扩展性也不错。如果要在生产环境部署还需要考虑性能优化、错误处理等细节。
版权声明:本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!

怎么建设个人网站 新手学做网站万维网申请网站域名

想要在VSCode中体验真正的Vim编辑乐趣?vscode-neovim插件的状态栏定制功能让你能够完全掌控编辑环境的每一个细节!🎯 作为业界领先的Vim模式集成方案,vscode-neovim通过嵌入式Neovim引擎为开发者提供无缝的Vim编辑体验&#xff0c…

张小明 2025/12/23 16:22:40 网站建设

制作网站的手机软件互联网建网站

想在Mac电脑上畅玩各种游戏却苦于没有合适的手柄支持?360Controller开源驱动让你轻松实现Xbox 360手柄的完美适配。这款免费工具支持有线和无线两种连接方式,为macOS用户提供了完整的游戏手柄解决方案。无论你是休闲玩家还是硬核游戏爱好者,都…

张小明 2025/12/23 16:21:37 网站建设

服务器做网站用什么系统医院网站建设入门

Qwen3-VL-8B 支持 Docker 部署的完整指南 🐳📦 在智能应用日益依赖视觉理解能力的今天,一个现实问题始终困扰着开发者:为什么模型在本地跑得好好的,一上服务器就“显存爆炸”或“环境错乱”? 你不是一个人…

张小明 2025/12/23 16:20:34 网站建设

中国动漫影视培训网站源码广州越秀区酒店

网络组件与架构:TCP/IP 模型深度解析 在当今的数字化时代,网络已经成为了人们生活和工作中不可或缺的一部分。了解网络的组件和架构对于深入理解网络通信的原理至关重要。本文将详细介绍网络服务、TCP/IP 层模型以及各层的具体协议和功能。 网络服务 网络服务是允许用户共…

张小明 2025/12/23 16:18:27 网站建设

做app必须有网站吗申请注册一个商标多少钱

「記憶體安全」 vs 「零成本抽象」:以 C20 實現超越 Rust 的系統安全架構前言:安全性的本質之爭在現代系統程式設計領域,Rust 以其「記憶體安全」哲學席捲開發者社群,而 C 則憑藉「零成本抽象」原則堅守效能至上的陣地。這兩種看似…

张小明 2025/12/23 16:17:22 网站建设

多模室内设计网站制作微网站的费用

AhabAssistantLimbusCompany(简称AALC)是一款专为《Limbus Company》游戏设计的PC端自动化辅助工具。通过先进的图像识别技术和智能操作脚本,帮助玩家自动完成日常任务、智能领取奖励、高效管理游戏资源,让您从繁琐的重复操作中解…

张小明 2025/12/23 16:16:18 网站建设