repositories
loading repo index
repositories
loading repo index
repository
loading code, commits, and activity
public Clawd ADK gateway launch mirror
stars
latest
clone command
git clone gitlawb://did:key:z6Mkq5mY...iFZ5/my-project-publ...git clone gitlawb://did:key:z6Mkq5mY.../my-project-publ...2fa351d6docs: add automaton and perps launch sources16d ago| #1 | import { Ollama } from "ollama"; |
| #2 | import { Embedder } from "./base"; |
| #3 | import { EmbeddingConfig } from "../types"; |
| #4 | import { logger } from "../utils/logger"; |
| #5 | |
| #6 | export class OllamaEmbedder implements Embedder { |
| #7 | private ollama: Ollama; |
| #8 | private model: string; |
| #9 | private embeddingDims?: number; |
| #10 | // Using this variable to avoid calling the Ollama server multiple times |
| #11 | private initialized: boolean = false; |
| #12 | |
| #13 | constructor(config: EmbeddingConfig) { |
| #14 | this.ollama = new Ollama({ |
| #15 | host: config.url || "http://localhost:11434", |
| #16 | }); |
| #17 | this.model = config.model || "nomic-embed-text:latest"; |
| #18 | this.embeddingDims = config.embeddingDims || 768; |
| #19 | this.ensureModelExists().catch((err) => { |
| #20 | logger.error(`Error ensuring model exists: ${err}`); |
| #21 | }); |
| #22 | } |
| #23 | |
| #24 | async embed(text: string): Promise<number[]> { |
| #25 | try { |
| #26 | await this.ensureModelExists(); |
| #27 | } catch (err) { |
| #28 | logger.error(`Error ensuring model exists: ${err}`); |
| #29 | } |
| #30 | // Ollama's Go server requires prompt to be a string. Coerce defensively |
| #31 | // since callers may pass values parsed from untrusted LLM JSON output. |
| #32 | const prompt = typeof text === "string" ? text : JSON.stringify(text); |
| #33 | const response = await this.ollama.embeddings({ |
| #34 | model: this.model, |
| #35 | prompt, |
| #36 | }); |
| #37 | return response.embedding; |
| #38 | } |
| #39 | |
| #40 | async embedBatch(texts: string[]): Promise<number[][]> { |
| #41 | const response = await Promise.all(texts.map((text) => this.embed(text))); |
| #42 | return response; |
| #43 | } |
| #44 | |
| #45 | private async ensureModelExists(): Promise<boolean> { |
| #46 | if (this.initialized) { |
| #47 | return true; |
| #48 | } |
| #49 | const local_models = await this.ollama.list(); |
| #50 | if (!local_models.models.find((m: any) => m.name === this.model)) { |
| #51 | logger.info(`Pulling model ${this.model}...`); |
| #52 | await this.ollama.pull({ model: this.model }); |
| #53 | } |
| #54 | this.initialized = true; |
| #55 | return true; |
| #56 | } |
| #57 | } |
| #58 |