Skip to content

Commit

Permalink
reformatted with 2 spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
adeelehsan committed Jul 24, 2024
1 parent 604aa7d commit c23be36
Showing 1 changed file with 185 additions and 185 deletions.
370 changes: 185 additions & 185 deletions src/utils/sendSearchRequest.ts
Original file line number Diff line number Diff line change
@@ -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<string, number>;
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<string, number>;
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
};
}
};


Expand All @@ -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()
};

0 comments on commit c23be36

Please sign in to comment.