-
Notifications
You must be signed in to change notification settings - Fork 0
feat(text-embedding): add text embedding function with Ollama #32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| import { createMockContext } from '../../../tests/helpers/mock-context'; | ||
|
|
||
| const mockGenerateEmbedding = jest.fn(); | ||
|
|
||
| jest.mock('@agentic-kit/ollama', () => { | ||
| return { | ||
| __esModule: true, | ||
| default: class OllamaClient { | ||
| constructor() {} | ||
| async generateEmbedding(text: string, model: string) { | ||
| return mockGenerateEmbedding(text, model); | ||
| } | ||
| }, | ||
| }; | ||
| }); | ||
|
|
||
| const loadHandler = () => { | ||
| const mod = require('../handler'); | ||
| return mod.default ?? mod; | ||
| }; | ||
|
|
||
| describe('text-embedding handler', () => { | ||
| beforeEach(() => { | ||
| mockGenerateEmbedding.mockClear(); | ||
| mockGenerateEmbedding.mockResolvedValue([0.1, 0.2, 0.3, 0.4, 0.5]); | ||
| }); | ||
|
|
||
| it('should generate embedding for valid text', async () => { | ||
| const mockEmbedding = [0.1, 0.2, 0.3, 0.4, 0.5]; | ||
| mockGenerateEmbedding.mockResolvedValue(mockEmbedding); | ||
|
|
||
| const handler = loadHandler(); | ||
| const result = await handler( | ||
| { text: 'Hello world' }, | ||
| createMockContext({ env: { OLLAMA_URL: 'http://localhost:11434' } }) | ||
| ); | ||
|
|
||
| expect(result).toEqual({ | ||
| embedding: mockEmbedding, | ||
| dimensions: 5, | ||
| model: 'nomic-embed-text:latest', | ||
| }); | ||
| }); | ||
|
|
||
| it('should use custom model when provided', async () => { | ||
| const mockEmbedding = [0.1, 0.2, 0.3]; | ||
| mockGenerateEmbedding.mockResolvedValue(mockEmbedding); | ||
|
|
||
| const handler = loadHandler(); | ||
| const result = await handler( | ||
| { text: 'Test', model: 'mxbai-embed-large' }, | ||
| createMockContext() | ||
| ); | ||
|
|
||
| expect(result.model).toBe('mxbai-embed-large'); | ||
| expect(mockGenerateEmbedding).toHaveBeenCalledWith('Test', 'mxbai-embed-large'); | ||
| }); | ||
|
|
||
| it('should use EMBEDDING_MODEL env var as default', async () => { | ||
| const mockEmbedding = [0.1, 0.2]; | ||
| mockGenerateEmbedding.mockResolvedValue(mockEmbedding); | ||
|
|
||
| const handler = loadHandler(); | ||
| const result = await handler( | ||
| { text: 'Test' }, | ||
| createMockContext({ env: { EMBEDDING_MODEL: 'all-minilm' } }) | ||
| ); | ||
|
|
||
| expect(result.model).toBe('all-minilm'); | ||
| }); | ||
|
|
||
| it('should throw error when text is missing', async () => { | ||
| const handler = loadHandler(); | ||
| await expect( | ||
| handler({}, createMockContext()) | ||
| ).rejects.toThrow('Missing required param: text'); | ||
| }); | ||
|
|
||
| it('should throw error when text is not a string', async () => { | ||
| const handler = loadHandler(); | ||
| await expect( | ||
| handler({ text: 123 }, createMockContext()) | ||
| ).rejects.toThrow('Missing required param: text'); | ||
| }); | ||
|
|
||
| it('should throw error when text is empty', async () => { | ||
| const handler = loadHandler(); | ||
| await expect( | ||
| handler({ text: '' }, createMockContext()) | ||
| ).rejects.toThrow('Missing required param: text'); | ||
| }); | ||
|
|
||
| it('should return mock embedding in DRY_RUN mode', async () => { | ||
| const handler = loadHandler(); | ||
| const result = await handler( | ||
| { text: 'Test' }, | ||
| createMockContext({ env: { TEXT_EMBEDDING_DRY_RUN: 'true' } }) | ||
| ); | ||
|
|
||
| expect(result).toEqual({ | ||
| embedding: Array(768).fill(0), | ||
| dimensions: 768, | ||
| model: 'dry-run', | ||
| }); | ||
| expect(mockGenerateEmbedding).not.toHaveBeenCalled(); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| { | ||
| "name": "text-embedding", | ||
| "version": "1.0.0", | ||
| "type": "node-graphql", | ||
| "port": 8084, | ||
| "description": "Generate text embeddings using Ollama", | ||
| "dependencies": { | ||
| "@agentic-kit/ollama": "^1.0.3" | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import type { FunctionHandler } from '@constructive-io/fn-runtime'; | ||
| import OllamaClient from '@agentic-kit/ollama'; | ||
|
|
||
| interface Params { | ||
| text: string; | ||
| model?: string; | ||
| } | ||
|
|
||
| interface Result { | ||
| embedding: number[]; | ||
| dimensions: number; | ||
| model: string; | ||
| } | ||
|
|
||
| const handler: FunctionHandler<Params, Result> = async (params, context) => { | ||
| const { log, env } = context; | ||
|
|
||
| const { text, model } = params; | ||
|
|
||
| if (!text || typeof text !== 'string') { | ||
| throw new Error('Missing required param: text'); | ||
| } | ||
|
|
||
| // DRY_RUN mode for CI testing - skip actual Ollama call | ||
| if (env.TEXT_EMBEDDING_DRY_RUN === 'true') { | ||
| log.info('[text-embedding] DRY_RUN mode, returning mock embedding'); | ||
| return { | ||
| embedding: Array(768).fill(0), | ||
| dimensions: 768, | ||
| model: 'dry-run', | ||
| }; | ||
| } | ||
|
|
||
| const ollamaUrl = env.OLLAMA_URL || 'http://localhost:11434'; | ||
| const embeddingModel = model || env.EMBEDDING_MODEL || 'nomic-embed-text:latest'; | ||
|
|
||
| log.info('[text-embedding] Generating embedding', { | ||
| textLength: text.length, | ||
| model: embeddingModel, | ||
| }); | ||
|
|
||
| const ollama = new OllamaClient(ollamaUrl); | ||
| const embedding = await ollama.generateEmbedding(text, embeddingModel); | ||
|
|
||
| log.info('[text-embedding] Complete', { | ||
| dimensions: embedding.length, | ||
| }); | ||
|
|
||
| return { | ||
| embedding, | ||
| dimensions: embedding.length, | ||
| model: embeddingModel, | ||
| }; | ||
| }; | ||
|
|
||
| export default handler; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| apiVersion: batch/v1 | ||
| kind: Job | ||
| metadata: | ||
| name: ollama-pull-model | ||
| spec: | ||
| ttlSecondsAfterFinished: 300 | ||
| template: | ||
| spec: | ||
| restartPolicy: OnFailure | ||
| initContainers: | ||
| - name: wait-for-ollama | ||
| image: busybox:1.36 | ||
| command: ['sh', '-c', 'until nc -z ollama 11434; do echo "Waiting for Ollama..."; sleep 2; done'] | ||
| containers: | ||
| - name: pull-model | ||
| image: curlimages/curl:8.5.0 | ||
| command: | ||
| - sh | ||
| - -c | ||
| - | | ||
| echo "Pulling nomic-embed-text model..." | ||
| curl -X POST http://ollama:11434/api/pull \ | ||
| -H "Content-Type: application/json" \ | ||
| -d '{"name": "nomic-embed-text:latest"}' \ | ||
| --max-time 600 | ||
| echo "Model pull complete" | ||
|
Comment on lines
+20
to
+26
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,53 @@ | ||||||
| apiVersion: apps/v1 | ||||||
| kind: Deployment | ||||||
| metadata: | ||||||
| name: ollama | ||||||
| labels: | ||||||
| app: ollama | ||||||
| spec: | ||||||
| replicas: 1 | ||||||
| selector: | ||||||
| matchLabels: | ||||||
| app: ollama | ||||||
| template: | ||||||
| metadata: | ||||||
| labels: | ||||||
| app: ollama | ||||||
| spec: | ||||||
| containers: | ||||||
| - name: ollama | ||||||
| image: ollama/ollama:latest | ||||||
|
||||||
| image: ollama/ollama:latest | |
| image: ollama/ollama:0.3.14 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
textvalidation only rejects empty string, but allows whitespace-only values (e.g.' '), since it doesn't trim. Other handlers treat whitespace-only strings as missing (e.g.isNonEmptyString(...).trim().length > 0). Consider usingtext.trim().length > 0in the validation so the function doesn't generate embeddings for blank input.