Skip to content

Commit 1555755

Browse files
committed
refactor: rename 'keywords' to 'searchTerms' for semantic clarity
1 parent d4a5a09 commit 1555755

File tree

12 files changed

+31
-31
lines changed

12 files changed

+31
-31
lines changed

packages/ai-tool-retriever/README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ This library provides a lean core and a set of optional providers, allowing you
1616
- **Pluggable Architecture**: Explicitly choose and provide your embedding model and vector store.
1717
- **Default Local Providers**: Start with `@xenova/transformers` and an in-memory vector store.
1818
- **Bring Your Own Tech**: Implement your own `EmbeddingProvider` and `ToolStore` interfaces to use external services.
19-
- **Keyword Enhanced**: Improve search accuracy by adding custom `keywords` to your tool definitions.
19+
- **Search Term Enhanced**: Improve search accuracy by adding custom `searchTerms` to your tool definitions.
2020
- **Explicit Tool Forcing**: Users can force tool inclusion with a simple `[toolName]` syntax in their query.
2121
- **Configurable Retrieval**: Fine-tune results with similarity thresholds and control behavior for missing tools.
2222
- **Idempotent Syncing**: Built-in content hashing utilities to efficiently sync tools with vector stores.
@@ -61,7 +61,7 @@ const allMyTools: ToolDefinition[] = [
6161
description: "Fetches the weather for a given location.",
6262
parameters: z.object({ city: z.string() }),
6363
}),
64-
keywords: ["forecast", "temperature", "climate", "rain", "sun"],
64+
searchTerms: ["forecast", "temperature", "climate", "rain", "sun"],
6565
},
6666
{
6767
name: "searchFinancialNews",
@@ -70,7 +70,7 @@ const allMyTools: ToolDefinition[] = [
7070
"Searches for financial news articles about a company.",
7171
parameters: z.object({ ticker: z.string() }),
7272
}),
73-
keywords: ["stocks", "market", "earnings", "sec filings", "investing"],
73+
searchTerms: ["stocks", "market", "earnings", "sec filings", "investing"],
7474
},
7575
];
7676

@@ -198,13 +198,13 @@ To use a different vector database, implement the `ToolStore` interface. The `sy
198198

199199
> **Best Practice: Crafting Text for High-Quality Embeddings**
200200
>
201-
> The quality of the semantic search is highly dependent on the text used to generate the embedding for each tool. For best results, your `ToolStore`'s `sync` method should create a single, rich string that includes the tool's name, its detailed description, and any relevant keywords.
201+
> The quality of the semantic search is highly dependent on the text used to generate the embedding for each tool. For best results, your `ToolStore`'s `sync` method should create a single, rich string that includes the tool's name, its detailed description, and any relevant searchTerms.
202202
>
203203
> The default `InMemoryStore` uses the following format as its best practice, and replicating this pattern in your custom store will ensure you get great search accuracy:
204204
>
205205
> ```ts
206206
> const textToEmbed =
207-
> `${definition.name}: ${definition.tool.description}. Keywords: ${definition.keywords?.join(", ")}`.trim();
207+
> `${definition.name}: ${definition.tool.description}. Search Terms: ${definition.searchTerms?.join(", ")}`.trim();
208208
> ```
209209
210210
For persistent stores (like Supabase or Pinecone), it's crucial to implement `sync` efficiently to avoid re-calculating embeddings for unchanged tools on every application restart. The library provides a `createToolContentHash` utility to help with this.
@@ -316,8 +316,8 @@ export class SupabaseStore implements ToolStore {
316316
// Create the rich text for each tool to be embedded
317317
const textsToEmbed = toolsToUpsert.map((t) => {
318318
const description = t.tool.description || "";
319-
const keywords = t.keywords?.join(", ") || "";
320-
return `${t.name}: ${description}. Keywords: ${keywords}`.trim();
319+
const searchTerms = t.searchTerms?.join(", ") || "";
320+
return `${t.name}: ${description}. Search Terms: ${searchTerms}`.trim();
321321
});
322322

323323
// Use the provided embedding provider to get embeddings

packages/ai-tool-retriever/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "ai-tool-retriever",
33
"type": "module",
4-
"version": "1.0.4",
4+
"version": "1.1.0",
55
"description": "tool retriever for the ai-sdk lib",
66
"author": "gorango",
77
"license": "MIT",

packages/ai-tool-retriever/src/core/store.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export interface ToolWithMetadata {
1111
*/
1212
embedding: number[]
1313
/**
14-
* A hash of the tool's content (description, keywords, etc.) to detect changes.
14+
* A hash of the tool's content (description, searchTerms, etc.) to detect changes.
1515
*/
1616
contentHash: string
1717
}

packages/ai-tool-retriever/src/core/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ export interface ToolDefinition {
1515
*/
1616
tool: Tool<any, any>
1717
/**
18-
* A list of keywords, synonyms, or example use cases.
18+
* A list of search terms, synonyms, or example use cases.
1919
* This text is combined with the tool's description to create
2020
* a richer embedding for semantic search.
2121
*/
22-
keywords?: string[]
22+
searchTerms?: string[]
2323
}

packages/ai-tool-retriever/src/providers/store/in-memory.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ import { InMemoryStore } from './in-memory'
88
const toolADef: ToolDefinition = {
99
name: 'toolA',
1010
tool: createTool({ description: 'Does A', inputSchema: z.object({}) }),
11-
keywords: ['alpha', 'apple'],
11+
searchTerms: ['alpha', 'apple'],
1212
}
1313
const toolBDef: ToolDefinition = {
1414
name: 'toolB',
1515
tool: createTool({ description: 'Does B', inputSchema: z.object({}) }),
16-
keywords: ['bravo', 'banana'],
16+
searchTerms: ['bravo', 'banana'],
1717
}
1818
const toolCDef: ToolDefinition = {
1919
name: 'toolC',
@@ -121,19 +121,19 @@ describe('InMemoryStore', () => {
121121
await store.sync([toolADef, toolBDef], mockEmbeddingProvider)
122122
expect(mockEmbeddingProvider.getFloatEmbeddingsBatch).toHaveBeenCalledTimes(1)
123123
expect(mockEmbeddingProvider.getFloatEmbeddingsBatch).toHaveBeenCalledWith([
124-
'toolA: Does A. Keywords: alpha, apple',
125-
'toolB: Does B. Keywords: banana, bravo',
124+
'toolA: Does A. Search Terms: alpha, apple',
125+
'toolB: Does B. Search Terms: banana, bravo',
126126
])
127127

128128
vi.clearAllMocks()
129129

130-
const updatedToolADef = { ...toolADef, keywords: ['alpha', 'avocado'] } // Change toolA
130+
const updatedToolADef = { ...toolADef, searchTerms: ['alpha', 'avocado'] } // Change toolA
131131

132132
// Second sync, should only re-embed the changed tool (toolA)
133133
await store.sync([updatedToolADef, toolBDef], mockEmbeddingProvider)
134134
expect(mockEmbeddingProvider.getFloatEmbeddingsBatch).toHaveBeenCalledTimes(1)
135135
expect(mockEmbeddingProvider.getFloatEmbeddingsBatch).toHaveBeenCalledWith([
136-
'toolA: Does A. Keywords: alpha, avocado',
136+
'toolA: Does A. Search Terms: alpha, avocado',
137137
])
138138
})
139139
})

packages/ai-tool-retriever/src/providers/store/in-memory.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ export class InMemoryStore implements ToolStore {
3838
if (toolsToEmbed.length > 0) {
3939
const textsToEmbed = toolsToEmbed.map((definition) => {
4040
const description = definition.tool.description || ''
41-
const keywords = definition.keywords?.join(', ') || ''
42-
return `${definition.name}: ${description}. Keywords: ${keywords}`.trim()
41+
const searchTerms = definition.searchTerms?.join(', ') || ''
42+
return `${definition.name}: ${description}. Search Terms: ${searchTerms}`.trim()
4343
})
4444

4545
const embeddings = await embeddingProvider.getFloatEmbeddingsBatch(textsToEmbed)

packages/ai-tool-retriever/src/utils/utils.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,14 @@ describe('createToolContentHash', () => {
8181
description: 'A test tool',
8282
inputSchema: z.object({ param: z.string() }),
8383
}),
84-
keywords: ['test', 'alpha'],
84+
searchTerms: ['test', 'alpha'],
8585
}
8686

8787
it('should be deterministic for the same tool definition', () => {
8888
const hash1 = createToolContentHash(baseTool)
8989
const hash2 = createToolContentHash(baseTool)
9090
expect(hash1).toBe(hash2)
91-
expect(hash1).toBe('9fcdb9d4de7269f1a936c267bb6eb8a54bf4f9999dea7117d5b8a652a49a8fce')
91+
expect(hash1).toBe('dc843f3c4ed33aa68c4287504d5402eb5e55f215dca028f28e54e9fcaf0b7d3c')
9292
})
9393

9494
it('should produce a different hash if the description changes', () => {
@@ -101,20 +101,20 @@ describe('createToolContentHash', () => {
101101
expect(hash1).not.toBe(hash2)
102102
})
103103

104-
it('should produce a different hash if keywords are added or changed', () => {
104+
it('should produce a different hash if searchTerms are added or changed', () => {
105105
const modifiedTool: ToolDefinition = {
106106
...baseTool,
107-
keywords: ['test', 'alpha', 'beta'],
107+
searchTerms: ['test', 'alpha', 'beta'],
108108
}
109109
const hash1 = createToolContentHash(baseTool)
110110
const hash2 = createToolContentHash(modifiedTool)
111111
expect(hash1).not.toBe(hash2)
112112
})
113113

114-
it('should produce the same hash regardless of keyword order', () => {
114+
it('should produce the same hash regardless of search term order', () => {
115115
const modifiedTool: ToolDefinition = {
116116
...baseTool,
117-
keywords: ['alpha', 'test'], // flipped order
117+
searchTerms: ['alpha', 'test'], // flipped order
118118
}
119119
const hash1 = createToolContentHash(baseTool)
120120
const hash2 = createToolContentHash(modifiedTool)

packages/ai-tool-retriever/src/utils/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export function createToolContentHash(definition: ToolDefinition): string {
5656
name: definition.name,
5757
description: definition.tool.description,
5858
parameters: definition.tool.inputSchema, // zod schema for the hash
59-
keywords: definition.keywords?.sort(), // sort keywords for a stable hash
59+
searchTerms: definition.searchTerms?.sort(), // sort searchTerms for a stable hash
6060
})
6161
return createHash('sha256').update(sourceText).digest('hex')
6262
}

packages/benchmark/src/search-scalability.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function createMockTools(count: number): ToolDefinition[] {
2525
description: `This is mock tool #${i}`,
2626
inputSchema: z.object({}),
2727
}),
28-
keywords: [randomBytes(4).toString('hex')],
28+
searchTerms: [randomBytes(4).toString('hex')],
2929
}))
3030
}
3131

packages/benchmark/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
"rootDir": "src",
55
"outDir": "dist"
66
},
7-
"include": ["./**/*.ts"]
7+
"include": ["src/**/*.ts"]
88
}

0 commit comments

Comments
 (0)