Skip to content

Commit 89831df

Browse files
authored
Merge pull request #284 from aandrew-me/develop
Refactoring code
2 parents 07b92f6 + 27caeae commit 89831df

File tree

14 files changed

+181
-270
lines changed

14 files changed

+181
-270
lines changed

helper.go

+124-214
Large diffs are not rendered by default.

main.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -259,11 +259,11 @@ func main() {
259259
if previousMessages == "" {
260260
input = *preprompt + input
261261
}
262-
responseJson, responseTxt := getData(input, true, structs.ExtraOptions{
262+
responseJson, responseTxt := getData(input, structs.Params{
263263
PrevMessages: previousMessages,
264264
ThreadID: threadID,
265265
Provider: *provider,
266-
})
266+
}, structs.ExtraOptions{IsInteractive: true})
267267
if len(*logFile) > 0 {
268268
utils.LogToFile(responseTxt, "ASSISTANT_RESPONSE", *logFile)
269269
}
@@ -298,12 +298,11 @@ func main() {
298298
utils.LogToFile(userInput, "USER_QUERY", *logFile)
299299
}
300300

301-
responseJson, responseTxt := getData(userInput, true, structs.ExtraOptions{
301+
responseJson, responseTxt := getData(userInput, structs.Params{
302302
PrevMessages: previousMessages,
303303
Provider: *provider,
304304
ThreadID: threadID,
305-
})
306-
305+
}, structs.ExtraOptions{IsInteractive: true})
307306
previousMessages += responseJson
308307
lastResponse = responseTxt
309308

@@ -339,7 +338,7 @@ func main() {
339338
os.Exit(1)
340339
}
341340

342-
getData(*preprompt+formattedInput+contextText+pipedInput, false, structs.ExtraOptions{})
341+
getData(*preprompt+formattedInput+contextText+pipedInput, structs.Params{}, structs.ExtraOptions{IsNormal: true, IsInteractive: false})
343342
}
344343

345344
} else {
@@ -348,7 +347,7 @@ func main() {
348347
input := scanner.Text()
349348
go loading(&stopSpin)
350349
formattedInput := strings.TrimSpace(input)
351-
getData(*preprompt+formattedInput+pipedInput, false, structs.ExtraOptions{})
350+
getData(*preprompt+formattedInput+pipedInput, structs.Params{}, structs.ExtraOptions{IsInteractive: false})
352351
}
353352
}
354353

providers/blackboxai/blackboxai.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"github.com/aandrew-me/tgpt/v2/structs"
1313
)
1414

15-
func NewRequest(input string, params structs.Params, prevMessages string) (*http.Response, error) {
15+
func NewRequest(input string, params structs.Params) (*http.Response, error) {
1616
client, err := client.NewClient()
1717
if err != nil {
1818
fmt.Println(err)
@@ -38,7 +38,7 @@ func NewRequest(input string, params structs.Params, prevMessages string) (*http
3838
"trendingAgentMode": {},
3939
"isMicMode": false
4040
}
41-
`,prevMessages, string(safeInput)))
41+
`,params.PrevMessages, string(safeInput)))
4242

4343
req, err := http.NewRequest("POST", "https://www.blackbox.ai/api/chat", data)
4444
if err != nil {

providers/groq/groq.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"github.com/aandrew-me/tgpt/v2/structs"
1313
)
1414

15-
func NewRequest(input string, params structs.Params, prevMessages string) (*http.Response, error) {
15+
func NewRequest(input string, params structs.Params) (*http.Response, error) {
1616
client, err := client.NewClient()
1717
if err != nil {
1818
fmt.Println(err)
@@ -51,7 +51,7 @@ func NewRequest(input string, params structs.Params, prevMessages string) (*http
5151
"temperature": %v,
5252
"top_p": %v
5353
}
54-
`, prevMessages, string(safeInput), model, temperature, top_p))
54+
`, params.PrevMessages, string(safeInput), model, temperature, top_p))
5555

5656
req, err := http.NewRequest("POST", "https://api.groq.com/openai/v1/chat/completions", data)
5757
if err != nil {

providers/koboldai/koboldai.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type Response struct {
1616
Token string `json:"token"`
1717
}
1818

19-
func NewRequest(input string, params structs.Params, prevMessages string) (*http.Response, error) {
19+
func NewRequest(input string, params structs.Params) (*http.Response, error) {
2020
client, err := client.NewClient()
2121
if err != nil {
2222
fmt.Println(err)

providers/koboldai/koboldai_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func TestRequest(t *testing.T) {
1515
Temperature: "0.5",
1616
Top_p: "0.5",
1717
Max_length: "300",
18-
}, "")
18+
})
1919
body, _ := io.ReadAll(resp.Body)
2020

2121
assert := assert.New(t)

providers/ollama/ollama.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"github.com/aandrew-me/tgpt/v2/structs"
1313
)
1414

15-
func NewRequest(input string, params structs.Params, prevMessages string) (*http.Response, error) {
15+
func NewRequest(input string, params structs.Params) (*http.Response, error) {
1616
client, err := client.NewClient()
1717
if err != nil {
1818
fmt.Println(err)
@@ -25,12 +25,12 @@ func NewRequest(input string, params structs.Params, prevMessages string) (*http
2525
}
2626

2727
temperature := "0.5"
28-
if params.Temperature != ""{
28+
if params.Temperature != "" {
2929
temperature = params.Temperature
3030
}
3131

3232
top_p := "0.5"
33-
if params.Top_p != ""{
33+
if params.Top_p != "" {
3434
top_p = params.Top_p
3535
}
3636

@@ -51,7 +51,7 @@ func NewRequest(input string, params structs.Params, prevMessages string) (*http
5151
"temperature": %v,
5252
"top_p": %v
5353
}
54-
`, prevMessages, string(safeInput), model, temperature, top_p))
54+
`, params.PrevMessages, string(safeInput), model, temperature, top_p))
5555

5656
req, err := http.NewRequest("POST", "http://localhost:11434/v1/chat/completions", data)
5757
if err != nil {
@@ -61,7 +61,7 @@ func NewRequest(input string, params structs.Params, prevMessages string) (*http
6161
}
6262
// Setting all the required headers
6363
req.Header.Set("Content-Type", "application/json")
64-
req.Header.Set("Authorization", "Bearer " + params.ApiKey)
64+
req.Header.Set("Authorization", "Bearer "+params.ApiKey)
6565

6666
// Return response
6767
return (client.Do(req))
@@ -83,4 +83,4 @@ func GetMainText(line string) (mainText string) {
8383
return mainText
8484
}
8585
return ""
86-
}
86+
}

providers/openai/openai.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"github.com/aandrew-me/tgpt/v2/structs"
1313
)
1414

15-
func NewRequest(input string, params structs.Params, prevMessages string) (*http.Response, error) {
15+
func NewRequest(input string, params structs.Params) (*http.Response, error) {
1616
client, err := client.NewClient()
1717
if err != nil {
1818
fmt.Println(err)
@@ -58,7 +58,7 @@ func NewRequest(input string, params structs.Params, prevMessages string) (*http
5858
"temperature": %v,
5959
"top_p": %v
6060
}
61-
`, prevMessages, string(safeInput), model, temperature, top_p))
61+
`, params.PrevMessages, string(safeInput), model, temperature, top_p))
6262

6363
req, err := http.NewRequest("POST", params.Url, data)
6464
if err != nil {

providers/opengpts/opengpts.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func RandomString(length int) string {
2828
return string(result)
2929
}
3030

31-
func NewRequest(input string, params structs.Params, extraOptions structs.ExtraOptions) (*http.Response, error) {
31+
func NewRequest(input string, params structs.Params) (*http.Response, error) {
3232
client, err := client.NewClient()
3333
if err != nil {
3434
fmt.Println(err)
@@ -38,8 +38,8 @@ func NewRequest(input string, params structs.Params, extraOptions structs.ExtraO
3838

3939
randID := utils.RandomString(36)
4040

41-
if len(extraOptions.ThreadID) > 1 {
42-
randID = extraOptions.ThreadID;
41+
if len(params.ThreadID) > 1 {
42+
randID = params.ThreadID;
4343
}
4444

4545
var data = strings.NewReader(fmt.Sprintf(`{

providers/opengpts/opengpts_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
)
1111

1212
func TestRequest(t *testing.T) {
13-
resp, err := NewRequest("What is 1+1", structs.Params{}, structs.ExtraOptions{})
13+
resp, err := NewRequest("What is 1+1", structs.Params{})
1414
body, _ := io.ReadAll(resp.Body)
1515

1616
assert := assert.New(t)

providers/phind/phind.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"github.com/aandrew-me/tgpt/v2/structs"
1313
)
1414

15-
func NewRequest(input string, params structs.Params, prevMessages string) (*http.Response, error) {
15+
func NewRequest(input string, params structs.Params) (*http.Response, error) {
1616
client, err := client.NewClient()
1717
if err != nil {
1818
fmt.Println(err)
@@ -53,7 +53,7 @@ func NewRequest(input string, params structs.Params, prevMessages string) (*http
5353
"requested_model": "%v",
5454
"user_input": %v
5555
}
56-
`, prevMessages, string(safeInput), model, string(safeInput)))
56+
`, params.PrevMessages, string(safeInput), model, string(safeInput)))
5757

5858
req, err := http.NewRequest("POST", "https://https.extension.phind.com/agent/", data)
5959
if err != nil {
@@ -67,7 +67,6 @@ func NewRequest(input string, params structs.Params, prevMessages string) (*http
6767
req.Header.Set("Accept", "*/*")
6868
req.Header.Set("Accept-Encoding", "Identity")
6969

70-
7170
// Return response
7271
return (client.Do(req))
7372
}

providers/phind/phind_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
)
1111

1212
func TestRequest(t *testing.T) {
13-
resp, err := NewRequest("What is 1+1", structs.Params{}, "")
13+
resp, err := NewRequest("What is 1+1", structs.Params{})
1414
body, _ := io.ReadAll(resp.Body)
1515

1616
assert := assert.New(t)

providers/providers.go

+23-24
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,22 @@ var availableProviders = []string{
2020
}
2121

2222
func GetMainText(line string, provider string, input string) string {
23-
if provider == "blackboxai" {
23+
switch provider {
24+
case "blackboxai":
2425
return blackboxai.GetMainText(line)
25-
} else if provider == "groq" {
26+
case "groq":
2627
return groq.GetMainText(line)
27-
} else if provider == "koboldai" {
28+
case "koboldai":
2829
return koboldai.GetMainText(line)
29-
} else if provider == "ollama" {
30+
case "ollama":
3031
return ollama.GetMainText(line)
31-
} else if provider == "opengpts" {
32+
case "opengpts":
3233
return opengpts.GetMainText(line, input)
33-
} else if provider == "openai" {
34+
case "openai":
3435
return openai.GetMainText(line)
35-
} else if provider == "phind" {
36+
default:
3637
return phind.GetMainText(line)
3738
}
38-
39-
return phind.GetMainText(line)
4039
}
4140

4241
func NewRequest(input string, params structs.Params, extraOptions structs.ExtraOptions) (*http.Response, error) {
@@ -52,21 +51,21 @@ func NewRequest(input string, params structs.Params, extraOptions structs.ExtraO
5251
os.Exit(1)
5352
}
5453

55-
if params.Provider == "blackboxai" {
56-
return blackboxai.NewRequest(input, params, extraOptions.PrevMessages)
57-
} else if params.Provider == "groq" {
58-
return groq.NewRequest(input, params, extraOptions.PrevMessages)
59-
} else if params.Provider == "koboldai" {
60-
return koboldai.NewRequest(input, params, "")
61-
} else if params.Provider == "ollama" {
62-
return ollama.NewRequest(input, params, extraOptions.PrevMessages)
63-
} else if params.Provider == "opengpts" {
64-
return opengpts.NewRequest(input, params, extraOptions)
65-
} else if params.Provider == "openai" {
66-
return openai.NewRequest(input, params, extraOptions.PrevMessages)
67-
} else if params.Provider == "phind" {
68-
return phind.NewRequest(input, params, extraOptions.PrevMessages)
54+
switch params.Provider {
55+
case "blackboxai":
56+
return blackboxai.NewRequest(input, params)
57+
case "groq":
58+
return groq.NewRequest(input, params)
59+
case "koboldai":
60+
return koboldai.NewRequest(input, params)
61+
case "ollama":
62+
return ollama.NewRequest(input, params)
63+
case "opengpts":
64+
return opengpts.NewRequest(input, params)
65+
case "openai":
66+
return openai.NewRequest(input, params)
67+
default:
68+
return phind.NewRequest(input, params)
6969
}
7070

71-
return phind.NewRequest(input, params, extraOptions.PrevMessages)
7271
}

structs/structs.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,16 @@ type Params struct {
1010
Preprompt string
1111
ThreadID string
1212
Url string
13+
PrevMessages string
1314
}
1415

1516
type ExtraOptions struct {
16-
ThreadID string
17-
PrevMessages string
18-
Provider string
17+
IsGetSilent bool
18+
IsGetWhole bool
19+
IsGetCommand bool
20+
IsNormal bool
21+
IsGetCode bool
22+
IsInteractive bool
1923
}
2024

2125
type CommonResponse struct {

0 commit comments

Comments
 (0)