Skip to content

Commit bb6d44a

Browse files
authored
Merge pull request #491 from M4xymm/RDBC-932
RDBC-932 - Add examples for using AI Agents in Node.js
2 parents ca6a717 + 1cd3360 commit bb6d44a

File tree

1 file changed

+107
-1
lines changed

1 file changed

+107
-1
lines changed

README.md

Lines changed: 107 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ npm install --save ravendb
2222
[Asynchronous call types](#supported-asynchronous-call-types),
2323
[Crud example](#crud-example),
2424
[Query documents](#query-documents),
25+
[Ai agents](#ai-agents),
2526
[Attachments](#attachments),
2627
[Time series](#timeseries),
2728
[Bulk insert](#bulk-insert),
@@ -777,6 +778,111 @@ const results = await session.query({ collection: "users" })
777778
> <small>[query first and single](https://github.com/ravendb/ravendb-nodejs-client/blob/5c14565d0c307d22e134530c8d63b09dfddcfb5b/test/Ported/QueryTest.ts#L467)</small>
778779
> <small>[query count](https://github.com/ravendb/ravendb-nodejs-client/blob/5c14565d0c307d22e134530c8d63b09dfddcfb5b/test/Ported/QueryTest.ts#L834)</small>
779780
781+
## Ai agents
782+
783+
#### Create an AI agent
784+
```javascript
785+
const agentConfiguration = {
786+
name: "ravendb-ai-agent",
787+
connectionStringName: "<Your connection string name>",
788+
systemPrompt: `
789+
You work for a human experience manager.
790+
The manager uses your services to find which employee earned the largest profit
791+
for the company and suggest a reward for this employee.
792+
The manager provides you with the name of a country, or with the word "everything"
793+
to indicate all countries.
794+
795+
Steps:
796+
1. Use a query tool to load all orders sent to the selected country (or all countries).
797+
2. Calculate which employee made the largest profit.
798+
3. Use a query tool to learn in what region the employee lives.
799+
4. Find suitable vacation sites or other rewards based on the employee's region.
800+
5. Use an action tool to store the employee's ID, profit, and reward suggestions in the database.
801+
802+
When you're done, return these details in your answer to the user as well.
803+
`,
804+
sampleObject: JSON.stringify({
805+
employeeID: "the ID of the employee that made the largest profit",
806+
profit: "the profit the employee made",
807+
suggestedReward: "your suggestions for a reward"
808+
}),
809+
parameters: [
810+
{
811+
name: "country",
812+
description: "A specific country that orders were shipped to, or 'everywhere' to look at all countries"
813+
}
814+
],
815+
maxModelIterationsPerCall: 3,
816+
chatTrimming: {
817+
tokens: {
818+
maxTokensBeforeSummarization: 32768,
819+
maxTokensAfterSummarization: 1024
820+
}
821+
},
822+
// Queries the agent can use
823+
queries: [
824+
{
825+
name: "retrieve-orders-sent-to-a-specific-country",
826+
description: "Retrieve all orders sent to a specific country",
827+
query: "from Orders as O where O.ShipTo.Country == $country select O.Employee, O.Lines.Quantity",
828+
parametersSampleObject: "{}"
829+
},
830+
{
831+
name: "retrieve-performer-living-region",
832+
description: "Retrieve an employee's country, city, and region by employee ID",
833+
query: "from Employees as E where id() == $employeeId select E.Address.Country, E.Address.City, E.Address.Region",
834+
parametersSampleObject: "{ \"employeeId\": \"embed the employee's ID here\" }"
835+
}
836+
],
837+
// Actions the agent can perform
838+
actions: [
839+
{
840+
name: "store-performer-details",
841+
description: "Store the employee ID, profit, and suggested reward in the database.",
842+
parametersSampleObject: "{ \"employeeID\": \"embed the employee's ID here\", \"profit\": \"embed the employee's profit here\", \"suggestedReward\": \"embed your suggestions for a reward here\" }"
843+
}
844+
]
845+
};
846+
847+
const agent = await store.ai.createAgent(agentConfiguration);
848+
```
849+
850+
>##### Related tests:
851+
> <small>[create an agent](https://github.com/ravendb/ravendb-nodejs-client/blob/f6a223593d16028f8207d0c3da1808744fe47323/test/Ported/Documents/Operations/AiAgentTest.ts#L22-L66)</small>
852+
> <small>[update an agent](https://github.com/ravendb/ravendb-nodejs-client/blob/f6a223593d16028f8207d0c3da1808744fe47323/test/Ported/Documents/Operations/AiAgentTest.ts#L68-L101)</small>
853+
854+
855+
#### Run a Conversation with Tools
856+
```javascript
857+
const chat = store.ai.conversation(agent.identifier, "Performers/", {
858+
parameters: {
859+
country: "France"
860+
}
861+
});
862+
863+
// Register handler for action tool: "store-performer-details"
864+
chat.handle("store-performer-details", async (req, performer) => {
865+
const session = store.openSession();
866+
const rewarded = new Performer(performer.employeeID, performer.profit, performer.suggestedReward);
867+
868+
await session.store(rewarded);
869+
await session.saveChanges();
870+
session.dispose();
871+
872+
return "done"; // return indication that the action succeeded
873+
});
874+
875+
// Ask the agent to suggest a reward
876+
chat.setUserPrompt("send a few suggestions to reward the employee that made the largest profit");
877+
878+
// Run the conversation
879+
const llmResponse = await chat.run();
880+
881+
console.log("Agent response:", llmResponse);
882+
883+
if (llmResponse.status === "Done") console.log("Conversation finished.")
884+
```
885+
780886
## Attachments
781887

782888
#### Store attachments
@@ -857,7 +963,7 @@ await session.advanced.attachments.getNames(doc);
857963
>##### Related tests:
858964
> <small>[get attachment names](https://github.com/ravendb/ravendb-nodejs-client/blob/5c14565d0c307d22e134530c8d63b09dfddcfb5b/test/Documents/ReadmeSamples.ts#L266)</small>
859965
> <small>[get attachment names 2](https://github.com/ravendb/ravendb-nodejs-client/blob/5c14565d0c307d22e134530c8d63b09dfddcfb5b/test/Ported/Attachments/AttachmentsSessionTest.ts#L288)</small>
860-
>
966+
861967
## TimeSeries
862968

863969
#### Store time series

0 commit comments

Comments
 (0)