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

fix: Update OpenAPIServiceConnector to new ChatMessage #8817

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

vblagoje
Copy link
Member

@vblagoje vblagoje commented Feb 5, 2025

Why:

Update OpenAPIServiceConnector to handle new ChatMessage format

What:

  • Patched the request method in OpenAPI to handle raw responses more effectively.
  • Reworked the processing of chat messages to extract tool calls and handle them for OpenAPI function invocations.

How can it be used:

The changes affect how OpenAPIServiceConnector handles new ChatMessage format involving build in tool_calls property.

Example usage:

connector.run(messages=[message], service_openapi_spec=a_spec_as_a_dict_object)

Where message should now contain tool_calls property.

How did you test it:

Tests verify new behavior of message processing with checks for valid tool calls. Patched OpenAPI methods are mocked to assess correct invocation and response handling. Integration tests are skipped unless specific API keys are set.

Notes for the reviewer:

Pay special attention to the new patch applied to the request method of OpenAPI, specifically concerning raw response handling. Ensure the test coverage is sufficient for edge cases in tool call processing and response verification.

@github-actions github-actions bot added topic:tests type:documentation Improvements on the docs labels Feb 5, 2025
@coveralls
Copy link
Collaborator

coveralls commented Feb 5, 2025

Pull Request Test Coverage Report for Build 13198769367

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • 83 unchanged lines in 7 files lost coverage.
  • Overall coverage increased (+0.9%) to 92.178%

Files with Coverage Reduction New Missed Lines %
components/generators/hugging_face_api.py 1 96.3%
core/pipeline/pipeline.py 1 96.43%
components/embedders/sentence_transformers_document_embedder.py 2 96.77%
components/embedders/sentence_transformers_text_embedder.py 2 96.3%
components/generators/chat/hugging_face_api.py 3 96.46%
core/pipeline/base.py 19 94.59%
components/connectors/openapi_service.py 55 62.59%
Totals Coverage Status
Change from base Build 13141979385: 0.9%
Covered Lines: 9027
Relevant Lines: 9793

💛 - Coveralls

@vblagoje
Copy link
Member Author

vblagoje commented Feb 5, 2025

@richardpaulhudson would you please give this branch a spin in your project by directly installing it via pip install git+https://github.com/deepset-ai/haystack.git@open_api_chat_message

@vblagoje vblagoje marked this pull request as ready for review February 5, 2025 16:34
@vblagoje vblagoje requested review from a team as code owners February 5, 2025 16:34
@vblagoje vblagoje requested review from dfokina and mpangrazzi and removed request for a team February 5, 2025 16:35
@vblagoje
Copy link
Member Author

vblagoje commented Feb 6, 2025

@mpangrazzi The reason you'll see this ugly monkey patch is better described at Dorthu/openapi3#124

For better integration with LLMs handling REST endpoint responses I believe that response validation should be optional. Currently the openapi3 implementation doesn’t offer a way to disable model validation, which can sometimes lead to unintended issues and poor overall UX. In response to user feedback we'll soon deprecate OpenAPIServiceConnector for OpenAPIConnector and OpenAPITool (see internal https://github.com/deepset-ai/haystack-private/issues/104 for details).

@vblagoje
Copy link
Member Author

vblagoje commented Feb 6, 2025

@dfokina I don't think there are any doc changes required here as we are not changing any public API signatures - we are just updating this component to work with the latest ChatMessage

@anakin87
Copy link
Member

anakin87 commented Feb 6, 2025

Just a (slightly unrelated) curiosity: OpenAPIConnector vs OpenAPITool
How do envision them? What's the difference?

@vblagoje
Copy link
Member Author

vblagoje commented Feb 6, 2025

Of course @anakin87 , shared this during brainstorming with Luke and Julian so here is the copy for the record:

Difference between OpenAPIConnector and OpenAPITool

  • OpenAPIConnector requires you to generate and pass parameters manually. It then invokes the remote REST endpoint.
  • OpenAPITool generates invocation parameters automatically from your chat message and directly invokes the remote REST endpoint.

OpenAPIConnector

Pros:

  • Easy to set up and explicit
  • No hallucination of invocation parameters
  • More deterministic behavior

Cons:

  • Requires manual preparation of REST invocation parameters (coming from other components in the pipeline or run pipeline parameters)
  • You need to structure the parameters into a JSON payload yourself

OpenAPITool

Pros:

  • Quick and easy to set up
  • Easy to configure and use
  • Just another tool in our new tooling architecture

Cons:

  • The LLM might not always generate the correct parameters (rarely due to hallucination, but more likely due to message misalignment with required invocation parameters)
  • Less deterministic when high precision is required

Copy link
Contributor

@mpangrazzi mpangrazzi 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! I only feel that some edge cases might be not covered by tests. For example, when request body is missing or when content type is unsupported. Do you think it may worth covering those?

@vblagoje
Copy link
Member Author

vblagoje commented Feb 6, 2025

Looks good! I only feel that some edge cases might be not covered by tests. For example, when request body is missing or when content type is unsupported. Do you think it may worth covering those?

Yes, good point. Let me see what I can do 🙏

Copy link
Contributor

@dfokina dfokina left a comment

Choose a reason for hiding this comment

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

While I was looking, found a few improvements to the docstrings

haystack/components/connectors/openapi_service.py Outdated Show resolved Hide resolved
haystack/components/connectors/openapi_service.py Outdated Show resolved Hide resolved
haystack/components/connectors/openapi_service.py Outdated Show resolved Hide resolved
haystack/components/connectors/openapi_service.py Outdated Show resolved Hide resolved
haystack/components/connectors/openapi_service.py Outdated Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic:tests type:documentation Improvements on the docs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

OpenAPIServiceConnector does not support the v2.9.x ChatMessage structure
5 participants