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

cohere-api.js

v1.0.1

Published

A simple interface to interact with the Cohere API for natural language processing.

Downloads

307

Readme

Cohere API

A simple interface to interact with the Cohere API for natural language processing.

Version

1.0.1-ESM

Features

  • Sends prompts to the Cohere API for AI-generated responses.
  • Validates the length of prompts (max 2000 characters).
  • Implements event handling for "error" and "debug".
  • Optional terminal exit behavior after execution.

Requirement

Requires ES Module by Version 1.0.1-ESM

Installation

To install the required dependencies, run:

npm install

Usage

Import the CohereAPI

const { CohereAPI } = require('cohere-api');

Create an Instance

Create a new instance of the CohereAPI:

const ai = new CohereAPI({ apiKey: 'YourApiKeyHere', maxTokens: 1000 });

Set Up Event Handlers

You can set up handlers for "error" and "debug" events:

ai.on("error", (errorMessage) => {
    console.error("Error Event Triggered:", errorMessage);
});

ai.on("debug", (debugMessage) => {
    console.log("Debug Event Triggered:", debugMessage);
});

Sending a Prompt

To send a prompt and get a response from the AI:

ai.execute("What are the health benefits of green tea?")
    .then(response => {
        console.log("AI Response:", response);
    })
    .catch(error => {
        console.error("Error:", error);
    });

API Methods

send(prompt)

Sends a prompt to the Cohere API and returns the generated response. It validates the prompt length before making the API call.

Parameters:

  • prompt (string): The text to send to the AI.

execute(prompt)

Executes the send method and logs the response. It will exit the process based on the noExit flag set during instantiation.

Parameters:

  • prompt (string): The text to send to the AI.

on(event, callback)

Registers a callback for a specified event.

Parameters:

  • event (string): The event to listen for. Supported events: error, debug.
  • callback (function): The function to call when the event is triggered.

Error Handling

If an error occurs while sending the prompt or processing the response, an "error" event will be triggered, and the message can be handled in the callback.

Debugging

Debug messages can be emitted using the "debug" event, allowing you to track the internal state and flow of operations.

License

This project is licensed under the ISC License.

Author

Leonel Joel [email protected]


### Summary of Changes
- **Overview**: Added an overview of the `CohereAPI` functionality.
- **Installation Instructions**: Included instructions for installing dependencies.
- **Usage**: Provided examples of how to create an instance, set up event handlers, and send prompts.
- **API Methods**: Documented the main methods (`send`, `execute`, `on`) and their parameters.
- **Error Handling**: Explained how to handle errors through events.
- **Debugging**: Explained the debug event usage.