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 OpenAPI support, OpenAPITool component #8

Merged
merged 42 commits into from
Jun 25, 2024
Merged

feat: Add OpenAPI support, OpenAPITool component #8

merged 42 commits into from
Jun 25, 2024

Conversation

vblagoje
Copy link
Member

@vblagoje vblagoje commented May 28, 2024

This PR introduces the OpenAPITool, a powerful new component that enables calling OpenAPI service endpoints using payloads generated by chat generators from human instructions.

Usage Examples

Example 1: Searching with Serper

The following example demonstrates how to use the OpenAPITool with the SerperDev API to perform a search:

from haystack_experimental.components.tools.openapi import OpenAPITool, LLMProvider
from haystack.dataclasses import ChatMessage
from haystack.utils import Secret

tool = OpenAPITool(generator_api=LLMProvider.OPENAI,   
                                   spec="https://bit.ly/serper_dev_spec_yaml",
                                   credentials=Secret.from_token("your_serper_key"))

results = tool.run(messages=[ChatMessage.from_user("Do a search for 'Who was Nikola Tesla?'")])
print(results)

Example 2: Scraping with Firecrawl

The following example demonstrates how to use the OpenAPITool with the Firecrawl API to scrape a webpage:

from haystack_experimental.components.tools.openapi import OpenAPITool, LLMProvider
from haystack.dataclasses import ChatMessage
from haystack.utils import Secret

tool = OpenAPITool(generator_api=LLMProvider.OPENAI, 
                   spec="https://raw.githubusercontent.com/mendableai/firecrawl/main/apps/api/openapi.json",
                   credentials=Secret.from_env_var("FIRECRAWL_API_KEY"))

results = tool.run(messages=[ChatMessage.from_user("Scrape URL: https://news.ycombinator.com/")])
print(results)

Example 3: Searching with Firecrawl using Anthropic Claude

The following example demonstrates how to use the OpenAPITool with the Firecrawl API to search and get back scraped pages

from haystack_experimental.components.tools.openapi import OpenAPITool, LLMProvider
from haystack.dataclasses import ChatMessage
from haystack.utils import Secret

tool = OpenAPITool(generator_api=LLMProvider.ANTHROPIC, generator_api_params={"model":"claude-3-opus-20240229"},
                   spec="https://raw.githubusercontent.com/mendableai/firecrawl/main/apps/api/openapi.json",
                   credentials=Secret.from_env_var("FIRECRAWL_API_KEY"))

results = tool.run(messages=[ChatMessage.from_user("Scrape https://haystack.deepset.ai/blog/rag-evaluation-with-prometheus-2")])
print(results)

Demo

Here is a demo notebook featuring RAG+SerperDev, here is notebook demoing use of Anthropic models in Firecrawl WebQa RAG and finally a Cohere notebook variant of the same

@vblagoje vblagoje requested a review from a team as a code owner May 28, 2024 11:26
@vblagoje vblagoje requested review from masci and removed request for a team May 28, 2024 11:26
@coveralls
Copy link

coveralls commented May 28, 2024

Pull Request Test Coverage Report for Build 9649571985

Details

  • 815 of 1086 (75.05%) changed or added relevant lines in 23 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-13.8%) to 84.481%

Changes Missing Coverage Covered Lines Changed/Added Lines %
haystack_experimental/util/auth.py 7 8 87.5%
test/components/tools/openapi/conftest.py 33 39 84.62%
haystack_experimental/components/tools/openapi/_payload_extraction.py 32 43 74.42%
haystack_experimental/components/tools/openapi/_schema_conversion.py 95 106 89.62%
test/components/tools/openapi/test_openapi_client_live.py 13 24 54.17%
haystack_experimental/components/tools/openapi/types.py 82 98 83.67%
test/components/tools/openapi/test_openapi_client_live_anthropic.py 13 29 44.83%
test/components/tools/openapi/test_openapi_client_live_cohere.py 14 30 46.67%
test/components/tools/openapi/test_openapi_client_live_openai.py 18 49 36.73%
test/components/tools/openapi/test_openapi_client_auth.py 69 102 67.65%
Totals Coverage Status
Change from base Build 9646483893: -13.8%
Covered Lines: 1546
Relevant Lines: 1830

💛 - Coveralls

@vblagoje
Copy link
Member Author

This is the first part of the task @shadeMe as we agreed, new tool impl comes next.
@masci the specific filesystem layout was a consensus between two of us.

@vblagoje vblagoje requested a review from shadeMe May 28, 2024 11:58
@vblagoje vblagoje changed the title feat: Add initial openapi support, reimplement OpenAPIServiceToFunctions and OpenAPIServiceConnector feat: Add initial OpenAPI support, reimplement OpenAPIServiceToFunctions and OpenAPIServiceConnector May 28, 2024
@vblagoje
Copy link
Member Author

I ran all the integration live tests as well locally; for those we need to setup integration runs and api keys as well

@vblagoje vblagoje force-pushed the openapi branch 2 times, most recently from 0765b5c to 83e8f88 Compare June 5, 2024 13:30
@vblagoje vblagoje changed the title feat: Add initial OpenAPI support, reimplement OpenAPIServiceToFunctions and OpenAPIServiceConnector feat: Add OpenAPI support, OpenAPITool component Jun 5, 2024
@vblagoje
Copy link
Member Author

vblagoje commented Jun 6, 2024

We currently support OpenAI function calling compatible models (OpenAI, fireworks, anyscale) etc
After integration of deepset-ai/haystack-core-integrations#790 users will be able to use Cohere and Anthropic function calling models.
More function calling models support should come soon, including open source models from HF

@TuanaCelik
Copy link
Member

TuanaCelik commented Jun 6, 2024

My first question looking at this at high level: How would I provide other haystack pipelines as tools with this component?

@vblagoje
Copy link
Member Author

vblagoje commented Jun 6, 2024

My first question looking at this at high level: How would I provide other haystack pipelines as tools with this component?

@TuanaCelik we'll have another component for that use case. This one is specific for openapi integration, hence the package name and the component name. We'll add another component in tools.pipeline or smth similar for that use case.

For this use case see https://github.com/vblagoje/notebooks/blob/main/haystack2x-demos/haystack_rag_serperdev_demo_new.ipynb

@vblagoje
Copy link
Member Author

vblagoje commented Jun 6, 2024

@shadeMe I addressed 98% of the use cases as you suggested. I deviated from your advice in these situations:

  • I did not lift up OpenAPISpecification to a first class citizen as it complicates use of OpenAPITool and its DX
  • I left from OpenAPISpecification.from_file (needed for testing) and I removed from_dict instead of it :-)
  • I left the name credentials because although we mostly use tokens for auth, you'll see that a request for at least u/p will show up soon, and then we'll need another parameter for it breaking the OpenAPITool init
  • some unintended omission might have slipped as well

@masci masci removed their request for review June 10, 2024 06:09
@vblagoje
Copy link
Member Author

@shadeMe I added back in http bearer token auth, as this one is fairly common type of auth in addition to apiKey. Even our own firecrawl example uses it:

from haystack_experimental.components.tools.openapi import OpenAPITool, LLMProvider
from haystack.dataclasses import ChatMessage

tool = OpenAPITool(generator_api=LLMProvider.ANTHROPIC, generator_api_params={"model":"claude-3-opus-20240229"},
                   tool_spec="https://raw.githubusercontent.com/mendableai/firecrawl/main/apps/api/openapi.json",
                   tool_credentials="your_firecrawl_key")

results = tool.run(messages=[ChatMessage.from_user("Search for 'What was the twitter debate between Elon Musk and Yann LeCun all about?'")])
print(results)

For reference OpenAPI Specification 3.x supports the following four types of authentication:

  • HTTP authentication schemes (e.g., Basic, Bearer).
  • API keys (sent as a query parameter, header, or cookie).
  • OAuth2 (for various OAuth2 flows such as implicit, password, client credentials, and authorization code).
  • OpenID Connect Discovery (for federated identity and single sign-on).

We support only first two excluding basic http (that's user password http auth)

haystack_experimental/components/tools/openapi/_openapi.py Outdated Show resolved Hide resolved
haystack_experimental/components/tools/openapi/_openapi.py Outdated Show resolved Hide resolved
haystack_experimental/components/tools/openapi/_openapi.py Outdated Show resolved Hide resolved
haystack_experimental/components/tools/openapi/_openapi.py Outdated Show resolved Hide resolved
haystack_experimental/components/tools/openapi/_openapi.py Outdated Show resolved Hide resolved
haystack_experimental/components/tools/openapi/_openapi.py Outdated Show resolved Hide resolved
haystack_experimental/components/tools/openapi/types.py Outdated Show resolved Hide resolved
Copy link
Contributor

@shadeMe shadeMe left a comment

Choose a reason for hiding this comment

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

FYI - There are a few more unresolved comments from the previous review.

@julian-risch
Copy link
Member

@vblagoje Please also add the new feature to the catalog in the readme: https://github.com/deepset-ai/haystack-experimental?tab=readme-ov-file#experiments-catalog

@TuanaCelik
Copy link
Member

@vblagoje Please also add the new feature to the catalog in the readme: https://github.com/deepset-ai/haystack-experimental?tab=readme-ov-file#experiments-catalog

This made me realize that I didn't add an entry for the OpenAIFunctionCaller - I'll make a separate PR for it?

Copy link
Contributor

@shadeMe shadeMe left a comment

Choose a reason for hiding this comment

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

We just need to add the component to the readMe.

@vblagoje vblagoje requested a review from a team as a code owner June 24, 2024 17:09
@vblagoje vblagoje requested review from dfokina and removed request for a team June 24, 2024 17:09
@vblagoje
Copy link
Member Author

@shadeMe Updated README to include the component 💪

@shadeMe shadeMe merged commit 42eac81 into main Jun 25, 2024
5 checks passed
@shadeMe shadeMe deleted the openapi branch June 25, 2024 08:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants