yogai/process/openai.go

55 lines
1.3 KiB
Go
Raw Normal View History

2023-07-06 13:25:51 +02:00
package process
2023-05-13 12:53:37 +02:00
import (
"context"
"fmt"
2023-05-31 16:27:35 +02:00
2024-09-17 07:10:35 +02:00
"go-mod.ewintr.nl/yogai/model"
2023-05-13 12:53:37 +02:00
"github.com/sashabaranov/go-openai"
)
2023-07-06 13:25:51 +02:00
type OpenAISummarizer struct {
2023-05-13 12:53:37 +02:00
client *openai.Client
}
2023-07-06 13:25:51 +02:00
func NewOpenAISummarizer(client *openai.Client) *OpenAISummarizer {
return &OpenAISummarizer{
client: client,
2023-05-13 12:53:37 +02:00
}
}
2023-07-06 13:25:51 +02:00
func (sum *OpenAISummarizer) Name() string {
return "openai summarizer"
}
func (sum *OpenAISummarizer) Do(ctx context.Context, video *model.Video) error {
const summarizePrompt = `You are an helpful assistant. Your task is to extract all text that refers to the content of a yoga workout video from the description a user gives you.
You will not add introductory sentences like "This text is about", or "Summary of...". Just give the words verbatim. Trim any white space back to a simple space
`
resp, err := sum.client.CreateChatCompletion(
ctx,
2023-05-13 12:53:37 +02:00
openai.ChatCompletionRequest{
Model: openai.GPT4,
Messages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleSystem,
Content: summarizePrompt,
},
{
Role: openai.ChatMessageRoleUser,
2023-05-31 16:27:35 +02:00
Content: fmt.Sprintf("%s\n\n%s", video.YoutubeTitle, video.YoutubeDescription),
2023-05-13 12:53:37 +02:00
},
},
})
if err != nil {
return fmt.Errorf("failed to fetch summary: %w", err)
}
video.Summary = resp.Choices[len(resp.Choices)-1].Message.Content
return nil
}