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

gen-ai-chat

v0.2.4

Published

Ask yout questions to AI and get response in terminal

Downloads

27

Readme

Google Generative AI CLI

This is a Node.js command-line interface (CLI) tool created by Arindam that interacts with the Google Generative AI API to generate content based on user input. The tool allows you to ask questions directly or provide context from files, directories, or PDFs.

Installation

To install this package, you need to have Node.js and npm installed. You can install the package globally using npm:

npm install -g gen-ai-chat

Usage

Basic Usage

To ask a question directly from the command line:

npx gen-ai-chat "Your question here"

Using a File for Context

To provide additional context from a file, use the -f flag followed by the file path:

npx gen-ai-chat "Your question here" -f /path/to/your/file.txt

Using a Directory for Context

To provide additional context from all files in a directory, use the -d flag followed by the directory path:

npx gen-ai-chat "Your question here" -d /path/to/your/directory

Using a PDF for Context

To provide additional context from a PDF file, use the --pdf or -p flag followed by the file path:

npx gen-ai-chat "Your question here" --pdf /path/to/your/file.pdf

Interactive Mode

To start the tool in interactive mode, where you can ask multiple questions in a session:

npx gen-ai-chat -i

or

npx gen-ai-chat --interactive

In interactive mode, the prompt gen-ai-chat> will appear, indicating that the tool is ready for you to type your question or command.

To exit interactive mode, type exit or quit and press Enter.

Choosing Your Favorite Model

You can choose your favorite model interactively by using the --choose-model option:

npx gen-ai-chat --choose-model

This command will prompt you to select a model from the available options:

? Please select a model: (Use arrow keys)
❯ gemini-pro 
  gemini-1.5-flash-latest 
  gemini-1.5-pro-latest 
  gemini-pro-vision 
  text-embedding-004

Alternatively, you can specify the model directly using the --model option followed by the model name:

npx gen-ai-chat "Your question here" --model gemini-1.5-flash-latest

Writing Logs to File

By default, logs are stored in memory. To write the in-memory logs to a file, use the --write-logs option:

npx gen-ai-chat --write-logs

Environment Variables

You can provide your own API key by setting the API_KEY environment variable in a .env file:

API_KEY=your_google_gemini_api_key

If you do not provide your own API key, the tool will use a default key with a request limit of 10 requests per hour.

Example

Asking a Question

npx gen-ai-chat "What is the capital of France?"

Asking a Question with File Context

npx gen-ai-chat "Summarize the content of this file" -f ./example.txt

Asking a Question with Directory Context

npx gen-ai-chat "Summarize the content of these files" -d ./example-directory

Asking a Question with PDF Context

npx gen-ai-chat "Summarize the content of this PDF" --pdf ./example.pdf

Choosing a Model

npx gen-ai-chat --choose-model

or

npx gen-ai-chat "Your question here" --model gemini-1.5-flash-latest

Writing Logs to a File

npx gen-ai-chat --write-logs

This command will write all in-memory logs to a file in the logs directory.

Error Handling

  • If you do not provide a question, the CLI will prompt you to ask a question.
  • If the file path provided with the -f flag is invalid, an error message will be displayed.
  • If the directory path provided with the -d flag is invalid, an error message will be displayed.
  • If the request limit is reached and no user API key is provided, a message will be displayed indicating that the request limit has been reached.
  • If the selected model fails, the tool will fallback to gemini-pro and try again.
  • If the API key is missing or invalid, an error message will be displayed.
  • If there is a network issue, an error message will be displayed indicating the problem.
  • If the response from the API is malformed or unexpected, an error message will be displayed.

Key Points

  • Presence of .env File: The .env file must be present in the directory from which the npx command is executed.
  • Loading Environment Variables: The script uses require('dotenv').config(); to load the environment variables from the .env file.
  • Accessing the API Key: The script accesses the API key from process.env.API_KEY.
  • Model Fallback: If the selected model fails, the tool will fallback to gemini-pro and try again.

By ensuring the .env file is in the correct location and the script is configured to load it, the script will be able to access the API key when run with npx.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Contributing

Contributions are welcome! Please open an issue or submit a pull request for any improvements or bug fixes.

Contact

For any questions or issues, please open an issue on the GitHub repository.