Skip to content

Commit 40e4cdc

Browse files
committed
Remove unused filepath import and use in-memory index for memory creation
1 parent 28d6a74 commit 40e4cdc

File tree

5 files changed

+98
-17
lines changed

5 files changed

+98
-17
lines changed

cmd/ask.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package cmd
55

66
import (
77
"os"
8-
"path/filepath"
98

109
"github.com/chand1012/git2gpt/prompt"
1110
"github.com/chand1012/memory"
@@ -74,12 +73,10 @@ Requires a path to a repository or file as a positional argument.`,
7473
os.Exit(1)
7574
}
7675
log.Debug("Constructing repo memory...")
77-
// Define a temporary path for the index file
78-
testIndexPath := filepath.Join(repoPath, ".index.memory")
7976

8077
log.Debug("Creating memory index...")
8178
// Create a new memory index
82-
m, _, err := memory.New(testIndexPath)
79+
m, _, err := memory.New(":memory:")
8380
if err != nil {
8481
log.Errorf("Failed to create memory index: %s", err)
8582
os.Exit(1)

cmd/edit.go

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ import (
99
"os"
1010
"strings"
1111

12+
"github.com/chand1012/memory"
1213
"github.com/chand1012/ottodocs/pkg/calc"
1314
"github.com/chand1012/ottodocs/pkg/config"
1415
"github.com/chand1012/ottodocs/pkg/constants"
16+
"github.com/chand1012/ottodocs/pkg/git"
1517
"github.com/chand1012/ottodocs/pkg/textfile"
1618
"github.com/chand1012/ottodocs/pkg/utils"
1719
l "github.com/charmbracelet/log"
@@ -96,6 +98,77 @@ Example: otto edit main.go --start 1 --end 10 --goal "Refactor the function"`,
9698
prompt = "GOAL: " + chatPrompt + "\n\nFILE: " + filePath + "\n\n" + contents
9799
}
98100

101+
client := openai.NewClient(c.APIKey)
102+
103+
if repoContext {
104+
repo, err := git.GetRepo(".", "", false)
105+
if err != nil {
106+
log.Errorf("Error getting repo: %s", err)
107+
os.Exit(1)
108+
}
109+
110+
m, _, err := memory.New(":memory:")
111+
if err != nil {
112+
log.Errorf("Error creating memory: %s", err)
113+
os.Exit(1)
114+
}
115+
116+
for _, file := range repo.Files {
117+
err = m.Add(file.Path, file.Contents)
118+
if err != nil {
119+
log.Errorf("Error indexing file: %s", err)
120+
os.Exit(1)
121+
}
122+
}
123+
124+
queryConstructorPrompt := []openai.ChatCompletionMessage{
125+
{
126+
Role: openai.ChatMessageRoleUser,
127+
Content: "Convert this goal into a terms to use for a search query. They do not have to be organized, nor a complete sentence. Use no form of punctuation or quotations. Only return the query and nothing else: " + chatPrompt,
128+
},
129+
}
130+
131+
utils.PrintColoredText("Otto: ", c.OttoColor)
132+
fmt.Println("Ok! Here is the query, taking your input into account.")
133+
utils.PrintColoredText("Otto: ", c.OttoColor)
134+
stream, err := client.CreateChatCompletionStream(context.Background(), openai.ChatCompletionRequest{
135+
Model: "gpt-3.5-turbo",
136+
Messages: queryConstructorPrompt,
137+
})
138+
139+
if err != nil {
140+
log.Errorf("Error requesting from OpenAI: %s", err)
141+
os.Exit(1)
142+
}
143+
144+
query, err := utils.PrintChatCompletionStream(stream)
145+
if err != nil {
146+
log.Errorf("Error printing chat completion stream: %s", err)
147+
os.Exit(1)
148+
}
149+
150+
utils.PrintColoredText("Otto: ", c.OttoColor)
151+
fmt.Println("Searching repo for files that match the query...")
152+
153+
resp, err := m.Search(query)
154+
if err != nil {
155+
log.Errorf("Error searching memory: %s", err)
156+
os.Exit(1)
157+
}
158+
159+
contextFiles = []string{}
160+
for _, file := range resp {
161+
contextFiles = append(contextFiles, file.ID)
162+
}
163+
log.Debugf("context files: %s", contextFiles)
164+
}
165+
tokens, err := calc.PreciseTokens(prompt)
166+
if err != nil {
167+
log.Errorf("Error calculating tokens: %s", err)
168+
os.Exit(1)
169+
}
170+
171+
maxTokens := calc.GetMaxTokens(c.Model)
99172
if len(contextFiles) > 0 {
100173
var contextContent string
101174
for _, contextFile := range contextFiles {
@@ -104,11 +177,20 @@ Example: otto edit main.go --start 1 --end 10 --goal "Refactor the function"`,
104177
log.Errorf("Error loading context file: %s", err)
105178
continue
106179
}
180+
contentTokens, err := calc.PreciseTokens(contextContent)
181+
if err != nil {
182+
log.Errorf("Error loading context file: %s", err)
183+
continue
184+
}
185+
if contentTokens+tokens > maxTokens {
186+
break
187+
}
107188
prompt += "\n\nCONTEXT: " + contextFile + "\n\n" + contextContent
189+
tokens += contentTokens
108190
}
109191
}
110192

111-
client := openai.NewClient(c.APIKey)
193+
log.Debugf("prompt: %s", prompt)
112194

113195
messages = []openai.ChatCompletionMessage{
114196
{
@@ -220,6 +302,7 @@ func init() {
220302
editCmd.Flags().BoolVarP(&force, "force", "f", false, "Force overwrite of existing files")
221303
editCmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "Verbose output")
222304
editCmd.Flags().BoolVarP(&appendFile, "append", "a", false, "Append to the end of a file instead of overwriting it")
305+
editCmd.Flags().BoolVarP(&repoContext, "repo", "r", false, "Use the current repo as context")
223306
editCmd.Flags().IntVarP(&startLine, "start", "s", 1, "Start line")
224307
editCmd.Flags().IntVarP(&endLine, "end", "e", 0, "End line")
225308
editCmd.Flags().StringVarP(&chatPrompt, "goal", "g", "", "Goal of the edit")

cmd/vars.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ var loadHistory string
5656
var deleteHistory string
5757
var readOnly bool
5858
var clearHistory bool
59+
var repoContext bool
5960

6061
var log = l.NewWithOptions(os.Stderr, l.Options{
6162
Level: l.InfoLevel,

go.mod

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@ go 1.20
44

55
require (
66
github.com/chand1012/git2gpt v0.4.1
7-
github.com/chand1012/memory v0.3.0
7+
github.com/chand1012/memory v0.4.0
88
github.com/charmbracelet/lipgloss v0.7.1
99
github.com/charmbracelet/log v0.2.1
1010
github.com/pkoukk/tiktoken-go v0.1.1
11-
github.com/sashabaranov/go-openai v1.9.0
11+
github.com/sashabaranov/go-openai v1.9.3
1212
github.com/spf13/cobra v1.7.0
1313
)
1414

1515
require (
1616
github.com/RoaringBitmap/roaring v1.2.3 // indirect
1717
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
18-
github.com/bits-and-blooms/bitset v1.6.0 // indirect
18+
github.com/bits-and-blooms/bitset v1.7.0 // indirect
1919
github.com/blevesearch/bleve v1.0.14 // indirect
2020
github.com/blevesearch/go-porterstemmer v1.0.3 // indirect
2121
github.com/blevesearch/mmap-go v1.0.4 // indirect
@@ -49,7 +49,7 @@ require (
4949
github.com/steveyen/gtreap v0.1.0 // indirect
5050
github.com/willf/bitset v1.1.11 // indirect
5151
go.etcd.io/bbolt v1.3.7 // indirect
52-
golang.org/x/sys v0.7.0 // indirect
52+
golang.org/x/sys v0.8.0 // indirect
5353
google.golang.org/protobuf v1.30.0 // indirect
5454
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
5555
gopkg.in/yaml.v3 v3.0.1 // indirect

go.sum

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5
66
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
77
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
88
github.com/bits-and-blooms/bitset v1.2.0/go.mod h1:gIdJ4wp64HaoK2YrL1Q5/N7Y16edYb8uY+O0FJTyyDA=
9-
github.com/bits-and-blooms/bitset v1.6.0 h1:FVfaUsleKAUTJnaN9Fd1YFFi1S8vAX5xeXnXHFYOojM=
10-
github.com/bits-and-blooms/bitset v1.6.0/go.mod h1:gIdJ4wp64HaoK2YrL1Q5/N7Y16edYb8uY+O0FJTyyDA=
9+
github.com/bits-and-blooms/bitset v1.7.0 h1:YjAGVd3XmtK9ktAbX8Zg2g2PwLIMjGREZJHlV4j7NEo=
10+
github.com/bits-and-blooms/bitset v1.7.0/go.mod h1:gIdJ4wp64HaoK2YrL1Q5/N7Y16edYb8uY+O0FJTyyDA=
1111
github.com/blevesearch/bleve v1.0.14 h1:Q8r+fHTt35jtGXJUM0ULwM3Tzg+MRfyai4ZkWDy2xO4=
1212
github.com/blevesearch/bleve v1.0.14/go.mod h1:e/LJTr+E7EaoVdkQZTfoz7dt4KoDNvDbLb8MSKuNTLQ=
1313
github.com/blevesearch/blevex v1.0.0 h1:pnilj2Qi3YSEGdWgLj1Pn9Io7ukfXPoQcpAI1Bv8n/o=
@@ -35,8 +35,8 @@ github.com/blevesearch/zap/v15 v15.0.3 h1:Ylj8Oe+mo0P25tr9iLPp33lN6d4qcztGjaIsP5
3535
github.com/blevesearch/zap/v15 v15.0.3/go.mod h1:iuwQrImsh1WjWJ0Ue2kBqY83a0rFtJTqfa9fp1rbVVU=
3636
github.com/chand1012/git2gpt v0.4.1 h1:FotHFBRp724J5Nft865JBJF6PMJcNuKrpDHbdhI7WE8=
3737
github.com/chand1012/git2gpt v0.4.1/go.mod h1:tT4D74zqhWjDkQCyYqmEclIhOqI3t+hEmUHDTPjiIr4=
38-
github.com/chand1012/memory v0.3.0 h1:jdmoKNFgPXv+yZdgt2Tf2BOBaX+FbqkLHUKa2twh+s4=
39-
github.com/chand1012/memory v0.3.0/go.mod h1:kim7W02RagO3vJBU8JckOO4nJGArmHZJLzq8fZ7dkq0=
38+
github.com/chand1012/memory v0.4.0 h1:eK6wYzZ8OHzNbT4Y4u3x2QB72dDPcUEMw2FA5stXgJQ=
39+
github.com/chand1012/memory v0.4.0/go.mod h1:o8pdLH/jR/fhh3qwNvPXGb02D0E8KwaPnKNdzimVpl8=
4040
github.com/charmbracelet/lipgloss v0.7.1 h1:17WMwi7N1b1rVWOjMT+rCh7sQkvDU75B2hbZpc5Kc1E=
4141
github.com/charmbracelet/lipgloss v0.7.1/go.mod h1:yG0k3giv8Qj8edTCbbg6AlQ5e8KNWpFujkNawKNhE2c=
4242
github.com/charmbracelet/log v0.2.1 h1:1z7jpkk4yKyjwlmKmKMM5qnEDSpV32E7XtWhuv0mTZE=
@@ -140,8 +140,8 @@ github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTE
140140
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
141141
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
142142
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
143-
github.com/sashabaranov/go-openai v1.9.0 h1:NoiO++IISxxJ1pRc0n7uZvMGMake0G+FJ1XPwXtprsA=
144-
github.com/sashabaranov/go-openai v1.9.0/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg=
143+
github.com/sashabaranov/go-openai v1.9.3 h1:uNak3Rn5pPsKRs9bdT7RqRZEyej/zdZOEI2/8wvrFtM=
144+
github.com/sashabaranov/go-openai v1.9.3/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg=
145145
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
146146
github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
147147
github.com/spf13/cobra v0.0.5/go.mod h1:3K3wKZymM7VvHMDS9+Akkh4K60UwM26emMESw8tLCHU=
@@ -183,8 +183,8 @@ golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7w
183183
golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
184184
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
185185
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
186-
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
187-
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
186+
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
187+
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
188188
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
189189
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
190190
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=

0 commit comments

Comments
 (0)