fixed a bug

This commit is contained in:
Dhravya 2024-06-16 11:58:27 -05:00
parent a68d7f0a75
commit 5cb5bcdbda
4 changed files with 19 additions and 16 deletions

View file

@ -101,7 +101,7 @@ app.post(
const body = c.req.valid("json");
const sourcesOnly = query.sourcesOnly === "true";
const spaces = query.spaces?.split(",") ?? [""];
const spaces = query.spaces?.split(",") ?? [undefined];
// Get the AI model maker and vector store
const { model, store } = await initQuery(c, query.model);
@ -118,7 +118,7 @@ app.post(
// SLICED to 5 to avoid too many queries
for (const space of spaces.slice(0, 5)) {
console.log("space", space);
if (space !== "") {
if (!space && spaces.length > 1) {
// it's possible for space list to be [undefined] so we only add space filter conditionally
filter.space = space;
}
@ -183,19 +183,11 @@ app.post(
return c.json({ ids: storedContent });
}
const vec = responses.matches.map((data) => ({ metadata: data.metadata }));
const vecWithScores = vec.map((v, i) => ({
...v,
score: sortedHighScoreData[i].score,
normalisedScore: sortedHighScoreData[i].normalizedScore,
}));
const preparedContext = vecWithScores.map(
({ metadata, score, normalisedScore }) => ({
const preparedContext = normalizedData.map(
({ metadata, score, normalizedScore }) => ({
context: `Website title: ${metadata!.title}\nDescription: ${metadata!.description}\nURL: ${metadata!.url}\nContent: ${metadata!.text}`,
score,
normalisedScore,
normalizedScore,
}),
);

View file

@ -18,13 +18,12 @@ export const template = ({ contexts, question }) => {
// Map over contexts to generate the context and score parts
const contextParts = contexts
.map(
({ context, score, normalisedScore }) => `
({ context, normalisedScore }) => `
<context>
${context}
</context>
<context_score>
score: ${score}
normalisedScore: ${normalisedScore}
</context_score>`,
)

View file

@ -46,6 +46,18 @@ function ChatWindow({
const router = useRouter();
const getAnswer = async (query: string, spaces: string[]) => {
const sourcesFetch = await fetch(
`/api/chat?q=${query}&spaces=${spaces}&sourcesOnly=true`,
{
method: "POST",
body: JSON.stringify({ chatHistory }),
},
);
// TODO: handle this properly
const sources = await sourcesFetch.json();
console.log(sources);
const resp = await fetch(`/api/chat?q=${query}&spaces=${spaces}`, {
method: "POST",
body: JSON.stringify({ chatHistory }),

View file

@ -54,7 +54,7 @@ export async function POST(req: NextRequest) {
);
const resp = await fetch(
`${process.env.BACKEND_BASE_URL}/api/chat?query=${query}&user=${session.user.email}&sourcesOnly=${sourcesOnly}&spaces=${spaces}`,
`${process.env.BACKEND_BASE_URL}/api/chat?query=${query}&user=${session.user.id}&sourcesOnly=${sourcesOnly}&spaces=${spaces}`,
{
headers: {
Authorization: `Bearer ${process.env.BACKEND_SECURITY_KEY}`,