Skip to content

Commit 697fd04

Browse files
committed
feat(ai): improve AI provider integration and configuration handling
- Introduced 'systemPrompt' in 'OpenAIProvider' to enable configurable system messages - Refactored 'NewOpenAIProvider' to accept 'systemPrompt' for more contextual AI responses - Replaced 'Provider' interface definition in 'ai/provider.go' with a structured 'types.Provider' - Enhanced 'Config' to expose getter methods for AI configuration values - Added default values for AI settings, including a default system prompt - Created 'internal/types/common.go' to define AI provider interface and configuration abstraction
1 parent d669c20 commit 697fd04

File tree

6 files changed

+66
-18
lines changed

6 files changed

+66
-18
lines changed

cmd/review.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ func formatCommitHash(hash string) string {
1818

1919
// executeReview executes the main logic of the program
2020
func executeReview() {
21+
// provider, err := AppConfig.GetAIProvider()
22+
// if err != nil {
23+
// fmt.Printf("Error getting AI provider: %v\n", err)
24+
// os.Exit(1)
25+
// }
26+
2127
// Format commit hashes
2228
initialCommit = formatCommitHash(initialCommit)
2329

cmd/root.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ var (
2020
AppVersion = "dev"
2121

2222
// Configuration
23-
appConfig *config.Config
23+
AppConfig *config.Config
2424

2525
// Root command
2626
rootCmd = &cobra.Command{
@@ -55,7 +55,7 @@ changes between Git commits.`,
5555

5656
// SetConfig sets the application configuration
5757
func SetConfig(cfg *config.Config) {
58-
appConfig = cfg
58+
AppConfig = cfg
5959
}
6060

6161
// InitializeFlags parses command line flags before executing the main command

internal/ai/openai.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,38 @@ package ai
33
import (
44
"context"
55

6+
"github.com/gifflet/git-review/internal/types"
67
"github.com/tmc/langchaingo/llms"
78
"github.com/tmc/langchaingo/llms/openai"
89
)
910

1011
type OpenAIProvider struct {
11-
llm llms.LLM
12+
llm llms.LLM
13+
systemPrompt string
1214
}
1315

14-
func NewOpenAIProvider(token, model string) (Provider, error) {
16+
func NewOpenAIProvider(token, model, systemPrompt string) (types.Provider, error) {
1517
llm, err := openai.New(
1618
openai.WithToken(token),
1719
openai.WithModel(model),
1820
)
1921
if err != nil {
2022
return nil, err
2123
}
22-
return &OpenAIProvider{llm: llm}, nil
24+
return &OpenAIProvider{
25+
llm: llm,
26+
systemPrompt: systemPrompt,
27+
}, nil
2328
}
2429

2530
func (p *OpenAIProvider) Generate(ctx context.Context, prompt string) (string, error) {
26-
return llms.GenerateFromSinglePrompt(ctx, p.llm, prompt)
31+
content := []llms.MessageContent{
32+
llms.TextParts(llms.ChatMessageTypeSystem, p.systemPrompt),
33+
llms.TextParts(llms.ChatMessageTypeHuman, prompt),
34+
}
35+
completion, err := p.llm.GenerateContent(ctx, content)
36+
if err != nil {
37+
return "", err
38+
}
39+
return completion.Choices[0].Content, nil
2740
}

internal/ai/provider.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
package ai
22

33
import (
4-
"context"
54
"fmt"
6-
)
75

8-
// Provider defines the interface for AI providers
9-
type Provider interface {
10-
Generate(ctx context.Context, prompt string) (string, error)
11-
}
6+
"github.com/gifflet/git-review/internal/types"
7+
)
128

139
// NewProvider creates a new AI provider based on configuration
14-
func NewProvider(providerName, token, model string) (Provider, error) {
10+
func NewProvider(providerName string, c types.AIConfig) (types.Provider, error) {
1511
switch providerName {
1612
case "openai":
17-
return NewOpenAIProvider(token, model)
13+
return NewOpenAIProvider(c.GetOpenAIToken(), c.GetOpenAIModel(), c.GetSystemPrompt())
1814
default:
1915
return nil, fmt.Errorf("unsupported AI provider: %s", providerName)
2016
}

internal/config/config.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"path/filepath"
77

88
"github.com/gifflet/git-review/internal/ai"
9+
"github.com/gifflet/git-review/internal/types"
910
"github.com/spf13/viper"
1011
)
1112

@@ -15,15 +16,27 @@ type Config struct {
1516
Provider string `mapstructure:"provider"`
1617
OpenAI struct {
1718
Token string `mapstructure:"token"`
18-
Model string `mapstructure:"model" default:"gpt-4o"`
19+
Model string `mapstructure:"model"`
1920
} `mapstructure:"openai"`
21+
SystemPrompt string `mapstructure:"system_prompt"`
2022
} `mapstructure:"ai"`
2123
}
2224

25+
func (c *Config) GetOpenAIToken() string {
26+
return c.AI.OpenAI.Token
27+
}
28+
29+
func (c *Config) GetOpenAIModel() string {
30+
return c.AI.OpenAI.Model
31+
}
32+
33+
func (c *Config) GetSystemPrompt() string {
34+
return c.AI.SystemPrompt
35+
}
36+
2337
// GetAIProvider creates a new AI provider based on configuration
24-
func (c *Config) GetAIProvider() (ai.Provider, error) {
25-
cfg := c.AI
26-
return ai.NewProvider(cfg.Provider, cfg.OpenAI.Token, cfg.OpenAI.Model)
38+
func (c *Config) GetAIProvider() (types.Provider, error) {
39+
return ai.NewProvider(c.AI.Provider, c)
2740
}
2841

2942
// LoadConfig reads configuration from files and environment variables
@@ -34,6 +47,11 @@ func LoadConfig(projectPath string) (*Config, error) {
3447
v.SetConfigName("config")
3548
v.SetConfigType("yaml")
3649

50+
// Set default values
51+
v.SetDefault("ai.provider", "openai")
52+
v.SetDefault("ai.openai.model", "gpt-4o")
53+
v.SetDefault("ai.system_prompt", "You are a helpful assistant that reviews code changes. You are given a git diff with the changes made to the code. You need to review the changes and provide a list of potential improvements.")
54+
3755
// Add global config paths
3856
homeDir, err := os.UserHomeDir()
3957
if err != nil {

internal/types/common.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package types
2+
3+
import "context"
4+
5+
// Provider defines the interface for AI providers
6+
type Provider interface {
7+
Generate(ctx context.Context, prompt string) (string, error)
8+
}
9+
10+
// AIConfig holds the configuration for AI providers
11+
type AIConfig interface {
12+
GetOpenAIToken() string
13+
GetOpenAIModel() string
14+
GetSystemPrompt() string
15+
}

0 commit comments

Comments
 (0)