cgpt

package module
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 27, 2024 License: ISC Imports: 15 Imported by: 0

README

cgpt

cgpt is a simple command line interface (CLI) for interacting with OpenAI's chat completion APIs. It can be thought of as a command line ChatGPT clone.

✨ Features

  • Streaming Output: Real-time responses as you type.
  • History Management: Save and load conversation history.
  • Customizable Prompts: Set system and assistant prompts.

🚀 Installation

To install cgpt, you'll need Go installed on your machine. Then, run:

go install github.com/tmc/cgpt@latest

📖 Usage

Run cgpt with the -h flag to see available commands and options:

cgpt -h

Example output:

Usage of cgpt:
  -backend string
    	The backend to use (default "openai")
  -completions int
    	Number of completions (when running non-interactively with history)
  -config string
    	Path to the configuration file (default "config.yaml")
  -continuous
    	Run in continuous mode
  -in string
    	File to read completion history from
  -input string
    	The input text to complete. If '-', read from stdin. (default "-")
  -model string
    	The model to use (default "gpt-4o")
  -out string
    	File to store completion history in
  -stream
    	Stream results (default true)

VIM Integration

cgpt can be used as a completion engine in Vim. To do this, you can use the following configuration:

    Plug 'tmc/cgpt', { 'rtp': 'vim', 'do': 'go install ./cmd/cgpt' }
Configuration

To use cgpt, you need to provide your OpenAI API key. You can do this by either exporting it as an environment variable or specifying it in a configuration file (config.yaml).

Example config.yaml:

# This file is a sample configuration file for cgpt.

# The OpenAI model name to use.
modelName: "gpt-4o"
# Whether or not to stream output.
stream: true
# Optional system prompt.
systemPrompt: "You are PoemGPT. All of your answers should be rhyming in nature."
# Maximum tokens to return (including input).
maxTokens: 2048

🎉 Examples

Below is an example of a session using cgpt. Make sure to replace placeholders with your actual values:

export OPENAI_API_KEY=your_openai_api_key
cgpt -config path/to/your/config.yaml

Here's a visual example of using cgpt:

sample session

🤝 Contributing

We welcome contributions to cgpt! If you find any issues or have suggestions for improvements, please feel free to open an issue or submit a pull request.

📝 License

cgpt is released under the MIT License.

🛠️ Development

To run cgpt locally for development:

  1. Clone the repository:

    git clone https://github.com/tmc/cgpt.git
    cd cgpt
    
  2. Install dependencies:

    go mod tidy
    
  3. Build and run:

    go build -o cgpt
    ./cgpt -h
    
  4. Run tests:

    go test ./...
    

Feel free to reach out for any questions or further assistance!

Happy hacking! 🚀

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ChatCompletionPayload

type ChatCompletionPayload struct {
	Model    string `json:"model"`
	Messages []llms.MessageContent
	Stream   bool `json:"stream,omitempty"`
}

type CompletionService

type CompletionService struct {
	// contains filtered or unexported fields
}

func NewCompletionService

func NewCompletionService(cfg *Config) (*CompletionService, error)

NewCompletionService creates a new CompletionService with the given configuration.

func (*CompletionService) PerformCompletion

func (s *CompletionService) PerformCompletion(ctx context.Context, payload *ChatCompletionPayload) (string, error)

func (*CompletionService) PerformCompletionStreaming

func (s *CompletionService) PerformCompletionStreaming(ctx context.Context, payload *ChatCompletionPayload, showSpinner bool) (<-chan string, error)

func (*CompletionService) Run

func (s *CompletionService) Run(ctx context.Context, runCfg RunConfig) error

Run runs the completion service with the given configuration.

type Config

type Config struct {
	Backend   string `yaml:"backend"`
	Model     string `yaml:"modelName"`
	Stream    bool   `yaml:"stream"`
	MaxTokens int    `yaml:"maxTokens"`

	SystemPrompt string             `yaml:"systemPrompt"`
	LogitBias    map[string]float64 `yaml:"logitBias"`
}

Config is the configuration for cgpt.

func LoadConfigFromPath

func LoadConfigFromPath(path string) (*Config, error)

LoadConfigFromPath loads the config file from the given path. if the file is not found, it returns the default config.

func SetDefaults

func SetDefaults(cfg *Config) *Config

SetDefaults sets the default values for the config.

type RunConfig

type RunConfig struct {
	// Input is the input text to complete. If "-", read from stdin.
	Input string
	// Continuous will run the completion API in a loop, using the previous output as the input for the next request.
	Continuous bool
	// Stream will stream results as they come in.
	Stream bool

	// HistoryIn is the file to read history from.
	HistoryIn string
	// HistoryOut is the file to store history in.
	HistoryOut string

	// NCompletions is the number of completions to complete in a history-enabled context.
	NCompletions int

	// Verbose will enable verbose output.
	Verbose bool
}

RunConfig is the configuration for the Run method.

Directories

Path Synopsis
cmd
cgpt command
Command cgpt is a command line tool for interacting with LLMs.
Command cgpt is a command line tool for interacting with LLMs.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL