aws-samples / serverless-pdf-chat

LLM-powered document chat using Amazon Bedrock and AWS Serverless
https://aws.amazon.com/blogs/compute/building-a-serverless-document-chat-with-aws-lambda-and-amazon-bedrock/
MIT No Attribution
224 stars 202 forks source link

Malformed input keys #35

Closed Thato-Lesetla closed 3 weeks ago

Thato-Lesetla commented 6 months ago

Good day, I'm currently getting the below error when triggering the response lambda. I'm currently using a different model then the one specified on the repo.

[ERROR] ValueError: Error raised by bedrock service: An error occurred (ValidationException) when calling the InvokeModel operation: Malformed input request: extraneous key [textGenerationConfig] is not permitted, please reformat your input and try again.

` logger.info("Executing vector embeddings") embeddings, llm = BedrockEmbeddings( model_id="amazon.titan-embed-text-v1", client=bedrock_runtime, region_name="us-east-1", ), Bedrock( model_id="amazon.titan-embed-text-v1", client=bedrock_runtime, region_name="us-east-1" ) faiss_index = FAISS.load_local("/tmp", embeddings)

message_history = DynamoDBChatMessageHistory(
    table_name=MEMORY_TABLE, session_id=conversation_id
)

memory = ConversationBufferMemory(
    memory_key="chat_history",
    chat_memory=message_history,
    input_key="question",
    output_key="answer",
    return_messages=True,
)

logger.info(memory)

logger.info("Executing conversational retrival chain")
qa = ConversationalRetrievalChain.from_llm(
    llm=llm,
    retriever=faiss_index.as_retriever(),
    memory=memory,
    return_source_documents=True,
)

logger.info("Executing question using human input variable")
res = qa({ "question": human_input })

`

chakeega commented 6 months ago

i am getting this same error with:

    PROMPT = PromptTemplate(input_variables=["context", "question"], template=prompt_template)

    query_answer = RetrievalQA.from_chain_type(
        llm=bedrock_llm,
        chain_type="stuff",
        retriever=opensearch_vector_search_client.as_retriever(),
        return_source_documents=True,
        chain_type_kwargs={
            "prompt": PROMPT,
            "verbose": True,
        },
        verbose=True,
    )
    response = query_answer(inputs={"query": question, "context": context})

langchain==1.0.9 boto3==1.34.49 opensearch-py==2.4.2

Lambda is using Python3.12

MNorgren commented 1 month ago

I am getting this same error, but in the node library. Did you guys find the solution?

moserda commented 3 weeks ago

Hi @Thato-Lesetla,

looking at your code snippet I believe the reason for the error is that you're specifying the Titan embedding model as the chat model.

), Bedrock(
model_id="amazon.titan-embed-text-v1", client=bedrock_runtime, region_name="us-east-1"
)

I was able to reproduce this with amazon.titan-embed-text-v1. However, if you use e.g. amazon.titan-text-premier-v1:0 it should work (since the Titan text generation models support the textGenerationConfig parameter, see the docs).

Please also note that since you opened the issue the code has changed quite a bit, most notably we're not using the plain Bedrock class anymore but instead ChatBedrock.

Closing this issue for now.