Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ai-fns

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ai-fns

Instantly convert your JavaScript functions into functions which can be called by GPT-3.5 and GPT-4.

  • 1.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
7
Maintainers
1
Weekly downloads
 
Created
Source

ai-fns

ai-fns is a a library for building 100% type-safe functions which can be called by OpenAI's ChatGPT

Features

  • 100% Type-safe - ai-fns uses zod to validate the input and output of your functions
  • Easy to use - ai-fns provides a simple API for creating functions
  • Lightweight - ai-fns is a small library with minimal dependencies

Installation

pnpm install ai-fns

Basic Example

Here's an example of a function that calculates the output of a given mathematical expression:

import { Parser } from "expr-eval";
import { z } from "zod";
import { aifn } from "../utils";

const parser = new Parser();

export default aifn(
  "calculator",
  "Calculate the output of a given mathematical expression",
  z.object({
    expression: z.string(),
  }),
  ({ expression }) => {
    try {
      const result = parser.parse(expression).evaluate();
      return result;
    } catch (error) {
      return `Failed to execute script: ${error.message}`;
    }
  }
);

Now, you can just ask ChatGPT to do some math for you:

User: What's 45^(2.12) / 45?
Assistant: The result of 45^(2.12) / 45 is approximately 71.06.

Advanced Example

Here's an example of a function that fetches the latest news from an rss feed:

import Parser from "rss-parser";
import { z } from "zod";
import { aifn } from "../utils";

const parser = new Parser();

export const name = "rss";
export const description = "Get the latest news from an rss feed";
export const schema = z.object({
  url: z.string(),
});

export const rss = async ({ url }: z.infer<typeof schema>) => {
  try {
    const feed = await parser.parseURL(url);
    return feed;
  } catch (error) {
    return `Failed to execute script: ${error.message}`;
  }
};
export default aifn(name, description, schema, rss);

Let's ask ChatGPT to fetch the latest news from the Hacker News RSS feed

User: What's the top story on Hacker News today?
Assistant: The top story on Hacker News today is "A new method to reprogram human cells to better mimic embryonic stem cells". It has a score of 294 and 72 comments. You can read more about it [here](https://www.uwa.edu.au/news/Article/2023/August/Scientists-find-way-to-wipe-a-cells-memory-to-reprogram-it-as-a-stem-cell).

Make your own!

You can make your own functions by creating a new file in the tools directory. Here's a template:

import { z } from "zod";
import { aifn } from "../utils";

export const name = "name";
export const description = "description";
export const schema = z.object({
  // schema
});
export const fn = async ({}: /* schema */ z.infer<typeof schema>) => {
  // function body
};

export default aifn(name, description, schema, fn);

FAQs

Package last updated on 28 Aug 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc