import { OllamaEmbeddingService } from "../../embedding/ollama"; import arrow from 'apache-arrow' import { embedding } from "@lancedb/lancedb"; import { FunctionOptions } from "@lancedb/lancedb/dist/embedding/embedding_function"; import { EmbeddingAPIType } from "../../../types/embedding"; import { IEmbeddings } from "../../../interfaces/IEmbedding"; import { OpenAIEmbeddingService } from "../../embedding/openai"; @embedding.register("reai_embedding") export class ReAIEmbeddingFunction extends embedding.EmbeddingFunction { type: EmbeddingAPIType baseUrl: string model: string apiKey: string #ndims!: number; constructor(options?: FunctionOptions) { super() this.type = options?.type || EmbeddingAPIType.OLLAMA this.model = options?.model || "milkey/m3e:latest" this.baseUrl = options?.baseUrl || "http://127.0.0.1:11434" this.apiKey = options?.apiKey || "" this.#ndims = options?.ndims || 1536 if (this.model.indexOf("m3e")) { this.#ndims = 1024 } else if (this.model.indexOf("gte")) { this.#ndims = 1024 } } toJSON(): object { return { type: this.type, model: this.model, baseUrl: this.baseUrl }; } ndims() { return this.#ndims } embeddingDataType(): arrow.Float { return new arrow.Float32(); } async computeQueryEmbeddings(_data: string) { // This is a placeholder for a real embedding function const api = this.getEmbeddingAPI(this.type) const ret = await api.getEmbeddings(_data) return ret[0] } async computeSourceEmbeddings(data: string[]) { // This is a placeholder for a real embedding function // return Array.from({ length: data.length }).fill([1, 2, 3]) as number[][]; const api = this.getEmbeddingAPI(this.type) const ret = await api.getEmbeddings(data) return ret } getEmbeddingAPI(type: EmbeddingAPIType) : IEmbeddings { if (type === EmbeddingAPIType.OPENAI) { return new OpenAIEmbeddingService({ baseUrl: this.baseUrl, model: this.model, apiKey: this.apiKey }) } else if (type === EmbeddingAPIType.OLLAMA) { return new OllamaEmbeddingService({ baseUrl: this.baseUrl, model: this.model }) } throw new Error("Embedding API not found") } }