@retrivora-ai/rag-engine 0.2.2 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -18,6 +18,8 @@ interface ChatOptions {
18
18
  interface EmbedOptions {
19
19
  /** Override model for this specific embed call */
20
20
  model?: string;
21
+ /** Specify the task type for models that require prefixes (e.g. nomic-embed-text) */
22
+ taskType?: 'query' | 'document';
21
23
  }
22
24
  interface ILLMProvider {
23
25
  /**
@@ -18,6 +18,8 @@ interface ChatOptions {
18
18
  interface EmbedOptions {
19
19
  /** Override model for this specific embed call */
20
20
  model?: string;
21
+ /** Specify the task type for models that require prefixes (e.g. nomic-embed-text) */
22
+ taskType?: 'query' | 'document';
21
23
  }
22
24
  interface ILLMProvider {
23
25
  /**
@@ -85,6 +85,10 @@ interface EmbeddingConfig {
85
85
  baseUrl?: string;
86
86
  /** Output vector dimension — must match the index dimension */
87
87
  dimensions?: number;
88
+ /** Optional prefix to prepend to queries (for models like nomic-embed-text) */
89
+ queryPrefix?: string;
90
+ /** Optional prefix to prepend to documents during ingestion */
91
+ documentPrefix?: string;
88
92
  /** Provider-specific options for custom adapters */
89
93
  options?: Record<string, unknown>;
90
94
  }
@@ -85,6 +85,10 @@ interface EmbeddingConfig {
85
85
  baseUrl?: string;
86
86
  /** Output vector dimension — must match the index dimension */
87
87
  dimensions?: number;
88
+ /** Optional prefix to prepend to queries (for models like nomic-embed-text) */
89
+ queryPrefix?: string;
90
+ /** Optional prefix to prepend to documents during ingestion */
91
+ documentPrefix?: string;
88
92
  /** Provider-specific options for custom adapters */
89
93
  options?: Record<string, unknown>;
90
94
  }
@@ -130,6 +130,8 @@ function getRagConfig(env = process.env) {
130
130
  apiKey: embeddingApiKeyByProvider[embeddingProvider],
131
131
  baseUrl: readString(env, "EMBEDDING_BASE_URL"),
132
132
  dimensions: embeddingDimensions,
133
+ queryPrefix: readString(env, "EMBEDDING_QUERY_PREFIX"),
134
+ documentPrefix: readString(env, "EMBEDDING_DOCUMENT_PREFIX"),
133
135
  options: {
134
136
  profile: readString(env, "EMBEDDING_UNIVERSAL_PROFILE")
135
137
  }
@@ -903,13 +905,25 @@ ${context}`;
903
905
  return data.message.content;
904
906
  }
905
907
  async embed(text, options) {
906
- var _a, _b, _c, _d, _e, _f, _g;
908
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i;
907
909
  const model = (_c = (_b = options == null ? void 0 : options.model) != null ? _b : (_a = this.embeddingConfig) == null ? void 0 : _a.model) != null ? _c : "nomic-embed-text";
908
910
  const baseURL = (_f = (_e = (_d = this.embeddingConfig) == null ? void 0 : _d.baseUrl) != null ? _e : this.llmConfig.baseUrl) != null ? _f : "http://localhost:11434";
909
911
  const client = baseURL !== ((_g = this.llmConfig.baseUrl) != null ? _g : "http://localhost:11434") ? axios.create({ baseURL, timeout: 6e4 }) : this.http;
912
+ let prompt = text;
913
+ const queryPrefix = (_h = this.embeddingConfig) == null ? void 0 : _h.queryPrefix;
914
+ const docPrefix = (_i = this.embeddingConfig) == null ? void 0 : _i.documentPrefix;
915
+ if ((options == null ? void 0 : options.taskType) === "query" && queryPrefix) {
916
+ if (!prompt.startsWith(queryPrefix)) {
917
+ prompt = `${queryPrefix}${text}`;
918
+ }
919
+ } else if ((options == null ? void 0 : options.taskType) === "document" && docPrefix) {
920
+ if (!prompt.startsWith(docPrefix)) {
921
+ prompt = `${docPrefix}${text}`;
922
+ }
923
+ }
910
924
  const { data } = await client.post("/api/embeddings", {
911
925
  model,
912
- prompt: text
926
+ prompt
913
927
  });
914
928
  return data.embedding;
915
929
  }
@@ -1582,7 +1596,7 @@ var Pipeline = class {
1582
1596
  };
1583
1597
  const vectors = await BatchProcessor.mapWithRetry(
1584
1598
  chunks.map((c) => c.content),
1585
- (text) => this.embeddingProvider.embed(text),
1599
+ (text) => this.embeddingProvider.embed(text, { taskType: "document" }),
1586
1600
  embedBatchOptions
1587
1601
  );
1588
1602
  if (vectors.length !== chunks.length) {
@@ -1625,7 +1639,7 @@ var Pipeline = class {
1625
1639
  const topK = (_b = (_a = this.config.rag) == null ? void 0 : _a.topK) != null ? _b : 5;
1626
1640
  const scoreThreshold = (_d = (_c = this.config.rag) == null ? void 0 : _c.scoreThreshold) != null ? _d : 0;
1627
1641
  try {
1628
- const queryVector = await this.embeddingProvider.embed(question);
1642
+ const queryVector = await this.embeddingProvider.embed(question, { taskType: "query" });
1629
1643
  const rawMatches = await this.vectorDB.query(queryVector, topK, ns);
1630
1644
  const sources = rawMatches.filter((m) => m.score >= scoreThreshold);
1631
1645
  const context = sources.length ? sources.map((m, i) => `[Source ${i + 1}]
@@ -1,3 +1,3 @@
1
- import '../RagConfig-D_rSf8ep.mjs';
2
- export { c as createChatHandler, a as createHealthHandler, b as createIngestHandler, d as createUploadHandler } from '../index-BJ8CUArE.mjs';
1
+ import '../RagConfig-Ttch1N4d.mjs';
2
+ export { c as createChatHandler, a as createHealthHandler, b as createIngestHandler, d as createUploadHandler } from '../index-sbCtrIRT.mjs';
3
3
  import 'next/server';
@@ -1,3 +1,3 @@
1
- import '../RagConfig-D_rSf8ep.js';
2
- export { c as createChatHandler, a as createHealthHandler, b as createIngestHandler, d as createUploadHandler } from '../index-DtNprGGj.js';
1
+ import '../RagConfig-Ttch1N4d.js';
2
+ export { c as createChatHandler, a as createHealthHandler, b as createIngestHandler, d as createUploadHandler } from '../index-rK0KAr2S.js';
3
3
  import 'next/server';
@@ -1284,6 +1284,8 @@ function getRagConfig(env = process.env) {
1284
1284
  apiKey: embeddingApiKeyByProvider[embeddingProvider],
1285
1285
  baseUrl: readString(env, "EMBEDDING_BASE_URL"),
1286
1286
  dimensions: embeddingDimensions,
1287
+ queryPrefix: readString(env, "EMBEDDING_QUERY_PREFIX"),
1288
+ documentPrefix: readString(env, "EMBEDDING_DOCUMENT_PREFIX"),
1287
1289
  options: {
1288
1290
  profile: readString(env, "EMBEDDING_UNIVERSAL_PROFILE")
1289
1291
  }
@@ -2057,13 +2059,25 @@ ${context}`;
2057
2059
  return data.message.content;
2058
2060
  }
2059
2061
  async embed(text, options) {
2060
- var _a, _b, _c, _d, _e, _f, _g;
2062
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i;
2061
2063
  const model = (_c = (_b = options == null ? void 0 : options.model) != null ? _b : (_a = this.embeddingConfig) == null ? void 0 : _a.model) != null ? _c : "nomic-embed-text";
2062
2064
  const baseURL = (_f = (_e = (_d = this.embeddingConfig) == null ? void 0 : _d.baseUrl) != null ? _e : this.llmConfig.baseUrl) != null ? _f : "http://localhost:11434";
2063
2065
  const client = baseURL !== ((_g = this.llmConfig.baseUrl) != null ? _g : "http://localhost:11434") ? import_axios.default.create({ baseURL, timeout: 6e4 }) : this.http;
2066
+ let prompt = text;
2067
+ const queryPrefix = (_h = this.embeddingConfig) == null ? void 0 : _h.queryPrefix;
2068
+ const docPrefix = (_i = this.embeddingConfig) == null ? void 0 : _i.documentPrefix;
2069
+ if ((options == null ? void 0 : options.taskType) === "query" && queryPrefix) {
2070
+ if (!prompt.startsWith(queryPrefix)) {
2071
+ prompt = `${queryPrefix}${text}`;
2072
+ }
2073
+ } else if ((options == null ? void 0 : options.taskType) === "document" && docPrefix) {
2074
+ if (!prompt.startsWith(docPrefix)) {
2075
+ prompt = `${docPrefix}${text}`;
2076
+ }
2077
+ }
2064
2078
  const { data } = await client.post("/api/embeddings", {
2065
2079
  model,
2066
- prompt: text
2080
+ prompt
2067
2081
  });
2068
2082
  return data.embedding;
2069
2083
  }
@@ -2692,7 +2706,7 @@ var Pipeline = class {
2692
2706
  };
2693
2707
  const vectors = await BatchProcessor.mapWithRetry(
2694
2708
  chunks.map((c) => c.content),
2695
- (text) => this.embeddingProvider.embed(text),
2709
+ (text) => this.embeddingProvider.embed(text, { taskType: "document" }),
2696
2710
  embedBatchOptions
2697
2711
  );
2698
2712
  if (vectors.length !== chunks.length) {
@@ -2735,7 +2749,7 @@ var Pipeline = class {
2735
2749
  const topK = (_b = (_a = this.config.rag) == null ? void 0 : _a.topK) != null ? _b : 5;
2736
2750
  const scoreThreshold = (_d = (_c = this.config.rag) == null ? void 0 : _c.scoreThreshold) != null ? _d : 0;
2737
2751
  try {
2738
- const queryVector = await this.embeddingProvider.embed(question);
2752
+ const queryVector = await this.embeddingProvider.embed(question, { taskType: "query" });
2739
2753
  const rawMatches = await this.vectorDB.query(queryVector, topK, ns);
2740
2754
  const sources = rawMatches.filter((m) => m.score >= scoreThreshold);
2741
2755
  const context = sources.length ? sources.map((m, i) => `[Source ${i + 1}]
@@ -3,7 +3,7 @@ import {
3
3
  createHealthHandler,
4
4
  createIngestHandler,
5
5
  createUploadHandler
6
- } from "../chunk-2VR5ZMXV.mjs";
6
+ } from "../chunk-K53N7SEE.mjs";
7
7
  import "../chunk-EDLTMSNY.mjs";
8
8
  import "../chunk-FWCSY2DS.mjs";
9
9
  export {
@@ -1,4 +1,4 @@
1
- import { c as VectorDBConfig, L as LLMConfig, E as EmbeddingConfig, a as RagConfig, C as ChatResponse } from './RagConfig-D_rSf8ep.js';
1
+ import { c as VectorDBConfig, L as LLMConfig, E as EmbeddingConfig, a as RagConfig, C as ChatResponse } from './RagConfig-Ttch1N4d.js';
2
2
  import { NextRequest, NextResponse } from 'next/server';
3
3
 
4
4
  /**
@@ -1,4 +1,4 @@
1
- import { c as VectorDBConfig, L as LLMConfig, E as EmbeddingConfig, a as RagConfig, C as ChatResponse } from './RagConfig-D_rSf8ep.mjs';
1
+ import { c as VectorDBConfig, L as LLMConfig, E as EmbeddingConfig, a as RagConfig, C as ChatResponse } from './RagConfig-Ttch1N4d.mjs';
2
2
  import { NextRequest, NextResponse } from 'next/server';
3
3
 
4
4
  /**
package/dist/index.d.mts CHANGED
@@ -1,9 +1,9 @@
1
1
  import * as react_jsx_runtime from 'react/jsx-runtime';
2
2
  import React$1, { ReactNode } from 'react';
3
- import { C as ChatMessage } from './DocumentChunker-BEyzadsv.mjs';
4
- export { a as ChatOptions, b as Chunk, c as ChunkOptions, E as EmbedOptions, I as ILLMProvider } from './DocumentChunker-BEyzadsv.mjs';
5
- import { V as VectorMatch, U as UIConfig } from './RagConfig-D_rSf8ep.mjs';
6
- export { C as ChatResponse, E as EmbeddingConfig, I as IngestDocument, L as LLMConfig, R as RAGConfig, a as RagConfig, b as UpsertDocument, c as VectorDBConfig } from './RagConfig-D_rSf8ep.mjs';
3
+ import { C as ChatMessage } from './DocumentChunker-BICIjSuG.mjs';
4
+ export { a as ChatOptions, b as Chunk, c as ChunkOptions, E as EmbedOptions, I as ILLMProvider } from './DocumentChunker-BICIjSuG.mjs';
5
+ import { V as VectorMatch, U as UIConfig } from './RagConfig-Ttch1N4d.mjs';
6
+ export { C as ChatResponse, E as EmbeddingConfig, I as IngestDocument, L as LLMConfig, R as RAGConfig, a as RagConfig, b as UpsertDocument, c as VectorDBConfig } from './RagConfig-Ttch1N4d.mjs';
7
7
 
8
8
  interface ChatWidgetProps {
9
9
  /** Position of the floating button. Defaults to bottom-right. */
package/dist/index.d.ts CHANGED
@@ -1,9 +1,9 @@
1
1
  import * as react_jsx_runtime from 'react/jsx-runtime';
2
2
  import React$1, { ReactNode } from 'react';
3
- import { C as ChatMessage } from './DocumentChunker-BEyzadsv.js';
4
- export { a as ChatOptions, b as Chunk, c as ChunkOptions, E as EmbedOptions, I as ILLMProvider } from './DocumentChunker-BEyzadsv.js';
5
- import { V as VectorMatch, U as UIConfig } from './RagConfig-D_rSf8ep.js';
6
- export { C as ChatResponse, E as EmbeddingConfig, I as IngestDocument, L as LLMConfig, R as RAGConfig, a as RagConfig, b as UpsertDocument, c as VectorDBConfig } from './RagConfig-D_rSf8ep.js';
3
+ import { C as ChatMessage } from './DocumentChunker-BICIjSuG.js';
4
+ export { a as ChatOptions, b as Chunk, c as ChunkOptions, E as EmbedOptions, I as ILLMProvider } from './DocumentChunker-BICIjSuG.js';
5
+ import { V as VectorMatch, U as UIConfig } from './RagConfig-Ttch1N4d.js';
6
+ export { C as ChatResponse, E as EmbeddingConfig, I as IngestDocument, L as LLMConfig, R as RAGConfig, a as RagConfig, b as UpsertDocument, c as VectorDBConfig } from './RagConfig-Ttch1N4d.js';
7
7
 
8
8
  interface ChatWidgetProps {
9
9
  /** Position of the floating button. Defaults to bottom-right. */
package/dist/server.d.mts CHANGED
@@ -1,9 +1,9 @@
1
- import { a as RagConfig, C as ChatResponse, I as IngestDocument, c as VectorDBConfig, b as UpsertDocument, V as VectorMatch, L as LLMConfig, E as EmbeddingConfig, d as VectorDBProvider, e as LLMProvider, f as EmbeddingProvider } from './RagConfig-D_rSf8ep.mjs';
2
- export { R as RAGConfig, U as UIConfig } from './RagConfig-D_rSf8ep.mjs';
3
- import { C as ChatMessage, I as ILLMProvider, a as ChatOptions, E as EmbedOptions } from './DocumentChunker-BEyzadsv.mjs';
4
- export { b as Chunk, c as ChunkOptions, D as DocumentChunker } from './DocumentChunker-BEyzadsv.mjs';
5
- import { H as HealthCheckResult } from './index-BJ8CUArE.mjs';
6
- export { P as ProviderHealthCheck, c as createChatHandler, a as createHealthHandler, b as createIngestHandler, d as createUploadHandler } from './index-BJ8CUArE.mjs';
1
+ import { a as RagConfig, C as ChatResponse, I as IngestDocument, c as VectorDBConfig, b as UpsertDocument, V as VectorMatch, L as LLMConfig, E as EmbeddingConfig, d as VectorDBProvider, e as LLMProvider, f as EmbeddingProvider } from './RagConfig-Ttch1N4d.mjs';
2
+ export { R as RAGConfig, U as UIConfig } from './RagConfig-Ttch1N4d.mjs';
3
+ import { C as ChatMessage, I as ILLMProvider, a as ChatOptions, E as EmbedOptions } from './DocumentChunker-BICIjSuG.mjs';
4
+ export { b as Chunk, c as ChunkOptions, D as DocumentChunker } from './DocumentChunker-BICIjSuG.mjs';
5
+ import { H as HealthCheckResult } from './index-sbCtrIRT.mjs';
6
+ export { P as ProviderHealthCheck, c as createChatHandler, a as createHealthHandler, b as createIngestHandler, d as createUploadHandler } from './index-sbCtrIRT.mjs';
7
7
  import 'next/server';
8
8
 
9
9
  /**
package/dist/server.d.ts CHANGED
@@ -1,9 +1,9 @@
1
- import { a as RagConfig, C as ChatResponse, I as IngestDocument, c as VectorDBConfig, b as UpsertDocument, V as VectorMatch, L as LLMConfig, E as EmbeddingConfig, d as VectorDBProvider, e as LLMProvider, f as EmbeddingProvider } from './RagConfig-D_rSf8ep.js';
2
- export { R as RAGConfig, U as UIConfig } from './RagConfig-D_rSf8ep.js';
3
- import { C as ChatMessage, I as ILLMProvider, a as ChatOptions, E as EmbedOptions } from './DocumentChunker-BEyzadsv.js';
4
- export { b as Chunk, c as ChunkOptions, D as DocumentChunker } from './DocumentChunker-BEyzadsv.js';
5
- import { H as HealthCheckResult } from './index-DtNprGGj.js';
6
- export { P as ProviderHealthCheck, c as createChatHandler, a as createHealthHandler, b as createIngestHandler, d as createUploadHandler } from './index-DtNprGGj.js';
1
+ import { a as RagConfig, C as ChatResponse, I as IngestDocument, c as VectorDBConfig, b as UpsertDocument, V as VectorMatch, L as LLMConfig, E as EmbeddingConfig, d as VectorDBProvider, e as LLMProvider, f as EmbeddingProvider } from './RagConfig-Ttch1N4d.js';
2
+ export { R as RAGConfig, U as UIConfig } from './RagConfig-Ttch1N4d.js';
3
+ import { C as ChatMessage, I as ILLMProvider, a as ChatOptions, E as EmbedOptions } from './DocumentChunker-BICIjSuG.js';
4
+ export { b as Chunk, c as ChunkOptions, D as DocumentChunker } from './DocumentChunker-BICIjSuG.js';
5
+ import { H as HealthCheckResult } from './index-rK0KAr2S.js';
6
+ export { P as ProviderHealthCheck, c as createChatHandler, a as createHealthHandler, b as createIngestHandler, d as createUploadHandler } from './index-rK0KAr2S.js';
7
7
  import 'next/server';
8
8
 
9
9
  /**
package/dist/server.js CHANGED
@@ -1328,6 +1328,8 @@ function getRagConfig(env = process.env) {
1328
1328
  apiKey: embeddingApiKeyByProvider[embeddingProvider],
1329
1329
  baseUrl: readString(env, "EMBEDDING_BASE_URL"),
1330
1330
  dimensions: embeddingDimensions,
1331
+ queryPrefix: readString(env, "EMBEDDING_QUERY_PREFIX"),
1332
+ documentPrefix: readString(env, "EMBEDDING_DOCUMENT_PREFIX"),
1331
1333
  options: {
1332
1334
  profile: readString(env, "EMBEDDING_UNIVERSAL_PROFILE")
1333
1335
  }
@@ -2101,13 +2103,25 @@ ${context}`;
2101
2103
  return data.message.content;
2102
2104
  }
2103
2105
  async embed(text, options) {
2104
- var _a, _b, _c, _d, _e, _f, _g;
2106
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i;
2105
2107
  const model = (_c = (_b = options == null ? void 0 : options.model) != null ? _b : (_a = this.embeddingConfig) == null ? void 0 : _a.model) != null ? _c : "nomic-embed-text";
2106
2108
  const baseURL = (_f = (_e = (_d = this.embeddingConfig) == null ? void 0 : _d.baseUrl) != null ? _e : this.llmConfig.baseUrl) != null ? _f : "http://localhost:11434";
2107
2109
  const client = baseURL !== ((_g = this.llmConfig.baseUrl) != null ? _g : "http://localhost:11434") ? import_axios.default.create({ baseURL, timeout: 6e4 }) : this.http;
2110
+ let prompt = text;
2111
+ const queryPrefix = (_h = this.embeddingConfig) == null ? void 0 : _h.queryPrefix;
2112
+ const docPrefix = (_i = this.embeddingConfig) == null ? void 0 : _i.documentPrefix;
2113
+ if ((options == null ? void 0 : options.taskType) === "query" && queryPrefix) {
2114
+ if (!prompt.startsWith(queryPrefix)) {
2115
+ prompt = `${queryPrefix}${text}`;
2116
+ }
2117
+ } else if ((options == null ? void 0 : options.taskType) === "document" && docPrefix) {
2118
+ if (!prompt.startsWith(docPrefix)) {
2119
+ prompt = `${docPrefix}${text}`;
2120
+ }
2121
+ }
2108
2122
  const { data } = await client.post("/api/embeddings", {
2109
2123
  model,
2110
- prompt: text
2124
+ prompt
2111
2125
  });
2112
2126
  return data.embedding;
2113
2127
  }
@@ -2781,7 +2795,7 @@ var Pipeline = class {
2781
2795
  };
2782
2796
  const vectors = await BatchProcessor.mapWithRetry(
2783
2797
  chunks.map((c) => c.content),
2784
- (text) => this.embeddingProvider.embed(text),
2798
+ (text) => this.embeddingProvider.embed(text, { taskType: "document" }),
2785
2799
  embedBatchOptions
2786
2800
  );
2787
2801
  if (vectors.length !== chunks.length) {
@@ -2824,7 +2838,7 @@ var Pipeline = class {
2824
2838
  const topK = (_b = (_a = this.config.rag) == null ? void 0 : _a.topK) != null ? _b : 5;
2825
2839
  const scoreThreshold = (_d = (_c = this.config.rag) == null ? void 0 : _c.scoreThreshold) != null ? _d : 0;
2826
2840
  try {
2827
- const queryVector = await this.embeddingProvider.embed(question);
2841
+ const queryVector = await this.embeddingProvider.embed(question, { taskType: "query" });
2828
2842
  const rawMatches = await this.vectorDB.query(queryVector, topK, ns);
2829
2843
  const sources = rawMatches.filter((m) => m.score >= scoreThreshold);
2830
2844
  const context = sources.length ? sources.map((m, i) => `[Source ${i + 1}]
package/dist/server.mjs CHANGED
@@ -34,7 +34,7 @@ import {
34
34
  createIngestHandler,
35
35
  createUploadHandler,
36
36
  getRagConfig
37
- } from "./chunk-2VR5ZMXV.mjs";
37
+ } from "./chunk-K53N7SEE.mjs";
38
38
  import "./chunk-EDLTMSNY.mjs";
39
39
  import {
40
40
  PineconeProvider
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@retrivora-ai/rag-engine",
3
- "version": "0.2.2",
3
+ "version": "0.2.3",
4
4
  "description": "Retrivora AI is a plug-and-play AI engine for RAG chat experiences — generic vector DB + LLM provider, embeddable or standalone.",
5
5
  "author": "Abhinav Alkuchi",
6
6
  "license": "MIT",
@@ -81,6 +81,10 @@ export interface EmbeddingConfig {
81
81
  baseUrl?: string;
82
82
  /** Output vector dimension — must match the index dimension */
83
83
  dimensions?: number;
84
+ /** Optional prefix to prepend to queries (for models like nomic-embed-text) */
85
+ queryPrefix?: string;
86
+ /** Optional prefix to prepend to documents during ingestion */
87
+ documentPrefix?: string;
84
88
  /** Provider-specific options for custom adapters */
85
89
  options?: Record<string, unknown>;
86
90
  }
@@ -150,6 +150,8 @@ export function getRagConfig(env: Record<string, string | undefined> = process.e
150
150
  apiKey: embeddingApiKeyByProvider[embeddingProvider],
151
151
  baseUrl: readString(env, 'EMBEDDING_BASE_URL'),
152
152
  dimensions: embeddingDimensions,
153
+ queryPrefix: readString(env, 'EMBEDDING_QUERY_PREFIX'),
154
+ documentPrefix: readString(env, 'EMBEDDING_DOCUMENT_PREFIX'),
153
155
  options: {
154
156
  profile: readString(env, 'EMBEDDING_UNIVERSAL_PROFILE'),
155
157
  },
@@ -82,7 +82,7 @@ export class Pipeline {
82
82
 
83
83
  const vectors = await BatchProcessor.mapWithRetry(
84
84
  chunks.map(c => c.content),
85
- (text) => this.embeddingProvider.embed(text),
85
+ (text) => this.embeddingProvider.embed(text, { taskType: 'document' }),
86
86
  embedBatchOptions
87
87
  );
88
88
 
@@ -134,7 +134,7 @@ export class Pipeline {
134
134
  const scoreThreshold = this.config.rag?.scoreThreshold ?? 0.0;
135
135
 
136
136
  try {
137
- const queryVector = await this.embeddingProvider.embed(question);
137
+ const queryVector = await this.embeddingProvider.embed(question, { taskType: 'query' });
138
138
  const rawMatches = await this.vectorDB.query(queryVector, topK, ns);
139
139
 
140
140
  const sources = rawMatches.filter((m) => m.score >= scoreThreshold);
@@ -21,6 +21,8 @@ export interface ChatOptions {
21
21
  export interface EmbedOptions {
22
22
  /** Override model for this specific embed call */
23
23
  model?: string;
24
+ /** Specify the task type for models that require prefixes (e.g. nomic-embed-text) */
25
+ taskType?: 'query' | 'document';
24
26
  }
25
27
 
26
28
  export interface ILLMProvider {
@@ -73,9 +73,25 @@ export class OllamaProvider implements ILLMProvider {
73
73
  ? axios.create({ baseURL, timeout: 60_000 })
74
74
  : this.http;
75
75
 
76
+ let prompt = text;
77
+
78
+ // Dynamically apply prefixes from configuration if they exist
79
+ const queryPrefix = this.embeddingConfig?.queryPrefix;
80
+ const docPrefix = this.embeddingConfig?.documentPrefix;
81
+
82
+ if (options?.taskType === 'query' && queryPrefix) {
83
+ if (!prompt.startsWith(queryPrefix)) {
84
+ prompt = `${queryPrefix}${text}`;
85
+ }
86
+ } else if (options?.taskType === 'document' && docPrefix) {
87
+ if (!prompt.startsWith(docPrefix)) {
88
+ prompt = `${docPrefix}${text}`;
89
+ }
90
+ }
91
+
76
92
  const { data } = await client.post<OllamaEmbedResponse>('/api/embeddings', {
77
93
  model,
78
- prompt: text,
94
+ prompt,
79
95
  });
80
96
 
81
97
  return data.embedding;