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 external tool support to ChatAgent & Refactor #830

Merged
merged 30 commits into from
Sep 11, 2024

Conversation

WHALEEYE
Copy link
Collaborator

@WHALEEYE WHALEEYE commented Aug 16, 2024

Description

Add a set of tools named external_tools into ChatAgent, allowing users to directly get tool calling requests for a certain set of external tools. This will also do refactoring to the step() and step_async() function we currently have.

Motivation and Context

This agent can facilitate the integration of CAMEL into CRAB.
We'll see if there's any workaround at CRAB's side in the future.
This will also close #894.

Copy link

coderabbitai bot commented Aug 16, 2024

Important

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.
    • @coderabbitai help me debug CodeRabbit configuration file.

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 using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration 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.

@WHALEEYE WHALEEYE changed the title add external tool agent support Feat: add external tool agent support Aug 16, 2024
@Wendong-Fan Wendong-Fan added the Agent Related to camel agents label Aug 16, 2024
@Wendong-Fan Wendong-Fan added this to the Sprint 10 milestone Aug 16, 2024
@WHALEEYE WHALEEYE marked this pull request as ready for review August 22, 2024 16:45
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 @WHALEEYE ! Does this agent need structured output if the target of this agent is just to return the tool calling request from external tools?

Also for now if external tools is added or both internal and external tools added, this agent would not have any content in the response, does this make sense?

Could you also add some test for this agent? Thanks!

@WHALEEYE
Copy link
Collaborator Author

WHALEEYE commented Aug 30, 2024

Does this agent need structured output if the target of this agent is just to return the tool calling request from external tools?

I'm doing this mainly out of the reason that we may further integrate external tool support directly into the ChatAgent, instead of making it a special agent. I demonstrated this in a case in the example (also unit test), where user doesn't want to get the external tool requests but just want normal responses (some internal tools can be called in the process).

Also for now if external tools is added or both internal and external tools added, this agent would not have any content in the response, does this make sense?

Yes, if the external tools are called, then response.msg would be empty and the tool calling request would be set in response.info. However, if no external tools are called, the agent just acts like a ChatAgent. I think this would not cause much confusion if a user chooses to pass in external tools, which means that he intentionally want the agent to return tool calling requests directly.

@WHALEEYE WHALEEYE changed the title Feat: add external tool agent support [Draft] Feat: add external tool support to ChatAgent Sep 3, 2024
@WHALEEYE WHALEEYE marked this pull request as draft September 3, 2024 18:45
@WHALEEYE WHALEEYE marked this pull request as ready for review September 5, 2024 02:31
@WHALEEYE WHALEEYE changed the title [Draft] Feat: add external tool support to ChatAgent Feat: Add external tool support to ChatAgent & Refactor Sep 5, 2024
@WHALEEYE WHALEEYE added the enhancement New feature or request label Sep 5, 2024
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 @WHALEEYE , left some comments below, overall LGTM

examples/external_tools/use_external_tools.py Outdated Show resolved Hide resolved
camel/agents/chat_agent.py Show resolved Hide resolved
camel/agents/chat_agent.py Outdated Show resolved Hide resolved
camel/agents/chat_agent.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 @WHALEEYE , left some comments

Comment on lines -374 to +381
self.is_tools_added()
and isinstance(response, ChatCompletion)
and response.choices[0].message.tool_calls is not None
not self.is_tools_added()
or not isinstance(response, ChatCompletion)
or response.choices[0].message.tool_calls is None
Copy link
Member

Choose a reason for hiding this comment

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

if user sets tool_choice="required" in LLM config, then tools will always be added, which would lead to infinite while loop

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think it's the desired behavior in current design. Acutally the tool_choice config should be managed by the agent rather than the user.

Copy link
Member

Choose a reason for hiding this comment

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

Hey @dandansamax , I think since currently we support tool_choice in LLM parameter then we need to make sure there will not have this kind of infinite loop in ChatAgent, I discussed with @WHALEEYE , we can handle tool_choice separately when doing ChatAgent refactor further

camel/agents/chat_agent.py Outdated Show resolved Hide resolved
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.

Generally great. But so many legacy problems in the old code. We should open up new issues to tackle them.

camel/agents/chat_agent.py Outdated Show resolved Hide resolved
camel/agents/chat_agent.py Outdated Show resolved Hide resolved
# Format messages and get the token number
openai_messages: Optional[List[OpenAIMessage]]

# Check if token has exceeded
try:
openai_messages, num_tokens = self.memory.get_context()
except RuntimeError as e:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Not related to this PR: why treats all RuntimeError as step token exceed?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's open up a new issue for this

base_message_item.content = str(info['tool_calls'][0].result)
# Normal function calling
tool_call_records.append(
await self._step_tool_call_and_update_async(response)
Copy link
Collaborator

Choose a reason for hiding this comment

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

The async implementation is meaningless. We are only using the sequential behavior. Please open up an issue to remove all duplicated async code.

test/agents/test_chat_agent.py Outdated Show resolved Hide resolved
@camel-ai camel-ai deleted a comment from dandansamax Sep 10, 2024
camel/agents/chat_agent.py Show resolved Hide resolved
test/agents/test_chat_agent.py Outdated Show resolved Hide resolved
@WHALEEYE
Copy link
Collaborator Author

WHALEEYE commented Sep 10, 2024

After discussing with @dandansamax, I think we'd better do some refactoring to the whole ChatAgent for a better code quality and maintainability. But I think we can merge it for now because the external tool has a high priority, and create a issue later on the refactoring. wdyt @Wendong-Fan

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.

LGTM. Thanks

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.

As discussed, some enhancement would be implemented in another refactor PR by @WHALEEYE

@Wendong-Fan Wendong-Fan merged commit 55c6fad into master Sep 11, 2024
6 checks passed
@Wendong-Fan Wendong-Fan deleted the feature/adapt-crab branch September 11, 2024 16:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Agent Related to camel agents enhancement New feature or request
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

[BUG] Tools will be invalid after calling step() with output schema
3 participants