Project refactor, add anthropic API support
- Split pkg/cli/cmd.go into new pkg/cmd package - Split pkg/cli/functions.go into pkg/lmcli/tools package - Refactor pkg/cli/openai.go to pkg/lmcli/provider/openai Other changes: - Made models configurable - Slight config reorganization
This commit is contained in:
97
pkg/lmcli/lmcli.go
Normal file
97
pkg/lmcli/lmcli.go
Normal file
@@ -0,0 +1,97 @@
|
||||
package lmcli
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"git.mlow.ca/mlow/lmcli/pkg/lmcli/provider"
|
||||
"git.mlow.ca/mlow/lmcli/pkg/lmcli/provider/anthropic"
|
||||
"git.mlow.ca/mlow/lmcli/pkg/lmcli/provider/openai"
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Context struct {
|
||||
Config Config
|
||||
Store ConversationStore
|
||||
}
|
||||
|
||||
func NewContext() (*Context, error) {
|
||||
configFile := filepath.Join(configDir(), "config.yaml")
|
||||
config, err := NewConfig(configFile)
|
||||
if err != nil {
|
||||
Fatal("%v\n", err)
|
||||
}
|
||||
|
||||
databaseFile := filepath.Join(dataDir(), "conversations.db")
|
||||
db, err := gorm.Open(sqlite.Open(databaseFile), &gorm.Config{})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Error establishing connection to store: %v", err)
|
||||
}
|
||||
s, err := NewSQLStore(db)
|
||||
if err != nil {
|
||||
Fatal("%v\n", err)
|
||||
}
|
||||
|
||||
return &Context{*config, s}, nil
|
||||
}
|
||||
|
||||
func (c *Context) GetCompletionProvider(model string) (provider.ChatCompletionClient, error) {
|
||||
for _, m := range *c.Config.Anthropic.Models {
|
||||
if m == model {
|
||||
anthropic := &anthropic.AnthropicClient{
|
||||
APIKey: *c.Config.Anthropic.APIKey,
|
||||
}
|
||||
return anthropic, nil
|
||||
}
|
||||
}
|
||||
for _, m := range *c.Config.OpenAI.Models {
|
||||
if m == model {
|
||||
openai := &openai.OpenAIClient{
|
||||
APIKey: *c.Config.OpenAI.APIKey,
|
||||
}
|
||||
return openai, nil
|
||||
}
|
||||
}
|
||||
return nil, fmt.Errorf("unknown model: %s", model)
|
||||
}
|
||||
|
||||
func configDir() 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 dataDir() 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 Fatal(format string, args ...any) {
|
||||
fmt.Fprintf(os.Stderr, format, args...)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
func Warn(format string, args ...any) {
|
||||
fmt.Fprintf(os.Stderr, format, args...)
|
||||
}
|
||||
Reference in New Issue
Block a user