LemonAI Documentation

repository·main·Indexed 23 days ago

https://github.com/hexdocom/lemonai

LemonAI (v0.4.0) is an open-source, self-evolving general AI agent framework. It provides a local-first alternative to cloud platforms, featuring a Docker-based VM sandbox for safe code execution and support for local LLMs via Ollama or VLLM. The framework includes a browser-server for web automation, a specialized human-AI collaboration editor, and an API for triggering agents to perform tasks.

Tokens
18.2K
Snippets
27
Records
96
Agent score
81%

What's inside LemonAI

  1. What is Lemon AI

    main

    Lemon AI is a full-stack, open-source, self-evolving general AI agent framework. It serves as a local alternative to agentic platforms like Manus and Genspark AI.

    Key capabilities include:

    • Deep Research & Web Browsing: Conducting searches and generating reports.
    • Code Generation & Data Analysis: Writing and executing code safely.
    • Content Creation: Document processing and creative writing.
    • Self-Evolution: Building personalized memory for each conversation to improve performance over time.
    • Secure Execution: Uses a Docker-based Virtual Machine (VM) sandbox to execute code, protecting your host file system and operating system.

    It supports local LLMs via Ollama or VLLM (e.g., DeepSeek, Qwen, Llama) for privacy, but can also be configured to use cloud APIs like Claude, GPT, Gemini, or Grok.

  2. Optimize Agent Efficiency and Tool Usage

    main

    To minimize overhead and maximize speed, the agent follows these efficiency patterns:

    • Operation Consolidation: Combines multiple operations into one (e.g., combining multiple bash commands into a single execution).
    • Tool Selection: Uses high-efficiency tools like sed, grep, find, and git with appropriate filters to minimize unnecessary operations when exploring or editing codebases.
  3. Understand the Lemon AI Agent's Problem-Solving Workflow

    main

    The Lemon AI agent follows a structured five-step workflow to ensure high-quality task resolution and code interaction:

    1. Explore: Thoroughly investigate relevant files and understand the context before proposing a solution.
    2. Analyze: Consider multiple approaches and select the most promising one.
    3. Test:
      • For bug fixes: Create tests to verify the issue before implementing the fix.
      • For new features: Use Test-Driven Development (TDD) where appropriate.
      • Note: If the codebase lacks testing infrastructure and setup is significant, the agent will consult you before proceeding.
    4. Implement: Make targeted, minimal changes to address the problem.
    5. Verify: Thoroughly test the implementation, including edge cases.
  4. Follow File System and Code Quality Guidelines

    main

    When interacting with the file system and writing code, the agent adheres to these principles:

    File System

    • Path Discovery: Do not assume provided file paths are relative to the current working directory; the agent will explore the file system to locate them first.
    • Direct Modification: When editing, the agent modifies existing files directly rather than creating new files with different names.
    • Efficient Editing: For global search and replace, the agent prefers using sed to avoid opening editors multiple times.

    Code Quality

    • Conciseness: Write efficient code with minimal, non-redundant comments.
    • Minimalism: Focus on the minimum changes necessary to solve a problem.
    • Exploration First: The agent explores the codebase thoroughly before implementing changes.
    • Refactoring: Large additions to functions or files will be broken down into smaller, manageable parts.
  5. Understand the Browser Agent Input Format

    main

    The AI agent receives browser state information in a structured format to understand the current context. The input includes:

    • Task: The ultimate goal.
    • Previous steps: History of actions taken.
    • Current URL: The active web address.
    • Open Tabs: List of currently active tabs.
    • Interactive Elements: A list of elements formatted as [index]<type>text</type>.

    Key Element Rules:

    • Index: Only elements with numeric identifiers in brackets (e.g., [33]) are interactive.
    • Hierarchy: Indentation (using \t) indicates that an element is a child of the element above it with a lower index.
    • New Elements: Elements marked with an asterisk (*) are new elements added since the previous step (if the URL has not changed).

    Example Element List:

    [33]<div>User form</div>
    	*[35]*<button aria-label='Submit form'>Submit</button>
  6. How Lemon AI Editor works

    main

    The Lemon AI Editor is a specialized tool designed for seamless human-AI collaboration, specifically for refining generated HTML content. It allows users to iteratively optimize results without regenerating entire files.

    It features two primary modes:

    1. AI Editing Mode: Allows the agent to modify specific sections of a page, insert new paragraphs/content, or reformat the entire page based on natural language instructions.
    2. Advanced Editing Mode: Provides direct manual control for users to quickly adjust text and elements themselves.

    The editor follows a 'what you see is what you get' (WYSIWYG) and 'point-and-click' philosophy.

  7. Handle SSE streaming responses from LLMs

    main

    When using stream: true, the LLM returns data via Server-Sent Events (SSE). To process the incoming stream, use the following logic:

    1. Splitter: Use \n\n to separate individual data chunks.
    2. Parsing: For each chunk, strip the data: prefix and parse the remaining JSON string. The content is typically located at choices[0].delta.content.
  8. How the Lemon AI Editor works

    main

    The Lemon AI Editor is a specialized interface designed for seamless human-AI collaboration when refining generated results (like research reports or HTML pages).

    It features two primary modes:

    1. AI Editing Mode: Allows you to instruct the AI to modify specific sections, insert new paragraphs, or reformat the entire page.
    2. Advanced Edit Mode: Provides direct manual text adjustment for quick, precise changes.

    This 'what you see is what you get' approach allows users to iteratively refine outputs without needing to re-generate entire files.

  9. Task Termination Conditions for the Agent

    main

    The agent is instructed to terminate tasks under specific failure conditions to prevent infinite loops or wasted resources. Termination should be triggered in the following scenarios:

    • Multiple Attempts Without Progress: If no substantial progress is made after 3 consecutive action sequences and the plan remains unchanged.
    • CAPTCHA or Human Verification: If human verification (e.g., sliders, image selection) is encountered. The agent should try alternative websites first. If verification appears on 3 consecutive different websites, terminate.
    • Stuck in a Loop: If the agent executes 3 consecutive actions without significant page state changes (e.g., unchanged URL or interactive elements).

    When terminating, the agent must use the done action, set success to false, and provide a detailed explanation in the text parameter.

  10. Configure LLM chat/completions requests

    main

    To call an LLM (using DeepSeek as an example), you must perform an http.post request to the completions endpoint. The request requires an Authorization header with your API_KEY and should set stream: true to enable Server-Sent Events (SSE) for real-time responses.

    const { url, API_KEY, model, temperature = 0 } = config;
    const config = {
      method: "post",
      maxBodyLength: Infinity,
      url,
      headers: {
        Authorization: `Bearer ${API_KEY}`,
        "Content-Type": "application/json",
      },
      data: {
        model, // 调用模型
        messages, // chat 提示词
        stream: true, // 流式输出
        temperature,
      },
      responseType: "stream",
    };
    
    const response = await axios.request(config).catch((err) => {
      return err;
    });
    return response;
  11. Run an agent via the API

    main

    You can trigger an agent to perform tasks by sending a POST request to the /api/agent/run endpoint. The request requires a JSON body containing a question string, which describes the task you want the agent to execute. You must also provide a Bearer token in the Authorization header for authentication.

    curl -N --location 'http://localhost:3000/api/agent/run' \
    --header 'Content-Type: application/json' \
    --header 'Authorization: Bearer xxx' \
    --data '{"question": "请查看工作目录中的文件, 找到 README.md 文件, 读取文件内容, 并输出内容"}'