在GraphRAG中配置阿里云API碰到的问题以及解决方案

最近在使用GraphRAG项目集成阿里云API时,遇到了一些配置问题。本文记录了这些问题的排查过程和解决方案,希望能帮助遇到类似情况的朋友。

问题一:text-embedding-v4 的批量处理限制

问题描述:
阿里云的 text-embedding-v4 模型对批量处理有限制,最大 batch_size 为 10。
解决方案:
在配置文件中,将 batch_size 设置为小于等于 10 的值。为了更稳定的表现,我设置为 8:

embedding_models:
  default_embedding_model:
    model_provider: openai
    model: text-embedding-v4
    auth_method: api_key
    api_base: https://dashscope.aliyuncs.com/compatible-mode/v1
    api_key: your-apikey
    batch_size: 8

问题二:encoding_format 参数不兼容

问题描述:
Failed to validate embedding model (default_embedding_model) params litellm.BadRequestError: OpenAIException - Error code: 400 - {'error': {'message': "'encoding_format' only support with [float, base64]", 'type': 'invalid_request_error', 'param': None, 'code': None}, 'request_id': 'c977f42d-d9a7-939a-ba88-e9fbfec90dad'}
阿里云API 仅支持 'float''base64' 格式的 encoding_format 参数,而默认配置可能使用了不支持的格式。
解决方案:
在配置文件的 call_args 中明确指定 encoding_format"float"

embedding_models:
  default_embedding_model:
    # ... 其他配置 ...
    call_args:
      encoding_format: "float"
    retry:
      type: exponential_backoff

完整的配置示例如下:

embedding_models:
  default_embedding_model:
    model_provider: openai
    model: text-embedding-v4
    auth_method: api_key
    api_base: https://dashscope.aliyuncs.com/compatible-mode/v1
    api_key: your-apikey
    batch_size: 8
    call_args:
      encoding_format: "float"
    retry:
      type: exponential_backoff

问题三:向量维度不匹配错误

问题描述:
运行 Pipeline 时出现以下错误:

Pipeline error: Column 1 named vector expected length 162 but got length 54

原因分析:
这个错误的根本原因是向量维度不匹配。阿里云 text-embedding-v4 模型的嵌入维度为 1024,而 GraphRAG 默认配置的维度为 3072,导致向量存储时出现长度不一致的问题。

参考资料:GitHub Commit
解决方案:
修改 setting.yaml 配置文件中的 vector_store 项,将所有向量大小统一设置为 1024:

vector_store:
  type: lancedb
  db_uri: output\lancedb
  index_schema:
    text_unit_text:
      vector_size: 1024
    entity_description:
      vector_size: 1024
    community_full_content:
      vector_size: 1024

奉上完整的setting.yaml文件

### This config file contains required core defaults that must be set, along with a handful of common optional settings.
### For a full list of available settings, see https://microsoft.github.io/graphrag/config/yaml/

### LLM settings ###
## There are a number of settings to tune the threading and token limits for LLM calls - check the docs.

completion_models:
  default_completion_model:
    model_provider: openai
    model: qwen-plus
    auth_method: api_key
    api_base: https://dashscope.aliyuncs.com/compatible-mode/v1
    api_key: sk-***
    retry:
      type: exponential_backoff

embedding_models:
  default_embedding_model:
    model_provider: openai
    model: text-embedding-v4
    auth_method: api_key
    api_base: https://dashscope.aliyuncs.com/compatible-mode/v1
    api_key: sk-***
    batch_size: 8
    call_args:
      encoding_format: "float"
    retry:
      type: exponential_backoff
    

### Document processing settings ###

input:
  type: text # [csv, text, json, jsonl]

chunking:
  type: tokens
  size: 1200
  overlap: 100
  encoding_model: o200k_base

### Storage settings ###
## If blob storage is specified in the following four sections,
## connection_string and container_name must be provided

input_storage:
  type: file # [file, blob, cosmosdb]
  base_dir: "input"

output_storage:
  type: file # [file, blob, cosmosdb]
  base_dir: "output"

reporting:
  type: file # [file, blob]
  base_dir: "logs"

cache:
  type: json # [json, memory, none]
  storage:
    type: file # [file, blob, cosmosdb]
    base_dir: "cache"
    
vector_store:
  type: lancedb
  db_uri: output\lancedb
  index_schema:
    text_unit_text:
      vector_size: 1024
    entity_description:
      vector_size: 1024
    community_full_content:
      vector_size: 1024

### Workflow settings ###

embed_text:
  embedding_model_id: default_embedding_model
  batch_size: 8

extract_graph:
  completion_model_id: default_completion_model
  prompt: "prompts/extract_graph.txt"
  entity_types: [organization,person,geo,event]
  max_gleanings: 1

summarize_descriptions:
  completion_model_id: default_completion_model
  prompt: "prompts/summarize_descriptions.txt"
  max_length: 500

extract_graph_nlp:
  text_analyzer:
    extractor_type: regex_english # [regex_english, syntactic_parser, cfg]

cluster_graph:
  max_cluster_size: 10

extract_claims:
  enabled: false
  completion_model_id: default_completion_model
  prompt: "prompts/extract_claims.txt"
  description: "Any claims or facts that could be relevant to information discovery."
  max_gleanings: 1

community_reports:
  completion_model_id: default_completion_model
  graph_prompt: "prompts/community_report_graph.txt"
  text_prompt: "prompts/community_report_text.txt"
  max_length: 2000
  max_input_length: 8000

snapshots:
  graphml: false
  embeddings: false

### Query settings ###
## The prompt locations are required here, but each search method has a number of optional knobs that can be tuned.
## See the config docs: https://microsoft.github.io/graphrag/config/yaml/#query

local_search:
  completion_model_id: default_completion_model
  embedding_model_id: default_embedding_model
  prompt: "prompts/local_search_system_prompt.txt"

global_search:
  completion_model_id: default_completion_model
  map_prompt: "prompts/global_search_map_system_prompt.txt"
  reduce_prompt: "prompts/global_search_reduce_system_prompt.txt"
  knowledge_prompt: "prompts/global_search_knowledge_system_prompt.txt"

drift_search:
  completion_model_id: default_completion_model
  embedding_model_id: default_embedding_model
  prompt: "prompts/drift_search_system_prompt.txt"
  reduce_prompt: "prompts/drift_search_reduce_prompt.txt"

basic_search:
  completion_model_id: default_completion_model
  embedding_model_id: default_embedding_model
  prompt: "prompts/basic_search_system_prompt.txt"
posted @ 2026-03-22 16:31  木石zzz  阅读(222)  评论(1)    收藏  举报