-
Notifications
You must be signed in to change notification settings - Fork 2k
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 HuggingFaceAPIChatGenerator
#8661
Conversation
Pull Request Test Coverage Report for Build 12413858648Details
💛 - Coveralls |
@@ -216,6 +216,19 @@ def _remove_title_from_schema(schema: Dict[str, Any]): | |||
del property_schema[key] | |||
|
|||
|
|||
def _check_duplicate_tool_names(tools: List[Tool]) -> None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is a common check we do, so I moved it to the Tool
module
@@ -270,6 +270,44 @@ def check_generation_params(kwargs: Optional[Dict[str, Any]], additional_accepte | |||
) | |||
|
|||
|
|||
def convert_message_to_hf_format(message: ChatMessage) -> Dict[str, Any]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should use this function in the HF Local Chat Generator too (I recently discovered that it does not work properly without the conversion), so I moved it to utils.
(I will open issue + PR to update the HF Local Chat Generator.)
@@ -14,7 +14,7 @@ | |||
from haystack.utils.hf import HFEmbeddingAPIType, HFModelType, check_valid_model | |||
from haystack.utils.url_validation import is_valid_http_url | |||
|
|||
with LazyImport(message="Run 'pip install \"huggingface_hub>=0.23.0\"'") as huggingface_hub_import: | |||
with LazyImport(message="Run 'pip install \"huggingface_hub>=0.27.0\"'") as huggingface_hub_import: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is only a suggestion for users.
The ChatCompletionInputTool
type was introduced recently, so it is better to install the latest version.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, I left a a few optional comments
@@ -159,6 +163,11 @@ def __init__( # pylint: disable=too-many-positional-arguments | |||
msg = f"Unknown api_type {api_type}" | |||
raise ValueError(msg) | |||
|
|||
if tools: | |||
if streaming_callback is not None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we do this across the board for all CG - just something to write down and not forget
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No. Most of Generators support Tools + Streaming, I think.
Theoretically also HF could support this but it is highly undocumented and does not behave consistently.
See deepset-ai/haystack-experimental#120 (comment)
"finish_reason": choice.finish_reason, | ||
"index": choice.index, | ||
"usage": { | ||
"prompt_tokens": api_chat_output.usage.prompt_tokens, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ugh, I've been burned b4 on these usage structures assuming they are always there. Please check HF pydantic classes to make sure they are not optional.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have now taken a more cautious approach. Thank you!
haystack/dataclasses/tool.py
Outdated
@@ -216,6 +216,19 @@ def _remove_title_from_schema(schema: Dict[str, Any]): | |||
del property_schema[key] | |||
|
|||
|
|||
def _check_duplicate_tool_names(tools: List[Tool]) -> None: | |||
""" | |||
Check for duplicate tool names. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and raises ValueError if they are found.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed!
Related Issues
Proposed Changes:
HuggingFaceAPIChatGenerator
from experimentalHow did you test it?
CI, several new tests
Checklist
fix:
,feat:
,build:
,chore:
,ci:
,docs:
,style:
,refactor:
,perf:
,test:
and added!
in case the PR includes breaking changes.