Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for claude v3 models. #18630

Merged
merged 4 commits into from
Mar 6, 2024
Merged

Conversation

3coins
Copy link
Contributor

@3coins 3coins commented Mar 6, 2024

Fixes #18513.

Description

This PR attempts to fix the support for Anthropic Claude v3 models in BedrockChat LLM. The changes here has updated the payload to use the messages format instead of the formatted text prompt for all models; messages API is backwards compatible with all models in Anthropic, so this should not break the experience for any models.

Notes

The PR in the current form does not support the v3 models for the non-chat Bedrock LLM. This means, that with these changes, users won't be able to able to use the v3 models with the Bedrock LLM. I can open a separate PR to tackle this use-case, the intent here was to get this out quickly, so users can start using and test the chat LLM. The Bedrock LLM classes have also grown complex with a lot of conditions to support various providers and models, and is ripe for a refactor to make future changes more palatable. This refactor is likely to take longer, and requires more thorough testing from the community. Credit to PRs 18579 and 18548 for some of the code here.

Copy link

vercel bot commented Mar 6, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Ignored Deployment
Name Status Preview Comments Updated (UTC)
langchain ⬜️ Ignored (Inspect) Visit Preview Mar 6, 2024 10:00pm

@3coins 3coins marked this pull request as ready for review March 6, 2024 06:25
@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. Ɑ: models Related to LLMs or chat model modules 🔌: anthropic Primarily related to Anthropic integrations 🤖:improvement Medium size change to existing code to handle new use-cases labels Mar 6, 2024
Copy link
Contributor

@Barneyjm Barneyjm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me

@Adibuer-lab
Copy link

Adibuer-lab commented Mar 6, 2024

Very NIce @3coins . Also +1 on the refactor

@rsgrewal-aws
Copy link

Could e merge this please we urgently need this for the new Claude models

@Barneyjm
Copy link
Contributor

Barneyjm commented Mar 6, 2024

@efriis is this something you could look at?

) -> Dict[str, Any]:
input_body = {**model_kwargs}
if provider == "anthropic":
input_body["prompt"] = _human_assistant_format(prompt)
if messages:
input_body["anthropic_version"] = "bedrock-2023-05-31"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
input_body["anthropic_version"] = "bedrock-2023-05-31"
if "anthropic_version" not in input_body:
input_body["anthropic_version"] = "bedrock-2023-05-31"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Barneyjm
One concern with this is that allowing another version would have to match the implementation of that version as well, in case it differs from what is currently here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One way to tackle this would be to have version specific implementations, and making sure they work consistently.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah i agree it's not ideal, but providing some capability to pass this in will protect us in the future for whenever a new "anthropic_version" shows up.

@3coins
Copy link
Contributor Author

3coins commented Mar 6, 2024

@Adibuer-lab @Barneyjm
I had some discussion internally about the Bedrock LLM, and considering including the code that @Barneyjm had in his PR to convert the text input to messages. A lot of folks are still using the Bedrock LLM in Conversation chains etc. and want to use the v3 models without converting their code to the chat model. Let me know what you think. Here are the pros and cons.

Pros

  • Can use the v3 models with Bedrock LLM.
  • Don't need to make any changes to convert to the chat model if your code is using the Bedrock LLM

Cons

  • Support for legacy API might be dropped at some point, forcing users to migrate at that point.
  • Bedrock console does not recognize the Human: ...\nAssistant format for their text playground, so this is not an expected use-case for Bedrock LLM.

@efriis
Are there any side-effects of going with this approach? Any suggestions or feedback about this would be helpful.

@christopherwerner
Copy link

@Adibuer-lab @Barneyjm I had some discussion internally about the Bedrock LLM, and considering including the code that @Barneyjm had in his PR to convert the text input to messages. A lot of folks are still using the Bedrock LLM in Conversation chains etc. and want to use the v3 models without converting their code to the chat model. Let me know what you think. Here are the pros and cons.

Pros

  • Can use the v3 models with Bedrock LLM.
  • Don't need to make any changes to convert to the chat model if your code is using the Bedrock LLM

Cons

  • Support for legacy API might be dropped at some point, forcing users to migrate at that point.
  • Bedrock console does not recognize the Human: ...\nAssistant format for their text playground, so this is not an expected use-case for Bedrock LLM.

@efriis Are there any side-effects of going with this approach? Any suggestions or feedback about this would be helpful.

I'd err on the side of compatibility and a nice deprecation warning to allow for the 'might be dropped at some point' use case. Nothing's worse than upgrading langchain and having all your tests break. Actually, scratch that, there is something worse: not having the right tests in the first place and having your application break :)

@Adibuer-lab
Copy link

Adibuer-lab commented Mar 6, 2024

@Adibuer-lab @Barneyjm I had some discussion internally about the Bedrock LLM, and considering including the code that @Barneyjm had in his PR to convert the text input to messages. A lot of folks are still using the Bedrock LLM in Conversation chains etc. and want to use the v3 models without converting their code to the chat model. Let me know what you think. Here are the pros and cons.

Pros

  • Can use the v3 models with Bedrock LLM.
  • Don't need to make any changes to convert to the chat model if your code is using the Bedrock LLM

Cons

  • Support for legacy API might be dropped at some point, forcing users to migrate at that point.
  • Bedrock console does not recognize the Human: ...\nAssistant format for their text playground, so this is not an expected use-case for Bedrock LLM.

@efriis Are there any side-effects of going with this approach? Any suggestions or feedback about this would be helpful.

I agree with @christopherwerner and like what @Barneyjm did there. A lot of people have probably invested a lot of time in developing code for prompts that use human-assistant format so it would be great for them to have the opportunity to use the v3 models while refactoring to use messages api. Being able to easily work with images in messages should motivate to migrate to the messages api

On another note do you think the BedrockLLM could do with leveraging the BaseMessage class from langchain core similar to BedrockChat?

@3coins
Copy link
Contributor Author

3coins commented Mar 6, 2024

I just spoke with @efriis and his recommendation is to migrate the code to BedrockChat if you want to use v3 models. The Bedrock LLM is free-form prompt based, and not suited for converting the text to messages. Agents, anthropic functions etc. are only going to work with chat models. I looked at the Anthropic LLMs as well, they have not introduced any changes for the v3 model in their non-chat LLMs.

One action on my end is to put a warning/error if the v3 model is used with Bedrock LLM, so that users migrate to the BedrockChat if they want to use the v3 model. I will add that shortly.

@Adibuer-lab
Copy link

I just spoke with @efriis and his recommendation is to migrate the code to BedrockChat if you want to use v3 models. The Bedrock LLM is free-form prompt based, and not suited for converting the text to messages. Agents, anthropic functions etc. are only going to work with chat models. I looked at the Anthropic LLMs as well, they have not introduced any changes for the v3 model in their non-chat LLMs.

One action on my end is to put a warning/error if the v3 model is used with Bedrock LLM, so that users migrate to the BedrockChat if they want to use the v3 model. I will add that shortly.

Oh alright then BedrockLLM shouldn't need the BaseMessage class

if prompt:
input_body["prompt"] = _human_assistant_format(prompt)
if "max_tokens_to_sample" not in input_body:
input_body["max_tokens_to_sample"] = 256
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this one isn't necessary right? This was just a separate name for max_tokens previously?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, this is required, it seems like Bedrock is still expecting this.

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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we be using the value from the other here then?

@Barneyjm
Copy link
Contributor

Barneyjm commented Mar 6, 2024

i'm jealous that the linters seem to prefer your commits over my commits.

@dosubot dosubot bot added the lgtm PR looks good. Use to confirm that a PR is ready for merging. label Mar 6, 2024
@efriis efriis merged commit 2b234a4 into langchain-ai:master Mar 6, 2024
59 checks passed
@hteeyeoh
Copy link

hteeyeoh commented Mar 7, 2024

Tried with above changes seems like it not working with model claude2. claude 2.1 as well when using BedrockChat. Claude3 also facing the issue below:
Error raised by bedrock service: An error occurred (ValidationException) when calling the InvokeModel operation: Malformed input request: #: subject must not be valid against schema {"required":["messages"]}#: extraneous key [max_tokens_to_sample] is not permitted, please reformat your input and try again.

return BedrockChat(
"model_id": self.model_name,
"streaming": streaming,
"model_kwargs": {
"max_tokens_to_sample": max_tokens,
"temperature": temp,
},
"callbacks": callbacks,
}

I found out that removing "max_tokens_to_sample" it works. Is it means that we are not allow to parse the max_tokens into kwargs anymore with this PR?

@PTTrazavi
Copy link
Contributor

@hteeyeoh
Try 'max_tokens' instead of "max_tokens_to_sample", it worked for me.
Claude 3 changed the key to 'max_tokens'.

@hteeyeoh
Copy link

hteeyeoh commented Mar 7, 2024

Ya I have tested it out. It works seems this changes reflect to all other claude such as v2, v2.1 as well

@naity2
Copy link

naity2 commented Mar 8, 2024

@Adibuer-lab @Barneyjm I had some discussion internally about the Bedrock LLM, and considering including the code that @Barneyjm had in his PR to convert the text input to messages. A lot of folks are still using the Bedrock LLM in Conversation chains etc. and want to use the v3 models without converting their code to the chat model. Let me know what you think. Here are the pros and cons.

Pros

  • Can use the v3 models with Bedrock LLM.
  • Don't need to make any changes to convert to the chat model if your code is using the Bedrock LLM

Cons

  • Support for legacy API might be dropped at some point, forcing users to migrate at that point.
  • Bedrock console does not recognize the Human: ...\nAssistant format for their text playground, so this is not an expected use-case for Bedrock LLM.

@efriis Are there any side-effects of going with this approach? Any suggestions or feedback about this would be helpful.

Hi @3coins, I'm one of the folks who are still using Bedrock LLM in various chains as you mentioned. I was wondering if BedrockChat can be used in chains like LLMChain and ConversationalRetrievalChain in a similar way to Bedrock. Thank you!

@JGalego
Copy link
Contributor

JGalego commented Mar 8, 2024

@Adibuer-lab @Barneyjm I had some discussion internally about the Bedrock LLM, and considering including the code that @Barneyjm had in his PR to convert the text input to messages. A lot of folks are still using the Bedrock LLM in Conversation chains etc. and want to use the v3 models without converting their code to the chat model. Let me know what you think. Here are the pros and cons.
Pros

  • Can use the v3 models with Bedrock LLM.
  • Don't need to make any changes to convert to the chat model if your code is using the Bedrock LLM

Cons

  • Support for legacy API might be dropped at some point, forcing users to migrate at that point.
  • Bedrock console does not recognize the Human: ...\nAssistant format for their text playground, so this is not an expected use-case for Bedrock LLM.

@efriis Are there any side-effects of going with this approach? Any suggestions or feedback about this would be helpful.

Hi @3coins, I'm one of the folks who are still using Bedrock LLM in various chains as you mentioned. I was wondering if BedrockChat can be used in chains like LLMChain and ConversationalRetrievalChain in a similar way to Bedrock. Thank you!

Works just fine with LLMChain

from langchain.chains.llm import LLMChain
from langchain_core.prompts import PromptTemplate
from langchain_community.chat_models import BedrockChat

chat = BedrockChat(
    model_id="anthropic.claude-3-sonnet-20240229-v1:0",
    model_kwargs={'temperature': 0.1}
)

prompt_template = "Tell me a {adjective} joke"
prompt = PromptTemplate(
    input_variables=["adjective"], template=prompt_template
)

llm = LLMChain(llm=chat, prompt=prompt)

response=llm.invoke({'adjective': "funny"})
print(response['text'])

Output:

Here's a funny joke for you:

Why can't a bicycle stand up by itself? It's two-tired!

@JGalego
Copy link
Contributor

JGalego commented Mar 8, 2024

As for ConversationalRetrievalChain (using Cohere Embed v3 via Bedrock to generate the embeddings)

import os

from urllib.request import urlretrieve

from langchain.chains import ConversationalRetrievalChain

from langchain_community.chat_models import BedrockChat
from langchain_community.document_loaders import TextLoader
from langchain_community.embeddings.bedrock import BedrockEmbeddings
from langchain_community.vectorstores import Chroma

from langchain_text_splitters import CharacterTextSplitter

if not os.path.isfile("state_of_the_union.txt"):
    urlretrieve(
        "https://raw.githubusercontent.com/hwchase17/chat-your-data/master/state_of_the_union.txt",
        "state_of_the_union.txt"
    )

# Vector store + retriever
raw_documents = TextLoader('state_of_the_union.txt').load()
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
documents = text_splitter.split_documents(raw_documents)
vectorstore = Chroma.from_documents(
    documents, BedrockEmbeddings(model_id="cohere.embed-english-v3")
)
retriever = vectorstore.as_retriever()

# Model
chat = BedrockChat(
    model_id="anthropic.claude-3-sonnet-20240229-v1:0",
    model_kwargs={'temperature': 0.0}
)

# Chain
chain = ConversationalRetrievalChain.from_llm(
    llm=chat,
    chain_type="stuff",
    retriever=vectorstore.as_retriever(),
    return_generated_question=True,
    verbose=False,
)

# Request
response = chain.invoke({
    'question': "What did the president say about Ketanji Brown Jackson",
    'chat_history': []})
print(response['answer'])

Output:

In his State of the Union address, President Biden spoke about his recent nomination of Ketanji Brown Jackson to the Supreme Court, saying:

"One of the most serious constitutional responsibilities a President has is nominating someone to serve on the United States Supreme Court. And I did that 4 days ago, when I nominated Circuit Court of Appeals Judge Ketanji Brown Jackson. One of our nation's top legal minds, who will continue Justice Breyer's legacy of excellence."

He praised Judge Jackson as "one of our nation's top legal minds" who will continue the legacy of the retiring Justice Stephen Breyer on the Supreme Court. Biden highlighted his constitutional duty to nominate Supreme Court justices and noted that he had just nominated Judge Jackson 4 days prior to his State of the Union address.

@Barneyjm Barneyjm mentioned this pull request Mar 9, 2024
5 tasks
@minorun365
Copy link

I wonder Bedrock LLM should be seperated by each providor models.
(e.g. BedrockClaude, etc.)

It can be complicated when we continue managing whole the models on Bedrock as all-in-one 🤔

@rsgrewal-aws
Copy link

rsgrewal-aws commented Mar 10, 2024 via email

@wewebber-merlin
Copy link
Contributor

I'm backporting these changes to langchain_community.chat_models.anthropic and I can't see the purpose of detecting the "image_url" type and converting it to the "image" type in _format_image. Is this for backwards compatibility with an earlier API version? Or is this something specific to Bedrock?

@ks233ever
Copy link

ks233ever commented Mar 11, 2024

It's not working for me in LLMChain as mentioned above. Running this code, I get an LLMChain error:

llm = BedrockChat(model_id="anthropic.claude-3-sonnet-20240229-v1:0", client=bedrock_client, model_kwargs={'max_tokens_to_sample':500})

qa = RetrievalQA.from_chain_type(
        llm=llm,
        chain_type="stuff",
        retriever=db.as_retriever(
            search_type="similarity", search_kwargs={"k": 20}
        ),
        return_source_documents=True,
        chain_type_kwargs={"prompt": PROMPT}
    )
    answer = qa({"query": query})
`llm_chain = LLMChain(
     74         llm=llm,
     75         prompt=_prompt,
     76         verbose=verbose,
     77         callback_manager=callback_manager,
     78         callbacks=callbacks,
     79     )
     80     # TODO: document prompt
     81     return StuffDocumentsChain(
     82         llm_chain=llm_chain,
     83         document_variable_name=document_variable_name,
   (...)
     87         **kwargs,
     88     )

File /opt/conda/lib/python3.10/site-packages/langchain/load/serializable.py:97, in Serializable.__init__(self, **kwargs)
     96 def __init__(self, **kwargs: Any) -> None:
---> 97     super().__init__(**kwargs)
     98     self._lc_kwargs = kwargs

File /opt/conda/lib/python3.10/site-packages/pydantic/v1/main.py:341, in BaseModel.__init__(__pydantic_self__, **data)
    339 values, fields_set, validation_error = validate_model(__pydantic_self__.__class__, data)
    340 if validation_error:
--> 341     raise validation_error
    342 try:
    343     object_setattr(__pydantic_self__, '__dict__', values)

ValidationError: 1 validation error for LLMChain
llm
  Can't instantiate abstract class BaseLanguageModel with abstract methods agenerate_prompt, apredict, apredict_messages, generate_prompt, invoke, predict, predict_messages (type=type_error)

`

@ralliart2004
Copy link

@3coins When will this get tagged? I want to use this update.

@3coins
Copy link
Contributor Author

3coins commented Mar 12, 2024

@ks233ever
Use max_tokens instead of max_tokens_to_sample for the v3 models.

bechbd pushed a commit to bechbd/langchain that referenced this pull request Mar 29, 2024
Fixes langchain-ai#18513.

## Description
This PR attempts to fix the support for Anthropic Claude v3 models in
BedrockChat LLM. The changes here has updated the payload to use the
`messages` format instead of the formatted text prompt for all models;
`messages` API is backwards compatible with all models in Anthropic, so
this should not break the experience for any models.


## Notes
The PR in the current form does not support the v3 models for the
non-chat Bedrock LLM. This means, that with these changes, users won't
be able to able to use the v3 models with the Bedrock LLM. I can open a
separate PR to tackle this use-case, the intent here was to get this out
quickly, so users can start using and test the chat LLM. The Bedrock LLM
classes have also grown complex with a lot of conditions to support
various providers and models, and is ripe for a refactor to make future
changes more palatable. This refactor is likely to take longer, and
requires more thorough testing from the community. Credit to PRs
[18579](langchain-ai#18579) and
[18548](langchain-ai#18548) for some
of the code here.

---------

Co-authored-by: Erick Friis <erick@langchain.dev>
gkorland pushed a commit to FalkorDB/langchain that referenced this pull request Mar 30, 2024
Fixes langchain-ai#18513.

## Description
This PR attempts to fix the support for Anthropic Claude v3 models in
BedrockChat LLM. The changes here has updated the payload to use the
`messages` format instead of the formatted text prompt for all models;
`messages` API is backwards compatible with all models in Anthropic, so
this should not break the experience for any models.


## Notes
The PR in the current form does not support the v3 models for the
non-chat Bedrock LLM. This means, that with these changes, users won't
be able to able to use the v3 models with the Bedrock LLM. I can open a
separate PR to tackle this use-case, the intent here was to get this out
quickly, so users can start using and test the chat LLM. The Bedrock LLM
classes have also grown complex with a lot of conditions to support
various providers and models, and is ripe for a refactor to make future
changes more palatable. This refactor is likely to take longer, and
requires more thorough testing from the community. Credit to PRs
[18579](langchain-ai#18579) and
[18548](langchain-ai#18548) for some
of the code here.

---------

Co-authored-by: Erick Friis <erick@langchain.dev>
@tomnijsdruva
Copy link

tomnijsdruva commented Apr 3, 2024

Quick note here, going off of the XML Agent documentation, "Use with regular LLMs, not with chat models." is stated as a tip for anthropic models. Should an exception be added for Claude V3 models as the Bedrock class doesn't support messages-based API?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🔌: anthropic Primarily related to Anthropic integrations 🤖:improvement Medium size change to existing code to handle new use-cases lgtm PR looks good. Use to confirm that a PR is ready for merging. Ɑ: models Related to LLMs or chat model modules size:L This PR changes 100-499 lines, ignoring generated files.
Projects
None yet