-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmain.go
159 lines (128 loc) · 3.93 KB
/
main.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main
import (
"bytes"
"encoding/json"
"log"
"net/http"
"os"
"text/template"
"github.com/gin-gonic/gin"
"github.com/go-rod/rod"
"github.com/go-rod/rod/lib/proto"
openai "github.com/sashabaranov/go-openai"
)
type Request struct {
Url string `json:"url"`
Schema map[string]json.RawMessage `json:"schema"`
}
const extractionScript = `() => {
function textNodesUnder(el){
var n, a=[], walk=document.createTreeWalker(el,NodeFilter.SHOW_TEXT,null,false);
while(n=walk.nextNode()) a.push(n);
return a;
}
return textNodesUnder(document.body)
.filter(
element => element.parentElement.tagName !== 'SCRIPT' &&
element.parentElement.tagName !== 'STYLE' &&
element.parentElement.tagName !== 'NOSCRIPT'
)
.map(v => v.nodeValue)
.map(v => v.trim())
.filter(v => v.length > 0)
.join(' ')
}`
func main() {
OPEN_AI_KEY := os.Getenv("OPEN_AI_KEY")
wsURL := os.Getenv("WS_URL")
if OPEN_AI_KEY == "" {
log.Fatal("OPEN_AI_KEY is not set")
}
if wsURL == "" {
log.Fatal("WS_URL is not set")
}
log.Println("Starting server...")
log.Println("WS_URL: ", wsURL)
openAIclient := openai.NewClient(OPEN_AI_KEY)
r := gin.Default()
log.Println("Connecting to browser...")
browser := rod.New().ControlURL(wsURL).MustConnect()
log.Println("Connected to browser")
defer browser.MustClose()
r.POST("/lookup", func(c *gin.Context) {
var requestBody Request
if err := c.BindJSON(&requestBody); err != nil {
log.Fatal(err)
}
log.Println("Request URL: ", requestBody.Url)
var pageText string
page := browser.MustPage(requestBody.Url)
pageRouter := page.HijackRequests()
// Do not load any images or css files
pageRouter.MustAdd("*", func(ctx *rod.Hijack) {
// There're a lot of types you can use in this enum, like NetworkResourceTypeScript for javascript files
// In this case we're using NetworkResourceTypeImage to block images
if ctx.Request.Type() == proto.NetworkResourceTypeImage ||
ctx.Request.Type() == proto.NetworkResourceTypeStylesheet ||
ctx.Request.Type() == proto.NetworkResourceTypeFont ||
ctx.Request.Type() == proto.NetworkResourceTypeMedia ||
ctx.Request.Type() == proto.NetworkResourceTypeManifest ||
ctx.Request.Type() == proto.NetworkResourceTypeOther {
ctx.Response.Fail(proto.NetworkErrorReasonBlockedByClient)
return
}
ctx.ContinueRequest(&proto.FetchContinueRequest{})
})
// since we are only hijacking a specific page, even using the "*" won't affect much of the performance
go pageRouter.Run()
page.MustWaitDOMStable()
pageText = page.MustEval(extractionScript).Str()
prompt := `
I have text data that was extracted from a webpage. The text data is as follows:
{{.PageText}}
I need to extract information from this data. I will provide JSON schema for the data. Return me the data in JSON format.
`
t := template.Must(template.New("prompt").Parse(prompt))
data := struct {
PageText string
}{
PageText: pageText,
}
buf := &bytes.Buffer{}
err := t.Execute(buf, data)
if err != nil {
log.Fatal(err)
}
req := openai.ChatCompletionRequest{
Model: openai.GPT3Dot5Turbo16K0613,
Messages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleUser,
Content: buf.String(),
},
},
Functions: []openai.FunctionDefinition{
{
Name: "ParseDataToJSON",
Description: "Parses text data from the webpage to JSON format",
Parameters: requestBody.Schema,
},
},
}
resp, err := openAIclient.CreateChatCompletion(c, req)
if err != nil {
log.Printf("Completion error: %v\n", err)
return
}
if err != nil {
log.Fatal(err)
}
log.Println(resp.Choices[0].Message.FunctionCall.Arguments)
var jsonMap map[string]interface{}
json.Unmarshal([]byte(resp.Choices[0].Message.FunctionCall.Arguments), &jsonMap)
c.JSON(http.StatusOK, gin.H{
"result": jsonMap,
})
})
r.Run(":8080")
}