# IMentorRagSource interface

> The IMentorRagSource interface of Novior.MentorAgent.Rag: Contract for RAG (Retrieval-Augmented Generation) sources in MentorAgent. Implement this interface…

- URL: https://docs.mentoragent.net/api/novior-mentoragent-rag/imentorragsource
- Verified with: MentorAgent 1.2.0
- Last updated: 2026-10-27
- Packages: Novior.MentorAgent

Contract for RAG (Retrieval-Augmented Generation) sources in MentorAgent. Implement this interface to connect any vector store or search backend (Azure AI Search, Qdrant, Pinecone, Weaviate, custom, etc.) to the AI coordinator. **Basic custom implementation:**

```text title="Example"
public class MyRagSource : IMentorRagSource
{
    public async Task<IReadOnlyList<MentorRagResult>> SearchAsync(
        string query, int maxResults, CancellationToken ct = default)
    {
        var hits = await _myStore.SearchAsync(query, maxResults, ct);
        return hits.Select(h => new MentorRagResult(h.Text, h.Url, h.Title, h.Score)).ToList();
    }
}

// Register in Program.cs BEFORE AddMentorAgent():
builder.Services.AddSingleton<IMentorRagSource, MyRagSource>();
builder.Services.AddMentorAgent(options => { options.UseRag = true; });
```

An `ITextSearch` is not wrapped for you (`TextSearchRagSource` is a placeholder): call your search client from your own implementation, as above. **Who is asking, and from where:** implement the [`MentorRagRequest`](https://docs.mentoragent.net/api/novior-mentoragent-rag/mentorragrequest) overload as well. MentorAgent calls that one; its default implementation calls the text-only overload, so a source written for that keeps working unchanged. A source with cosine scores in \[0, 1\] declares its own threshold with [`IMentorRagSource.DefaultMinScore`](https://docs.mentoragent.net/api/novior-mentoragent-rag/imentorragsource#defaultminscore):

```text title="Example"
public class MyVectorSource : IMentorRagSource
{
    public float? DefaultMinScore => 0.6f;   // cosine similarity: 0.5 loose … 0.75 strict

    public Task<IReadOnlyList<MentorRagResult>> SearchAsync(
        MentorRagRequest request, CancellationToken ct = default)
        => SearchCoreAsync(request.Query, request.MaxResults, request.User, ct);

    // Still required by the interface; MentorAgent itself calls the overload above.
    public Task<IReadOnlyList<MentorRagResult>> SearchAsync(
        string query, int maxResults, CancellationToken ct = default)
        => SearchCoreAsync(query, maxResults, user: null, ct);
}
```

Do not make each overload call the other: with both delegating, a search never returns.

## Definition

Namespace: `Novior.MentorAgent.Rag`. Package: `Novior.MentorAgent`.

`public interface IMentorRagSource`

```shell
dotnet add package Novior.MentorAgent
```

## Properties

### `DefaultMinScore`

`float? DefaultMinScore { get; }`

The relevance threshold suited to this source's scores, used when the host has not set `RagMinScore` itself. `null` (the default) leaves the option's default, 2, which suits integer-like keyword scores. A source whose scores are cosine similarity in \[0, 1\] — a vector store — declares one in that range (0.5 loose to 0.75 strict), or nothing it returns can clear the default. A `RagMinScore` the host sets, in code or in configuration, wins over it.

## Methods

### `SearchAsync`

`Task<IReadOnlyList<MentorRagResult>> SearchAsync(MentorRagRequest request, CancellationToken ct = default)`

Searches with the whole request: the message, the recent conversation, the user and the current page. This is the overload MentorAgent calls. The default implementation calls [`IMentorRagSource.SearchAsync`](https://docs.mentoragent.net/api/novior-mentoragent-rag/imentorragsource#searchasync) with [`MentorRagRequest.Query`](https://docs.mentoragent.net/api/novior-mentoragent-rag/mentorragrequest#query) and [`MentorRagRequest.MaxResults`](https://docs.mentoragent.net/api/novior-mentoragent-rag/mentorragrequest#maxresults).

**Parameters**

- `request`: What is searched for, and the context it was asked in.
- `ct`: Cancellation token.

**Returns:** List of relevant documents ordered by relevance (most relevant first).

`Task<IReadOnlyList<MentorRagResult>> SearchAsync(string query, int maxResults, CancellationToken ct = default)`

Searches for documents relevant to the given query. Called by the coordinator before each user message is sent to the AI, through the [`MentorRagRequest`](https://docs.mentoragent.net/api/novior-mentoragent-rag/mentorragrequest) overload unless the source replaces that one.

**Parameters**

- `query`: The user's message (or a reformulated search query).
- `maxResults`: Maximum number of results to return.
- `ct`: Cancellation token.

**Returns:** List of relevant documents ordered by relevance (most relevant first).

---

- Previous: [IMentorQueryEmbedding interface](https://docs.mentoragent.net/api/novior-mentoragent-rag/imentorqueryembedding.md)
- Next: [MentorRagRequest class](https://docs.mentoragent.net/api/novior-mentoragent-rag/mentorragrequest.md)
