Orchardcore ai data sources

From CrestApps/CrestApps.AgentSkills · View interactive version

Orchardcore ai data sources is an AI agent skill from the GitHub repository CrestApps/CrestApps.AgentSkills. Skill for CrestApps AI Data Sources in Orchard Core 4.0 (CrestApps.OrchardCore 3.0). Covers knowledge base indexes, RAG retrieval, the source types SearchIndexProfile, Elasticsearch, AzureAISearch, PostgreSQL, Web and File, embedding deployments picked by the embedding slot, OData filters and transl

How to use this skill

Skill file

Orchard Core AI Data Sources

Configure AI data sources and knowledge base retrieval

You are an Orchard Core expert. Generate configuration, recipes, and code for CrestApps AI data sources: a data source reads documents from a source, chunks and embeds them into a shared AI knowledge base index, and AI profiles, chat interactions, workflow tasks, and tool instances retrieve from it (RAG).

Guidelines

Features

| Feature | Feature ID | Depends on |

|---|---|---|

| AI Data Sources | CrestApps.OrchardCore.AI.DataSources | CrestApps.OrchardCore.AI, OrchardCore.Indexing (dependency only) |

| AI Data Sources - Elasticsearch | CrestApps.OrchardCore.AI.DataSources.Elasticsearch | AI Data Sources, OrchardCore.Elasticsearch |

| AI Data Sources - Azure AI Search | CrestApps.OrchardCore.AI.DataSources.AzureAI | AI Data Sources, OrchardCore.Indexing, OrchardCore.AzureAI |

| AI Data Sources - PostgreSQL | CrestApps.OrchardCore.AI.DataSources.PostgreSQL | AI Data Sources |

| AI Web Crawlers | CrestApps.OrchardCore.AI.WebCrawlers | AI Data Sources (see orchardcore-ai-web-crawlers) |

| AI File Sources | CrestApps.OrchardCore.AI.FileSources | AI Data Sources, AI Documents (see orchardcore-ai-file-sources) |

PostgreSQL only reads source rows. Pair it with the Elasticsearch or Azure AI Search feature, which provides the knowledge base index.

Source types

| Source value | Added by | Reads from |

|---|---|---|

| SearchIndexProfile | AI Data Sources | An Orchard Elasticsearch or Azure AI Search index profile, for example the content index |

| Elasticsearch | Elasticsearch feature | An external Elasticsearch index (self-managed URL or Elastic Cloud ID) |

| AzureAISearch | Azure AI Search feature | An external Azure AI Search index |

| PostgreSQL | PostgreSQL feature | A PostgreSQL table, read in batches ordered by the key column |

| Web | AI Web Crawlers | Pages scraped by web crawlers that target the data source |

| File | AI File Sources | Files ingested by file sources that target the data source |

Enable Elasticsearch-backed data sources

{
  "steps": [
    {
      "name": "Feature",
      "enable": [
        "OrchardCore.Elasticsearch",
        "CrestApps.OrchardCore.AI.DataSources.Elasticsearch"
      ],
      "disable": []
    }
  ]
}

For Azure AI Search, enable OrchardCore.AzureAI and CrestApps.OrchardCore.AI.DataSources.AzureAI. To read a PostgreSQL table, add CrestApps.OrchardCore.AI.DataSources.PostgreSQL next to one of them.

End-to-end setup

  1. Create an embedding deployment that declares textEmbedding (see orchardcore-ai-model-capabilities).
  2. Go to Search > Indexing, add AI Knowledge Base Index (Elasticsearch) or (Azure AI Search), and pick the embedding deployment.
  3. Go to Artificial Intelligence > Data Sources, choose Add Data Source, pick a source type, then set the source settings, the destination index, and the field mapping.
  4. Saving queues a full sync. Use Sync index in the row's Actions menu to re-run one later.
  5. Attach the data source on the Knowledge tab of an AI profile, profile template, or chat interaction, or bind it to a Data source search tool instance (see orchardcore-ai-tool-instances).

Field defaults for the content index: Elasticsearch uses key ContentItemId, title Content.ContentItem.DisplayText.keyword, content Content.ContentItem.FullText; Azure AI Search uses ContentItemId, Content__ContentItem__DisplayText__keyword, Content__ContentItem__FullText.

Create a data source with a recipe

{
  "steps": [
    {
      "name": "AIDataSource",
      "DataSources": [
        {
          "ItemId": "articlesdatasource01",
          "DisplayText": "Articles",
          "Source": "SearchIndexProfile",
          "SourceIndexProfileName": "articles",
          "AIKnowledgeBaseIndexProfileName": "AIRagKnowledgeBase",
          "KeyFieldName": "ContentItemId",
          "TitleFieldName": "Content.ContentItem.DisplayText.keyword",
          "ContentFieldName": "Content.ContentItem.FullText"
        }
      ]
    }
  ]
}

The AIDataSource deployment step (Artificial Intelligence category) exports all or selected data sources (IncludeAll, SourceIds).

Use a data connection

With the Data Connectors feature on, the Elasticsearch and AzureAISearch source editors offer a

Data connection picker to people who may manage data connections and their credentials. The choice is

stored as Properties.DataConnectionReference.ConnectionId and the data source then reads its endpoint and

credentials from the connection, where the secrets stay protected and are never written to the data source or

a deployment plan:

authentication, extra headers, timeout); everything but the index name comes from it. OAuth 2.0, OpenID

Connect and query-string API keys are refused.

API key authentication; a connection without authentication uses the default Azure credential.

the deployment step leaves the stored ApiKey, Password and Base64ApiKey out while a connection is used.

See orchardcore-data-connectors for connections.

Attach a data source to an AI profile

The profile stores the selection in DataSourceMetadata and the retrieval settings in AIDataSourceRagMetadata:

{
  "steps": [
    {
      "name": "AIProfile",
      "profiles": [
        {
          "Name": "knowledge-assistant",
          "DisplayText": "Knowledge Assistant",
          "Type": "Chat",
          "Properties": {
            "AIProfileMetadata": {
              "SystemMessage": "Answer using the provided knowledge base context."
            },
            "DataSourceMetadata": {
              "DataSourceId": "articlesdatasource01"
            },
            "AIDataSourceRagMetadata": {
              "Strictness": 3,
              "TopNDocuments": 5,
              "IsInScope": true,
              "Filter": "category eq 'docs'"
            }
          }
        }
      ]
    }
  ]
}

| Editor field (Knowledge tab) | Property | Notes |

|---|---|---|

| Data source | DataSourceMetadata.DataSourceId | |

| Strictness | AIDataSourceRagMetadata.Strictness | 1 to 5, blank uses the site default |

| Retrieved documents | AIDataSourceRagMetadata.TopNDocuments | 3 to 20, blank uses the site default |

| Restrict answers to retrieved data only | AIDataSourceRagMetadata.IsInScope | |

| Filter | AIDataSourceRagMetadata.Filter | OData, validated with IODataValidator on save |

Site defaults live in the Data Sources section of Settings > Artificial Intelligence (AIDataSourceSettings.DefaultStrictness, default 3, and DefaultTopNDocuments, default 5). Preemptive retrieval is the Enable preemptive retrieval-augmented generation (RAG) switch of the Default Orchestrator section; when a profile has tools disabled, preemptive RAG always runs.

Retrieval behavior

| Preemptive RAG | IsInScope | Behavior |

|---|---|---|

| On | On | Context is injected; the model must answer only from it |

| On | Off | Context is injected; the model may add general knowledge |

| Off | On | No injected context; the model must call search_data_sources first and use nothing else |

| Off | Off | No injected context; the model calls search_data_sources first, then may use general knowledge |

The preemptive retrieval and the search_data_sources tool read the same filter, strictness, and retrieved document count from the profile or chat interaction. Injected sections use bracket headers such as [Data Source Context], [Knowledge Source Instructions], and [Scope Constraint].

OData filters

A knowledge base chunk stores every source field in a filters object. Write a field by its source name (category eq 'docs'); the translator adds the filters. prefix. The typed columns contentType, rootId, parentId, and page are not prefixed.

| Knowledge base index | Filter support |

|---|---|

| Elasticsearch | Translated by the CrestApps.Core Elasticsearch IODataFilterTranslator (AddElasticsearchODataFilterTranslator()) into a bool query. Strings are dynamically mapped as text plus keyword, so use category.keyword eq 'Support Docs' for an exact match. |

| Azure AI Search | Not supported. No translator is registered, so the filter is ignored and No OData filter translator available for provider is logged. |

Citations and links

Answers carry [doc:N] markers. Each reference has a ReferenceId (source key), a ReferenceType (the source index profile type), and a title. Indices are shared across data sources, documents, and tool calls through AIInvocationScope.Current. A reference gets a link only when an IAIReferenceLinkResolver is registered for its type:

public sealed class Startup : StartupBase
{
    public override void ConfigureServices(IServiceCollection services)
    {
        services.AddKeyedScoped<IAIReferenceLinkResolver, ProductLinkResolver>("ProductIndex");
    }
}

The Web source registers its own resolver, so crawled pages link back to their URL.

Keeping the knowledge base in sync

Restricted content stays out

A knowledge base answers anyone allowed to query its profiles, without the source's own access checks, so DataSourceIndexingService indexes only documents everyone may read. Before indexing a source document (full and targeted syncs), it asks every IIndexedDocumentVisibility (CrestApps.OrchardCore.Indexing, registered with services.AddIndexedDocumentVisibility<T>()) through IsForEveryoneAsync(documentId); a document that is not for everyone is skipped and its chunks are deleted, so a record restricted after indexing leaves at its next sync. Content Access Control registers one for content items (an item restricted to roles that leave out Anonymous). The check uses the reference id, so keep a content index's key field as the content item id.

Per-request result filters

IAIDataSourceResultFilter (CrestApps.OrchardCore.AI.Core.DataSources) decides per request which retrieved chunks a person may receive, with what the index does not know. FilterAsync(AIDataSourceResultFilterContext, IReadOnlyList<DataSourceSearchResult>) gets User, Resource (the AI profile or chat interaction), ProfileId, DataSourceId, IndexProfile and CancellationToken, and returns the chunks to keep. Learning AI uses it to keep exam content away from models and to scope a tutor to the learner's enrollments.

public sealed class InternalOnlyResultFilter : IAIDataSourceResultFilter
{
    public Task<IReadOnlyList<DataSourceSearchResult>> FilterAsync(
        AIDataSourceResultFilterContext context,
        IReadOnlyList<DataSourceSearchResult> results)
    {
        if (context.User?.Identity?.IsAuthenticated == true)
        {
            return Task.FromResult(results);
        }

        IReadOnlyList<DataSourceSearchResult> kept = results
            .Where(result => result.ReferenceType != "InternalArticle")
            .ToList();

        return Task.FromResult(kept);
    }
}

// In the module's startup:
services.AddAIDataSourceResultFilter<InternalOnlyResultFilter>();

Extend with a custom source type

A source type is a keyed IAIDataSourceSourceHandler, an entry in AIDataSourceSourceOptions, and a display driver for its editor section. Opt into the shared field mapping with AIDataSourceFieldMappingOptions.Require:

public sealed class Startup : StartupBase
{
    internal readonly IStringLocalizer S;

    public Startup(IStringLocalizer<Startup> stringLocalizer)
    {
        S = stringLocalizer;
    }

    public override void ConfigureServices(IServiceCollection services)
    {
        services.AddKeyedScoped<IAIDataSourceSourceHandler, CrmAIDataSourceSourceHandler>("Crm");
        services.AddDisplayDriver<AIDataSource, CrmAIDataSourceDisplayDriver>();
        services.Configure<AIDataSourceFieldMappingOptions>(options => options.Require("Crm"));
        services.Configure<AIDataSourceSourceOptions>(options => options.AddOrUpdate(
            "Crm",
            S["CRM"],
            S["Read source documents from the CRM."]));
    }
}

The handler returns its key from SourceType, validates its settings (ValidateAsync), reports the reference type (GetReferenceTypeAsync), and streams documents (ReadAsync, ReadByIdsAsync). Follow the PostgreSQL handler: store settings as a metadata object on the data source, protect secrets with IDataProtectionProvider and AIDataSourceProtectionConstants.SourceSecretPurpose, and show the driver only when dataSource.Source matches. The shared contracts are covered in crestapps-core-data-sources.

Admin list

Artificial Intelligence > Data Sources renders through the Orchard Core AdminList shape under the list name AIDataSources, with the grid columns Name, Indexes, Modified, and Actions. Each row is the AIDataSource shape in the SummaryAdmin (List) and SummaryAdminGrid (Grid) display types. A driver that adds to a row must place its shape for both display types; add a column with an IAdminListColumnsProvider for AIDataSources. Mechanics are in orchardcore-admin-lists.

Permissions

ManageAIDataSources (Administrator by default) protects the data source screens. The Data Sources settings section is part of the AI settings group, guarded by ManageAIProfiles.

Upgrading from 2.x

Troubleshooting

| Symptom | Check |

|---|---|

| No knowledge base index type in Search > Indexing | Enable the Elasticsearch or Azure AI Search data source feature and its Orchard Core search feature. |

| No embedding deployment to pick | Declare textEmbedding on an embedding deployment. Chat deployments are not offered. |

| Index stays empty | The knowledge base index has no embedding deployment, or the data source was never synced. |

| The selected source index provider cannot read source documents | The source index is Lucene. Use an Elasticsearch or Azure AI Search index. |

| Filter has no effect | The knowledge base is Azure AI Search, or the string needs .keyword on Elasticsearch. |

| External source misses changes | External sources sync only on Sync index, on save, or nightly. |

| A content item is missing from the knowledge base | It is restricted (an IIndexedDocumentVisibility, such as Content Access Control, says it is not for everyone), or the index key is not the content item id. |

| Monitoring syncs | crestapps.ai.datasource.sync.duration (outcome partial when some documents failed) and crestapps.ai.datasource.documents on the CrestApps.OrchardCore.AI meter; see orchardcore-observability. |

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.