forked from i-am-bee/bee-agent-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tool): add elasticsearch (i-am-bee#138)
Signed-off-by: Mahmoud Abughali <[email protected]> Signed-off-by: Tomas Dvorak <[email protected]> Co-authored-by: Mahmoud Abughali <[email protected]> Co-authored-by: Tomas Dvorak <[email protected]>
- Loading branch information
1 parent
c749a90
commit b603265
Showing
9 changed files
with
455 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import "dotenv/config.js"; | ||
import { BeeAgent } from "bee-agent-framework/agents/bee/agent"; | ||
import { OpenAIChatLLM } from "bee-agent-framework/adapters/openai/chat"; | ||
import { ElasticSearchTool } from "bee-agent-framework/tools/database/elasticsearch"; | ||
import { FrameworkError } from "bee-agent-framework/errors"; | ||
import { UnconstrainedMemory } from "bee-agent-framework/memory/unconstrainedMemory"; | ||
import { createConsoleReader } from "../helpers/io.js"; | ||
|
||
const llm = new OpenAIChatLLM(); | ||
|
||
const elasticSearchTool = new ElasticSearchTool({ | ||
connection: { | ||
node: process.env.ELASTICSEARCH_NODE, | ||
auth: { | ||
apiKey: process.env.ELASTICSEARCH_API_KEY || "", | ||
}, | ||
}, | ||
}); | ||
|
||
const agent = new BeeAgent({ | ||
llm, | ||
memory: new UnconstrainedMemory(), | ||
tools: [elasticSearchTool], | ||
}); | ||
|
||
const reader = createConsoleReader(); | ||
const prompt = await reader.prompt(); | ||
|
||
try { | ||
const response = await agent | ||
.run( | ||
{ prompt }, | ||
{ | ||
execution: { | ||
maxRetriesPerStep: 5, | ||
totalMaxRetries: 10, | ||
maxIterations: 15, | ||
}, | ||
}, | ||
) | ||
.observe((emitter) => { | ||
emitter.on("error", ({ error }) => { | ||
console.log(`Agent 🤖 : `, FrameworkError.ensure(error).dump()); | ||
}); | ||
emitter.on("retry", () => { | ||
console.log(`Agent 🤖 : `, "retrying the action..."); | ||
}); | ||
emitter.on("update", async ({ data, update, meta }) => { | ||
console.log(`Agent (${update.key}) 🤖 : `, update.value); | ||
}); | ||
}); | ||
|
||
console.log(`Agent 🤖 : `, response.result.text); | ||
} catch (error) { | ||
console.error(FrameworkError.ensure(error).dump()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/** | ||
* Copyright 2024 IBM Corp. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { describe, it, expect, beforeEach, vi } from "vitest"; | ||
import { ElasticSearchTool, ElasticSearchToolOptions } from "@/tools/database/elasticsearch.js"; | ||
import { verifyDeserialization } from "@tests/e2e/utils.js"; | ||
import { JSONToolOutput } from "@/tools/base.js"; | ||
import { SlidingCache } from "@/cache/slidingCache.js"; | ||
import { Task } from "promise-based-task"; | ||
|
||
const mockClient = { | ||
cat: { indices: vi.fn() }, | ||
indices: { getMapping: vi.fn() }, | ||
search: vi.fn(), | ||
info: vi.fn(), | ||
}; | ||
|
||
vi.mock("@elastic/elasticsearch", () => ({ | ||
Client: vi.fn(() => mockClient), | ||
})); | ||
|
||
describe("ElasticSearchTool", () => { | ||
let elasticSearchTool: ElasticSearchTool; | ||
|
||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
elasticSearchTool = new ElasticSearchTool({ | ||
connection: { node: "http://localhost:9200" }, | ||
} as ElasticSearchToolOptions); | ||
}); | ||
|
||
it("lists indices correctly", async () => { | ||
const mockIndices = [{ index: "index1" }, { index: "index2" }]; | ||
mockClient.cat.indices.mockResolvedValueOnce(mockIndices); | ||
|
||
const response = await elasticSearchTool.run({ action: "LIST_INDICES" }); | ||
expect(response.result).toEqual([{ index: "index1" }, { index: "index2" }]); | ||
}); | ||
|
||
it("gets index details", async () => { | ||
const indexName = "index1"; | ||
const mockIndexDetails = { | ||
[indexName]: { mappings: { properties: { field1: { type: "text" } } } }, | ||
}; | ||
mockClient.indices.getMapping.mockResolvedValueOnce(mockIndexDetails); | ||
|
||
const response = await elasticSearchTool.run({ action: "GET_INDEX_DETAILS", indexName }); | ||
expect(response.result).toEqual(mockIndexDetails); | ||
}); | ||
|
||
it("performs a search", async () => { | ||
const indexName = "index1"; | ||
const query = JSON.stringify({ query: { match_all: {} } }); | ||
const mockSearchResponse = { hits: { hits: [{ _source: { field1: "value1" } }] } }; | ||
mockClient.search.mockResolvedValueOnce(mockSearchResponse); | ||
|
||
const response = await elasticSearchTool.run({ | ||
action: "SEARCH", | ||
indexName, | ||
query, | ||
start: 0, | ||
size: 1, | ||
}); | ||
expect(response.result).toEqual([{ field1: "value1" }]); | ||
}); | ||
|
||
it("throws missing index name error", async () => { | ||
await expect(elasticSearchTool.run({ action: "GET_INDEX_DETAILS" })).rejects.toThrow( | ||
"Index name is required for GET_INDEX_DETAILS action.", | ||
); | ||
}); | ||
|
||
it("throws missing index and query error", async () => { | ||
await expect(elasticSearchTool.run({ action: "SEARCH" })).rejects.toThrow( | ||
"Both index name and query are required for SEARCH action.", | ||
); | ||
}); | ||
|
||
it("serializes", async () => { | ||
const elasticSearchTool = new ElasticSearchTool({ | ||
connection: { node: "http://localhost:9200" }, | ||
cache: new SlidingCache({ | ||
size: 10, | ||
ttl: 1000, | ||
}), | ||
}); | ||
|
||
await elasticSearchTool.cache!.set( | ||
"connection", | ||
Task.resolve(new JSONToolOutput([{ index: "index1", detail: "sample" }])), | ||
); | ||
|
||
const serialized = elasticSearchTool.serialize(); | ||
const deserializedTool = ElasticSearchTool.fromSerialized(serialized); | ||
|
||
expect(await deserializedTool.cache.get("connection")).toStrictEqual( | ||
await elasticSearchTool.cache.get("connection"), | ||
); | ||
verifyDeserialization(elasticSearchTool, deserializedTool); | ||
}); | ||
}); |
Oops, something went wrong.