Skip to content

Commit

Permalink
added logs for debbuging
Browse files Browse the repository at this point in the history
  • Loading branch information
FreddyAyala committed Oct 16, 2024
1 parent 85eb013 commit 2144f7d
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export const SimpleSearch = async (
filter?: string
): Promise<ServerActionResponse<Array<DocumentSearchResponse>>> => {
try {
console.log("Executing SimpleSearch with searchText:", searchText, "filter:", filter);
const instance = AzureAISearchInstance<AzureSearchDocumentIndex>();
const searchResults = await instance.search(searchText, { filter: filter });

Expand All @@ -45,11 +46,13 @@ export const SimpleSearch = async (
});
}

console.log("SimpleSearch results:", results);
return {
status: "OK",
response: results,
};
} catch (e) {
console.error("SimpleSearch error:", e);
return {
status: "ERROR",
errors: [
Expand All @@ -67,14 +70,16 @@ export const SimilaritySearch = async (
filter?: string
): Promise<ServerActionResponse<Array<DocumentSearchResponse>>> => {
try {
console.log("Executing SimilaritySearch with searchText:", searchText, "k:", k, "filter:", filter);
const openai = OpenAIEmbeddingInstance();
const embeddings = await openai.embeddings.create({
input: searchText,
model: "",
});

const searchClient = AzureAISearchInstance<AzureSearchDocumentIndex>();
console.log("Embeddings obtained:", embeddings);

const searchClient = AzureAISearchInstance<AzureSearchDocumentIndex>();
const searchResults = await searchClient.search(searchText, {
top: k,
filter: filter,
Expand All @@ -98,11 +103,13 @@ export const SimilaritySearch = async (
});
}

console.log("SimilaritySearch results:", results);
return {
status: "OK",
response: results,
};
} catch (e) {
console.error("SimilaritySearch error:", e);
return {
status: "ERROR",
errors: [
Expand All @@ -122,17 +129,19 @@ export const ExtensionSimilaritySearch = async (props: {
indexName: string;
}): Promise<ServerActionResponse<Array<DocumentSearchResponse>>> => {
try {
console.log("Executing ExtensionSimilaritySearch with props:", props);
const openai = OpenAIEmbeddingInstance();
const { searchText, vectors, apiKey, searchName, indexName } = props;

const embeddings = await openai.embeddings.create({
input: searchText,
model: "",
});
const endpointSuffix = process.env.AZURE_SEARCH_ENDPOINT_SUFFIX || "search.windows.net";

const endpoint = `https://${searchName}.${endpointSuffix}`;
console.log("Embeddings obtained:", embeddings);

const endpointSuffix = process.env.AZURE_SEARCH_ENDPOINT_SUFFIX || "search.windows.net";
const endpoint = `https://${searchName}.${endpointSuffix}`;
const searchClient = new SearchClient(
endpoint,
indexName,
Expand All @@ -141,8 +150,6 @@ export const ExtensionSimilaritySearch = async (props: {

const searchResults = await searchClient.search(searchText, {
top: 3,

// filter: filter,
vectorSearchOptions: {
queries: [
{
Expand All @@ -162,13 +169,9 @@ export const ExtensionSimilaritySearch = async (props: {
document: result.document,
};

// exclude the all the fields that are not in the fields array
const document = item.document as any;
const newDocument: any = {};

// iterate over the object entries in document
// and only include the fields that are in the fields array

for (const key in document) {
const hasKey = vectors.includes(key);
if (!hasKey) {
Expand All @@ -178,15 +181,17 @@ export const ExtensionSimilaritySearch = async (props: {

results.push({
score: result.score,
document: newDocument, // Use the newDocument object instead of the original document
document: newDocument,
});
}

console.log("ExtensionSimilaritySearch results:", results);
return {
status: "OK",
response: results,
};
} catch (e) {
console.error("ExtensionSimilaritySearch error:", e);
return {
status: "ERROR",
errors: [
Expand All @@ -204,6 +209,7 @@ export const IndexDocuments = async (
chatThreadId: string
): Promise<Array<ServerActionResponse<boolean>>> => {
try {
console.log("Indexing documents with fileName:", fileName, "chatThreadId:", chatThreadId);
const documentsToIndex: AzureSearchDocumentIndex[] = [];

for (const doc of docs) {
Expand All @@ -219,6 +225,8 @@ export const IndexDocuments = async (
documentsToIndex.push(docToAdd);
}

console.log("Documents to index:", documentsToIndex);

const instance = AzureAISearchInstance();
const embeddingsResponse = await EmbedDocuments(documentsToIndex);

Expand Down Expand Up @@ -246,11 +254,13 @@ export const IndexDocuments = async (
}
});

console.log("IndexDocuments response:", response);
return response;
}

return [embeddingsResponse];
} catch (e) {
console.error("IndexDocuments error:", e);
return [
{
status: "ERROR",
Expand All @@ -268,7 +278,7 @@ export const DeleteDocuments = async (
chatThreadId: string
): Promise<Array<ServerActionResponse<boolean>>> => {
try {
// find all documents for chat thread
console.log("Deleting documents for chatThreadId:", chatThreadId);
const documentsInChatResponse = await SimpleSearch(
undefined,
`chatThreadId eq '${chatThreadId}'`
Expand All @@ -279,6 +289,7 @@ export const DeleteDocuments = async (
const deletedResponse = await instance.deleteDocuments(
documentsInChatResponse.response.map((r) => r.document)
);

const response: Array<ServerActionResponse<boolean>> = [];
deletedResponse.results.forEach((r) => {
if (r.succeeded) {
Expand All @@ -298,11 +309,13 @@ export const DeleteDocuments = async (
}
});

console.log("DeleteDocuments response:", response);
return response;
}

return [documentsInChatResponse];
} catch (e) {
console.error("DeleteDocuments error:", e);
return [
{
status: "ERROR",
Expand All @@ -320,24 +333,28 @@ export const EmbedDocuments = async (
documents: Array<AzureSearchDocumentIndex>
): Promise<ServerActionResponse<Array<AzureSearchDocumentIndex>>> => {
try {
console.log("Embedding documents:", documents.map((d) => d.id));
const openai = OpenAIEmbeddingInstance();

const contentsToEmbed = documents.map((d) => d.pageContent);

const embeddings = await openai.embeddings.create({
input: contentsToEmbed,
model: process.env.AZURE_OPENAI_API_EMBEDDINGS_DEPLOYMENT_NAME,
});

console.log("Embeddings received:", embeddings);

embeddings.data.forEach((embedding, index) => {
documents[index].embedding = embedding.embedding;
});

console.log("Documents after embedding:", documents);
return {
status: "OK",
response: documents,
};
} catch (e) {
console.error("EmbedDocuments error:", e);
return {
status: "ERROR",
errors: [
Expand All @@ -353,13 +370,16 @@ export const EnsureIndexIsCreated = async (): Promise<
ServerActionResponse<SearchIndex>
> => {
try {
console.log("Ensuring index is created");
const client = AzureAISearchIndexClientInstance();
const result = await client.getIndex(process.env.AZURE_SEARCH_INDEX_NAME);
console.log("Index already exists:", result);
return {
status: "OK",
response: result,
};
} catch (e) {
console.log("Index does not exist, creating new index");
return await CreateSearchIndex();
}
};
Expand All @@ -368,6 +388,7 @@ const CreateSearchIndex = async (): Promise<
ServerActionResponse<SearchIndex>
> => {
try {
console.log("Creating search index");
const client = AzureAISearchIndexClientInstance();
const result = await client.createIndex({
name: process.env.AZURE_SEARCH_INDEX_NAME,
Expand Down Expand Up @@ -433,11 +454,13 @@ const CreateSearchIndex = async (): Promise<
],
});

console.log("Search index created:", result);
return {
status: "OK",
response: result,
};
} catch (e) {
console.error("CreateSearchIndex error:", e);
return {
status: "ERROR",
errors: [
Expand Down
Loading

0 comments on commit 2144f7d

Please sign in to comment.