Skip to content

Commit

Permalink
Adopt questions endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
serefyarar committed May 17, 2024
1 parent 0308637 commit 58378b7
Show file tree
Hide file tree
Showing 9 changed files with 786 additions and 666 deletions.
73 changes: 39 additions & 34 deletions api/src/controllers/discovery.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,12 @@
import axios from "axios";
import { DIDService } from "../services/did.js";

import { ethers } from "ethers";

import RedisClient from "../clients/redis.js";
import { flattenSources } from "../utils/helpers.js";

const redis = RedisClient.getInstance();


const flattenSources = async (sources) => {
const didService = new DIDService();

const sourcePromises = sources.map(async (source) => {
if (source.includes("did:")) {
// TODO: check better
const did = source.split("/")[0];

let type;
if (source.includes("/index/starred")) {
type = "starred";
} else if (source.includes("/index/owned")) {
type = "owned";
}

return didService
.getIndexes(did, type)
.then((indexes) => indexes.map((i) => i.id));
} else {
const result = [source];
const subIndexes = await redis.hKeys(`index:${source}:subIndexes`);
if (subIndexes.length > 0) {
result.push(...subIndexes);
}
return Promise.resolve(result);
}
});

const results = await Promise.all(sourcePromises);
return results.flat();
};

export const chat = async (req, res, next) => {
const { id, messages, sources, ...rest } = req.body;

Expand Down Expand Up @@ -95,3 +64,39 @@ export const search = async (req, res, next) => {
res.status(500).json({ error: "Internal Server Error" });
}
};

export const questions = async (req, res, next) => {
try {
console.log(`mala`);
const { sources } = req.body;

const reqIndexIds = await flattenSources(sources);

const sourcesHash = ethers.utils.keccak256(
ethers.utils.toUtf8Bytes(JSON.stringify(reqIndexIds)),
);

const questionCache = await redis.get(`questions:${sourcesHash}`);

if (questionCache) {
return res.status(200).json(JSON.parse(questionCache));
}

try {
let response = await axios.post(
`${process.env.LLM_INDEXER_HOST}/chat/questions`,
{
indexIds: reqIndexIds,
},
);
redis.set(`questions:${sourcesHash}`, JSON.stringify(response.data), {
EX: 86400,
});
res.status(200).json(response.data);
} catch (error) {
return res.status(400).json({ error: error.message });
}
} catch (error) {
res.status(500).json({ error: error.message });
}
};
31 changes: 0 additions & 31 deletions api/src/controllers/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { getAddress } from "@ethersproject/address";
import axios from "axios";
import RedisClient from "../clients/redis.js";
import {
getPKPSession,
Expand Down Expand Up @@ -182,32 +180,3 @@ export const deleteIndex = async (req, res, next) => {
res.status(500).json({ error: error.message });
}
};
export const getQuestions = async (req, res, next) => {
try {
const indexService = new IndexService();
const index = await indexService.getIndexById(req.params.id);
if (!index) {
return res.status(404).json({ error: "Index not found" });
}

const question_cache = await redis.get(`questions:${req.params.id}`);

if (question_cache) {
return res.status(200).json(JSON.parse(question_cache));
}

try {
let response = await axios.get(
`${process.env.LLM_INDEXER_HOST}/chat/generate?indexId=${req.params.id}`,
);
redis.set(`questions:${req.params.id}`, JSON.stringify(response.data), {
EX: 86400,
});
res.status(200).json(response.data);
} catch (error) {
return res.status(400).json({ error: error.message });
}
} catch (error) {
res.status(500).json({ error: error.message });
}
};
Loading

0 comments on commit 58378b7

Please sign in to comment.