Skip to content

Commit

Permalink
Fixed proxy issue (#186)
Browse files Browse the repository at this point in the history
* Fixed proxy issue

* update
  • Loading branch information
adeelehsan authored Nov 8, 2024
1 parent b2e9110 commit a6b38ec
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 29 deletions.
5 changes: 3 additions & 2 deletions server/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const express = require("express");

const { createProxyMiddleware } = require("http-proxy-middleware");
const { legacyCreateProxyMiddleware:createProxyMiddleware } = require("http-proxy-middleware");
require("dotenv").config();
const app = express();
const port = process.env.PORT || 4444; // default port 4444 for local development and 3000 for docker
Expand All @@ -15,7 +15,7 @@ app.get("/", function (req, res) {
const proxyOptions = {
target: `https://${process.env.endpoint}`,
changeOrigin: true,
pathRewrite: { "^/v1/query": "/v1/query" },
pathRewrite: { "^/v1/query": "/v1/query", "^/v2/query": "/v2/query" },
onProxyReq: (proxyReq, req) => {
proxyReq.setHeader("Content-Type", "application/json");
proxyReq.setHeader("Accept", "application/json");
Expand All @@ -39,6 +39,7 @@ const proxyOptions = {
},
};
app.use("/v1/query", createProxyMiddleware(proxyOptions));
app.use("/v2/query", createProxyMiddleware(proxyOptions));

app.post("/config", (req, res) => {
const {
Expand Down
42 changes: 27 additions & 15 deletions src/contexts/apiV2sendSearchRequest.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {
SummaryLanguage,
QueryBody,
QueryRequestHeaders,
ChainReranker,
} from "../views/search/types";
import axios from "axios";

export type Reranker = {type: "none"}
| { type: "customer_reranker"; rerankerId: string }
Expand Down Expand Up @@ -213,26 +213,38 @@ export const apiV2sendSearchRequest = async ({
};
}

const headers: QueryRequestHeaders = {
"customer-id": customerId,
"Content-Type": "application/json"
};

if (apiKey) headers["x-api-key"] = apiKey;
let headers = {};
let url = "";
if (process.env.NODE_ENV === "production") {
// Call proxy server if in production
url = `/v2/query`;
headers = {
headers: {
"Content-Type": "application/json",
Accept: "application/json",
},
};
} else {
// Call directly if in development
url = `https://${endpoint}/v2/query`;
headers = {
headers: {
"Content-Type": "application/json",
Accept: "application/json",
"customer-id": customerId,
"x-api-key": apiKey,
},
};
}

const url = `https://${endpoint}/v2/query`;
const response = await fetch(url, {
method: "POST",
headers: headers,
body: JSON.stringify(body)
});
const response = await axios.post(url, body, headers);

if (response.status === 400 || response.status === 403 || response.status === 404) {
const result = await response.json();
const result = await response.data;
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()
return await response.data
};
5 changes: 1 addition & 4 deletions src/contexts/sendSearchRequest.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import axios from "axios";
import { START_TAG, END_TAG } from "../utils/parseSnippet";
import { normal_reranker_id, slingshot_reranker_id, SummaryLanguage } from "../views/search/types";
import { SummaryLanguage } from "../views/search/types";

type Reranker = {
isEnabled?: boolean,
Expand Down Expand Up @@ -65,14 +65,12 @@ const convertReranker = (reranker?: Reranker) => {
case "slingshot":
return {
reranker_name: "vectara-rrk-v1.0.0",
reranker_id: slingshot_reranker_id,
...(index + 1 < rerankerNames.length && {next_reranking_config: buildRerankingConfig(index + 1)})
};

case "normal":
return {
reranker_name: "Rerank_Multilingual_v1",
reranker_id: normal_reranker_id,
...(index + 1 < rerankerNames.length && {next_reranking_config: buildRerankingConfig(index + 1)})
};

Expand All @@ -83,7 +81,6 @@ const convertReranker = (reranker?: Reranker) => {
...(index + 1 < rerankerNames.length && {next_reranking_config: buildRerankingConfig(index + 1)})
};

// Add other reranker types as needed
default:
return {}
}
Expand Down
2 changes: 1 addition & 1 deletion src/views/search/SummaryUx.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export const SummaryUx = () => {

<VuiSpacer size="s" />

{(fcsMode !== "disable") && (
{(fcsMode !== "disable" && !isSummarizing) && (
<FactualConsistencyBadge
score={factualConsistencyScore}
fcsMode={fcsMode}
Expand Down
7 changes: 0 additions & 7 deletions src/views/search/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,13 +274,6 @@ export type QueryBody = {
generation?: GenerationConfiguration;
};

export type QueryRequestHeaders = {
["customer-id"]: string;
["Content-Type"]: string;
["x-api-key"]?: string;
["Authorization"]?: string;
};

export type ApiV2SearchResponse = {
search_results: SearchResult[];
factual_consistency_score: number;
Expand Down

0 comments on commit a6b38ec

Please sign in to comment.