Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
patelvivekdev committed Nov 19, 2024
0 parents commit dd5d56a
Show file tree
Hide file tree
Showing 20 changed files with 703 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .changeset/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Changesets

Hello and welcome! This folder has been automatically generated by `@changesets/cli`, a build tool that works
with multi-package repos, or single-package repos to help you version and publish your code. You can
find the full documentation for it [in our repository](https://github.com/changesets/changesets)

We have a quick list of common questions to get you started engaging with this project in
[our documentation](https://github.com/changesets/changesets/blob/main/docs/common-questions.md)
11 changes: 11 additions & 0 deletions .changeset/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"$schema": "https://unpkg.com/@changesets/[email protected]/schema.json",
"changelog": "@changesets/cli/changelog",
"commit": true,
"fixed": [],
"linked": [],
"access": "public",
"baseBranch": "master",
"updateInternalDependencies": "patch",
"ignore": []
}
14 changes: 14 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.DS_Store
node_modules
.turbo
*.log
.next
dist
dist-ssr
*.local
.env
.cache
server/dist
public/dist
.turbo
test-results
2 changes: 2 additions & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
auto-install-peers = true
strict-peer-dependencies = false
7 changes: 7 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"semi": true,
"singleQuote": true,
"trailingComma": "all",
"printWidth": 80,
"tabWidth": 2
}
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# voyage-ai-provider

## 0.0.1

### Patch Changes

- First public release
11 changes: 11 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Binary file added bun.lockb
Binary file not shown.
78 changes: 78 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
{
"name": "voyage-ai-provider",
"version": "0.0.1",
"description": "Voyage AI Provider for running Voyage AI models with Vercel AI SDK",
"author": "Vivek Patel <[email protected]>",
"license": "Apache-2.0",
"keywords": [
"ai",
"vercel-ai",
"voyage",
"embeddings"
],
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"files": [
"dist/**/*",
"CHANGELOG.md"
],
"scripts": {
"build": "tsup",
"clean": "rm -rf dist",
"check-exports": "attw --pack .",
"dev": "tsup --watch",
"lint": "eslint \"./**/*.ts*\"",
"type-check": "tsc --noEmit",
"format": "prettier --write .",
"check-format": "prettier --check .",
"test": "bun test:node && bun test:edge",
"test:edge": "vitest --config vitest.edge.config.js --run",
"test:node": "vitest --config vitest.node.config.js --run",
"ci": "bun run build && bun run check-format && bun run test",
"local-release": "changeset version && changeset publish",
"prepublishOnly": "npm run ci"
},
"exports": {
"./package.json": "./package.json",
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.mjs",
"require": "./dist/index.js"
}
},
"dependencies": {
"@ai-sdk/provider": "^1.0.0",
"@ai-sdk/provider-utils": "^2.0.0"
},
"devDependencies": {
"@changesets/cli": "^2.27.9",
"@edge-runtime/vm": "^3.2.0",
"@types/node": "^18.19.64",
"prettier": "^3.3.3",
"tsup": "^8.3.5",
"typescript": "5.5.4",
"vite-tsconfig-paths": "^4.3.2",
"vitest": "^2.1.5",
"zod": "^3.23.8"
},
"peerDependencies": {
"zod": "^3.0.0"
},
"peerDependenciesMeta": {
"zod": {
"optional": true
}
},
"publishConfig": {
"access": "public"
},
"homepage": "https://github.com/patelvivekdev/voyage-ai-provider",
"repository": {
"type": "git",
"url": "git+https://github.com/patelvivekdev/voyage-ai-provider.git"
},
"bugs": {
"url": "https://github.com/patelvivekdev/voyage-ai-provider/issues"
}
}
83 changes: 83 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# AI SDK - Voyage AI Provider

## Introduction

The Voyage AI Provider is a provider for the AI SDK. It provides a simple interface to the Voyage AI API.

## Installation

```bash
npm install voyage-ai-provider

# or

yarn add voyage-ai-provider

# or

pnpm add voyage-ai-provider

# or

bun add voyage-ai-provider
```

## Configuration

The Voyage AI Provider requires an API key to be configured. You can obtain an API key by signing up at [Voyage](https://voyageai.com).

add the following to your `.env` file:

```bash
VOYAGE_API_KEY=your-api-key
```

## Usage

```typescript
import { voyage } from 'voyage-ai-provider';
import { embedMany } from 'ai';

const embeddingModel = voyage.textEmbeddingModel('voyage-3-lite');

export const generateEmbeddings = async (
value: string,
): Promise<Array<{ embedding: number[]; content: string }>> => {
// Generate chunks from the input value
const chunks = value.split('\n');

// Optional: You can also split the input value by comma
// const chunks = value.split('.');

// Or you can use LLM to generate chunks(summarize) from the input value

const { embeddings } = await embedMany({
model: embeddingModel,
values: chunks,
});
return embeddings.map((e, i) => ({ content: chunks[i], embedding: e }));
};
```

### Add settings to the model

The settings object should contain the settings you want to add to the model. You can find the available settings for the model in the Voyage API documentation: https://docs.voyageai.com/reference/embeddings-api

```typescript
const voyage = createVoyage({
apiKey: process.env.VOYAGE_API_KEY,
});

// Initialize the embedding model
const embeddingModel = voyage.textEmbeddingModel(
'voyage-3-lite',
// adding settings
{
inputType: 'document',
},
);
```

## Authors

- [patelvivekdev](https://patelvivek.dev)
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { createVoyage, voyage } from './voyage-provider';
export type { VoyageProvider, VoyageProviderSettings } from './voyage-provider';
130 changes: 130 additions & 0 deletions src/voyage-embedding-model.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import { EmbeddingModelV1Embedding } from '@ai-sdk/provider';
import { JsonTestServer } from '@ai-sdk/provider-utils/test';
import { createVoyage } from './voyage-provider';

const dummyEmbeddings = [
[0.1, 0.2, 0.3, 0.4, 0.5],
[0.6, 0.7, 0.8, 0.9, 1],
];
const testValues = ['sunny day at the beach', 'rainy day in the city'];

const provider = createVoyage({
baseURL: 'https://api.voyage.ai/v1',
apiKey: 'test-api-key',
});
const model = provider('voyage-3-lite');

describe('doEmbed', () => {
const server = new JsonTestServer('https://api.voyage.ai/v1/embeddings');

server.setupTestEnvironment();

function prepareJsonResponse({
embeddings = dummyEmbeddings,
usage = {
prompt_tokens: 4,
total_tokens: 12,
},
}: {
embeddings?: EmbeddingModelV1Embedding[];
usage?: { prompt_tokens: number; total_tokens: number };
} = {}) {
server.responseBodyJson = {
object: 'list',
data: embeddings.map((embedding, i) => ({
object: 'embedding',
embedding,
index: i,
})),
model: 'voyage-3-lite',
normalized: true,
encoding_format: 'float',
usage,
};
}

it('should extract embedding', async () => {
prepareJsonResponse();

const { embeddings } = await model.doEmbed({ values: testValues });

expect(embeddings).toStrictEqual(dummyEmbeddings);
});

it('should expose the raw response headers', async () => {
prepareJsonResponse();

server.responseHeaders = {
'test-header': 'test-value',
};

const { rawResponse } = await model.doEmbed({ values: testValues });

expect(rawResponse?.headers).toStrictEqual({
'content-length': '272',
// default headers:
'content-type': 'application/json',

// custom header
'test-header': 'test-value',
});
});

it('should pass the model and the values', async () => {
prepareJsonResponse();

await model.doEmbed({ values: testValues });

expect(await server.getRequestBodyJson()).toStrictEqual({
input: testValues,
model: 'voyage-3-lite',
});
});

it('should pass the settings ', async () => {
prepareJsonResponse();

const voyage = createVoyage({
baseURL: 'https://api.voyage.ai/v1',
apiKey: 'test-api-key',
});

await voyage
.textEmbeddingModel('voyage-3-lite', {
inputType: 'document',
})
.doEmbed({
values: testValues,
});

expect(await server.getRequestBodyJson()).toStrictEqual({
input: testValues,
model: 'voyage-3-lite',
input_type: 'document',
});
});

it('should pass custom headers', async () => {
prepareJsonResponse();

const voyage = createVoyage({
baseURL: 'https://api.voyage.ai/v1',
apiKey: 'test-api-key',
headers: {
'Custom-Header': 'test-header',
},
});

await voyage.textEmbeddingModel('voyage-3-lite').doEmbed({
values: testValues,
});

const requestHeaders = await server.getRequestHeaders();

expect(requestHeaders).toStrictEqual({
authorization: 'Bearer test-api-key',
'content-type': 'application/json',
'custom-header': 'test-header',
});
});
});
Loading

0 comments on commit dd5d56a

Please sign in to comment.