Skip to content

Commit 7339d97

Browse files
committed
release semantic search v1
1 parent b5adfcb commit 7339d97

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ server.setRequestHandler(ListToolsRequestSchema, async () => {
9494
description:
9595
"Use semantic search to find an endpoint on the `Hugging Face Spaces` service. The search term will usually " +
9696
"be 3-7 words describing a task or activity the Person is trying to accomplish. The results are returned in a markdown table. " +
97-
"After use, await specific guidance from the Person before taking further action or tool calls. ",
97+
"Present all results to the Person. Await specific guidance from the Person before making further Tool calls.",
9898
inputSchema: {
9999
type: "object",
100100
properties: {

src/semantic_search.ts

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { config } from "./config.js";
2+
13
export interface SearchResult {
24
id: string;
35
title?: string;
@@ -11,26 +13,36 @@ export interface SearchResult {
1113
semanticRelevancyScore?: number;
1214
}
1315

16+
const RESULTS_TO_RETURN = 10;
17+
1418
export class SemanticSearch {
1519
private readonly apiUrl: string;
1620

17-
constructor() {
18-
// Allow overriding the API URL for testing via environment variable
19-
this.apiUrl =
20-
process.env.HF_SEARCH_API_URL ||
21-
"https://huggingface.co/api/spaces/semantic-search";
21+
constructor(apiUrl?: string) {
22+
this.apiUrl = apiUrl || "https://huggingface.co/api/spaces/semantic-search";
2223
}
2324

24-
async search(query: string, limit: number = 10): Promise<SearchResult[]> {
25+
async search(
26+
query: string,
27+
limit: number = RESULTS_TO_RETURN
28+
): Promise<SearchResult[]> {
2529
try {
2630
if (!query) {
2731
return [];
2832
}
2933

30-
const encodedQuery = encodeURIComponent(query);
31-
const url = `${this.apiUrl}?q=${encodedQuery}&sdk=gradio`;
34+
const url =
35+
`${this.apiUrl}?` + new URLSearchParams({ q: query, sdk: "gradio" });
36+
37+
const headers: HeadersInit = {
38+
"Content-Type": "application/json",
39+
};
40+
41+
if (config.hfToken) {
42+
headers["Authorization"] = `Bearer ${config.hfToken}`;
43+
}
3244

33-
const response = await fetch(url);
45+
const response = await fetch(url, headers);
3446

3547
if (!response.ok) {
3648
throw new Error(
@@ -67,7 +79,11 @@ export class SemanticSearch {
6779
const author = result.authorData?.fullname || result.author || "Unknown";
6880
const id = result.id || "";
6981

70-
markdown += `| ${escapeMarkdown(title)} | ${escapeMarkdown(description)} | ${escapeMarkdown(author)} | \`${escapeMarkdown(id)}\` |\n`;
82+
markdown +=
83+
`| [${escapeMarkdown(title)}](https://huggingface.co/api/spaces/${id}) ` +
84+
`| ${escapeMarkdown(description)} ` +
85+
`| ${escapeMarkdown(author)} ` +
86+
`| \`${escapeMarkdown(id)}\` |\n`;
7187
}
7288

7389
markdown +=

0 commit comments

Comments
 (0)