Project restructure
Moved source files into cmd/ and pkg/ directories
This commit is contained in:
139
pkg/cli/cmd.go
Normal file
139
pkg/cli/cmd.go
Normal file
@@ -0,0 +1,139 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var config = LoadConfig()
|
||||
var store, storeError = InitializeStore()
|
||||
|
||||
func checkStore() {
|
||||
if storeError != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error establishing connection to store: %v\n", storeError)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "lm",
|
||||
Short: "Interact with Large Language Models",
|
||||
Long: `lm is a CLI tool to interact with OpenAI's GPT 3.5 and GPT 4.`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
checkStore()
|
||||
// execute `lm ls` by default
|
||||
},
|
||||
}
|
||||
|
||||
var lsCmd = &cobra.Command{
|
||||
Use: "ls",
|
||||
Short: "List existing conversations",
|
||||
Long: `List all existing conversations in descending order of recent activity.`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
fmt.Println("Listing conversations...")
|
||||
// Example output, asterisk to indicate current converation
|
||||
|
||||
// $ lm ls
|
||||
// last hour:
|
||||
// 98sg - 12 minutes ago - Project discussion
|
||||
// last day:
|
||||
// tj3l - 10 hours ago - Deep learning concepts
|
||||
// last week:
|
||||
// bwfm - 2 days ago - Machine learning study
|
||||
// * 8n3h - 3 days ago - Weekend plans
|
||||
// f3n7 - 6 days ago - CLI development
|
||||
// last month:
|
||||
// 5hn2 - 8 days ago - Book club discussion
|
||||
// b7ze - 20 days ago - Gardening tips and tricks
|
||||
// last 6 months:
|
||||
// 3jn2 - 30 days ago - Web development best practices
|
||||
// 43jk - 2 months ago - Longboard maintenance
|
||||
// g8d9 - 3 months ago - History book club
|
||||
// 4lk3 - 4 months ago - Local events and meetups
|
||||
// 43jn - 6 months ago - Mobile photography techniques
|
||||
},
|
||||
}
|
||||
|
||||
var msgCmd = &cobra.Command{
|
||||
Use: "msg",
|
||||
Short: "Send a message to active conversation",
|
||||
Long: `Send a message to the active conversation and receive a message from the LLM in return.`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
fmt.Println("Sending message to active conversation...")
|
||||
// If no messsage provided via args, we should open an editor ala `git commit`
|
||||
// After submitting the message, the
|
||||
},
|
||||
}
|
||||
|
||||
var viewCmd = &cobra.Command{
|
||||
Use: "view",
|
||||
Short: "View messages in a conversation",
|
||||
Long: `Displays all the messages in a coversation.`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
fmt.Println("Displaying conversation messages...")
|
||||
},
|
||||
}
|
||||
|
||||
var newCmd = &cobra.Command{
|
||||
Use: "new",
|
||||
Short: "Start a new conversation",
|
||||
Long: `Start a new conversation with the Large Language Model.`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
messageContents, err := InputFromEditor("# What would you like to say?", "message.*.md")
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error receiving message input: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if messageContents == "" {
|
||||
fmt.Fprintf(os.Stderr, "No message was provided.\n")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
fmt.Printf("> %s\n", messageContents)
|
||||
|
||||
messages := []Message{
|
||||
{
|
||||
OriginalContent: messageContents,
|
||||
Role: "user",
|
||||
},
|
||||
}
|
||||
|
||||
err = CreateChatCompletionStream("You are a helpful assistant.", messages, os.Stdout)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "An error occured: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
fmt.Println()
|
||||
},
|
||||
}
|
||||
|
||||
var promptCmd = &cobra.Command{
|
||||
Use: "prompt",
|
||||
Short: "Do a one-shot prompt",
|
||||
Long: `Prompt the Large Language Model and get a response.`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
messages := []Message{
|
||||
{
|
||||
OriginalContent: strings.Join(args, " "),
|
||||
Role: "user",
|
||||
},
|
||||
}
|
||||
|
||||
err := CreateChatCompletionStream("You are a helpful assistant.", messages, os.Stdout)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "An error occured: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
fmt.Println()
|
||||
},
|
||||
}
|
||||
|
||||
func NewRootCmd() *cobra.Command {
|
||||
rootCmd.AddCommand(newCmd, promptCmd)
|
||||
return rootCmd;
|
||||
}
|
||||
64
pkg/cli/config.go
Normal file
64
pkg/cli/config.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/go-yaml/yaml"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
OpenAI struct {
|
||||
APIKey string `yaml:"apiKey"`
|
||||
} `yaml:"openai"`
|
||||
}
|
||||
|
||||
func getConfigDir() string {
|
||||
var configDir string
|
||||
|
||||
xdgConfigHome := os.Getenv("XDG_CONFIG_HOME")
|
||||
if xdgConfigHome != "" {
|
||||
configDir = filepath.Join(xdgConfigHome, "lmcli")
|
||||
} else {
|
||||
userHomeDir, _ := os.UserHomeDir()
|
||||
configDir = filepath.Join(userHomeDir, ".config/lmcli")
|
||||
}
|
||||
|
||||
os.MkdirAll(configDir, 0755)
|
||||
return configDir
|
||||
}
|
||||
|
||||
func LoadConfig() *Config {
|
||||
configFile := filepath.Join(getConfigDir(), "config.yaml")
|
||||
|
||||
configBytes, err := os.ReadFile(configFile)
|
||||
if os.IsNotExist(err) {
|
||||
defaultConfig := &Config{}
|
||||
defaultConfig.OpenAI.APIKey = "your_key_here"
|
||||
|
||||
|
||||
file, err := os.Create(configFile)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Could not open config file for writing: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
fmt.Printf("Writing default configuration to: %s\n", configFile)
|
||||
|
||||
bytes, _ := yaml.Marshal(defaultConfig)
|
||||
|
||||
_, err = file.Write(bytes)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Could not save default configuratoin: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
} else if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Could not read config file: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
config := &Config{}
|
||||
yaml.Unmarshal(configBytes, config)
|
||||
return config
|
||||
}
|
||||
71
pkg/cli/openai.go
Normal file
71
pkg/cli/openai.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
openai "github.com/sashabaranov/go-openai"
|
||||
)
|
||||
|
||||
func CreateChatCompletionRequest(messages []Message) (openai.ChatCompletionRequest) {
|
||||
var chatCompletionMessages []openai.ChatCompletionMessage
|
||||
for _, m := range(messages) {
|
||||
chatCompletionMessages = append(chatCompletionMessages, openai.ChatCompletionMessage{
|
||||
Role: m.Role,
|
||||
Content: m.OriginalContent,
|
||||
})
|
||||
}
|
||||
|
||||
return openai.ChatCompletionRequest{
|
||||
Model: openai.GPT4,
|
||||
MaxTokens: 256,
|
||||
Messages: chatCompletionMessages,
|
||||
Stream: true,
|
||||
}
|
||||
}
|
||||
|
||||
// CreateChatCompletion accepts a slice of Message and returns the response
|
||||
// of the Large Language Model.
|
||||
func CreateChatCompletion(system string, messages []Message) (string, error) {
|
||||
client := openai.NewClient(config.OpenAI.APIKey)
|
||||
resp, err := client.CreateChatCompletion(
|
||||
context.Background(),
|
||||
CreateChatCompletionRequest(messages),
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("ChatCompletion error: %v\n", err)
|
||||
}
|
||||
|
||||
return resp.Choices[0].Message.Content, nil
|
||||
}
|
||||
|
||||
func CreateChatCompletionStream(system string, messages []Message, output io.Writer) (error) {
|
||||
client := openai.NewClient(config.OpenAI.APIKey)
|
||||
ctx := context.Background()
|
||||
|
||||
req := CreateChatCompletionRequest(messages)
|
||||
req.Stream = true
|
||||
|
||||
stream, err := client.CreateChatCompletionStream(ctx, req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer stream.Close()
|
||||
|
||||
for {
|
||||
response, err := stream.Recv()
|
||||
if errors.Is(err, io.EOF) {
|
||||
return nil
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
//fmt.Printf("\nStream error: %v\n", err)
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Fprint(output, response.Choices[0].Delta.Content)
|
||||
}
|
||||
}
|
||||
102
pkg/cli/store.go
Normal file
102
pkg/cli/store.go
Normal file
@@ -0,0 +1,102 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/driver/sqlite"
|
||||
sqids "github.com/sqids/sqids-go"
|
||||
)
|
||||
|
||||
type Store struct {
|
||||
db *gorm.DB
|
||||
sqids *sqids.Sqids
|
||||
}
|
||||
|
||||
type Message struct {
|
||||
ID uint `gorm:"primaryKey"`
|
||||
ConversationID uint `gorm:"foreignKey:ConversationID"`
|
||||
Conversation Conversation
|
||||
OriginalContent string
|
||||
Role string // 'user' or 'assistant'
|
||||
}
|
||||
|
||||
type Conversation struct {
|
||||
ID uint `gorm:"primaryKey"`
|
||||
ShortName sql.NullString
|
||||
Title string
|
||||
}
|
||||
|
||||
|
||||
func getDataDir() string {
|
||||
var dataDir string;
|
||||
|
||||
xdgDataHome := os.Getenv("XDG_DATA_HOME")
|
||||
if xdgDataHome != "" {
|
||||
dataDir = filepath.Join(xdgDataHome, "lmcli")
|
||||
} else {
|
||||
userHomeDir, _ := os.UserHomeDir()
|
||||
dataDir = filepath.Join(userHomeDir, ".local/share/lmcli")
|
||||
}
|
||||
|
||||
os.MkdirAll(dataDir, 0755)
|
||||
return dataDir
|
||||
}
|
||||
|
||||
func InitializeStore() (*Store, error) {
|
||||
databaseFile := filepath.Join(getDataDir(), "conversations.db")
|
||||
db, err := gorm.Open(sqlite.Open(databaseFile), &gorm.Config{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
models := []any{
|
||||
&Conversation{},
|
||||
&Message{},
|
||||
}
|
||||
|
||||
for _, x := range(models) {
|
||||
err := db.AutoMigrate(x)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
_sqids, _ := sqids.New(sqids.Options{
|
||||
MinLength: 4,
|
||||
})
|
||||
|
||||
return &Store{db: db, sqids: _sqids}, nil
|
||||
}
|
||||
|
||||
func (s *Store) SaveConversation(conversation *Conversation) error {
|
||||
err := s.db.Save(&conversation).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !conversation.ShortName.Valid {
|
||||
shortName, _ := s.sqids.Encode([]uint64{ uint64(conversation.ID) })
|
||||
conversation.ShortName = sql.NullString{String: shortName, Valid: true}
|
||||
err = s.db.Save(&conversation).Error
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *Store) SaveMessage(message *Message) error {
|
||||
return s.db.Create(message).Error
|
||||
}
|
||||
|
||||
func (s *Store) GetConversations() ([]Conversation, error) {
|
||||
var conversations []Conversation
|
||||
err := s.db.Find(&conversations).Error
|
||||
return conversations, err
|
||||
}
|
||||
|
||||
func (s *Store) GetMessages(conversation *Conversation) ([]Message, error) {
|
||||
var messages []Message
|
||||
err := s.db.Where("conversation_id = ?", conversation.ID).Find(&messages).Error
|
||||
return messages, err
|
||||
}
|
||||
41
pkg/cli/util.go
Normal file
41
pkg/cli/util.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
// InputFromEditor retrieves user input by opening an editor on a temporary
|
||||
// file. Once the editor closes, the contents of the temporary file are
|
||||
// returned. If the contents exactly match the placeholder (no edits to the
|
||||
// file were made), then an empty string is returned.
|
||||
// Example patten: message.*.md
|
||||
func InputFromEditor(placeholder string, pattern string) (string, error) {
|
||||
msgFile, _ := os.CreateTemp("/tmp", pattern)
|
||||
defer os.Remove(msgFile.Name())
|
||||
|
||||
os.WriteFile(msgFile.Name(), []byte(placeholder), os.ModeAppend)
|
||||
|
||||
editor := os.Getenv("EDITOR")
|
||||
if editor == "" {
|
||||
editor = "vim" // default to vim if no EDITOR env variable
|
||||
}
|
||||
|
||||
execCmd := exec.Command(editor, msgFile.Name())
|
||||
execCmd.Stdin = os.Stdin
|
||||
execCmd.Stdout = os.Stdout
|
||||
execCmd.Stderr = os.Stderr
|
||||
|
||||
if err := execCmd.Run(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
bytes, _ := os.ReadFile(msgFile.Name())
|
||||
content := string(bytes)
|
||||
|
||||
if content == placeholder {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
return content, nil
|
||||
}
|
||||
Reference in New Issue
Block a user