-
Notifications
You must be signed in to change notification settings - Fork 135
/
phind.go
100 lines (83 loc) · 1.95 KB
/
phind.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package phind
import (
"encoding/json"
"fmt"
"os"
"strings"
http "github.com/bogdanfinn/fhttp"
"github.com/aandrew-me/tgpt/v2/client"
"github.com/aandrew-me/tgpt/v2/structs"
)
type Response struct {
ID string `json:"id"`
Choices []struct {
Delta struct {
Content string `json:"content"`
} `json:"delta"`
} `json:"choices"`
}
func NewRequest(input string, params structs.Params, prevMessages string) (*http.Response, error) {
client, err := client.NewClient()
if err != nil {
fmt.Println(err)
os.Exit(0)
}
model := "Phind Model"
if params.ApiModel != "" {
model = params.ApiModel
}
// preprompt := "You are a helpful assistant"
// if params.Preprompt != "" {
// preprompt = params.Preprompt
// }
// finalPreprompt := fmt.Sprintf(`
// {
// "content": "%v",
// "role": "system"
// },
// `, preprompt)
safeInput, _ := json.Marshal(input)
var data = strings.NewReader(fmt.Sprintf(`{
"additional_extension_context": "",
"allow_magic_buttons": true,
"is_vscode_extension": true,
"message_history": [
%v
{
"content": %v,
"role": "user"
}
],
"requested_model": "%v",
"user_input": %v
}
`, prevMessages, string(safeInput), model, string(safeInput)))
req, err := http.NewRequest("POST", "https://https.extension.phind.com/agent/", data)
if err != nil {
fmt.Println("\nSome error has occurred.")
fmt.Println("Error:", err)
os.Exit(0)
}
// Setting all the required headers
req.Header.Set("Content-Type", "application/json")
req.Header.Set("User-Agent", "")
req.Header.Set("Accept", "*/*")
req.Header.Set("Accept-Encoding", "Identity")
// Return response
return (client.Do(req))
}
func GetMainText(line string) (mainText string) {
var obj = "{}"
if len(line) > 1 {
obj = strings.Split(line, "data: ")[1]
}
var d Response
if err := json.Unmarshal([]byte(obj), &d); err != nil {
return ""
}
if d.Choices != nil {
mainText = d.Choices[0].Delta.Content
return mainText
}
return ""
}