使用LangChain调用GPT-4做摘要时出现ValueError: Got unknown type S错误求助
LangChain调用GPT-4执行文本摘要任务时出现
ValueError: Got unknown type S的解决方法 问题场景
使用LangChain结合GPT-4执行文本摘要任务时,切换到GPT-4模型后立即触发以下错误:
ValueError: Got unknown type S
GPT-3.5-turbo模型下代码运行正常,触发错误的核心代码如下:
llm = ChatOpenAI(temperature=0, model_name="gpt-4") text_splitter = RecursiveCharacterTextSplitter( chunk_size=4000, chunk_overlap=0, separators=[" ", ",", "\n"] ) texts = text_splitter.split_text(readme_content) docs = [Document(page_content=t) for t in texts] prompt_template = """template""" PROMPT = PromptTemplate(template=prompt_template, input_variables=["text"]) chain = load_summarize_chain(llm, chain_type="map_reduce", map_prompt=PROMPT, combine_prompt=PROMPT) summary = chain.run(docs)
完整错误回溯:
Traceback (most recent call last): File "/Users/maxhager/Projects2023/githubgpt/testing_env/test_single.py", line 136, in <module> print(create_tweet(info, readme_content)) File "/Users/maxhager/Projects2023/githubgpt/testing_env/test_single.py", line 120, in create_tweet summary = llm(prompt) File "/Users/maxhager/.virtualenvs/githubgpt/lib/python3.10/site-packages/langchain/chat_models/base.py", line 128, in __call__ return self._generate(messages, stop=stop).generations[0].message File "/Users/maxhager/.virtualenvs/githubgpt/lib/python3.10/site-packages/langchain/chat_models/openai.py", line 247, in _generate message_dicts, params = self._create_message_dicts(messages, stop) File "/Users/maxhager/.virtualenvs/githubgpt/lib/python3.10/site-packages/langchain/chat_models/openai.py", line 277, in _create_message_dicts message_dicts = [_convert_message_to_dict(m) for m in messages] File "/Users/maxhager/.virtualenvs/githubgpt/lib/python3.10/site-packages/langchain/chat_models/openai.py", line 277, in <listcomp> message_dicts = [_convert_message_to_dict(m) for m in messages] File "/Users/maxhager/.virtualenvs/githubgpt/lib/python3.10/site-packages/langchain/chat_models/openai.py", line 88, in _convert_message_to_dict raise ValueError(f"Got unknown type {message}") ValueError: Got unknown type S
错误原因
从回溯信息可定位问题:ChatOpenAI作为聊天模型,要求输入提示必须是聊天消息格式(即ChatMessage结构),但代码中使用的是普通PromptTemplate,生成的是纯文本格式提示。GPT-3.5-turbo对格式兼容性较强,能自动适配,但GPT-4的接口对输入格式要求更严格,导致消息转换时抛出类型错误。
解决方法
1. 替换为ChatPromptTemplate
将普通PromptTemplate替换为ChatPromptTemplate,构建符合聊天模型要求的消息结构(包含system、human等角色消息):
from langchain.prompts import ChatPromptTemplate # 定义map阶段的聊天提示 map_prompt_template = """请总结以下文本的核心内容: {text}""" MAP_PROMPT = ChatPromptTemplate.from_messages([ ("system", "你是一个精准的文本摘要助手,需提炼文本关键信息,避免冗余"), ("human", map_prompt_template) ]) # 定义combine阶段的聊天提示 combine_prompt_template = """请将以下多个文本片段的摘要合并为一个连贯、全面的总摘要: {text}""" COMBINE_PROMPT = ChatPromptTemplate.from_messages([ ("system", "你需要整合多个片段的摘要,确保逻辑通顺、覆盖所有核心要点"), ("human", combine_prompt_template) ])
2. 调整load_summarize_chain参数
使用修改后的ChatPromptTemplate初始化摘要链:
chain = load_summarize_chain( llm, chain_type="map_reduce", map_prompt=MAP_PROMPT, combine_prompt=COMBINE_PROMPT ) summary = chain.run(docs)
3. 其他排查项
- 升级LangChain版本:旧版本可能存在GPT-4兼容漏洞,执行
pip install --upgrade langchain升级到最新稳定版 - 验证API权限:确认你的OpenAI API密钥拥有GPT-4模型的调用权限,避免因权限不足导致的异常返回
- 清理文本内容:检查待处理的文本块是否包含特殊字符或异常格式,可尝试先清理文本(如移除不可见字符)再进行拆分和摘要
内容的提问来源于stack exchange,提问作者yemy
相关产品推荐
相关产品推荐

