GenSX Documentation

repository·main·Indexed 19 days ago

https://github.com/gensx-inc/gensx

GenSX is a TypeScript-based workflow engine for building complex LLM applications, such as agents and chatbots, using functional composition instead of traditional graph-based models. It includes tools for deploying workflows to GenSX Cloud, managing them via the GenSX console, and running them locally via a CLI or API server. The engine supports integrations with providers like Anthropic, OpenAI, and Perplexity, and features capabilities for persistent chat memory and automated content generation.

Tokens
114K
Snippets
381
Records
500
Agent score
63%

What's inside GenSX

  1. What is GenSX?

    main
    GenSX is a TypeScript framework and workflow engine designed for building complex LLM applications such as agents, chatbots, and long-running workflows. Unlike graph-oriented frameworks that use nodes and edges, GenSX uses a functional composition model where workflows are built by composing pure TypeScript functions called Components.
  2. Overview of GenSX App Templates

    main

    GenSX provides several Next.js-based templates to jumpstart development:

    Chat UX

    Focuses on chat-based interfaces. Features include:

    • Streaming chat: Real-time message streaming.
    • AI thinking: Visible reasoning process.
    • Tool integration: Built-in tools for web scraping, search, and data processing.
    • Thread history: Persistent history using GenSX Storage.

    Deep Research

    Focuses on iterative research workflows. Features include:

    • Multi-step streaming workflow: Iterative process for building reports.
    • Web search and summarization: Uses Tavily for search and extractive summarization.
    • Detailed report generation: Creates structured, in-depth reports.

    Draft Pad

    Focuses on AI-powered writing. Features include:

    • Real-time streaming: Live content updates.
    • Draft versioning: Track and navigate changes.
    • Live progress tracking: Detailed workflow event updates.
    • Multi-provider support: Works with OpenAI, Anthropic, Google, etc.

    Client Side Tools

    Focuses on client-side interactivity. Demonstrates how AI can control client-side components like maps via tool calling.

  3. Explore GenSX Basic Examples

    main

    GenSX provides several basic examples to demonstrate core patterns and provider integrations:

    • Reflection: Demonstrates the self-reflection pattern.
    • Anthropic Examples: Shows usage of the @gensx/anthropic component.
    • OpenAI Examples: Shows usage of the @gensx/openai component.
    • Vercel AI SDK Examples: Shows usage of the @gensx/vercel-ai component.
  4. Explore GenSX Full Examples

    main

    GenSX provides complex, end-to-end workflow examples:

    • Hacker News Analyzer: Analyzes HN posts to generate summaries and trends in Paul Graham's writing style.
    • Blog Writer: An end-to-end workflow for topic research and content creation.
    • Deep Research: Generates reports by researching and summarizing a list of research papers.
    • Computer Use: Demonstrates the OpenAI computer use tool with GenSX.
    • Text to SQL: Uses database storage to translate natural language into SQL queries.
    • RAG: Demonstrates Retrieval Augmented Generation using vector search storage.
    • Chat Memory: Shows how to build a chat application with persistent history using blob storage.
  5. What is CLAUDE.md and how does it help?

    main

    In GenSX projects, CLAUDE.md acts as a persistent memory layer for Claude (or other LLMs). It provides the model with essential context to ensure accurate and contextual assistance. The file typically contains:

    • Common project commands (build, test, etc.)
    • Code style preferences
    • Project structure overview
    • Common patterns and examples for GenSX components
    • LLM provider configuration examples
    • Project-specific notes
  6. Implement RAG with the useSearch hook

    main

    GenSX provides a useSearch hook to implement Retrieval Augmented Generation (RAG) workflows. This allows an LLM to query a vector search namespace to find relevant information before generating an answer.

    A typical RAG workflow in GenSX consists of two components:

    1. Initialization: A component (e.g., InitializeSearch) that creates and populates a vector search namespace with data.
    2. Workflow Execution: A component (e.g., RagWorkflow) that processes user questions by searching the vector store and using the retrieved context to generate responses.

    Data used in these workflows is stored in a GenSX Cloud search namespace.

  7. Implement self-reflection patterns in GenSX workflows

    main

    Self-reflection is a prompting technique where an LLM evaluates its own output and then improves it. In GenSX, this is implemented using a Reflection component that creates a programmatic loop between an evaluation component and an improvement component.

    To implement this pattern, you need:

    1. An evaluation component (EvaluateFn): Assesses the current output and provides feedback and a decision on whether to continue.
    2. An improvement component (ImproveFn): Processes the input using the provided feedback to generate a revised version.

    The Reflection component iterates until either the EvaluateFn returns continueProcessing: false or the maxIterations limit is reached.

    interface ReflectionProps<TInput> {
      // The initial input to process
      input: TInput;
      // Component to process the input and generate new output
      ImproveFn: (props: { input: TInput; feedback: string }) => Promise<TInput>;
      // Component to evaluate if we should continue processing and provide feedback
      EvaluateFn: (props: { input: TInput }) => Promise<ReflectionOutput>;
      // Maximum number of iterations allowed
      maxIterations?: number;
    }
    
    const Reflection = gensx.Component(
      "Reflection",
      async <TInput>({
        input,
        ImproveFn,
        EvaluateFn,
        maxIterations = 3,
      }: ReflectionProps<TInput>): Promise<TInput> => {
        let currentInput = input;
        let iteration = 0;
    
        while (iteration < maxIterations) {
          const { feedback, continueProcessing } = await EvaluateFn({
            input: currentInput,
          });
    
          if (!continueProcessing) {
            break;
          }
    
          currentInput = await ImproveFn({ input: currentInput, feedback });
          iteration++;
        }
    
        return currentInput;
      },
    );
  8. Observability and Tracing in GenSX Cloud

    main

    GenSX Cloud automatically captures detailed execution traces for all workflows and agents without requiring additional instrumentation.

    What is captured:

    • LLM Calls: Full prompts, parameters, and responses.
    • Tool Invocations: Input arguments and return values.
    • Component Lifecycle: Inputs, outputs, and intermediate state changes for every component.
    • Execution Flow: A hierarchical component tree and timeline of execution.

    Analysis Tools:

    • Timeline View: Visualizes the sequence and duration of component execution.
    • Component Tree: Navigates the hierarchical structure of the workflow.
    • Input/Output Inspector: Examines data flowing between components.
    • Error Highlighting: Identifies exactly where failures occurred.
    // Traces are automatically captured when workflows are executed
    // No additional instrumentation required
    const result = await MyWorkflow({ input: "some query" });
  9. Understand SQL database environments

    main

    GenSX SQL databases provide a seamless transition between local development and cloud production:

    • Local development: Databases are stored as SQLite files in the .gensx/databases directory by default using libsql.
    • Cloud deployment: Databases are automatically provisioned in the cloud (powered by Turso) with millisecond latency.

    No code changes are required when moving from development to production environments.

  10. How the Human-in-the-Loop mechanism works

    main

    The requestInput mechanism operates through the following lifecycle:

    1. URL Generation: GenSX generates a unique callback URL tied to the specific execution node.
    2. Triggering: The callbackUrl is passed into your provided trigger function.
    3. Pausing: The workflow execution is paused at the await requestInput(...) line.
    4. Resumption: Once a POST request is received at the callbackUrl, the workflow resumes with the request body as the return value.

    The callback URL follows this format: ${process.env.GENSX_API_BASE_URL}/org/${process.env.GENSX_ORG}/workflowExecutions/${process.env.GENSX_EXECUTION_ID}/fulfill/${nodeId}

  11. How GenSX represents workflows as trees

    main

    GenSX uses a tree-based model for workflow orchestration instead of an explicit graph of nodes and edges. Workflows are expressed using JSX-like syntax where each component represents a step in the process.

    Data flows through the tree via child functions (render props pattern). A parent component executes and passes its output to a child function, which then allows the next component in the workflow to be nested. This makes data dependencies explicit and avoids global state.

    Key benefits:

    • Explicit Dependencies: Each child component only receives what its parent explicitly provides.
    • Readability: The workflow reads top-to-bottom like standard code.
    • Testability: Components are treated as pure functions of their inputs, making them easy to isolate and test.
    <FetchHNPosts limit={postCount}>
      {(stories) => (
        <AnalyzeHNPosts stories={stories}>
          {({ analyses }) => (
            <GenerateReport analyses={analyses}>
              {(report) => (
                <EditReport content={report}>
                  {(editedReport) => (
                    <WriteTweet
                      context={editedReport}
                      prompt="Summarize the HN trends in a tweet"
                    />
                  )}
                </EditReport>
              )}
            </GenerateReport>
          )}
        </AnalyzeHNPosts>
      )}
    </FetchHNPosts>
  12. Implement automatic contextual logging

    main

    GenSX supports contextual logging, which automatically captures relevant metadata (like query parameters, stack traces, or variable states) without requiring manual string formatting. This reduces boilerplate and provides richer debugging information.

    Instead of manually including error details in a string, you can pass a simple message, and the logger will enrich the entry with the necessary context.

    # Traditional logging approach
    logger.error(f"Database query failed: {error}")
    
    # With automatic contextual logging
    logger.error("Query failed")  # Automatically includes query details, parameters, and stack trace