Skip to content

Commit

Permalink
fix: support for external kendra indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
massi-ang authored and spugachev committed Oct 10, 2023
1 parent 9006de2 commit c4e3f45
Showing 1 changed file with 52 additions and 23 deletions.
75 changes: 52 additions & 23 deletions cli/magic-create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import {
import { LIB_VERSION } from "./version.js";
import * as fs from "fs";

const versionRegExp = /\d+.\d+.\d+/;
const iamRoleRegExp = RegExp(/arn:aws:iam::\d+:role\/[\w-_]+/);
const kendraIdRegExp = RegExp(/^\w{8}-\w{4}-\w{4}-\w{4}-\w{12}$/);

const embeddingModels = [
{
Expand Down Expand Up @@ -146,7 +147,7 @@ async function processCreateOptions(options: any): Promise<void> {
message:
"Cross account role arn to invoke Bedrock - leave empty if Bedrock is in same account",
validate: (v: string) => {
const valid = RegExp(/arn:aws:iam::\d+:role\/[\w-_]+/).test(v);
const valid = iamRoleRegExp.test(v);
return v.length === 0 || valid;
},
initial: options.bedrockRoleArn || "",
Expand Down Expand Up @@ -174,7 +175,7 @@ async function processCreateOptions(options: any): Promise<void> {
{ message: "OpenSearch", name: "opensearch" },
{ message: "Kendra (managed)", name: "kendra" },
],
skip: function (): boolean {
skip(): boolean {
// workaround for https://github.com/enquirer/enquirer/issues/298
(this as any).state._choices = (this as any).state.choices;
return !(this as any).state.answers.enableRag;
Expand All @@ -199,57 +200,76 @@ async function processCreateOptions(options: any): Promise<void> {
const answers: any = await enquirer.prompt(questions);
const kendraExternal = [];
let newKendra = answers.enableRag && answers.kendra;

const existingKendraIndices = Array.from(options.kendraExternal);
while (newKendra === true) {
let existingIndex: any = existingKendraIndices.pop();
console.log(
existingIndex?.region,
Object.values(SupportedRegion).indexOf(existingIndex?.region)
);
const kendraQ = [
{
type: "input",
name: "name",
message: "Kendra source name",
validate(v: string) {
return RegExp(/^\w[\w-_]*\w$/).test(v);
},
initial: existingIndex?.name,
},
{
type: "autocomplete",
limit: 8,
name: "region",
choices: Object.values(SupportedRegion),
message: "Region of the Kendra index",
message: `Region of the Kendra index${
existingIndex?.region ? " (" + existingIndex?.region + ")" : ""
}`,
initial: Object.values(SupportedRegion).indexOf(existingIndex?.region),
},
{
type: "input",
name: "roleArn",
message:
"Cross account role Arn to assume to call Kendra, leave empty if not needed",
validate: (v: string) => {
const valid = RegExp(/arn:aws:iam::\d+:role\/[\w-_]+/).test(v);
const valid = iamRoleRegExp.test(v);
return v.length === 0 || valid;
},
initial: "",
initial: existingIndex?.roleArn ?? "",
},
{
type: "input",
name: "kendraId",
message: "Kendra ID",
validate(v: string) {
return RegExp(/\w{8}-\w{4}-\w{4}-\w{4}-\w{12}/).test(v);
return kendraIdRegExp.test(v);
},
initial: existingIndex?.kendraId,
},
{
type: "confirm",
name: "enabled",
message: "Enable this index",
initial: existingIndex?.enabled ?? true,
},
{
type: "confirm",
name: "newKendra",
message: "Do you want to add another Kendra source",
default: false,
initial: false,
},
];
const kendraInstance: any = await enquirer.prompt(kendraQ);
const ext = (({ name, roleArn, kendraId, region }) => ({
const ext = (({ enabled, name, roleArn, kendraId, region }) => ({
enabled,
name,
roleArn,
kendraId,
region,
}))(kendraInstance);
if (ext.roleArn === "") ext.roleArn = undefined;
kendraExternal.push({
enabled: true,
...ext,
});
newKendra = kendraInstance.newKendra;
Expand All @@ -261,6 +281,9 @@ async function processCreateOptions(options: any): Promise<void> {
message: "Which is the default embedding model",
choices: embeddingModels.map((m) => ({ name: m.name, value: m })),
initial: options.defaultEmbedding || undefined,
skip(): boolean {
return !(this as any).state.answers.enableRag;
},
},
];
const models: any = await enquirer.prompt(modelsPrompts);
Expand Down Expand Up @@ -296,27 +319,33 @@ async function processCreateOptions(options: any): Promise<void> {
},
},
embeddingsModels: [{}],
crossEncoderModels: [
{
provider: "sagemaker",
name: "cross-encoder/ms-marco-MiniLM-L-12-v2",
default: true,
},
],
crossEncoderModels: [{}],
},
};

config.rag.engines.kendra.createIndex =
answers.ragsToEnable.includes("kendra");
config.rag.engines.kendra.enabled =
config.rag.engines.kendra.createIndex || kendraExternal.length > 0;
config.rag.engines.kendra.external = [...kendraExternal];
// If we have not enabled rag the default embedding is set to the first model
if (!answers.enableRag) {
models.defaultEmbedding = embeddingModels[0].name;
}

config.rag.crossEncoderModels[0] = {
provider: "sagemaker",
name: "cross-encoder/ms-marco-MiniLM-L-12-v2",
default: true,
};
config.rag.embeddingsModels = embeddingModels;
config.rag.embeddingsModels.forEach((m: any) => {
if (m.name === models.defaultEmbedding) {
m.default = true;
}
});

config.rag.engines.kendra.createIndex =
answers.ragsToEnable.includes("kendra");
config.rag.engines.kendra.enabled =
config.rag.engines.kendra.createIndex || kendraExternal.length > 0;
config.rag.engines.kendra.external = [...kendraExternal];

console.log("\n✨ This is the chosen configuration:\n");
console.log(JSON.stringify(config, undefined, 2));
(
Expand Down

0 comments on commit c4e3f45

Please sign in to comment.