Skip to content

Commit b35812c

Browse files
jacob/5890 (#5956)
* feat: add mixedbread ai integration * mxbai integration * mxbai integration * Naming correction * Docs nits and format --------- Co-authored-by: Julius Lipp <[email protected]> Co-authored-by: Julius Lipp <[email protected]>
1 parent 3ebc3a3 commit b35812c

22 files changed

+1177
-11
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Mixedbread AI reranking
2+
3+
## Overview
4+
5+
This guide will help you integrate and use the [Mixedbread AI](https://mixedbread.ai/) reranking API. The reranking API allows you to reorder a list of documents based on a given query, improving the relevance of search results or any ranked list.
6+
7+
## Installation
8+
9+
To get started, install the `@langchain/mixedbread-ai` package:
10+
11+
import IntegrationInstallTooltip from "@mdx_components/integration_install_tooltip.mdx";
12+
13+
<IntegrationInstallTooltip></IntegrationInstallTooltip>
14+
15+
```bash
16+
npm install @langchain/mixedbread-ai
17+
```
18+
19+
## Authentication
20+
21+
Obtain your API key by signing up at [Mixedbread AI](https://mixedbread.ai/). You can then set the `MXBAI_API_KEY` environment variable to your Mixedbread AI API key or pass it directly as the `apiKey` option when constructing the class.
22+
23+
## Using Reranking
24+
25+
The `MixedbreadAIReranker` class provides access to the reranking API. Here’s how to use it:
26+
27+
1. **Import the Class**: First, import the `MixedbreadAIReranker` class from the package.
28+
29+
```typescript
30+
import { MixedbreadAIReranker } from "@langchain/mixedbread-ai";
31+
```
32+
33+
2. **Instantiate the Class**: Create an instance of `MixedbreadAIReranker` with your API key.
34+
35+
```typescript
36+
const reranker = new MixedbreadAIReranker({ apiKey: "your-api-key" });
37+
```
38+
39+
3. **Rerank Documents**: Use the `rerankDocuments` method to reorder documents based on a query.
40+
41+
```typescript
42+
const documents = [
43+
{ pageContent: "To bake bread you need flour" },
44+
{ pageContent: "To bake bread you need yeast" },
45+
{ pageContent: "To eat bread you need nothing but good taste" },
46+
];
47+
const query = "What do you need to bake bread?";
48+
const result = await reranker.compressDocuments(documents, query);
49+
console.log(result);
50+
```
51+
52+
## Additional Resources
53+
54+
For more information, refer to the [Reranking API documentation](https://mixedbread.ai/docs/reranking).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Mixedbread AI Embeddings
2+
3+
The `MixedbreadAIEmbeddings` class uses the [Mixedbread AI](https://mixedbread.ai/) API to generate text embeddings. This guide will walk you through setting up and using the `MixedbreadAIEmbeddings` class, helping you integrate it into your project effectively.
4+
5+
## Installation
6+
7+
To install the `@langchain/mixedbread-ai` package, use the following command:
8+
9+
import IntegrationInstallTooltip from "@mdx_components/integration_install_tooltip.mdx";
10+
11+
<IntegrationInstallTooltip></IntegrationInstallTooltip>
12+
13+
```bash npm2yarn
14+
npm install @langchain/mixedbread-ai
15+
```
16+
17+
## Initialization
18+
19+
First, sign up on the Mixedbread AI website and get your API key from [here](https://mixedbread.ai/). You can then use this key to initialize the `MixedbreadAIEmbeddings` class.
20+
21+
You can pass the API key directly to the constructor or set it as an environment variable (`MXBAI_API_KEY`).
22+
23+
### Basic Usage
24+
25+
Here’s how to create an instance of `MixedbreadAIEmbeddings`:
26+
27+
```typescript
28+
import { MixedbreadAIEmbeddings } from "@langchain/mixedbread-ai";
29+
30+
const embeddings = new MixedbreadAIEmbeddings({
31+
apiKey: "YOUR_API_KEY",
32+
// Optionally specify model
33+
// model: "mixedbread-ai/mxbai-embed-large-v1",
34+
});
35+
```
36+
37+
If the `apiKey` is not provided, it will be read from the `MXBAI_API_KEY` environment variable.
38+
39+
## Generating Embeddings
40+
41+
### Embedding a Single Query
42+
43+
To generate embeddings for a single text query, use the `embedQuery` method:
44+
45+
```typescript
46+
const embedding = await embeddings.embedQuery(
47+
"Represent this sentence for searching relevant passages: Is baking fun?"
48+
);
49+
console.log(embedding);
50+
```
51+
52+
### Embedding Multiple Documents
53+
54+
To generate embeddings for multiple documents, use the `embedDocuments` method. This method handles batching automatically based on the `batchSize` parameter:
55+
56+
```typescript
57+
const documents = ["Baking bread is fun", "I love baking"];
58+
59+
const embeddingsArray = await embeddings.embedDocuments(documents);
60+
console.log(embeddingsArray);
61+
```
62+
63+
## Customizing Requests
64+
65+
You can customize the SDK by passing additional parameters.
66+
67+
```typescript
68+
const customEmbeddings = new MixedbreadAIEmbeddings({
69+
apiKey: "YOUR_API_KEY",
70+
baseUrl: "...",
71+
maxRetries: 6,
72+
});
73+
```
74+
75+
## Error Handling
76+
77+
If the API key is not provided and cannot be found in the environment variables, an error will be thrown:
78+
79+
```typescript
80+
try {
81+
const embeddings = new MixedbreadAIEmbeddings();
82+
} catch (error) {
83+
console.error(error);
84+
}
85+
```
+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
module.exports = {
2+
extends: [
3+
"airbnb-base",
4+
"eslint:recommended",
5+
"prettier",
6+
"plugin:@typescript-eslint/recommended",
7+
],
8+
parserOptions: {
9+
ecmaVersion: 12,
10+
parser: "@typescript-eslint/parser",
11+
project: "./tsconfig.json",
12+
sourceType: "module",
13+
},
14+
plugins: ["@typescript-eslint", "no-instanceof", "eslint-plugin-jest"],
15+
ignorePatterns: [
16+
".eslintrc.cjs",
17+
"scripts",
18+
"node_modules",
19+
"dist",
20+
"dist-cjs",
21+
"*.js",
22+
"*.cjs",
23+
"*.d.ts",
24+
],
25+
rules: {
26+
"no-process-env": 2,
27+
"no-instanceof/no-instanceof": 2,
28+
"@typescript-eslint/explicit-module-boundary-types": 0,
29+
"@typescript-eslint/no-empty-function": 0,
30+
"@typescript-eslint/no-shadow": 0,
31+
"@typescript-eslint/no-empty-interface": 0,
32+
"@typescript-eslint/no-use-before-define": ["error", "nofunc"],
33+
"@typescript-eslint/no-unused-vars": ["warn", { args: "none" }],
34+
"@typescript-eslint/no-floating-promises": "error",
35+
"@typescript-eslint/no-misused-promises": "error",
36+
camelcase: 0,
37+
"class-methods-use-this": 0,
38+
"import/extensions": [2, "ignorePackages"],
39+
"import/no-extraneous-dependencies": [
40+
"error",
41+
{ devDependencies: ["**/*.test.ts"] },
42+
],
43+
"import/no-unresolved": 0,
44+
"import/prefer-default-export": 0,
45+
"keyword-spacing": "error",
46+
"max-classes-per-file": 0,
47+
"max-len": 0,
48+
"no-await-in-loop": 0,
49+
"no-bitwise": 0,
50+
"no-console": 0,
51+
"no-restricted-syntax": 0,
52+
"no-shadow": 0,
53+
"no-continue": 0,
54+
"no-void": 0,
55+
"no-underscore-dangle": 0,
56+
"no-use-before-define": 0,
57+
"no-useless-constructor": 0,
58+
"no-return-await": 0,
59+
"consistent-return": 0,
60+
"no-else-return": 0,
61+
"func-names": 0,
62+
"no-lonely-if": 0,
63+
"prefer-rest-params": 0,
64+
'jest/no-focused-tests': 'error',
65+
"new-cap": ["error", { properties: false, capIsNew: false }],
66+
},
67+
overrides: [
68+
{
69+
files: ['**/*.test.ts'],
70+
rules: {
71+
'@typescript-eslint/no-unused-vars': 'off'
72+
}
73+
}
74+
]
75+
};
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
index.cjs
2+
index.js
3+
index.d.ts
4+
index.d.cts
5+
node_modules
6+
dist
7+
.yarn
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"$schema": "https://json.schemastore.org/prettierrc",
3+
"printWidth": 80,
4+
"tabWidth": 2,
5+
"useTabs": false,
6+
"semi": true,
7+
"singleQuote": false,
8+
"quoteProps": "as-needed",
9+
"jsxSingleQuote": false,
10+
"trailingComma": "es5",
11+
"bracketSpacing": true,
12+
"arrowParens": "always",
13+
"requirePragma": false,
14+
"insertPragma": false,
15+
"proseWrap": "preserve",
16+
"htmlWhitespaceSensitivity": "css",
17+
"vueIndentScriptAndStyle": false,
18+
"endOfLine": "lf"
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"github": {
3+
"release": true,
4+
"autoGenerate": true,
5+
"tokenRef": "GITHUB_TOKEN_RELEASE"
6+
},
7+
"npm": {
8+
"versionArgs": [
9+
"--workspaces-update=false"
10+
]
11+
}
12+
}

libs/langchain-mixedbread-ai/LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License
2+
3+
Copyright (c) 2023 LangChain
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.
+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# @langchain/mixedbread-ai
2+
3+
This package contains the LangChain.js integrations for the [Mixedbread AI API](https://mixedbread.ai/).
4+
5+
## Installation
6+
7+
```bash
8+
npm install @langchain/mixedbread-ai
9+
```
10+
11+
This package, along with the main LangChain package, depends on [`@langchain/core`](https://npmjs.com/package/@langchain/core/). If you are using this package with other LangChain packages, you should make sure that all of the packages depend on the same instance of `@langchain/core`.
12+
13+
## Authentication
14+
15+
To use this package, you need a Mixedbread AI API key. You can obtain your API key by signing up at [Mixedbread AI](https://mixedbread.ai).
16+
17+
Either set the `MXBAI_API_KEY` environment variable to your Mixedbread AI API key, or pass it as the `apiKey` option to the constructor of the class you are using.
18+
19+
## Embeddings
20+
21+
This package provides access to the different embedding models provided by the Mixedbread AI API, such as the "mixedbread-ai/mxbai-embed-large-v1" model.
22+
23+
Learn more: [Embeddings API](https://mixedbread.ai/docs/embeddings)
24+
25+
```typescript
26+
const embeddings = new MixedbreadAIEmbeddings({ apiKey: 'your-api-key' });
27+
const texts = ["Baking bread is fun", "I love baking"];
28+
const result = await embeddings.embedDocuments(texts);
29+
console.log(result);
30+
```
31+
32+
## Reranking
33+
34+
This package provides access to the reranking API provided by Mixedbread AI. It allows you to rerank a list of documents based on a query. Available models include "mixedbread-ai/mxbai-rerank-large-v1".
35+
36+
Learn more: [Reranking API](https://mixedbread.ai/docs/reranking)
37+
38+
```typescript
39+
const reranker = new MixedbreadAIReranker({ apiKey: 'your-api-key' });
40+
const documents = [{ pageContent: "To bake bread you need flour" }, { pageContent: "To bake bread you need yeast" }];
41+
const query = "What do you need to bake bread?";
42+
const result = await reranker.compressDocuments(documents, query);
43+
console.log(result);
44+
```
45+
46+
## Development
47+
48+
To develop the `@langchain/mixedbread-ai` package, follow these instructions:
49+
50+
### Install dependencies
51+
52+
```bash
53+
yarn install
54+
```
55+
56+
### Build the package
57+
58+
```bash
59+
yarn build
60+
```
61+
62+
Or from the repo root:
63+
64+
```bash
65+
yarn build --filter=@langchain/mixedbread-ai
66+
```
67+
68+
### Run tests
69+
70+
Test files should live within a `tests/` folder in the `src/` directory. Unit tests should end in `.test.ts` and integration tests should end in `.int.test.ts`:
71+
72+
```bash
73+
yarn test
74+
yarn test:int
75+
```
76+
77+
### Lint & Format
78+
79+
Run the linter & formatter to ensure your code is up to standard:
80+
81+
```bash
82+
yarn lint && yarn format
83+
```
84+
85+
### Adding new entry points
86+
87+
If you add a new file to be exported, either import & re-export from `src/index.ts`, or add it to the `entrypoints` field in the `config` variable located inside `langchain.config.js` and run `yarn build` to generate the new entry point.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/** @type {import('ts-jest').JestConfigWithTsJest} */
2+
module.exports = {
3+
preset: "ts-jest/presets/default-esm",
4+
testEnvironment: "./jest.env.cjs",
5+
modulePathIgnorePatterns: ["dist/", "docs/"],
6+
moduleNameMapper: {
7+
"^(\\.{1,2}/.*)\\.js$": "$1",
8+
},
9+
transform: {
10+
"^.+\\.tsx?$": ["@swc/jest"],
11+
},
12+
transformIgnorePatterns: [
13+
"/node_modules/",
14+
"\\.pnp\\.[^\\/]+$",
15+
"./scripts/jest-setup-after-env.js",
16+
],
17+
setupFiles: ["dotenv/config"],
18+
testTimeout: 20_000,
19+
passWithNoTests: true,
20+
collectCoverageFrom: ["src/**/*.ts"],
21+
};

0 commit comments

Comments
 (0)