-
Notifications
You must be signed in to change notification settings - Fork 410
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: integrate with azure cosmos db (#1444)
Co-authored-by: Wassim Chegham <[email protected]> Co-authored-by: Alex Yang <[email protected]>
- Loading branch information
1 parent
69f3095
commit 396b1e1
Showing
11 changed files
with
1,671 additions
and
565 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"llamaindex": patch | ||
--- | ||
|
||
feat: add Azure Cosmos DB DocumentStore, IndexStore, KVStore, update vectorStore and examples |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import "dotenv/config"; | ||
|
||
import { | ||
DefaultAzureCredential, | ||
getBearerTokenProvider, | ||
} from "@azure/identity"; | ||
import { | ||
AzureCosmosDBNoSqlVectorStore, | ||
AzureCosmosNoSqlDocumentStore, | ||
AzureCosmosNoSqlIndexStore, | ||
Document, | ||
OpenAI, | ||
OpenAIEmbedding, | ||
Settings, | ||
storageContextFromDefaults, | ||
VectorStoreIndex, | ||
} from "llamaindex"; | ||
/** | ||
* This example demonstrates how to use Azure CosmosDB with LlamaIndex. | ||
* It uses Azure CosmosDB as IndexStore, DocumentStore, and VectorStore. | ||
* | ||
* To run this example, create an .env file under /examples and set the following environment variables: | ||
* | ||
* AZURE_OPENAI_ENDPOINT="https://AOAI-ACCOUNT.openai.azure.com" // Sample Azure OpenAI endpoint. | ||
* AZURE_DEPLOYMENT_NAME="gpt-4o" // Sample Azure OpenAI deployment name. | ||
* EMBEDDING_MODEL="text-embedding-3-large" // Sample Azure OpenAI embedding model. | ||
* AZURE_COSMOSDB_NOSQL_ACCOUNT_ENDPOINT = "https://DB-ACCOUNT.documents.azure.com:443/" // Sample CosmosDB account endpoint. | ||
* | ||
* This example uses managed identity to authenticate with Azure CosmosDB and Azure OpenAI. Make sure to assign the required roles to the managed identity. | ||
* You can also use connectionString for Azure CosmosDB and Keys with Azure OpenAI for authentication. | ||
*/ | ||
(async () => { | ||
const credential = new DefaultAzureCredential(); | ||
const azureADTokenProvider = getBearerTokenProvider( | ||
credential, | ||
"https://cognitiveservices.azure.com/.default", | ||
); | ||
|
||
const azure = { | ||
azureADTokenProvider, | ||
deployment: process.env.AZURE_DEPLOYMENT_NAME, | ||
}; | ||
Settings.llm = new OpenAI({ azure }); | ||
Settings.embedModel = new OpenAIEmbedding({ | ||
model: process.env.EMBEDDING_MODEL, | ||
azure: { | ||
...azure, | ||
deployment: process.env.EMBEDDING_MODEL, | ||
}, | ||
}); | ||
const docStore = AzureCosmosNoSqlDocumentStore.fromAadToken(); | ||
console.log({ docStore }); | ||
const indexStore = AzureCosmosNoSqlIndexStore.fromAadToken(); | ||
console.log({ indexStore }); | ||
const vectorStore = AzureCosmosDBNoSqlVectorStore.fromUriAndManagedIdentity(); | ||
console.log({ vectorStore }); | ||
const storageContext = await storageContextFromDefaults({ | ||
docStore, | ||
indexStore, | ||
vectorStore, | ||
}); | ||
console.log({ storageContext }); | ||
|
||
const document = new Document({ text: "Test Text" }); | ||
const index = await VectorStoreIndex.fromDocuments([document], { | ||
storageContext, | ||
logProgress: true, | ||
}); | ||
|
||
console.log({ index }); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { TokenCredential } from "@azure/identity"; | ||
import { | ||
AzureCosmosDBNoSQLConfig, | ||
AzureCosmosDBNoSqlVectorStore, | ||
AzureCosmosNoSqlDocumentStore, | ||
AzureCosmosNoSqlIndexStore, | ||
} from "llamaindex"; | ||
|
||
/** | ||
* Util function to create AzureCosmosDB vectorStore, docStore, indexStore from connection string. | ||
*/ | ||
export const createStoresFromConnectionString = ( | ||
connectionString: string, | ||
dbConfig: AzureCosmosDBNoSQLConfig, | ||
) => { | ||
const vectorStore = AzureCosmosDBNoSqlVectorStore.fromConnectionString({ | ||
connectionString, | ||
...dbConfig, | ||
}); | ||
const docStore = AzureCosmosNoSqlDocumentStore.fromConnectionString({ | ||
connectionString, | ||
}); | ||
const indexStore = AzureCosmosNoSqlIndexStore.fromConnectionString({ | ||
connectionString, | ||
}); | ||
return { vectorStore, docStore, indexStore }; | ||
}; | ||
|
||
/** | ||
* Util function to create AzureCosmosDB vectorStore, docStore, indexStore from connection string. | ||
*/ | ||
export const createStoresFromManagedIdentity = ( | ||
endpoint: string, | ||
credential: TokenCredential, | ||
dbConfig: AzureCosmosDBNoSQLConfig, | ||
) => { | ||
const vectorStore = AzureCosmosDBNoSqlVectorStore.fromUriAndManagedIdentity({ | ||
endpoint, | ||
credential, | ||
...dbConfig, | ||
}); | ||
const docStore = AzureCosmosNoSqlDocumentStore.fromAadToken({ | ||
endpoint, | ||
credential, | ||
}); | ||
const indexStore = AzureCosmosNoSqlIndexStore.fromAadToken({ | ||
endpoint, | ||
credential, | ||
}); | ||
return { vectorStore, docStore, indexStore }; | ||
}; |
77 changes: 77 additions & 0 deletions
77
packages/llamaindex/src/storage/docStore/AzureCosmosNoSqlDocumentStore.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { | ||
AzureCosmosNoSqlKVStore, | ||
type AadTokenOptions, | ||
type AccountAndKeyOptions, | ||
type ConnectionStringOptions, | ||
} from "../kvStore/AzureCosmosNoSqlKVStore.js"; | ||
import { KVDocumentStore } from "./KVDocumentStore.js"; | ||
|
||
const DEFAULT_DATABASE = "DocumentStoreDB"; | ||
const DEFAULT_CONTAINER = "DocumentStoreContainer"; | ||
|
||
export interface AzureCosmosNoSqlDocumentStoreArgs { | ||
azureCosmosNoSqlKVStore: AzureCosmosNoSqlKVStore; | ||
namespace?: string; | ||
} | ||
|
||
export class AzureCosmosNoSqlDocumentStore extends KVDocumentStore { | ||
constructor({ | ||
azureCosmosNoSqlKVStore, | ||
namespace, | ||
}: AzureCosmosNoSqlDocumentStoreArgs) { | ||
super(azureCosmosNoSqlKVStore, namespace); | ||
} | ||
|
||
/** | ||
* Static method for creating an instance using a connection string. | ||
* If no connection string is provided, it will attempt to use the env variable `AZURE_COSMOSDB_NOSQL_CONNECTION_STRING` as connection string. | ||
* @returns Instance of AzureCosmosNoSqlDocumentStore | ||
*/ | ||
static fromConnectionString(options: ConnectionStringOptions = {}) { | ||
options.dbName = options.dbName || DEFAULT_DATABASE; | ||
options.containerName = options.containerName || DEFAULT_CONTAINER; | ||
|
||
const azureCosmosNoSqlKVStore = | ||
AzureCosmosNoSqlKVStore.fromConnectionString(options); | ||
const namespace = `${options.dbName}.${options.containerName}`; | ||
return new AzureCosmosNoSqlDocumentStore({ | ||
azureCosmosNoSqlKVStore, | ||
namespace, | ||
}); | ||
} | ||
|
||
/** | ||
* Static method for creating an instance using a account endpoint and key. | ||
* If no endpoint and key is provided, it will attempt to use the env variable `AZURE_COSMOSDB_NOSQL_ACCOUNT_ENDPOINT` as enpoint and `AZURE_COSMOSDB_NOSQL_ACCOUNT_KEY` as key. | ||
* @returns Instance of AzureCosmosNoSqlDocumentStore | ||
*/ | ||
static fromAccountAndKey(options: AccountAndKeyOptions = {}) { | ||
options.dbName = options.dbName || DEFAULT_DATABASE; | ||
options.containerName = options.containerName || DEFAULT_CONTAINER; | ||
|
||
const azureCosmosNoSqlKVStore = | ||
AzureCosmosNoSqlKVStore.fromAccountAndKey(options); | ||
const namespace = `${options.dbName}.${options.containerName}`; | ||
return new AzureCosmosNoSqlDocumentStore({ | ||
azureCosmosNoSqlKVStore, | ||
namespace, | ||
}); | ||
} | ||
|
||
/** | ||
* Static method for creating an instance using AAD token. | ||
* If no endpoint and credentials are provided, it will attempt to use the env variable `AZURE_COSMOSDB_NOSQL_ACCOUNT_ENDPOINT` as endpoint and use DefaultAzureCredential() as credentials. | ||
* @returns Instance of AzureCosmosNoSqlDocumentStore | ||
*/ | ||
static fromAadToken(options: AadTokenOptions = {}) { | ||
options.dbName = options.dbName || DEFAULT_DATABASE; | ||
options.containerName = options.containerName || DEFAULT_CONTAINER; | ||
const azureCosmosNoSqlKVStore = | ||
AzureCosmosNoSqlKVStore.fromAadToken(options); | ||
const namespace = `${options.dbName}.${options.containerName}`; | ||
return new AzureCosmosNoSqlDocumentStore({ | ||
azureCosmosNoSqlKVStore, | ||
namespace, | ||
}); | ||
} | ||
} |
Oops, something went wrong.