You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Colab集成PaLM与LlamaIndex查询谷歌云端硬盘文档卡顿求助

问题排查与修复方案

核心问题分析

  1. 变量重复赋值导致LLM配置失效
    代码中先创建了PaLM实例model = PaLM(api_key=palm_api_key),但后续又将model重新赋值为模型名称字符串model = models[0].name,直接覆盖了之前的PaLM实例。这导致索引和查询环节根本没有使用你配置的PaLM模型,大概率默认调用了OpenAI模型(你未提供对应API密钥,因此卡住)。

  2. 未实际应用ServiceContext配置
    你提到添加了service_context语句,但代码中没有将其传入索引创建或查询引擎的流程中,等于没生效。

修复后的完整代码

!pip install -q google-generativeai
!pip install docx2txt
!pip install pypdf
!pip install llama-index

import pprint
import google.generativeai as palm
from llama_index import (
    VectorStoreIndex, 
    SimpleDirectoryReader, 
    StorageContext, 
    ServiceContext,
    load_index_from_storage   
)
from llama_index.llms import PaLM

palm_api_key = "你的API密钥"
palm.configure(api_key=palm_api_key)

# 正确初始化PaLM实例,避免变量覆盖
palm_llm = PaLM(api_key=palm_api_key)

# 验证可用模型(可选步骤)
models = [m for m in palm.list_models() if 'generateText' in m.supported_generation_methods]
print(models[0].name)

from google.colab import drive
drive.mount('/content/drive')

# 创建ServiceContext并绑定PaLM实例
service_context = ServiceContext.from_defaults(llm=palm_llm)

documents = SimpleDirectoryReader('/content/drive/My Drive/Trajan GPT Corpus').load_data()
# 创建索引时传入配置好的ServiceContext
index = VectorStoreIndex.from_documents(documents, service_context=service_context)

# 查询引擎会自动继承索引的ServiceContext配置
query_engine = index.as_query_engine()
response = query_engine.query("What is trajan systems?")
print(response)

额外排查点

  • 检查Google云端硬盘文件夹内的文档格式是否都被支持(docx、pdf没问题,其他格式可能导致加载异常)
  • 确认PaLM API密钥权限正常、额度未耗尽
  • Colab访问PaLM API可能存在网络延迟,可尝试重启运行时后重新执行

内容的提问来源于stack exchange,提问作者Austin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 17:33:25