langgenius / dify

Dify is an open-source LLM app development platform. Dify's intuitive interface combines AI workflow, RAG pipeline, agent capabilities, model management, observability features and more, letting you quickly go from prototype to production.
https://dify.ai
Other
35.76k stars 4.82k forks source link

"custom tool" cannot import qdrant OpenAPI #5246

Open breakstring opened 2 weeks ago

breakstring commented 2 weeks ago

Self Checks

Dify version

0.6.11

Cloud or Self Hosted

Self Hosted (Docker)

Steps to reproduce

  1. In the top navigation, select "Tools" -> "Custom" -> "Create Custom Tool".

  2. Click the "+ Import from URL" button, enter the API definition URL for qdrant: https://raw.githubusercontent.com/qdrant/qdrant/master/docs/redoc/master/openapi.json, and click "OK".

  3. An error message is displayed on the interface.

    image
  4. Copying the JSON content into the text box below also results in an error message.

    image
  5. Validating the qdrant OpenAPI URL using the officially recommended Swagger tool at https://petstore.swagger.io/ successfully parses it.

    image

✔️ Expected Behavior

The Qdrant OpenAPI definition should be correctly imported into the tool. If there are any incompatible endpoints, it is recommended to highlight the specific endpoint information in the UI.

❌ Actual Behavior

It only pops up an error message, and the user doesn't know how to fix it.

dosubot[bot] commented 2 weeks ago

To resolve the issue of importing the Qdrant OpenAPI definition into the Dify tool version 0.6.11, you need to ensure that the schema is valid and correctly parsed. The ApiBasedToolSchemaParser class provides methods to handle schema parsing and validation.

Here is a step-by-step approach to troubleshoot and resolve the issue:

  1. Validate the Schema: Ensure that the OpenAPI schema is valid. You can use the parse_openapi_yaml_to_tool_bundle method to validate the schema.

  2. Check for Schema Type: Ensure that the schema type is supported. The auto_parse_to_tool_bundle method checks if the schema type is valid.

  3. Parse the Schema: Use the parse_openapi_to_tool_bundle method to parse the schema into tool bundles.

  4. Handle Errors: Catch and handle any exceptions that occur during the parsing process.

Here is a code snippet that demonstrates these steps:

from core.tools.utils.parser import ApiBasedToolSchemaParser
from core.tools.entities.tool_entities import ApiProviderSchemaType
from core.tools.errors import ToolApiSchemaError, ToolNotSupportedError, ToolProviderNotFoundError

def resolve_schema_validation_error(schema: str):
    try:
        # Step 1: Validate and parse the schema
        extra_info = {}
        warning = {}
        tool_bundles, schema_type = ApiBasedToolSchemaParser.auto_parse_to_tool_bundle(schema, extra_info=extra_info, warning=warning)

        # Step 2: Check for schema type (assuming 'openapi' for this example)
        if schema_type != ApiProviderSchemaType.OPENAPI.value:
            raise ValueError(f'Invalid schema type {schema_type}')

        # Step 3: Handle the parsed tool bundles (e.g., create a provider)
        # This is an example, adjust according to your actual implementation
        provider_name = "Qdrant"
        tenant_id = "your_tenant_id"
        user_id = "your_user_id"
        icon = {}  # Your icon data
        credentials = {"auth_type": "none"}  # Your credentials
        privacy_policy = "Your privacy policy"
        custom_disclaimer = "Your custom disclaimer"
        labels = ["label1", "label2"]

        result = ApiToolManageService.create_api_tool_provider(
            user_id=user_id,
            tenant_id=tenant_id,
            provider_name=provider_name,
            icon=icon,
            credentials=credentials,
            schema_type=schema_type,
            schema=schema,
            privacy_policy=privacy_policy,
            custom_disclaimer=custom_disclaimer,
            labels=labels
        )

        return result
    except (ToolApiSchemaError, ToolNotSupportedError, ToolProviderNotFoundError) as e:
        return {'error': str(e)}

# Example usage
schema = "your_openapi_schema_here"
result = resolve_schema_validation_error(schema)
print(result)

This code will help you validate and parse the OpenAPI schema, and handle any errors that may occur during the process. If the schema is valid and correctly parsed, it will proceed to create the API tool provider [1][2].

To continue talking to Dosu, mention @dosu.