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: support for tools in OpenAIChatGenerator #57

Merged
merged 30 commits into from
Aug 19, 2024

Conversation

anakin87
Copy link
Member

@anakin87 anakin87 commented Aug 12, 2024

Related Issues

Proposed Changes:

I'm creating a new version of the OpenAIChatGenerator to support tools (in __init__ and run methods).

Supporting the new ChatMessage and Tool required several changes, so I decided to completely override the original Haystack component, without altering its overall structure.

  • To ease review, I've commented on the most important parts of this PR.
  • I'm not supporting functions (deprecated months ago by OpenAI in favor of tools).

How did you test it?

Updated tests, new tests

Checklist

@anakin87 anakin87 changed the title Openaichatgenerator with tools feat: support for tools in OpenAIChatGenerator Aug 12, 2024
@coveralls
Copy link

coveralls commented Aug 12, 2024

Pull Request Test Coverage Report for Build 10451155974

Details

  • 432 of 435 (99.31%) changed or added relevant lines in 3 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+1.2%) to 92.948%

Changes Missing Coverage Covered Lines Changed/Added Lines %
haystack_experimental/components/generators/chat/openai.py 129 132 97.73%
Totals Coverage Status
Change from base Build 10450951420: 1.2%
Covered Lines: 2702
Relevant Lines: 2907

💛 - Coveralls

Comment on lines -193 to -194
if not text and not tool_calls:
raise ValueError("At least one of `text` or `tool_calls` must be provided.")
Copy link
Member Author

Choose a reason for hiding this comment

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

Empty messages, although uncommon, should be allowed.

Copy link
Contributor

Choose a reason for hiding this comment

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

Under what circumstances can we expect empty messages from the LLM? And what makes them non-exceptional?

Copy link
Member Author

Choose a reason for hiding this comment

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

Comment on lines 362 to 368
try:
arguments = json.loads(arguments_str)
except json.JSONDecodeError:
arguments = {"_malformed_json": arguments_str}
logger.warning(
"OpenAI returned a malformed JSON string. "
"You can find it under the `_malformed_json` key of `ToolCall.arguments`."
Copy link
Member Author

Choose a reason for hiding this comment

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

Important
OpenAI returns a JSON string that can be malformed. Other providers (Anthropic, Ollama) return a dict.

The current solution is just a placeholder.

How do we want to handle this?

  • create an InvalidToolCall content type in the ChatMessage
  • add a valid (or similar) bool attribute to ToolCall

Copy link
Contributor

Choose a reason for hiding this comment

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

If it's malformed, then we just bail and (re-)raise with some extra context - I don't see how we can handle such exceptions.

Copy link
Member Author

Choose a reason for hiding this comment

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

We can start with an approach similar to what you suggest, then see if users ask to return the invalid JSON string...

Since there may be more than one tool call and some of them may be valid, I would prefer to skip the invalid ones and emit a clear warning (instead of raising an exception). I will try this approach.

@anakin87 anakin87 marked this pull request as ready for review August 12, 2024 15:56
@anakin87 anakin87 requested a review from a team as a code owner August 12, 2024 15:56
@anakin87 anakin87 requested review from davidsbatista and shadeMe and removed request for a team August 12, 2024 15:56
haystack_experimental/components/generators/chat/openai.py Outdated Show resolved Hide resolved
haystack_experimental/components/generators/chat/openai.py Outdated Show resolved Hide resolved
haystack_experimental/components/generators/chat/openai.py Outdated Show resolved Hide resolved
haystack_experimental/components/generators/chat/openai.py Outdated Show resolved Hide resolved
haystack_experimental/components/generators/chat/openai.py Outdated Show resolved Hide resolved
haystack_experimental/components/generators/chat/openai.py Outdated Show resolved Hide resolved
haystack_experimental/components/generators/chat/openai.py Outdated Show resolved Hide resolved
Comment on lines 362 to 368
try:
arguments = json.loads(arguments_str)
except json.JSONDecodeError:
arguments = {"_malformed_json": arguments_str}
logger.warning(
"OpenAI returned a malformed JSON string. "
"You can find it under the `_malformed_json` key of `ToolCall.arguments`."
Copy link
Contributor

Choose a reason for hiding this comment

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

If it's malformed, then we just bail and (re-)raise with some extra context - I don't see how we can handle such exceptions.

haystack_experimental/components/generators/chat/openai.py Outdated Show resolved Hide resolved
Comment on lines -193 to -194
if not text and not tool_calls:
raise ValueError("At least one of `text` or `tool_calls` must be provided.")
Copy link
Contributor

Choose a reason for hiding this comment

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

Under what circumstances can we expect empty messages from the LLM? And what makes them non-exceptional?

@anakin87 anakin87 marked this pull request as draft August 13, 2024 14:40
@anakin87 anakin87 requested a review from shadeMe August 14, 2024 16:04
@anakin87 anakin87 marked this pull request as ready for review August 14, 2024 16:04
@anakin87 anakin87 requested a review from shadeMe August 19, 2024 09:54
@shadeMe shadeMe merged commit ab3fe0c into main Aug 19, 2024
5 checks passed
@shadeMe shadeMe deleted the openaichatgenerator-with-tools branch August 19, 2024 12:16
@cjayb
Copy link

cjayb commented Oct 7, 2024

@anakin87 great feature! I made a quick version of this for AzureOpenAIChatGenerator. I understand that you generally don't want PRs against experimental, so therefore: are you interested in receiving this one?

cjayb#1

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

Successfully merging this pull request may close these issues.

🧪 Tools: support for tools in OpenAIChatGenerator
4 participants