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

feat: add Anthropic support #288

Merged
merged 26 commits into from
Apr 21, 2024
Merged

Conversation

ocss884
Copy link
Member

@ocss884 ocss884 commented Sep 14, 2023

Description

Describe your changes in detail.

Motivation and Context

close #283

  • I have raised an issue to propose this change (required for new features and bug fixes)

Types of changes

What types of changes does your code introduce? Put an x in all the boxes that apply:

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds core functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation (update in the documentation)
  • Example (update in the folder of example)

Implemented Tasks

  • add Anthropic LLM (Claude-2, Claue-instant-1) backend
  • add an example of using Claude

Checklist

Go over all the following points, and put an x in all the boxes that apply.
If you are unsure about any of these, don't hesitate to ask. We are here to help!

  • I have read the CONTRIBUTION guide. (required)
  • My change requires a change to the documentation.
  • I have updated the tests accordingly. (required for a bug fix or a new feature)
  • I have updated the documentation accordingly.

@Benjamin-eecs Benjamin-eecs changed the title add Anthropic support #283 feat: add Anthropic support Sep 18, 2023
Copy link
Collaborator

@Obs01ete Obs01ete left a comment

Choose a reason for hiding this comment

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

Thank you @ocss884. This is a very well prepared PR. Let's sort out what is wrong with the two system messages in the main code. Also we will need tests for the code that you added including examples. Please wrap the code that would crash if not Anthropic key provided into skipif as here:

@pytest.mark.skipif(not check_server_running(DEFAULT_SERVER_URL),

Please run code coverage as in CONTRIBUTING.md and for all the code that you have added make sure that it covered.

ret = system_prompt
for msg in messages[1:]:
role, content = msg["role"], msg["content"]
# Claude does not perform well if the system message RULE OF USER/ASSISTANT is sent twice. (see role_playing/RolePlaying.init_chat())
Copy link
Collaborator

Choose a reason for hiding this comment

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

Thank you for spotting. Did you discover a bug of sending the system prompt twice? We never observed it. Could you create a bug ticket to reproduce it and fix? We'd better fix the erroneous behavior in the main code than create a detour here in your code.

Copy link
Member Author

@ocss884 ocss884 Oct 13, 2023

Choose a reason for hiding this comment

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

I think it is an expected behavior since in init_chat() the doc_string says "..sending the system message again to the agents using chat messages". The full doc_string is as below:

def init_chat(self) -> Tuple[BaseMessage, List[BaseMessage]]:
r"""Initializes the chat by resetting both of the assistant and user
agents, and sending the system messages again to the agents using
chat messages. Returns the assistant's introductory message and the
user's response messages.

I should say it is confusing for me since in role_play, this init_chat function is used to get the first input_assistant_msg and often it is just a modified version of user_sys_msg. Look at the first return value:

assistant_msg = BaseMessage.make_assistant_message(
role_name=self.assistant_sys_msg.role_name,
content=(f"{self.user_sys_msg.content}. "
"Now start to give me instructions one by one. "
"Only reply with Instruction and Input."))
user_msg = BaseMessage.make_user_message(
role_name=self.user_sys_msg.role_name,
content=f"{self.assistant_sys_msg.content}")
assistant_response = self.assistant_agent.step(user_msg)
if assistant_response.terminated or assistant_response.msgs is None:
raise ValueError(f"Assistant agent terminated unexpectedly. "
f"Error info: {assistant_response.info}")
return assistant_msg, assistant_response.msgs

For example in the open_source_example, it is used as input_assistant_msg
input_assistant_msg, _ = role_play_session.init_chat()

and then this modified user_sys_msg is send to usr once again during the first round of chatting
user_response = self.user_agent.step(assistant_msg)

So during the first API call, the assistant_msg is just the whole user_sys_msg append with two more instructions:
assistant_msg = BaseMessage.make_assistant_message(
role_name=self.assistant_sys_msg.role_name,
content=(f"{self.user_sys_msg.content}. "
"Now start to give me instructions one by one. "
"Only reply with Instruction and Input."))

That makes what I find a "duplicate system messages". I'm happy to create a ticket and solve it if you can confirm this is not desired.


For me, I would prefer to make

"Now start to give me instructions one by one. "
"Only reply with Instruction and Input."))

only as the very first assistant_msg. It is more intuitive to send the rule once and then instruct the user_model to go first using simple instructions. Also, I recommend adding more comments to init_chat if you want to keep it. It is confusing of making a variable called assistant_msg with the content of usr_sys_msg.

Copy link
Collaborator

@dandansamax dandansamax left a comment

Choose a reason for hiding this comment

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

Great code! Leave some comments.

camel/utils/token_counting.py Outdated Show resolved Hide resolved
camel/utils/token_counting.py Outdated Show resolved Hide resolved
examples/anthropic_models/role_playing_with_claude.py Outdated Show resolved Hide resolved
pyproject.toml Outdated Show resolved Hide resolved
camel/configs.py Outdated Show resolved Hide resolved
camel/configs.py Outdated Show resolved Hide resolved
@ocss884 ocss884 closed this Jan 3, 2024
@ocss884 ocss884 reopened this Jan 3, 2024
@ocss884
Copy link
Member Author

ocss884 commented Jan 3, 2024

Anthropic have a more openai-like 'Message completion' in beta now to replace their current text completion.
In the new function, people can use the user-assistant form, for example:

[
  {"role": "user", "content": "Hello there."},
  {"role": "assistant", "content": "Hi, I'm Claude. How can I help you?"},
  {"role": "user", "content": "Can you explain LLMs in plain English?"},
]

I will update once they are done.

@ocss884 ocss884 mentioned this pull request Jan 3, 2024
3 tasks
@ocss884 ocss884 mentioned this pull request Mar 7, 2024
2 tasks
@Wendong-Fan Wendong-Fan self-requested a review March 8, 2024 13:21
@Wendong-Fan
Copy link
Member

Hey @ocss884 , I plan to do the review later this week, would you like to solve the conflicts and update this PR with latest master branch? Thanks a lot!

Copy link

coderabbitai bot commented Mar 17, 2024

Important

Auto Review Skipped

Auto reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@ocss884
Copy link
Member Author

ocss884 commented Mar 17, 2024

Hey @ocss884 , I plan to do the review later this week, would you like to solve the conflicts and update this PR with latest master branch? Thanks a lot!

Hi @Wendong-Fan I just updated this PR. Due to an openai recent release, I limit the package version #474 . Some test cases are failed cuz this PR was opened when I was not a member and that makes the OPENAI_API_KEY secret empty (you can check the log). pytest . gives an all pass on my local.

Copy link
Member

@Wendong-Fan Wendong-Fan left a comment

Choose a reason for hiding this comment

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

Thanks for the contribution, overall looks great, left some comments, after your update we can merge it

camel/utils/commons.py Outdated Show resolved Hide resolved
pyproject.toml Outdated Show resolved Hide resolved
pyproject.toml Outdated Show resolved Hide resolved
camel/models/anthropic_model.py Outdated Show resolved Hide resolved
Copy link
Member

@Wendong-Fan Wendong-Fan left a comment

Choose a reason for hiding this comment

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

Thanks for the contribution! Please feel free to resolve the conflicts and merge it.

@ocss884
Copy link
Member Author

ocss884 commented Apr 1, 2024

Going to add Claude3 support before merge it.

@Wendong-Fan Wendong-Fan linked an issue Apr 6, 2024 that may be closed by this pull request
2 tasks
@ocss884 ocss884 requested a review from a team April 17, 2024 07:11
@ocss884
Copy link
Member Author

ocss884 commented Apr 17, 2024

@Wendong-Fan @dandansamax Hi Wendong and Tianqi. Claude3 is ready now. But it seems I cannot approve the PR myself. @dandansamax could you help take a look? Thanks!

@ocss884 ocss884 merged commit 25c8e42 into camel-ai:master Apr 21, 2024
1 of 6 checks passed
Appointat added a commit that referenced this pull request Apr 21, 2024
@Appointat
Copy link
Member

Appointat commented Apr 21, 2024

Hi, @ocss884 , I found this PR was merged without approval and the pipelines were not passed. It is the wrong approach, and it must be reverted.

Hi, @Wendong-Fan . You have to see this, please. ❗

image

And I created a PR to revert the last commit in master branch. #518

Returns:
int: Number of tokens in the messages.
"""
prompt = messages_to_prompt(messages, self.model_type)
Copy link
Member

Choose a reason for hiding this comment

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

It seems messages_to_prompt does not support anthropic models yet. How does it work?

def messages_to_prompt(messages: List[OpenAIMessage], model: ModelType) -> str:

elif model.is_anthropic:

# use XML tag to decorate prompt
# https://docs.anthropic.com/claude/docs/constructing-a-prompt#mark-different-parts-of-the-prompt
# https://docs.anthropic.com/claude/docs/roleplay-dialogue
# https://docs.anthropic.com/claude/docs/human-and-assistant-formatting
system_message = str(system_message)
ret = f"\n{system_message}\n"
for msg in messages[1:]:
role, content = msg["role"], msg["content"]
# Claude does not perform well if the system message RULE OF USER/ASSISTANT is sent twice. (see role_playing/RolePlaying.init_chat())
# Here is a special treatment for Claude to remove the redundant system message.
if not isinstance(content, str):
raise ValueError("Currently multimodal context is not "
"supported by the token counter.")
if content.startswith(system_message):
ret = content
continue
if role == "user":
ret += f"{HUMAN_PROMPT} {content}"
elif role == "assisstant":
ret += f"{AI_PROMPT} {content}"
return ret
else:
Copy link
Member

Choose a reason for hiding this comment

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

why did this part deleted? @ocss884

cc @lightaime

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Merged or Closed
Development

Successfully merging this pull request may close these issues.

[Feature Request] Integrate Claude 3 [Feature Request] Add support to Anthropic LLM
6 participants