Fragments by E2B Documentation

repository·main·Indexed 27 days ago

https://github.com/e2b-dev/fragments

An open-source platform for building AI-powered applications with interactive code execution using the E2B SDK to run code in secure sandboxes. Includes guides on installing the platform, configuring environment variables for various LLM providers, adding custom sandbox templates (personas), and managing LLM clients and API error handling.

Tokens
2.8K
Snippets
6
Records
20
Agent score
92%

What's inside Fragments by E2B

  1. Install and Setup Fragments by E2B

    main

    To run Fragments locally, follow these steps:

    1. Clone the repository:
      git clone https://github.com/e2b-dev/fragments.git
      cd fragments
    2. Install dependencies:
      npm i
    3. Configure environment variables: Create a .env.local file in the root directory and provide your API keys (see Environment Variables Reference).
    4. Run the development server:
      npm run dev
      Or build the application:
      npm run build
    git clone https://github.com/e2b-dev/fragments.git
    cd fragments
    npm i
    npm run dev
  2. Add Custom Personas (Sandbox Templates)

    main

    You can extend Fragments by adding new sandbox templates (personas) using the E2B CLI.

    1. Initialize Template: Create a new folder under sandbox-templates/ and run e2b template init inside it to generate an e2b.Dockerfile.
    2. Configure Dockerfile: Define your environment (e.g., Python, Node) and install dependencies. Example for Streamlit:
      FROM python:3.19-slim
      RUN pip3 install --no-cache-dir streamlit pandas numpy matplotlib requests seaborn plotly
      WORKDIR /home/user
      COPY . /home/user
    3. Set Start Command: In e2b.toml, specify the command to run your app:
      start_cmd = "cd /home/user && streamlit run app.py"
    4. Build and Deploy: Run e2b template build --name <template-name>.
    5. Register in UI: Add the template to lib/templates.json:
      "streamlit-developer": {
        "name": "Streamlit developer",
        "lib": ["streamlit", "pandas", "numpy", "matplotlib", "requests", "seaborn", "plotly"],
        "file": "app.py",
        "instructions": "A streamlit app that reloads automatically.",
        "port": 8501
      }
    e2b template init
    e2b template build --name <template-name>
  3. Add Custom LLM Models and Providers

    main

    To add new models or providers to the Fragments UI, modify lib/models.ts.

    Adding a Model

    Add an entry to the models list in lib/models.ts:

    {
      "id": "mistral-large",
      "name": "Mistral Large",
      "provider": "Ollama",
      "providerId": "ollama"
    }

    Adding a Provider

    Add a new configuration to the providerConfigs list in lib/models.ts. For example, to add Fireworks:

    fireworks: () => createOpenAI({ apiKey: apiKey || process.env.FIREWORKS_API_KEY, baseURL: baseURL || 'https://api.fireworks.ai/inference/v1' })(modelNameString),

    You can also customize the default structured output mode by adjusting the getDefaultMode function for specific providerIds.

  4. Environment Variables Reference

    main

    Fragments requires several environment variables for operation. Create a .env.local file to manage them.

    Required Keys

    • E2B_API_KEY: Your E2B API key.
    • LLM Provider Keys (at least one required):
      • OPENAI_API_KEY
      • ANTHROPIC_API_KEY
      • GROQ_API_KEY
      • FIREWORKS_API_KEY
      • TOGETHER_API_KEY
      • GOOGLE_AI_API_KEY
      • GOOGLE_VERTEX_CREDENTIALS
      • MISTRAL_API_KEY
      • XAI_API_KEY

    Optional Keys

    • MORPH_API_KEY: For Morph integration (on by default).
    • NEXT_PUBLIC_SITE_URL: The domain of the site.
    • RATE_LIMIT_MAX_REQUESTS / RATE_LIMIT_WINDOW: Rate limiting configuration.
    • KV_REST_API_URL / KV_REST_API_TOKEN: Vercel/Upstash KV for short URLs and rate limiting.
    • SUPABASE_URL / SUPABASE_ANON_KEY: For Supabase authentication.
    • NEXT_PUBLIC_POSTHOG_KEY / NEXT_PUBLIC_POSTHOG_HOST: For PostHog analytics.

    UI Configuration (Uncomment to enable)

    • NEXT_PUBLIC_NO_API_KEY_INPUT: Disables API key and base URL input in the chat.
    • NEXT_PUBLIC_NO_BASE_URL_INPUT: Disables base URL input in the chat.
    • NEXT_PUBLIC_HIDE_LOCAL_MODELS: Hides local models from the available models list.
  5. Create a basic Gradio application

    main

    To build a simple Gradio interface, define a function that processes inputs and returns an output, then wrap it in a gr.Interface. You can specify input and output types using strings like "text" or "slider". Finally, call .launch() to start the application.

    import gradio as gr
    
    def greet(name, intensity):
        return "Hello, " + name + "!" * int(intensity)
    
    demo = gr.Interface(
        fn=greet,
        inputs=["text", "slider"],
        outputs=["text"],
    )
    
    demo.launch()
  6. Fragment data schema

    main

    The fragmentSchema defines the structure for fragment generation data. Use this schema to validate objects containing metadata, code, and dependency information for a fragment.

    Key fields include:

    • commentary: Detailed description of the generation steps.
    • template: The name of the template used.
    • title: A short title (max 3 words).
    • description: A short description (max 1 sentence).
    • additional_dependencies: An array of strings for dependencies not included in the template.
    • has_additional_dependencies: Boolean indicating if extra dependencies are needed.
    • install_dependencies_command: The command used to install additional dependencies.
    • port: The port number used by the fragment (nullable if no ports are exposed).
    • file_path: Relative path to the file.
    • code: The generated runnable code.
  7. Convert templates to a prompt string

    main
    The templatesToPrompt function takes a Templates object and converts it into a formatted string suitable for LLM prompts. Each entry in the string includes the template ID, instructions, the primary file path, installed dependencies, and the listening port.
  8. Create rate limit responses with headers

    main

    Use createRateLimitResponse to generate a 429 Too Many Requests response that includes standard rate-limiting headers. This is useful for informing clients about their current usage status.

    Parameters:

    • limit: An object containing:
      • amount (number): The total limit.
      • remaining (number): The number of requests remaining.
      • reset (number): The timestamp or value indicating when the limit resets.
  9. Convert Fragments messages to AI SDK format

    main
    Use toAISDKMessages to convert an array of Message objects into a format compatible with the AI SDK. During conversion, any content block with type: 'code' is automatically transformed into a type: 'text' block to ensure compatibility.