npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

claude-function-ts

v0.1.2-beta-openai

Published

Function calling sdk for claude in typescript (supports JSONSchema)

Downloads

48

Readme

claude-function-ts

Function calling sdk for claude in typescript (supports JSONSchema)

Installation

You need to install the anthropic-sdk as well to use this sdk.

npm add claude-function-ts @anthropic-ai/sdk
yarn add claude-function-ts @anthropic-ai/sdk
pnpm add claude-function-ts @anthropic-ai/sdk
bun add claude-function-ts @anthropic-ai/sdk

Quick start (with Anthropic sdk)

import { Anthropic } from 'claude-function-ts';

const anthropic = new Anthropic({
  apiKey: "YOUR_API_KEY",
});

const resp = await anthropic.tools
  .create(
    [
    //  ...JSONSchema
    ] as const,
    {
      model: "claude-3-haiku-20240307",
      stream: false,
      max_tokens: 2000,
      messages: [
        {
          role: "user",
          content:
            "Send an email to rahul,zack and malay from scalar.video, I want to talk to him about how they built their CRDTs for their video editor; I am reaching out cold",
        },
      ],
    }
  )
  .manual();

Wrap Anthropic SDK

import {withTools} from 'claude-function-ts';
import {Anthropic} from '@anthropic/anthropic-sdk';

const anthropic = new Anthropic({
  apiKey: "YOUR_API_KEY",
});

const tools = withTools(anthropic);

tools.create(...)

API

Tools definition

Would recommend using something like zod to generate the json schema for the tools.

.tools.create(tool: Tool[], message: MessageParams, options: RequestOptions)

export type Tool = {
  name: Readonly<string>;
  description: string;
  parameters: JSONSchema;
};

Manual

Manual will just give you the output of one single function call, it will return either the assistant response or the tool inputs for your function call.

.tools.create(...).manual({forceFunctionCall = false}: {forceFunctionCall: boolean}): Promise<
      Result<
        | { role: "assistant"; content: string }
        | { role: "tool_inputs"; content?: string; tool_inputs: ToolInput[] },
        string
      >>;

export type ToolInput = {
  tool_name: string;
  tool_arguments: unknown;
};
  • the forceFunctionCall will ensure the model ends with a function call but forcing the stop sequence

Automatic

Automatic will keep calling the assistant with the function results till the assistant stops the conversation.

.tools.create(...).automatic(cb: {
    [toolName: string] : (params: unknown): Promise<{}> | {}
}): Promise<
      Result<
        { role: "assistant"; content: string },
        string
      >
    >

In automatic mode you need to ensure you have the tool callbacks for the tools you are using in the conversation.

  • if you use as const while defining the tools, you can use the toolNames and the params will be properly typed in the callback.

Example

Automatic Code Example

Disclaimer

I think the anthropic function calling api is kinda jank and this doesn't strictly follow it, sorta diverges to enable easy arrays and objects in the xml output. I expect to that the xml parsing here isn't perfect and happy to accept PRs to improve it.

This is based on the alpha-sdk for function calling from anthropic here