async def llm_model_func(prompt, system_prompt=None, history_messages=[], **kwargs) -> str:
original_prompt = system_prompt if system_prompt else ""
zh_system_prompt = original_prompt +(
"\n\n=== 额外指令(非常重要) ===\n"
"你是知识图谱构建助手。输入文本已带【数据来源:...】标签标明来源章节。\n"
"提取要求:\n"
"1. 实体名称和关系类型用中文,英文术语翻译为中文。\n"
"2. 在每个实体和关系的 description 开头自然引用数据来源标签中的章节路径,"
"格式示例:[来源: 第1章概述 > 1.1背景] ...\n"
"3. 如输入标签包含章节ID/小节ID/子小节ID,请在 description 末尾附加 "
"(chapter_id=X, section_id=Y.Z) 作为定位属性。\n"
"4. 准确提取实体和关系,不遗漏重要信息。\n"
)
async with llm_semaphore:
return await openai_complete_if_cache(
model=GraphConfig.LLM_MODEL,
api_key=GraphConfig.LLM_API_KEY,
base_url=GraphConfig.LLM_BASE_URL,
prompt=prompt,
system_prompt=zh_system_prompt,
history_messages=history_messages,
**kwargs
)
async def custom_embedding(texts):
MAX_CHARS = 7500
truncated = [t[:MAX_CHARS] if len(t) > MAX_CHARS else t for t in texts]
async with embed_semaphore:
return await openai_embed.func(
truncated,
model=GraphConfig.EMBEDDING_MODEL,
base_url=GraphConfig.EMBEDDING_BASE_URL,
api_key=GraphConfig.EMBEDDING_API_KEY,
)
rag = LightRAG(
working_dir=self.working_dir,
llm_model_func=llm_model_func,
chunk_token_size=GraphConfig.CHUNK_TOKEN_SIZE,
chunk_overlap_token_size=GraphConfig.CHUNK_OVERLAP_TOKEN_SIZE,
embedding_batch_num=GraphConfig.EMBEDDING_BATCH_NUM,
embedding_func=EmbeddingFunc(
embedding_dim=1024,
max_token_size=8192,
func=custom_embedding
),
addon_params={
"language": "Chinese",
"entity_types": GraphConfig.ENTITY_TYPES,
},
default_embedding_timeout=120,
default_llm_timeout=300,
)
await rag.initialize_storages()
await initialize_pipeline_status()
return rag
Do you need to ask a question?
Your Question
配置信息如下
LLM_API_BASE =https://dashscope.aliyuncs.com/compatible-mode/v1
LLM_MODEL_NAME =deepseek-v4-flash
EMBEDDING_API_BASE = https://api.siliconflow.cn/v1
EMBEDDING_MODEL_NAME =BAAI/bge-m3
chunk_token_size=1200
chunk_overlap_token_size=150
embedding_batch_num=16
LLM_CONCURRENCY = 15
EMBEDDING_CONCURRENCY = 4
Additional Context
代码相关:
async def _create_rag_instance(self) -> LightRAG:
# 分离LLM和Embedding的并发控制,互不阻塞
llm_semaphore = asyncio.Semaphore(GraphConfig.LLM_CONCURRENCY)
embed_semaphore = asyncio.Semaphore(GraphConfig.EMBEDDING_CONCURRENCY)