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

Sandbox doesn't support 3rd party python module #4612

Closed
4 tasks done
zhangever opened this issue May 23, 2024 · 5 comments
Closed
4 tasks done

Sandbox doesn't support 3rd party python module #4612

zhangever opened this issue May 23, 2024 · 5 comments
Labels
🐞 bug Something isn't working

Comments

@zhangever
Copy link
Contributor

Self Checks

  • This is only for bug report, if you would like to ask a question, please head to Discussions.
  • I have searched for existing issues search for existing issues, including closed ones.
  • I confirm that I am using English to submit this report (我已阅读并同意 Language Policy).
  • Please do not modify this template :) and fill in all the required fields.

Dify version

0.6.8

Cloud or Self Hosted

Self Hosted (Docker)

Steps to reproduce

  1. add modul to docker/volumes/sandbox/dependencies/python-requirements.txt:
    tiktoken~=0.6.0
  2. restart sandbox container, and verify that tiktoken is installed:
# pip list | grep tiktoken                                                                                                      
tiktoken           0.6.0
  1. add a new codeNode to a workflow, with the following script:
import tiktoken
def main(arg1: str) -> dict:
    // import tiktoken
    encoding = tiktoken.get_encoding("cl100k_base")

    return {
        "result": len(encoding.encode(arg1))
    }
  1. Run the node, it failed:
    image

✔️ Expected Behavior

the code should be run and returns the length of the input text.

❌ Actual Behavior

image

Copy link

dosubot bot commented May 23, 2024

To resolve the issue where the tiktoken module is not recognized in your self-hosted Dify version 0.6.8 running on Docker, follow these steps:

  1. Update requirements.txt:
    Ensure that the tiktoken module is listed in the requirements.txt file with the correct version. Update it to:

    tiktoken~=0.7.0
  2. Rebuild Docker Image:
    After updating the requirements.txt file, rebuild your Docker image to ensure all dependencies are correctly installed. Run the following commands:

    docker-compose down
    docker-compose build
    docker-compose up -d
  3. Verify Installation:
    Verify that the tiktoken module is installed in the sandbox environment:

    docker exec -it <sandbox_container_id> pip list | grep tiktoken

    Replace <sandbox_container_id> with the actual container ID of your sandbox environment.

  4. Check Environment Variables:
    Ensure that the CODE_EXECUTION_ENDPOINT and CODE_EXECUTION_API_KEY environment variables are correctly set, as these are crucial for the code execution service to function properly.

  5. Debugging and Logging:
    If the issue persists, use print statements and exception handling in your script to debug and log any errors:

    import tiktoken
    def main(arg1: str) -> dict:
        try:
            encoding = tiktoken.get_encoding("cl100k_base")
            return {
                "result": len(encoding.encode(arg1))
            }
        except ModuleNotFoundError as e:
            print("ModuleNotFoundError:", str(e))
            return {"error": "ModuleNotFoundError"}
        except Exception as e:
            print("Error:", str(e))
            return {"error": str(e)}

By following these steps, you should be able to resolve the issue and ensure that the tiktoken module is recognized and the script runs successfully.

Details

Sources


About Dosu
This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

To continue the conversation, mention @dosu.

@dosubot dosubot bot added the 🐞 bug Something isn't working label May 23, 2024
@zhangever
Copy link
Contributor Author

To resolve the issue where the tiktoken module is not recognized in your self-hosted Dify version 0.6.8 running on Docker, follow these steps:

  1. Update requirements.txt:
    Ensure that the tiktoken module is listed in the requirements.txt file with the correct version. Update it to:

    tiktoken~=0.7.0
    
  2. Rebuild Docker Image:
    After updating the requirements.txt file, rebuild your Docker image to ensure all dependencies are correctly installed. Run the following commands:

    docker-compose down
    docker-compose build
    docker-compose up -d
  3. Verify Installation:
    Verify that the tiktoken module is installed in the sandbox environment:

    docker exec -it <sandbox_container_id> pip list | grep tiktoken

    Replace <sandbox_container_id> with the actual container ID of your sandbox environment.

  4. Check Environment Variables:
    Ensure that the CODE_EXECUTION_ENDPOINT and CODE_EXECUTION_API_KEY environment variables are correctly set, as these are crucial for the code execution service to function properly.

  5. Debugging and Logging:
    If the issue persists, use print statements and exception handling in your script to debug and log any errors:

    import tiktoken
    def main(arg1: str) -> dict:
        try:
            encoding = tiktoken.get_encoding("cl100k_base")
            return {
                "result": len(encoding.encode(arg1))
            }
        except ModuleNotFoundError as e:
            print("ModuleNotFoundError:", str(e))
            return {"error": "ModuleNotFoundError"}
        except Exception as e:
            print("Error:", str(e))
            return {"error": str(e)}

By following these steps, you should be able to resolve the issue and ensure that the tiktoken module is recognized and the script runs successfully.

Details

Sources

About Dosu This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

To continue the conversation, mention @dosu.

I've update the tiktoken version to 0.7.0, but it still reports the same error.

Copy link

dosubot bot commented May 23, 2024

To resolve the ModuleNotFoundError for the tiktoken module in your self-hosted Dify instance's sandbox environment, follow these steps:

  1. Ensure tiktoken is in the requirements.txt file:
    Make sure that the tiktoken module is listed in your requirements.txt file:

    tiktoken~=0.7.0
  2. Modify the execute_code method to include tiktoken as a dependency:
    When executing the code, specify the dependencies explicitly:

    from core.helper.code_executor.entities import CodeDependency
    
    dependencies = [CodeDependency(name='tiktoken')]
    
    response = CodeExecutor.execute_code(
        language='python3',
        preload='',
        code='import tiktoken\nprint(tiktoken.__version__)',
        dependencies=dependencies
    )
    print(response)
  3. Restart the sandbox service using Docker:
    Ensure the sandbox environment is properly set up by restarting the sandbox service:

    docker-compose -f docker-compose.middleware.yaml up -d
  4. Verify the installation in the sandbox environment:
    After restarting, verify that the tiktoken module is installed:

    import tiktoken
    print(tiktoken.__version__)
  5. Update your workflow code node:
    Ensure your workflow code node includes the tiktoken module:

    import tiktoken
    
    def main(arg1: str) -> dict:
        encoding = tiktoken.get_encoding("cl100k_base")
        return {
            "result": len(encoding.encode(arg1))
        }

By following these steps, you should be able to resolve the issue and ensure that the tiktoken module is recognized and the script runs successfully. If the issue persists, please provide more details about the error message and the configuration of your sandbox environment.

Details

Sources


About Dosu
This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

To continue the conversation, mention @dosu.

@crazywoola
Copy link
Member

Duplicate see this thread #3580

@zhaopengme
Copy link

@zhangever 解决了吗

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐞 bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants