From 3269e9b67d21ab1c2df61dcc89a2c5f3c3ab5593 Mon Sep 17 00:00:00 2001 From: Travis Fischer Date: Tue, 4 Jun 2024 06:38:59 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=8F=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 10 ++++++++++ pnpm-lock.yaml | 8 ++++++++ src/services/perigon-client.ts | 4 +--- src/tools/calculator.ts | 22 ++++++++++++++++++++++ tsup.config.ts | 3 ++- 5 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 src/tools/calculator.ts diff --git a/package.json b/package.json index 42cd386..7728f8b 100644 --- a/package.json +++ b/package.json @@ -37,6 +37,11 @@ "types": "./dist/sdks/genkit.d.ts", "import": "./dist/sdks/genkit.js", "default": "./dist/sdks/genkit.js" + }, + "./calculator": { + "types": "./dist/tools/calculator.d.ts", + "import": "./dist/tools/calculator.js", + "default": "./dist/tools/calculator.js" } }, "files": [ @@ -81,6 +86,7 @@ "del-cli": "^5.1.0", "dotenv": "^16.4.5", "eslint": "^8.57.0", + "expr-eval": "^2.0.2", "husky": "^9.0.11", "lint-staged": "^15.2.5", "np": "^10.0.5", @@ -99,6 +105,7 @@ "@dexaai/dexter": "^2.0.3", "@genkit-ai/ai": "^0.5.2", "@langchain/core": "^0.2.5", + "expr-eval": "^2.0.2", "ai": "^3.1.22" }, "peerDependenciesMeta": { @@ -111,6 +118,9 @@ "@langchain/core": { "optional": true }, + "expr-eval": { + "optional": true + }, "ai": { "optional": true } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 2da3744..a16bcc4 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -75,6 +75,9 @@ importers: eslint: specifier: ^8.57.0 version: 8.57.0 + expr-eval: + specifier: ^2.0.2 + version: 2.0.2 husky: specifier: ^9.0.11 version: 9.0.11 @@ -1914,6 +1917,9 @@ packages: resolution: {integrity: sha512-Fqs7ChZm72y40wKjOFXBKg7nJZvQJmewP5/7LtePDdnah/+FH9Hp5sgMujSCMPXlxOAW2//1jrW9pnsY7o20vQ==} engines: {node: '>=18'} + expr-eval@2.0.2: + resolution: {integrity: sha512-4EMSHGOPSwAfBiibw3ndnP0AvjDWLsMvGOvWEZ2F96IGk0bIVdjQisOHxReSkE13mHcfbuCiXw+G4y0zv6N8Eg==} + express@4.19.2: resolution: {integrity: sha512-5T6nhjsT+EOMzuck8JjBHARTHfMht0POzlA60WV2pMD3gyXw2LZnZ+ueGdNxG+0calOJcWKbpFcuzLZ91YWq9Q==} engines: {node: '>= 0.10.0'} @@ -6519,6 +6525,8 @@ snapshots: exit-hook@4.0.0: {} + expr-eval@2.0.2: {} + express@4.19.2: dependencies: accepts: 1.3.8 diff --git a/src/services/perigon-client.ts b/src/services/perigon-client.ts index b239c9d..e378ed1 100644 --- a/src/services/perigon-client.ts +++ b/src/services/perigon-client.ts @@ -106,13 +106,11 @@ export namespace perigon { q: z.string() .describe(`Search query. It may use boolean operators (AND, OR, NOT) and quotes for exact matching. Example search queries: -- AI agents -- Compare the latest predictions and popular opinions on the 2024 US election +- election news - "elon musk" AND tesla - (upcoming release OR launch) AND apple - (Google OR Amazon) AND NOT ("Jeff Bezos" OR Android) - "climate change" -- Crypto* OR Bitcoin NOT Ethereum `), title: z .string() diff --git a/src/tools/calculator.ts b/src/tools/calculator.ts new file mode 100644 index 0000000..bed49c4 --- /dev/null +++ b/src/tools/calculator.ts @@ -0,0 +1,22 @@ +import { Parser } from 'expr-eval' +import { z } from 'zod' + +import { createAIFunction } from '../create-ai-function.js' + +export const CalculatorInputSchema = z.object({ + expr: z.string().describe('mathematical expression to evaluate') +}) +export type CalculatorInput = z.infer + +export const calculator = createAIFunction( + { + name: 'calculator', + description: + 'Computes the result of simple mathematical expressions. Handles basic arithmetic operations like addition, subtraction, multiplication, division, exponentiation, and common functions like sin, cos, abs, exp, and random.', + inputSchema: CalculatorInputSchema + }, + async (input: CalculatorInput) => { + const result: number = Parser.evaluate(input.expr) + return result + } +) diff --git a/tsup.config.ts b/tsup.config.ts index 65b2015..30d2242 100644 --- a/tsup.config.ts +++ b/tsup.config.ts @@ -6,7 +6,8 @@ export default defineConfig([ 'src/index.ts', 'src/sdks/ai-sdk.ts', 'src/sdks/dexter.ts', - 'src/sdks/genkit.ts' + 'src/sdks/genkit.ts', + 'src/tools/calculator.ts' ], outDir: 'dist', target: 'node18',