import {
  AzureAISearchVectorStore,
  AzureAISearchQueryType,
} from "@langchain/community/vectorstores/azure_aisearch";
import { OpenAIEmbeddings, AzureOpenAIEmbeddings } from "@langchain/openai";

const query = process.argv[2] || "What is CX Automation?";
const company = process.argv[3] || "default";

// the RAG widget uses the OpenAIEmbeddings class but the config will not work because you cannot pass in the api-version param. DO NOT USE
// const embedding = new OpenAIEmbeddings({
//     openAIApiKey: openAIApiKey,
//     configuration: {
//       baseURL: `https://${azureOpenAIApiInstanceName}.openai.azure.com/openai/deployments/${azureOpenAIApiDeploymentName}?api-version=${azureOpenAIApiVersion}`,
//     },
//   })

const embedding = new AzureOpenAIEmbeddings({
  azureOpenAIApiInstanceName: process.env.AZURE_OPENAI_API_INSTANCE_NAME,
  azureOpenAIApiDeploymentName: process.env.AZURE_OPENAI_API_DEPLOYMENT_NAME,
  azureOpenAIApiVersion: process.env.AZURE_OPENAI_API_VERSION,
  azureOpenAIApiKey: process.env.AZURE_OPENAI_API_KEY,
});

const store = new AzureAISearchVectorStore(embedding, {
  endpoint: process.env.AZURE_AISEARCH_ENDPOINT,
  key: process.env.AZURE_AISEARCH_KEY,
  indexName: process.env.AZURE_AISEARCH_INDEX_NAME,
  search: {
    type: AzureAISearchQueryType.SimilarityHybrid,
  },
});

function getSourceId(document) {
  if (document.metadata) {
    const mergedMetadata = Object.values(document.metadata).join("");
    const metatDataObj = JSON.parse(mergedMetadata);

    if ("sourceURL" in metatDataObj) {
      return metatDataObj.sourceURL;
    }
    if ("source" in metatDataObj) {
      return metatDataObj.source;
    }
    if ("source_id" in metatDataObj) {
      return metatDataObj.source_id;
    }
    if ("sourceName" in metatDataObj) {
      return metatDataObj.sourceName;
    }
  } else return "no source found";
}
const filter = {
  filterExpression: `search.in(company, '${company}')`,
};
const resultDocuments = await store.similaritySearch(query, 20, filter);
const sources = resultDocuments.map((doc) => ({
  source_id: getSourceId(doc),
  text: doc.pageContent,
}));

const cqaSources = {
  instances: [
    {
      sources: sources,
      question: query,
      knowledgebase_description: process.env.AZURE_AISEARCH_INDEX_NAME,
      extra_guidance: "",
      language_code: "en-GB",
    },
  ],
};

console.log(JSON.stringify(cqaSources, null, 2));
