[Bug]: llama_index与Bedrock:Anthropic Claude提示问题

oyt4ldly  于 23天前  发布在  其他
关注(0)|答案(4)|浏览(22)
错误描述:
llm = Bedrock(model="anthropic.claude-3-sonnet-20240229-v1:0")
messages = [
    ChatMessage(role="system", content="You are a pirate with a colorful personality"),
    ChatMessage(role="user", content="Tell me a story"),
]
llm.chat(messages=[
    ChatMessage(role="system", content="You are a pirate with a colorful personality"),
    ChatMessage(role="user", content="Tell me a story"),
])
以上代码抛出以下错误
文件 "C:\Users\Jay\OneDrivecoding_projects\RAG_AWS_Neptune\venv\Lib\site-packages\llama_index\llms\bedrock\utils.py", line 157, in get_request_body
如果 len(prompt) > 0 and prompt[0]["role"] == "system":
~~~~~~~~~^^^^^^^^
TypeError: string indices must be integers, not 'str'
这发生在'chat'和'complete'方法上
版本
0.10.68
重现步骤:

### 安装库:

pip install llama-index-llms-bedrock
pip install llama-index
from llama_index.llms.bedrock import Bedrock
llm = Bedrock(model="anthropic.claude-3-sonnet-20240229-v1:0", region_name="ap-south-1")
messages = [
    ChatMessage(role="system", content="You are a pirate with a colorful personality"),
    ChatMessage(role="user", content="Tell me a story"),
]
response=llm.chat(messages=[
    ChatMessage(role="system", content="You are a pirate with a colorful personality"),
    ChatMessage(role="user", content="Tell me a story"),
])
相关日志/回溯

* 无响应*
qgelzfjb

qgelzfjb1#

嘿,@jayanthma!我在这里帮助你解决任何错误、问题或贡献,当你等待人类维护者时。让我们一起解决这个问题!
你遇到的错误是由于 prompt 参数被期望为字符串,但代码试图将其作为字典列表访问。这种不匹配导致了 TypeError
llama_index.llms.bedrock.utils 中的 get_request_body 函数中,prompt 参数应该是一个字符串。对于 anthropic.claude 模型,提示应该按照以下格式进行:

"\n\nHuman: {user_message}\n\nAssistant: "

这是你可以修改代码以确保提示正确格式化的方法:

from llama_index.llms.bedrock import Bedrock
from llama_index.core.base.llms.types import ChatMessage

llm = Bedrock(model="anthropic.claude-3-sonnet-20240229-v1:0", region_name="ap-south-1")
messages = [
    ChatMessage(role="system", content="You are a pirate with a colorful personality"),
    ChatMessage(role="user", content="Tell me a story"),
]
response = llm.chat(messages=messages)

确保将 ChatMessage 对象正确传递给 llm.chat 方法。prompt 将由 get_request_body 函数内部格式化,以匹配 anthropic.claude 模型 1(https://github.com/run-llama/llama_index/blob/main/llama-index-legacy/tests/llms/test_bedrock.py) 的预期结构。

  • 要继续与 Dosu 交谈,请提及 @dosu。*
w6mmgewl

w6mmgewl2#

我没有在这里使用kwargs
@jayanthma 如果不使用kwarg会发生什么?llm.chat(messages)?

vhmi4jdf

vhmi4jdf3#

看起来LLM类中有一个bug。get_request_body函数期望一个字符串,但出于某种原因,它的人类版本被编写为期望一个聊天消息字典列表。

我建议仅使用bedrock-converse的LLM类,因为它也支持函数调用作为解决方法。

不确定何时可以解决这个问题,但如果你愿意的话,我建议你提交一个PR。我认为我们需要一种方法来决定get_request_body是否应该在字符串上调用,还是在聊天消息列表上调用?或者其他什么方法?

vx6bjr1n

vx6bjr1n4#

确认这是一个问题,我也遇到了。尝试更改为bedrock-converse llm类,但缺少_client属性。将llama-index降级到0.10.59解决了这个问题。

相关问题