/
04-inprompt.go
71 lines (58 loc) · 1.4 KB
/
04-inprompt.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"strings"
"unicode"
)
func findName(input string) string {
words := strings.Fields(input)
if len(words) == 0 {
return ""
}
lastWord := strings.TrimRightFunc(words[len(words)-1], func(r rune) bool {
return !unicode.IsLetter(r)
})
return lastWord
}
func inprompt() {
var resp bytes.Buffer
taskToken, resp, secrets := downloadTask("inprompt")
//____Solve_Task____
type Task struct {
Code int `json:"code"`
Msg string `json:"msg"`
Input []string `json:"input"`
Question string `json:"question"`
}
var task Task
err := json.NewDecoder(&resp).Decode(&task)
checkError(err)
name := findName(task.Question)
knowlade := ""
for _, input := range task.Input {
if strings.Contains(input, name) {
knowlade = input
}
}
var messages []Message
systemPrompt := "Just answer the questions based on this information:/\n###/\n"
systemPrompt += knowlade
messages = append(messages, Message{
Role: "system",
Content: systemPrompt,
})
messages = append(messages, Message{
Role: "user",
Content: task.Question,
})
response := completions(secrets.OpenaiAPIKey, "gpt-3.5-turbo-0125", messages, 0, nil)
result := response.Choices[0].Message.Content
fmt.Println(messages)
fmt.Println(response)
postBody, _ := json.Marshal(map[string]string{
"answer": result,
})
sendAnswer(taskToken, postBody, secrets)
}