-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsummary.go
89 lines (65 loc) · 2.22 KB
/
summary.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
package autonews
import (
"context"
"errors"
"fmt"
"strings"
"github.com/lemon-mint/coord/llm"
)
const summary_prompt = `Please carefully read the following document:
<document>
%s
</document>
After reading, identify the key features and main points covered in the document.
Then, write a friendly 3-paragraph summary of the document that covers those key points.
Make your sentences as lively as possible, you can even use exclamation points.
Don't start with "This document is".
Always start your summary by explaining what the project or document does.
Include the insightful points of the document.
The summary should use soft, positive language to convey the essence of the document in an easily understandable way.
Never use string formatting such as Markdown and HTML.
If there is no appropriate content to summarize or the given document is an error page, print "NO_CONTENT"
Write your summary inside <summary> tags. The summary should be in English.`
var ErrSummaryNotProvided = errors.New("summary not provided")
func generateSummary(ctx context.Context, model llm.LLM, document string) (string, error) {
response := model.GenerateStream(
ctx,
&llm.ChatContext{},
llm.TextContent(llm.RoleUser, fmt.Sprintf(summary_prompt, document)),
)
err := response.Wait()
if err != nil {
return "", err
}
texts := getTexts(response.Content)
if len(texts) == 0 {
return "", ErrSummaryNotProvided
}
text := strings.Join(texts, "")
if strings.Contains(text, "NO_CONTENT") {
return "", ErrSummaryNotProvided
}
_, after, ok := strings.Cut(text, "<summary>")
if !ok {
return "", ErrSummaryNotProvided
}
summary, _, ok := strings.Cut(after, "</summary>")
if !ok {
return "", ErrSummaryNotProvided
}
summary = strings.TrimSpace(summary)
summary = strings.ReplaceAll(summary, "\n\n", "\n")
summary = strings.ReplaceAll(summary, "\n\n", "\n")
summary = strings.ReplaceAll(summary, "\n", "\n\n")
lines := strings.Split(summary, "\n")
var LinesWithoutEmpty []string
for _, line := range lines {
line = strings.TrimSpace(line)
if len(line) > 0 {
LinesWithoutEmpty = append(LinesWithoutEmpty, line)
}
}
summary = strings.Join(LinesWithoutEmpty, "\n\n")
summary = strings.TrimSpace(summary)
return summary, nil
}