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

community: Add "headers" parameter support to OpenAPI tools #29007

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

unkindypie
Copy link

  • Description: Allow OpenAPI agent to use request headers.
    It adds the following:
    • request tool now supports passing of the headers parameter. Headers are optional not to introduce breaking changes. Request headers are merged with headers passed to RequestWrapper class overwriting duplicate names.
    • OpenAPI planner prompts were updated to support new parameter.
    • reduce_openapi_spec now has new optional parameter "remove_optional", which allows to control whether optional parameters will be added to reduced API specification. This is required for the API I am integrating langchain with and often needed in real world scenarios.
    • new functionality for requests tool covered with tests
    • docstrings updated

Copy link

vercel bot commented Jan 3, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
langchain ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 3, 2025 3:41pm

@dosubot dosubot bot added size:XL This PR changes 500-999 lines, ignoring generated files. community Related to langchain-community Ɑ: agent Related to agents module labels Jan 3, 2025
- **Description:** This PR is intended to allow OpenAPI agent to use
  request headers.
  It adds the following:
   * request tool now supports passing of the headers parameter. Headers are
   optional not to introduce breaking changes. Request headers are merged with
   headers passed to RequestWrapper class overwriting duplicate names.
   * OpenAPI planner prompts were updated to support new parameter.
   * `reduce_openapi_spec` now has new optional parameter
     "remove_optional", which allows to control whether optional
     parameters will be added to reduced API specification. This is
     required for the API I am integrating langchain with and often
     needed in real world cases.
@unkindypie
Copy link
Author

Example repo which uses new features, although client code won't differ from the current version except for new parameter in the reduce_openapi_spec function https://github.com/unkindypie/langchain-playground/blob/master/openapi_agent.py

Copy link
Collaborator

@ccurme ccurme 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 this. I'm wary of the current implementation because all LLMs currently using the requests tools will need to begin populating (empty) headers in their tool calls.

Is it desirable for the LLM to generate headers? Curious about the context. If they can be programmatically generated outside the LLM, we have a guide on passing those to tools here.

@@ -23,7 +23,9 @@ class ReducedOpenAPISpec:
endpoints: List[Tuple[str, str, dict]]


def reduce_openapi_spec(spec: dict, dereference: bool = True) -> ReducedOpenAPISpec:
def reduce_openapi_spec(
spec: dict, dereference: bool = True, remove_optional: bool = False
Copy link
Collaborator

Choose a reason for hiding this comment

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

Does defaulting to False change behavior for existing users?

Currently we are effectively removing optional by checking parameter.get("required"), right?

Copy link
Author

@unkindypie unkindypie Jan 6, 2025

Choose a reason for hiding this comment

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

No, False will behave as it was working before my changes and True will keep the optional params which is really useful in my case

Currently we are effectively removing optional by checking parameter.get("required"), right?

Right, we just remove anything which is not required at the moment

@ccurme ccurme self-assigned this Jan 6, 2025
@unkindypie
Copy link
Author

Thanks for this. I'm wary of the current implementation because all LLMs currently using the requests tools will need to begin populating (empty) headers in their tool calls.

Yes, but in theory nothing should change as the description of the tool is updated. I've tried on my end and it worked on GPT 4.0

Is it desirable for the LLM to generate headers? Curious about the context. If they can be programmatically generated outside the LLM, we have a guide on passing those to tools here.

It's desirable in my case because headers must be passed only in a certain case based on description of a header which an LLM should determine, here is an example from our Swagger, but thank you for sharing.
image
After my changes to the package works quite cool, LLM reads from what endpoint it needs to get ID based on a description from an optional header, adds it to the path and makes an additional request

@unkindypie
Copy link
Author

unkindypie commented Jan 6, 2025

@ccurme Also regarding filling out empty headers problem, for this purpose I've made it default to {}, so there must be no changes required for the existing code and also LLMs can just omit and don't pass it, and the code will still work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Ɑ: agent Related to agents module community Related to langchain-community size:XL This PR changes 500-999 lines, ignoring generated files.
Projects
Status: Needs support
Development

Successfully merging this pull request may close these issues.

2 participants