1.0.85 • Published 12 months ago

@brainstack/tools v1.0.85

Weekly downloads
-
License
MIT
Repository
-
Last release
12 months ago

@brainstack/tools

A collection of tools for building chat completion models with OpenAI.

Getting Started

To use this package, you'll need to create a new tool by extending the BaseTool abstract class. This class provides a basic implementation of a tool, including configuration management and a definition for the OpenAI Chat Completion model.

Creating a New Tool

To create a new tool, follow these steps:

  1. Create a new file for your tool, e.g. MyTool.ts.
  2. Import the BaseTool class from @brainstack/tools.
  3. Extend the BaseTool class and implement the required abstract methods:
    • name: a string that identifies your tool.
    • description: a string that describes what your tool does.
    • args: an object that defines the input parameters for your tool.
    • loadConfig(): a method that loads the configuration for your tool.
    • execute(args): a method that executes your tool with the given input parameters.
  4. Implement any additional logic or functionality required by your tool.

Here's an example of what your MyTool.ts file might look like:

import { BaseTool } from '@brainstack/tools';

interface MyConfig {
  foo: string;
  bar: number;
}

class MyTool extends BaseTool<MyConfig> {
  name = 'My Tool';
  description = 'This is my tool';
  args = {
    foo: { type: 'string', description: 'Foo parameter' },
    bar: { type: 'number', description: 'Bar parameter' },
  };

  protected loadConfig(): MyConfig {
    // Load configuration from a file or database
    return { foo: 'default foo', bar: 42 };
  }

  execute(args: any): Promise<string> {
    // Execute your tool logic here
    return Promise.resolve(`Hello, world! Foo is ${args.foo} and bar is ${args.bar}`);
  }
}

Using Your Tool

Once you've created your tool, you can use it in your application like this:

const tools: Tools = {
  database: new DatabaseTool(),
  finance: new MockTool(),
};

const userInput = 'What is the stock price of Apple?';
const context = 'finance';

const aiResponse = await this.aiService.askWithTool(
  userInput,
  context,
  tools
);
console.log(aiResponse);

In this example, we create an object tools that contains instances of different tools. We then pass this object to the askWithTool method, along with the user input and context. The askWithTool method will determine which tool to use based on the context and execute the tool with the given input parameters.

Configuration Management

The BaseTool class provides basic configuration management features. You can save and read configuration values using the saveConfig() and readConfig() methods.

For example:

const myTool = new MyTool();
myTool.saveConfig('foo', 'new foo value');
console.log(myTool.readConfig('foo')); // Output: "new foo value"

Definition for OpenAI Chat Completion

The BaseTool class also provides a definition property that returns a OpenAI Chat Completion model definition for your tool.

For example:

console.log(myTool.definition);
// Output:
// {
//   type: "function",
//   function: {
//     name: "My Tool",
//     description: "This is my tool",
//     parameters: {
//       type: "object",
//       properties: {
//         foo: { type: "string", description: "Foo parameter" },
//         bar: { type: "number", description: "Bar parameter" },
//       },
//       required: ["foo", "bar"],
//     },
//   },
// }
1.0.79

12 months ago

1.0.78

1 year ago

1.0.80

12 months ago

1.0.84

12 months ago

1.0.83

12 months ago

1.0.82

12 months ago

1.0.81

12 months ago

1.0.85

12 months ago

1.0.73

1 year ago

1.0.75

1 year ago

1.0.74

1 year ago

1.0.72

2 years ago

1.0.71

2 years ago

1.0.70

2 years ago

1.0.66

2 years ago

1.0.65

2 years ago

1.0.64

2 years ago

1.0.63

2 years ago

1.0.69

2 years ago

1.0.68

2 years ago

1.0.67

2 years ago

1.0.62

2 years ago

1.0.61

2 years ago

1.0.60

2 years ago

1.0.59

2 years ago

1.0.58

2 years ago

1.0.56

2 years ago

1.0.51

2 years ago

1.0.50

2 years ago

1.0.55

2 years ago

1.0.54

2 years ago

1.0.53

2 years ago

1.0.52

2 years ago

1.0.48

2 years ago

1.0.47

2 years ago

1.0.49

2 years ago

1.0.39

2 years ago

1.0.40

2 years ago

1.0.44

2 years ago

1.0.43

2 years ago

1.0.42

2 years ago

1.0.41

2 years ago

1.0.46

2 years ago

1.0.45

2 years ago

1.0.38

2 years ago

1.0.29

2 years ago

1.0.33

2 years ago

1.0.32

2 years ago

1.0.31

2 years ago

1.0.30

2 years ago

1.0.37

2 years ago

1.0.36

2 years ago

1.0.35

2 years ago

1.0.34

2 years ago

1.0.28

2 years ago

1.0.27

2 years ago

1.0.26

2 years ago

1.0.25

2 years ago

1.0.24

2 years ago

1.0.23

2 years ago

1.0.16

2 years ago

1.0.14

2 years ago