New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

cohere-api

Package Overview
Dependencies
Maintainers
0
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cohere-api

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

  • 1.0.0
  • unpublished
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
0
Maintainers
0
Weekly downloads
 
Created
Source

Cohere API

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

Version

1.0.0

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.

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 floresgaunal@gmail.com


### 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.

Keywords

FAQs

Package last updated on 03 Nov 2024

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