From c23be36b1ef82a940ac7e1891d1903ec8894fc67 Mon Sep 17 00:00:00 2001 From: adeelehsan Date: Wed, 24 Jul 2024 12:06:54 -0700 Subject: [PATCH] reformatted with 2 spaces --- src/utils/sendSearchRequest.ts | 370 ++++++++++++++++----------------- 1 file changed, 185 insertions(+), 185 deletions(-) diff --git a/src/utils/sendSearchRequest.ts b/src/utils/sendSearchRequest.ts index 9520d76..350dd24 100644 --- a/src/utils/sendSearchRequest.ts +++ b/src/utils/sendSearchRequest.ts @@ -1,105 +1,105 @@ import {ChatQueryBody, DEFAULT_DOMAIN, ChatQueryRequestHeaders, SummaryLanguage} from "../types"; type GenerationConfig = { - promptName?: string; - maxUsedSearchResults?: number; - promptText?: string; - maxResponseCharacters?: number; - responseLanguage?: SummaryLanguage; - modelParameters?: { - maxTokens: number; - temperature: number; - frequencyPenalty: number; - presencePenalty: number; - }; - citations?: { - style: "none" | "numeric"; - } | { - style: "html" | "markdown"; - urlPattern: string; - textPattern: string; - }; - enableFactualConsistencyScore?: boolean; + promptName?: string; + maxUsedSearchResults?: number; + promptText?: string; + maxResponseCharacters?: number; + responseLanguage?: SummaryLanguage; + modelParameters?: { + maxTokens: number; + temperature: number; + frequencyPenalty: number; + presencePenalty: number; + }; + citations?: { + style: "none" | "numeric"; + } | { + style: "html" | "markdown"; + urlPattern: string; + textPattern: string; + }; + enableFactualConsistencyScore?: boolean; }; type Config = { - customerId: string; - apiKey?: string; - authToken?: string; - domain?: string; - query: string; - corpusKeys: string; - search: { - metadataFilter: string; - lexicalInterpolation?: number; - customDimensions?: Record; - semantics?: "default" | "query" | "response"; - offset: number; - limit?: number; - contextConfiguration?: { - charactersBefore?: number; - charactersAfter?: number; - sentencesBefore?: number; - sentencesAfter?: number; - startTag?: string; - endTag?: string; - }; - reranker?: { - type: "none"; - } | { - type: "customer_reranker"; - rerankerId: string; - } | { - type: "mmr"; - diversityBias: number; - }; - }; - generation?: GenerationConfig; - chat?: { - store?: boolean; - conversationId?: string; - }; + customerId: string; + apiKey?: string; + authToken?: string; + domain?: string; + query: string; + corpusKeys: string; + search: { + metadataFilter: string; + lexicalInterpolation?: number; + customDimensions?: Record; + semantics?: "default" | "query" | "response"; + offset: number; + limit?: number; + contextConfiguration?: { + charactersBefore?: number; + charactersAfter?: number; + sentencesBefore?: number; + sentencesAfter?: number; + startTag?: string; + endTag?: string; + }; + reranker?: { + type: "none"; + } | { + type: "customer_reranker"; + rerankerId: string; + } | { + type: "mmr"; + diversityBias: number; + }; + }; + generation?: GenerationConfig; + chat?: { + store?: boolean; + conversationId?: string; + }; }; const convertReranker = (reranker?: Config["search"]["reranker"]) => { - if (!reranker) return; - - if (reranker.type === "none") { - return { - type: reranker.type - }; - } - - if (reranker.type === "customer_reranker") { - return { - type: reranker.type, - reranker_id: reranker.rerankerId - }; - } - - if (reranker.type === "mmr") { - return { - type: reranker.type, - diversity_bias: reranker.diversityBias - }; - } + if (!reranker) return; + + if (reranker.type === "none") { + return { + type: reranker.type + }; + } + + if (reranker.type === "customer_reranker") { + return { + type: reranker.type, + reranker_id: reranker.rerankerId + }; + } + + if (reranker.type === "mmr") { + return { + type: reranker.type, + diversity_bias: reranker.diversityBias + }; + } }; const convertCitations = (citations?: GenerationConfig["citations"]) => { - if (!citations) return; - - if (citations.style === "none" || citations.style === "numeric") { - return { - style: citations.style - }; - } - - if (citations.style === "html" || citations.style === "markdown") { - return { - style: citations.style, - url_pattern: citations.urlPattern, - text_pattern: citations.textPattern - }; - } + if (!citations) return; + + if (citations.style === "none" || citations.style === "numeric") { + return { + style: citations.style + }; + } + + if (citations.style === "html" || citations.style === "markdown") { + return { + style: citations.style, + url_pattern: citations.urlPattern, + text_pattern: citations.textPattern + }; + } }; @@ -116,98 +116,98 @@ export const sendSearchRequest = async ({ generation, chat }: Config) => { - const { - metadataFilter, - lexicalInterpolation, - customDimensions, - semantics, - offset, - limit, - contextConfiguration, - reranker - } = search - - const body: ChatQueryBody = { - query, - search: { - corpora: corpusKeys.split(",").map((key) => ( - { - corpus_key: key, - metadata_filter: metadataFilter, - lexical_interpolation: lexicalInterpolation, - custom_dimensions: customDimensions, - semantics - } - )), - offset, - limit, - context_configuration: { - characters_before: contextConfiguration?.charactersBefore, - characters_after: contextConfiguration?.charactersAfter, - sentences_before: contextConfiguration?.sentencesBefore, - sentences_after: contextConfiguration?.sentencesAfter, - start_tag: contextConfiguration?.startTag, - end_tag: contextConfiguration?.endTag - }, - reranker: convertReranker(reranker) - } - }; - - if (generation) { - const { - promptName, - maxUsedSearchResults, - promptText, - maxResponseCharacters, - responseLanguage, - modelParameters, - citations, - enableFactualConsistencyScore - } = generation; - - body.generation = { - prompt_name: promptName, - max_used_search_results: maxUsedSearchResults, - prompt_text: promptText, - max_response_characters: maxResponseCharacters, - response_language: responseLanguage, - model_parameters: modelParameters && { - max_tokens: modelParameters.maxTokens, - temperature: modelParameters.temperature, - frequency_penalty: modelParameters.frequencyPenalty, - presence_penalty: modelParameters.presencePenalty - }, - citations: convertCitations(citations), - enable_factual_consistency_score: enableFactualConsistencyScore - }; - } - - if (chat) { - body.chat = { - store: chat.store - }; - } - - const headers: ChatQueryRequestHeaders = { - "customer-id": customerId, - "Content-Type": "application/json" - }; - - if (apiKey) headers["x-api-key"] = apiKey; - - const url = `${domain ?? DEFAULT_DOMAIN}/v2/chats`; - const response = await fetch(url, { - method: "POST", - headers: headers, - body: JSON.stringify(body) - }); - - if (response.status === 400 || response.status === 403 || response.status === 404) { - const result = await response.json(); - throw new Error(`BAD REQUEST: ${result?.messages[0] ?? result.field_errors}`); - } - - if (response.status !== 200) throw new Error(response.status.toString()); - - return await response.json() + const { + metadataFilter, + lexicalInterpolation, + customDimensions, + semantics, + offset, + limit, + contextConfiguration, + reranker + } = search + + const body: ChatQueryBody = { + query, + search: { + corpora: corpusKeys.split(",").map((key) => ( + { + corpus_key: key, + metadata_filter: metadataFilter, + lexical_interpolation: lexicalInterpolation, + custom_dimensions: customDimensions, + semantics + } + )), + offset, + limit, + context_configuration: { + characters_before: contextConfiguration?.charactersBefore, + characters_after: contextConfiguration?.charactersAfter, + sentences_before: contextConfiguration?.sentencesBefore, + sentences_after: contextConfiguration?.sentencesAfter, + start_tag: contextConfiguration?.startTag, + end_tag: contextConfiguration?.endTag + }, + reranker: convertReranker(reranker) + } + }; + + if (generation) { + const { + promptName, + maxUsedSearchResults, + promptText, + maxResponseCharacters, + responseLanguage, + modelParameters, + citations, + enableFactualConsistencyScore + } = generation; + + body.generation = { + prompt_name: promptName, + max_used_search_results: maxUsedSearchResults, + prompt_text: promptText, + max_response_characters: maxResponseCharacters, + response_language: responseLanguage, + model_parameters: modelParameters && { + max_tokens: modelParameters.maxTokens, + temperature: modelParameters.temperature, + frequency_penalty: modelParameters.frequencyPenalty, + presence_penalty: modelParameters.presencePenalty + }, + citations: convertCitations(citations), + enable_factual_consistency_score: enableFactualConsistencyScore + }; + } + + if (chat) { + body.chat = { + store: chat.store + }; + } + + const headers: ChatQueryRequestHeaders = { + "customer-id": customerId, + "Content-Type": "application/json" + }; + + if (apiKey) headers["x-api-key"] = apiKey; + + const url = `${domain ?? DEFAULT_DOMAIN}/v2/chats`; + const response = await fetch(url, { + method: "POST", + headers: headers, + body: JSON.stringify(body) + }); + + if (response.status === 400 || response.status === 403 || response.status === 404) { + const result = await response.json(); + throw new Error(`BAD REQUEST: ${result?.messages[0] ?? result.field_errors}`); + } + + if (response.status !== 200) throw new Error(response.status.toString()); + + return await response.json() };