-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
244 lines (196 loc) · 5.67 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package main
import (
"context"
"flag"
"fmt"
"os"
"strings"
_ "github.com./go-sql-driver/mysql"
"github.com./henomis/lingoose/llm/openai"
"github.com./henomis/lingoose/pipeline"
sqlpipeline "github.com./henomis/lingoose/pipeline/sql"
"github.com./henomis/lingoose/prompt"
"github.com./henomis/lingoose/types"
"github.com./jedib0t/go-pretty/v6/table"
_ "github.com./mattn/go-sqlite3"
)
func main() {
var dataSourceType string
var dataSourceName string
var question string
var plotQuestion string
var openaiApiKey string
flag.StringVar(&dataSourceType, "t", "", "type of the datasource (sqlite|mysql)")
flag.StringVar(&dataSourceName, "n", "", "name of the datasource (database path|connection string)")
flag.StringVar(&question, "q", "", "question to ask the datasource")
flag.StringVar(&plotQuestion, "p", "", "question to plot the datasource")
flag.StringVar(&openaiApiKey, "k", "", "openai api key (defaults to OPENAI_API_KEY env var)")
flag.Parse()
var sqlDataSource sqlpipeline.DataSourceType
if dataSourceType == "sqlite" {
sqlDataSource = sqlpipeline.DataSourceSqlite
} else if dataSourceType == "mysql" {
sqlDataSource = sqlpipeline.DataSourceSqlite
} else if openaiApiKey == "" {
openaiApiKey = os.Getenv("OPENAI_API_KEY")
if openaiApiKey == "" {
fmt.Println("Please provide an OpenAI API key")
os.Exit(1)
}
} else {
fmt.Println("Please provide a datasource type")
os.Exit(1)
}
if dataSourceName == "" {
fmt.Println("Please provide a datasource name")
os.Exit(1)
} else if question == "" {
fmt.Println("Please provide a question")
os.Exit(1)
}
sqlPipe, err := sqlpipeline.New(
openai.NewCompletion().WithMaxTokens(1000).WithTemperature(0),
sqlDataSource,
dataSourceName,
)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
answer, err := sqlPipe.Run(context.Background(), types.M{"question": question})
if err != nil {
fmt.Println(err)
os.Exit(1)
}
description := answer["output"].(string)
sqlQuery := answer["sql_query"].(string)
sqlResult := answer["sql_result"].(string)
fmt.Println()
renderQuestion(question)
renderSQLQuery(sqlQuery)
renderDescription(description)
renderSQLResultTable(sqlResult)
if plotQuestion != "" {
err = plotDataSource(plotQuestion, sqlResult)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
}
func renderQuestion(question string) {
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"SQL QUESTION"})
t.AppendRows([]table.Row{{question}})
t.SetStyle(table.StyleColoredBlackOnYellowWhite)
t.Render()
fmt.Println()
}
func renderSQLQuery(sqlQuery string) {
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"SQL QUERY"})
t.AppendRows([]table.Row{{sqlQuery}})
t.SetStyle(table.StyleColoredBlackOnGreenWhite)
t.Render()
fmt.Println()
}
func renderDescription(description string) {
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"DESCRIPTION"})
t.AppendRows([]table.Row{{description}})
t.SetStyle(table.StyleColoredBlackOnMagentaWhite)
t.Render()
fmt.Println()
}
func renderSQLResultTable(sqlResult string) {
sqlResultRows := strings.Split(sqlResult, "\n")
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
for i, row := range sqlResultRows {
cols := strings.Split(row, "|")
var tableRow table.Row
for _, col := range cols {
tableRow = append(tableRow, col)
}
if i == 0 {
t.AppendHeader(tableRow)
} else {
t.AppendRow(tableRow)
}
}
t.SetStyle(table.StyleColoredBlackOnCyanWhite)
t.Render()
fmt.Println()
}
func sqlResultTableToMarkdown(sqlResult string) string {
markdownTable := ""
sqlResultRows := strings.Split(sqlResult, "\n")
for i, row := range sqlResultRows {
cols := strings.Split(row, "|")
if i == 0 {
markdownTable += "|"
for _, col := range cols {
markdownTable += col + "|"
}
markdownTable += "\n|"
for i := 0; i < len(cols); i++ {
markdownTable += "---|"
}
markdownTable += "\n"
} else {
markdownTable += "|"
for _, col := range cols {
markdownTable += col + "|"
}
markdownTable += "\n"
}
}
return markdownTable
}
func sqlResultTableToHTML(sqlResult string) string {
htmlTable := "<br><table border=\"1\" style=\"border-collapse: collapse\">"
sqlResultRows := strings.Split(sqlResult, "\n")
for i, row := range sqlResultRows {
cols := strings.Split(row, "|")
if i == 0 {
htmlTable += "<tr>"
for _, col := range cols {
htmlTable += "<th>" + col + "</th>"
}
htmlTable += "</tr>"
} else {
htmlTable += "<tr>"
for _, col := range cols {
htmlTable += "<td>" + col + "</td>"
}
htmlTable += "</tr>"
}
}
htmlTable += "</table>"
return htmlTable
}
func plotDataSource(plotQuery, sqlResult string) error {
chartJS := pipeline.Llm{
LlmEngine: openai.NewCompletion().WithMaxTokens(1000).WithTemperature(0),
LlmMode: pipeline.LlmModeCompletion,
Prompt: prompt.NewPromptTemplate("Consider the following markdown data table:\n\n{{.sql_result}}\n\ncreate a chart.js canvas and script elements to plot such data. don't add other html elements.\n{{.plot_query}}"),
}
pipeChartJS := pipeline.NewTube(chartJS)
output, err := pipeChartJS.Run(context.Background(), types.M{"sql_result": sqlResultTableToMarkdown(sqlResult), "plot_query": plotQuery})
if err != nil {
return err
}
chartFile, err := os.Create("chart.html")
if err != nil {
return err
}
header := `<html><head><title>Chart.js Plot</title><script src="https://cdn.jsdelivr.net/npm/[email protected]"></script></head><body>`
chartFile.Write([]byte(header))
chartFile.Write([]byte(output["output"].(string)))
chartFile.Write([]byte(sqlResultTableToHTML(sqlResult)))
chartFile.Write([]byte("</body></html>"))
return nil
}