-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
69 lines (58 loc) · 1.61 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
package main
import (
"context"
"fmt"
"log"
"os"
gpt3 "github.com/PullRequestInc/go-gpt3"
"github.com/joho/godotenv"
"github.com/shomali11/slacker"
)
func main() {
godotenv.Load(".env")
bot := slacker.NewClient(os.Getenv("SLACK_BOT_TOKEN"), os.Getenv("SLACK_APP_TOKEN"))
apiKey := os.Getenv("API_KEY")
if apiKey == "" {
log.Fatal("Empty API_KEY")
}
ctx := context.Background()
client := gpt3.NewClient(apiKey)
go printCommandEvents(bot.CommandEvents())
bot.Command("<query>", &slacker.CommandDefinition{
Description: "Send any question to ChatGPT",
Examples: []string{"who is cristiano ronaldo?"},
Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
query := request.Param("query")
res := GetResponse(client, ctx, query)
response.Reply(res)
},
})
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
err := bot.Listen(ctx)
if err != nil {
log.Fatal(err)
}
}
func GetResponse(client gpt3.Client, ctx context.Context, question string) string {
resp, err := client.CompletionWithEngine(ctx, gpt3.TextDavinci003Engine, gpt3.CompletionRequest{
Prompt: []string{question},
MaxTokens: gpt3.IntPtr(3000),
Temperature: gpt3.Float32Ptr(0),
})
if err != nil {
fmt.Println(err)
return err.Error()
}
return resp.Choices[0].Text
}
func printCommandEvents(analyticsChannel <-chan *slacker.CommandEvent) {
for event := range analyticsChannel {
fmt.Println("Command Eventds")
fmt.Println(event.Timestamp)
fmt.Println(event.Command)
fmt.Println(event.Parameters)
fmt.Println(event.Event)
fmt.Println()
}
}