Skip to content

Commit

Permalink
[tool] hugging face docs tool
Browse files Browse the repository at this point in the history
  • Loading branch information
mishig25 committed Jul 2, 2024
1 parent 900a9b3 commit 62b10f6
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/lib/server/tools/hfDocs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import type { BackendTool } from ".";
import { callSpace, getIpToken } from "./utils";

const hfDocs: BackendTool = {
name: "hf_docs",
displayName: "Hugging Face Docs",
description:
"Use this tool to get relevant docs snippets regarding Hugging Face open source libraries (transformers, diffusers, accelerate, huggingface_hub) and Hugging Face hf.co platform.",
isOnByDefault: true,
parameterDefinitions: {
query: {
required: true,
type: "string",
description:
"A search query which will be used to fetch the most relevant docs snippets regarding the user's query",
},
},
async *call({ query }, { messages, ip, username }) {
const ipToken = await getIpToken(ip, username);

const userMessages = messages.filter(({ from }) => from === "user");
const previousUserMessages = userMessages.slice(0, -1);

const queryWithPreviousMsgs =
(previousUserMessages.length
? `Previous questions: \n${previousUserMessages
.map(({ content }) => `- ${content}`)
.join("\n")}`
: "") +
"\n\nCurrent Question: " +
String(query);

const outputs = await callSpace<string[], string[]>(
"huggingchat/hf-docs",
"/predict",
[queryWithPreviousMsgs, "RAG-friendly"],
ipToken
);

return {
outputs: [{ hfDocs: outputs[0] }],
display: false,
};
},
};

export default hfDocs;
2 changes: 2 additions & 0 deletions src/lib/server/tools/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { MessageUpdate } from "$lib/types/MessageUpdate";
import type { Tool, ToolResultSuccess } from "$lib/types/Tool";

import calculator from "./calculator";
import hfDocs from "./hfDocs";
import directlyAnswer from "./directlyAnswer";
import imageEditing from "./images/editing";
import imageGeneration from "./images/generation";
Expand Down Expand Up @@ -30,4 +31,5 @@ export const allTools: BackendTool[] = [
imageEditing,
documentParser,
calculator,
hfDocs,
];

0 comments on commit 62b10f6

Please sign in to comment.