-
Notifications
You must be signed in to change notification settings - Fork 0
/
newstitle.go
59 lines (42 loc) · 1.39 KB
/
newstitle.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
package autonews
import (
"context"
"fmt"
"strings"
"github.com/lemon-mint/coord/llm"
)
const newstitle_prompt = `Read this newsletter and come up with a different title based on the topic you think your readers will be most interested in!:
<news>
%s
</news>
After reading, identify the main features and points covered in the document.
Then write a friendly and interesting one-sentence title for the article that addresses those key points.
Make your sentences as vivid as possible, and you can even use exclamation marks.
The title should use soft, positive language to convey the essence of the document in an easily understandable way.
Avoid using string formats like Markdown and HTML.
Write the title within the <title> tag. The title must be written in English.`
func generateNewsTitle(ctx context.Context, model llm.LLM, document string) (string, error) {
response := model.GenerateStream(
ctx,
&llm.ChatContext{},
llm.TextContent(llm.RoleUser, fmt.Sprintf(newstitle_prompt, document)),
)
err := response.Wait()
if err != nil {
return "", err
}
texts := getTexts(response.Content)
if len(texts) == 0 {
return "", ErrTitleFailed
}
text := strings.Join(texts, "")
_, after, ok := strings.Cut(text, "<title>")
if !ok {
return "", ErrTitleFailed
}
title, _, ok := strings.Cut(after, "</title>")
if !ok {
return "", ErrTitleFailed
}
return strings.TrimSpace(title), nil
}