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

typechat-cli

v0.4.1

Published

CLI version of TypeChat

Downloads

21

Readme

TypeChat for the command line, on OpenAI, Ollama, and Anthropic

This is a utility application that wraps the TypeChat library and puts it in a command-line convenient for doing various repetitive AI-based tasks. Here's an example:

$ npx typechat-cli -s ./example/sentiment-schema.ts "This is cool and you are cool too"
{
  "sentiment": "positive"
}

The output of the command is a JSON object that conforms to the schema you give. Here's the example schema above, sentiment-schema.ts:

export interface ResponseShape {
  sentiment: 'positive' | 'negative' | 'neutral' // The sentiment of the input text, with positive, negative, and neutral as the only options
}

Note that the comments are very important! They are effectively your Prompt to OpenAI and it will be used to effectively process the input and generate the output. The schema is also used to validate the output of the OpenAI API. By default, typechat-cli will look for the root interface ResponseShape, though you can override this with the -t parameter.

Here's another example for extracting links found in a Markdown document:

// An extracted http or https link from the supplied input. If text does not have a link, it should be ignored.
export interface LinkInformation {
  url: string // The URL of the link. Ignore lines that do not have a link. Links must start with http:// or https://
  description: string // The description of the link given in the text. If no description is given, try to infer one from the URL
  category: string // The general category of the link and description, given as a single word
}

export interface ResponseShape {
  links: LinkInformation[]
}

You might use it like this:

## Extract all of the links from our docs
$ npx typechat-cli -s ./example/links-schema.ts ./docs/**/*.md

typechat-cli will dump a single JSON array out containing all of the results, in the same order as the files given.

A More Complex Example

This example from the TypeChat repo, shows just how complex you can make schemas and still get usable output. This example typically will have issues on less capable LLMs but works with GPT-4:

$ npx typechat-cli -s ./example/pizza-shop-schema.ts "I want three pizzas, one with mushrooms and the other two with sausage. Make one sausage a small. And give me a whole Greek and a Pale Ale. And give me a Mack and Jacks."

Output:

{
  "items": [
    { "itemType": "pizza", "addedToppings": ["mushrooms"], "quantity": 1 },
    { "itemType": "pizza", "addedToppings": ["sausage"], "quantity": 1 },
    {
      "itemType": "pizza",
      "size": "small",
      "addedToppings": ["sausage"],
      "quantity": 1
    },
    {
      "itemType": "salad",
      "portion": "whole",
      "style": "Greek",
      "quantity": 1
    },
    { "itemType": "beer", "kind": "Pale Ale", "quantity": 1 },
    { "itemType": "beer", "kind": "Mack and Jacks", "quantity": 1 }
  ]
}

How do I choose which AI service to use?

typechat-cli works with OpenAI, Anthropic's Claude, as well as Ollama. Setting the appropriate environment variable will choose which one to use:

  • OLLAMA_ENDPOINT - the base URL to use for Ollama, http://localhost:11434 is the usual one that Ollama runs on
  • OPENAI_API_KEY - the API key to use for OpenAI
  • ANTHROPIC_API_KEY - the API key to use for Claude

What model does this use?

By default, this uses gpt-4 for OpenAI, llama2 for Ollama, and claude-2.1 for Anthropic, though you can override it with the -m parameter. For many tasks, gpt-3.5-turbo will work and is much cheaper if using OpenAI!

Getting more verbose information

Sometimes when operating with an entire directory of files, it can be convenient to get the input file alongside the data. If the --with-text flag is passed in, the data will be returned in the form:

interface ReturnedData {
  filename: string | null // The fully qualified path to the input file, or null if prompt text was directly given
  input: string // The contents of the input file or prompt text
  data: ResponseShape // The data returned
}

Piping data in from stdin

You can also pipe data in from stdin, which will be used as the input text. For example:

$ echo "Yes this is very cool" | npx ts-node ./src/index.ts -s ./example/sentiment-schema.ts

No input provided, reading from stdin...
{
  "sentiment": "positive"
}

How do I run this from the Git repo?

$ npx ts-node ./src/index.ts -s ./example/sentiment-schema.ts "This is cool and you are cool too"