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

chatgpt-template

v2.1.2

Published

Talk to ChatGPT in JS TemplateString Style with Streaming Support

Downloads

26

Readme

GPT Template

Talk to ChatGPT in JS TemplateString Style with Streaming Support

Get Started

  1. Setup your env.OPENAI_API_KEY into your .env file
  2. Try following code:

Get full text once:

import { gpt } from "chatgpt-template";
console.log(await gpt`Hello, world`); // greetings

Get json:

import { gpt } from "chatgpt-template";

const obj = await gpt`
You are an AI assistant that speak only plain JSON without codeblock fence.

Now give me a json example about my dog.
`.json();

console.log(JSON.stringify(obj, null, 2));

/*
{
  "dog": {
    "name": "Buddy",
    "breed": "Golden Retriever",
    "age": 3,
    "color": "Golden",
    "weight": 70,
    "vaccinated": true,
    "favoriteToys": [
      "rubber ball",
      "tug rope",
      "squeaky toy"
    ],
    "owner": {
      "name": "John",
      "contact": {
        "phone": "123-456-7890",
        "email": "[email protected]"
      }
    }
  }
}
 */

Evaluate JS

import vm from "vm";
import { gpt } from "chatgpt-template";

const code = await gpt`
You are an AI assistant that speak only JavaScript (and JSDOC) without codeblock fence.

Now defined a function to validate password strength, level is from 1 to 5.

function passwordValidate(password): {level, notice};

Inputs: password
Output: { level, notice }

`.text();

console.log(code);
const context = vm.createContext();
const { level, notice } = new vm.Script(
  code + '; passwordValidate("Rue1DHuoP2DeCP16")',
).runInContext(context);
console.log({ level, notice });

Return as Streaming Response (Next.js)

import { gpt } from "chatgpt-template";
import DIE from "@snomiao/die";

export const GET = async (req: NextRequest) =>
  await gpt`
You are an AI flash card making assistant, please make flash cards for new words in given articles or note-lists, give me a csv with head Front,Back, which Front is Japanese word, and Back is "振仮名 of the Japanese world...<br />(English Translation...)"
Here is my input:

${req.nextUrl.searchParams.get("q") ?? DIE("Missing Query")}
`; // new Response( ... token ... stream ... )

Streaming to console:

import { gpt } from "chatgpt-template";
await gpt`
You are an AI flash card making assistant, please make flash cards for new words in given articles or note-lists, give me a csv with head Front,Back, which Front is Japanese word, and Back is "振仮名 of the Japanese world...<br />(English Translation...)"
Here is my input:

ロート製薬は、子どもの花粉症に関するアンケート調査の結果を公表した。花粉症で目がかゆい小学生の4人に1人が「授業など勉強に集中できない」と訴える一方、子どもが日常生活で感じる影響を親は十分に把握できていない状況も明らかになった。

【あなたの地域は?】スギ、ヒノキ…花粉が本格飛来する時期

 調査は1月24~25日、0歳から16歳の子どもを持つ親7131人を対象にインターネットで行った。

 子どもが「花粉症と診断された」「花粉症だと思う」と回答したのは42・6%で、2014年12月調査の「花粉症である」との回答(32・7%)から約10ポイント増加した。

 発症した平均年齢は5・8歳。症状を緩和するための対策で、最も多かったのは「マスクの着用」で60・9%、次いで「病院で処方された飲み薬の使用」が55・4%だった。小学生が感じる症状の第2位が「目のかゆみ」だったが、市販の目薬やメガネの使用などの対策はいずれも3割以下にとどまっていた。

 通学や外遊びなどで屋外にいる機会が多い子どもにとって「目のかゆみ」は日常生活への影響が大きい。親から「目のかゆみ」がある小学生に複数回答で状況を聞いてもらったところ、「日常生活に影響がある」と答えたのは53・9%に上り、「授業など勉強に集中できない」が25・6%、「外で遊びを楽しめない」が23%、「夜眠れない」が11%などと深刻な影響が出ていることが判明した。

 子どもが感じている目のかゆみによる日常生活への影響について、44・2%の親がこのアンケートに答えるまで把握していなかった内容があったと答えており、一緒に暮らしていても花粉症の影響を把握できていない実態も浮き彫りとなった。【佐久間一輝】

`.pipeTo(
  new WritableStream({
    write: (content) => {
      console.write(content);
    },
  }),
); // write to console

API v2 Design

gpt`...` => resposne...

type gptResponse = ReadableStream<string> & {
  [Symbol.AsyncIterable]: ..., // token stream,
  readable:
  writalbe: // fill to template blanks
  response: new response
  text:  ()=>Promise< string>,
  json:  ()=>Promise< any>,
  then: ()=>Promise< string>,
  catch: ()=>Promise<...>, // network error
}
type gpt = (tsa: tsa, slots: srcs)=>gptResponse

### More [./examples HERE](./examples)

## Reference

Inspired by zx & bun shell