txtai AI Framework

repository·master·Indexed 11 days ago

https://github.com/neuml/txtai

An all-in-one AI framework for semantic search, LLM orchestration, and language model workflows. Built on an embeddings database, it supports vector search, RAG, autonomous agents via smolagents, and multimodal indexing for text, audio, images, and video. It provides Web and Model Context Protocol (MCP) APIs with bindings for JavaScript, Java, Rust, and Go.

Tokens
101.8K
Snippets
399
Records
462
Agent score
93%

What's inside txtai

  1. Overview of the txtai AI framework

    master
    txtai is an all-in-one AI framework designed for semantic search, LLM orchestration, and language model workflows. It is built on a core embeddings database that combines vector indexes (both sparse and dense), graph networks, and relational databases. This architecture allows it to function as both a high-performance vector search engine and a knowledge source for LLM applications like Retrieval Augmented Generation (RAG).
  2. Overview of txtai capabilities

    master

    txtai is an all-in-one AI framework designed for semantic search, LLM orchestration, and language model workflows. Its core is an embeddings database that combines vector indexes (sparse and dense), graph networks, and relational databases.

    Key Features:

    • Vector Search: Supports SQL, object storage, topic modeling, graph analysis, and multimodal indexing.
    • Multimodal Embeddings: Create embeddings for text, documents, audio, images, and video.
    • LLM Pipelines: Powered by language models for tasks like question-answering, labeling, transcription, translation, and summarization.
    • Workflows: Join pipelines together to aggregate business logic into microservices or multi-model workflows.
    • Agents: Connect embeddings, pipelines, and workflows to solve complex problems autonomously.
    • APIs: Provides Web and Model Context Protocol (MCP) APIs with bindings for JavaScript, Java, Rust, and Go.
  3. txtai API Language Bindings and Compatibility

    master

    The txtai API is accessible through several official language bindings and standard protocols:

    Language Bindings:

    • Python
    • JavaScript
    • Java
    • Rust
    • Go

    Compatibility:

    • OpenAI-compatible: Can be used with standard OpenAI client libraries.
    • Model Context Protocol (MCP): Supports MCP endpoints.
  4. Use the Tokenizer pipeline for keyword indexing

    master

    The Tokenizer pipeline splits text into individual tokens, which is primarily used for keyword or term indexing.

    Important Note: This pipeline is intended for keyword indexing and is not designed for use with Transformers-based models, as those models use their own specialized tokenizers.

    from txtai.pipeline import Tokenizer
    
    tokenizer = Tokenizer()
    result = tokenizer("text to tokenize")
  5. Key features of txtai

    master

    txtai provides a comprehensive suite of AI capabilities:

    • Vector Search: Supports SQL, object storage, topic modeling, graph analysis, and multimodal indexing.
    • Multimodal Embeddings: Create embeddings for text, documents, audio, images, and video.
    • LLM Pipelines: Run language model tasks including LLM prompts, question-answering, labeling, transcription, translation, and summarization.
    • Workflows: Join multiple pipelines together to aggregate business logic, ranging from simple microservices to complex multi-model workflows.
    • Agents: Intelligently connect embeddings, pipelines, workflows, and other agents to solve complex problems autonomously.
    • APIs & Bindings: Provides Web and Model Context Protocol (MCP) APIs. Language bindings are available for JavaScript, Java, Rust, and Go.
    • Deployment: Designed to run locally or scale out using container orchestration.
  6. How semantic graphs work in txtai

    master

    Enabling a graph network adds a semantic graph at index time. txtai uses vector embeddings to automatically create relationships between nodes, but you can also specify them manually.

    Manual Relationships

    You can define relationships during the index call in two ways:

    1. By ID: Provide a list of target IDs.
    2. With Attributes: Provide a list of dictionaries containing the target id and a relationship type (e.g., MEMBER_OF).

    Graph Analysis

    Once indexed, you can interact with the graph using:

    • embeddings.graph.topics: Returns a mapping of discovered topics to associated IDs.
    • embeddings.graph.centrality(): Returns the most central nodes in the index.

    Graphs are persisted alongside the embeddings index during save and load operations.

    # Manual relationships by id
    embeddings.index([{"id": "0", "text": "...", "relationships": ["2"]}])
    
    # Manual relationships with additional edge attributes
    embeddings.index([{"id": "0", "text": "...", "relationships": [
        {"id": "2", "type": "MEMBER_OF"}
    ]}])
  7. Configure data merging (packing) in HFTrainer

    master

    For language-generation and language-modeling tasks, the merge parameter controls how text is packed into chunks to improve training efficiency by reducing padding.

    Merge Options

    • concat (default): Text is split into chunks up to maxlength. Data can be split across multiple chunks. This maximizes efficiency and is recommended for general masked language modeling.
    • pack: Text is split into chunks up to maxlength. Data is guaranteed to stay within the same chunk, though chunks may be smaller than maxlength. This is recommended for instruction/prompt fine-tuning to ensure complex logic isn't split.
    • None: Disables merging entirely.
  8. Implement Agentic RAG

    master

    Unlike standard Retrieval Augmented Generation (RAG) which performs a single vector search, Agentic RAG uses an agent to perform multiple iterations and potentially query multiple databases to reach a conclusion. This is useful for aggregating information from diverse sources to build complex reports.

    To implement this, pass a detailed prompt (instruction) to the Agent instance, instructing it on how to use its available tools to research and format the output.

    researcher_prompt = """
    You're an expert researcher looking to write a paper on {topic}.
    Search for websites, scientific papers and Wikipedia related to the topic.
    Write a report with summaries and references (with hyperlinks).
    Write the text as Markdown.
    """
    
    # The agent will use its tools to fulfill the multi-step research instruction
    agent(researcher_prompt.format(topic="alien life"))
  9. Build Agent Teams

    master

    In txtai, agents can be used as tools for other agents. This allows you to build Agent Teams, where a primary agent delegates tasks to specialized sub-agents. Each sub-agent has its own reasoning engine and toolset.

    To create an agent team:

    1. Define specialized sub-agents (e.g., a websearcher agent or a wikiman agent).
    2. Create a primary agent and pass the sub-agents into its tools list using a dictionary with a target key pointing to the sub-agent instance.

    Sub-agent tool dictionary format:

    • name: The name the primary agent uses to refer to the tool.
    • description: Instructions for the primary agent on when to use this sub-agent.
    • target: The actual Agent instance to be invoked.
    from txtai import Agent, LLM
    
    llm = LLM("Qwen/Qwen3-4B-Instruct-2507")
    
    # Define specialized sub-agents
    websearcher = Agent(model=llm, tools=["websearch"])
    
    wikiman = Agent(
        model=llm,
        tools=[{
            "name": "wikipedia",
            "description": "Searches a Wikipedia database",
            "provider": "huggingface-hub",
            "container": "neuml/txtai-wikipedia"
        }]
    )
    
    # Define the primary agent that uses the sub-agents as tools
    agent = Agent(
        model=llm,
        tools=[
            {
                "name": "wikiman",
                "description": "Wikipedia has all the answers, I search Wikipedia and answer questions",
                "target": wikiman
            },
            {
                "name": "websearcher",
                "description": "I run web searches, there is no answer a web search can't solve!",
                "target": websearcher
            }
        ],
        max_steps=10
    )
    
    agent("Research fundamental concepts about Signal Processing and build a comprehensive report.")
  10. Add middleware using Dependencies

    master

    To add custom logic that executes with every request, use Dependencies. Dependencies function as middleware and are ideal for implementing custom authorization steps, authentication methods, or other request-processing logic that should run before the main application logic.

    See the [API Authorization and Authentication](https://github.com/neuml/txtai/blob/master/examples/54_API_Authorization_and_Authentication.ipynb) notebook for a detailed implementation example.
  11. Query data using SQL and the `similar` clause

    master

    If content storage is enabled, you can use SQL to combine similarity searches with structured filters. The similar clause is the bridge between the similarity index and the relational database.

    Syntax: similar("query", "number of candidates", "index", "weights")

    ArgumentDescription
    queryNatural language query string
    number of candidatesNumber of candidate results to return. Should be larger than the desired LIMIT to account for filters. Defaults to query limit for single filters, or 10x limit for multiple filters.
    indexTarget subindex name (if subindexes are enabled)
    weightsHybrid score weights (for sparse/dense indexes)

    Example:

    SELECT id, text, score FROM txtai WHERE similar('feel good story')
  12. Explore LLM Orchestration and Agents

    master

    txtai provides orchestration for Large Language Models (LLMs), including autonomous agents, Retrieval Augmented Generation (RAG), and complex task chains.

    Agents

    Agents in txtai connect embeddings, pipelines, and workflows to solve complex problems autonomously. They are built on top of the smolagents framework and support:

    • LLM Providers: Hugging Face, llama.cpp, and OpenAI/Claude/AWS Bedrock (via LiteLLM).
    • Prompting Standards: Support for agents.md and skill.md specifications.
    • Toolkits: Access to the txtai agent toolkit for specialized tasks.

    Retrieval Augmented Generation (RAG)

    RAG reduces LLM hallucinations by providing a knowledge base as context (e.g., "chat with your data"). txtai supports:

    • Standard RAG: Building pipelines with citations.
    • Multi-source RAG: Retrieving context from Web, SQL, and other sources.
    • GraphRAG: Deep graph search powered RAG using knowledge graphs.