Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
reugn committed Feb 17, 2024
0 parents commit 265ddec
Show file tree
Hide file tree
Showing 18 changed files with 875 additions and 0 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Build

on:
push:
branches:
- '**'
pull_request:
branches:
- main

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
go-version: [1.21.x, 1.22.x]
steps:
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go-version }}

- name: Checkout code
uses: actions/checkout@v4

- name: Build
run: |
cd cmd/gemini
go build .
17 changes: 17 additions & 0 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: golangci-lint
on:
push:
branches:
- master
pull_request:

jobs:
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: golangci-lint
uses: golangci/golangci-lint-action@v4
29 changes: 29 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Release

on:
push:
tags:
- 'v*'

jobs:
goreleaser:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: 1.22.x

- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v5
with:
version: latest
workdir: ./cmd/gemini
args: release --clean
env:
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.idea/
.vscode/
dist/
coverage.out
38 changes: 38 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
run:
timeout: 1m

linters:
disable-all: true
enable:
- dupl
- errcheck
- errname
- errorlint
- exportloopref
- funlen
- gci
- goconst
- gocritic
- gocyclo
- gofmt
- goimports
- gosimple
- govet
- ineffassign
- lll
- misspell
- prealloc
- revive
- staticcheck
- stylecheck
- typecheck
- unconvert
- unparam
- unused

issues:
exclude-rules:
- path: _test\.go
linters:
- unparam
- funlen
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2024 reugn

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# gemini-cli
A command-line interface (CLI) for [Google Gemini](https://deepmind.google/technologies/gemini/).

Google Gemini is a family of multimodal artificial intelligence (AI) large language models that have
capabilities in language, audio, code and video understanding.

The current version only supports multi-turn conversations (chat), using the `gemini-pro` model.

## Installation
Choose a binary from the [releases](https://github.com/reugn/gemini-cli/releases).

### Build from Source
Download and [install Go](https://golang.org/doc/install).

Install the application:

```sh
go install github.com/reugn/gemini-cli/cmd/gemini@latest
```

See the [go install](https://go.dev/ref/mod#go-install) instructions for more information about the command.

## Usage

### API key
To use `gemini-cli`, you'll need an API key set in the `GEMINI_API_KEY` environment variable. If you don't already have one, create a key in [Google AI Studio](https://makersuite.google.com/app/apikey).

### System commands
The system chat message must begin with an exclamation mark and is used for internal operations.
A short list of supported system commands:

| Command | Descripton |
| --- | --- |
| !q | Quit the application |
| !p | Purge the chat history |

### CLI help
```
Gemini CLI Tool
Usage:
[flags]
Flags:
--chat start chat session (default true)
-h, --help help for this command
--stream use streaming (default true)
-v, --version version for this command
```

## License
MIT
68 changes: 68 additions & 0 deletions cli/chat.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package cli

import (
"bufio"
"fmt"
"os"
"strings"

"github.com/reugn/gemini-cli/gemini"
)

// ChatOpts represents Chat configuration options.
type ChatOpts struct {
Stream bool
}

// Chat controls the chat flow.
type Chat struct {
model *gemini.ChatSession
prompt *prompt
reader *bufio.Reader
opts *ChatOpts
}

// NewChat returns a new Chat.
func NewChat(user string, model *gemini.ChatSession, opts *ChatOpts) *Chat {
return &Chat{
model: model,
prompt: newPrompt(user),
reader: bufio.NewReader(os.Stdin),
opts: opts,
}
}

// StartChat starts the chat loop.
func (c *Chat) StartChat() {
for {
fmt.Print(c.prompt.user)
message, ok := c.readLine()
if !ok {
continue
}
command := c.parseCommand(message)
if quit := command.run(message); quit {
break
}
}
}

func (c *Chat) readLine() (string, bool) {
input, err := c.reader.ReadString('\n')
if err != nil {
fmt.Printf("%s%s\n", c.prompt.cli, err)
return "", false
}
input = strings.ReplaceAll(input, "\n", "")
if strings.TrimSpace(input) == "" {
return "", false
}
return input, true
}

func (c *Chat) parseCommand(message string) command {
if strings.HasPrefix(message, systemCmdPrefix) {
return newSystemCommand(c.model, c.prompt)
}
return newGeminiCommand(c.model, c.prompt, c.opts)
}
55 changes: 55 additions & 0 deletions cli/color/color.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package color

import "fmt"

var (
reset = "\033[0m"
red = "\033[31m"
green = "\033[32m"
yellow = "\033[33m"
blue = "\033[34m"
purple = "\033[35m"
cyan = "\033[36m"
gray = "\033[37m"
white = "\033[97m"
)

// Red adds red color to str in terminal.
func Red(str string) string {
return fmt.Sprintf("%s%s%s", red, str, reset)
}

// Green adds green color to str in terminal.
func Green(str string) string {
return fmt.Sprintf("%s%s%s", green, str, reset)
}

// Yellow adds yellow color to str in terminal.
func Yellow(str string) string {
return fmt.Sprintf("%s%s%s", yellow, str, reset)
}

// Blue adds blue color to str in terminal.
func Blue(str string) string {
return fmt.Sprintf("%s%s%s", blue, str, reset)
}

// Purple adds purple color to str in terminal.
func Purple(str string) string {
return fmt.Sprintf("%s%s%s", purple, str, reset)
}

// Cyan adds cyan color to str in terminal.
func Cyan(str string) string {
return fmt.Sprintf("%s%s%s", cyan, str, reset)
}

// Gray adds gray color to str in terminal.
func Gray(str string) string {
return fmt.Sprintf("%s%s%s", gray, str, reset)
}

// White adds white color to str in terminal.
func White(str string) string {
return fmt.Sprintf("%s%s%s", white, str, reset)
}
Loading

0 comments on commit 265ddec

Please sign in to comment.