Skip to content

Commit

Permalink
Merge branch 'dev' into mainnet
Browse files Browse the repository at this point in the history
  • Loading branch information
serefyarar committed May 16, 2024
2 parents 6e881fd + 3927aaf commit db9302c
Show file tree
Hide file tree
Showing 38 changed files with 25,839 additions and 3,115 deletions.
69 changes: 46 additions & 23 deletions api/src/controllers/discovery.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,38 @@
import axios from 'axios';
import { DIDService } from '../services/did.js';
import axios from "axios";
import { DIDService } from "../services/did.js";

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 {
return Promise.resolve([source]);
}
});

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

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

const reqIndexIds = await flattenSources(sources);

const { id, messages, indexIds, did, type, ...rest } = req.body;
let reqIndexIds = [];
if(did){
const didService = new DIDService();
const indexes = await didService.getIndexes(did, type);
reqIndexIds = indexes.map(i => i.id)
}else {
reqIndexIds = indexIds
}
try {
const chatRequest = {
indexIds: reqIndexIds,
Expand All @@ -20,21 +41,22 @@ export const chat = async (req, res, next) => {
chat_history: [...messages.slice(0, -1)],
},
model_args: {
...rest
}
}
...rest,
},
};
let resp = await axios.post(
`${process.env.LLM_INDEXER_HOST}/chat/stream`,
chatRequest,
{
responseType: 'stream'
})
responseType: "stream",
},
);
res.set(resp.headers);
resp.data.pipe(res);
} catch (error) {
// Handle the exception
console.error('An error occurred:', error);
res.status(500).json({ error: 'Internal Server Error' });
console.error("An error occurred:", error);
res.status(500).json({ error: "Internal Server Error" });
}
};

Expand All @@ -46,19 +68,20 @@ export const search = async (req, res, next) => {
page: req.body.page || 1,
limit: req.body.limit || 10,
filters: req.body.filters || [],
}
};

let resp = await axios.post(
`${process.env.LLM_INDEXER_HOST}/search/query`,
searchRequest,
{
responseType: 'stream'
})
responseType: "stream",
},
);
res.set(resp.headers);
resp.data.pipe(res);
} catch (error) {
// Handle the exception
console.error('An error occurred:', error);
res.status(500).json({ error: 'Internal Server Error' });
console.error("An error occurred:", error);
res.status(500).json({ error: "Internal Server Error" });
}
};
10 changes: 2 additions & 8 deletions api/src/packages/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -379,18 +379,12 @@ app.post(
Joi.object({
id: Joi.string().required(),
messages: Joi.array().required(),
did: Joi.string().optional(),
temperature: Joi.number().optional(),
avg_log_prob: Joi.number().optional(),
maxTokens: Joi.number().optional(),
maxRetries: Joi.number().optional(),
type: Joi.when("did", {
is: Joi.exist(),
then: Joi.string().valid("owned", "starred").optional(),
otherwise: Joi.forbidden(),
}),
indexIds: Joi.array().items(Joi.string()).optional(),
}).or("did", "indexIds"),
sources: Joi.array().items(Joi.string()).required(),
}),
),
discoveryController.chat,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,7 @@ export default function ChatTabSection() {
maxHeight: "calc(-30rem + 100dvh)",
}}
>
<AskIndexes
// did={viewedIndex?.ownerDID.id}
chatID={chatID}
indexIds={[viewedIndex?.id]}
/>
<AskIndexes chatID={chatID} sources={[viewedIndex?.id]} />
</div>
) : null;
}
Expand Down
6 changes: 1 addition & 5 deletions web-app/src/components/sections/UserConversation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,7 @@ export default function UserConversationSection() {
height: "100%",
}}
>
<AskIndexes
// indexIds={leftSectionIndexes.map((i) => i.id)}
chatID={chatID}
did={id}
/>
<AskIndexes chatID={chatID} sources={[id]} />
</div>
</div>
);
Expand Down
19 changes: 12 additions & 7 deletions web-app/src/components/site/indexes/AskIndexes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,14 @@ export interface ChatProps extends ComponentProps<"div"> {

export interface AskIndexesProps {
chatID: string;
did?: string;
indexIds?: string[];
sources?: string[];
}

export interface MessageWithIndex extends Message {
index?: number;
}

const AskIndexes: FC<AskIndexesProps> = ({ chatID, did, indexIds }) => {
const AskIndexes: FC<AskIndexesProps> = ({ chatID, sources }) => {
const { viewedProfile, leftSectionIndexes, leftTabKey } = useApp();

const { session } = useAuth();
Expand Down Expand Up @@ -139,8 +138,7 @@ const AskIndexes: FC<AskIndexesProps> = ({ chatID, did, indexIds }) => {
id: chatID,
body: {
id: chatID,
did,
indexIds,
sources,
},
headers: {
"Content-Type": "application/json; charset=utf-8",
Expand Down Expand Up @@ -174,7 +172,12 @@ const AskIndexes: FC<AskIndexesProps> = ({ chatID, did, indexIds }) => {
<Flex
id={chatID}
key={chatID}
className={indexIds ? "px-0 pt-7" : "px-md-10 px-0 px-4 pt-7"}
className={
sources &&
sources?.filter((source) => !source.includes("did:")).length > 0
? "px-0 pt-7"
: "px-md-10 px-4 pt-7"
}
flexdirection={"column"}
style={{
display: "flex",
Expand Down Expand Up @@ -222,7 +225,9 @@ const AskIndexes: FC<AskIndexesProps> = ({ chatID, did, indexIds }) => {
<EmptyScreen
contextMessage={getChatContextMessage()}
setInput={setInput}
indexIds={indexIds}
indexIds={sources?.filter(
(source) => !source.includes("did:"),
)}
defaultQuestions={defaultQuestions}
/>
</Flex>
Expand Down
1 change: 1 addition & 0 deletions widget-demo/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
dist
3,881 changes: 3,881 additions & 0 deletions widget-demo/dist/bundle.js

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions widget-demo/dist/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Index Network - UI SDK Demo</title>
<script defer src="bundle.js"></script></head>
<body>
<div id="root"></div>
</body>
</html>
Loading

0 comments on commit db9302c

Please sign in to comment.