| 1 | // Copyright 2023 Lance Developers. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | import { describe } from 'mocha' |
| 16 | import { assert } from 'chai' |
| 17 | |
| 18 | import { OpenAIEmbeddingFunction } from '../../embedding/openai' |
| 19 | import { isEmbeddingFunction } from '../../embedding/embedding_function' |
| 20 | |
| 21 | // eslint-disable-next-line @typescript-eslint/no-var-requires |
| 22 | const OpenAIApi = require('openai') |
| 23 | // eslint-disable-next-line @typescript-eslint/no-var-requires |
| 24 | const { stub } = require('sinon') |
| 25 | |
| 26 | describe('OpenAPIEmbeddings', function () { |
| 27 | const stubValue = { |
| 28 | data: [ |
| 29 | { |
| 30 | embedding: Array(1536).fill(1.0) |
| 31 | }, |
| 32 | { |
| 33 | embedding: Array(1536).fill(2.0) |
| 34 | } |
| 35 | ] |
| 36 | } |
| 37 | |
| 38 | describe('#embed', function () { |
| 39 | it('should create vector embeddings', async function () { |
| 40 | const openAIStub = stub(OpenAIApi.Embeddings.prototype, 'create').returns(stubValue) |
| 41 | const f = new OpenAIEmbeddingFunction('text', 'sk-key') |
| 42 | const vectors = await f.embed(['abc', 'def']) |
| 43 | assert.isTrue(openAIStub.calledOnce) |
| 44 | assert.equal(vectors.length, 2) |
| 45 | assert.deepEqual(vectors[0], stubValue.data[0].embedding) |
| 46 | assert.deepEqual(vectors[1], stubValue.data[1].embedding) |
| 47 | }) |
| 48 | }) |
| 49 | |
| 50 | describe('isEmbeddingFunction', function () { |
| 51 | it('should match the isEmbeddingFunction guard', function () { |
| 52 | assert.isTrue(isEmbeddingFunction(new OpenAIEmbeddingFunction('text', 'sk-key'))) |
| 53 | }) |
| 54 | }) |
| 55 | }) |