autocrossbow/cmd/acb/summarize.go

100 lines
3.2 KiB
Go
Raw Normal View History

2025-09-13 08:40:40 -04:00
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"time"
2025-10-28 12:28:04 -04:00
"o5r.ca/autocrossbow/contributions"
"o5r.ca/autocrossbow/issues"
"o5r.ca/autocrossbow/issues/vikunja"
2025-09-13 08:40:40 -04:00
)
2025-10-28 12:28:04 -04:00
const defaultPrompt = `I will provide you, for a given period, with an employee name and a list of Pull Request titles and summaries split by repository, and a list of Jira Issues an employee has worked on. I may also provide, optionally, the employee's self-assessment. If I do, integrate that.
I'd like you to summarize the employee's accomplishments for the quarter
I'd like the summary for the accomplishments to be in prose form, in a few paragraphs separated based on areas of work. Keep answers to 500 words for the summary.`
2025-09-13 08:40:40 -04:00
// SummarizeData takes GitHub PRs and Jira issues data and sends it to an OpenAI-compatible endpoint for summarization.
func SummarizeData(employeename string, prs map[string][]contributions.PullRequest, issues []issues.Issue, tasks []vikunja.Task, prompt string) (string, error) {
2025-09-13 08:40:40 -04:00
// Build a prompt string
fullPrompt := prompt + fmt.Sprintf("\n\nHere's the PRs and Tickets for the employee %s:\n\n", employeename)
2025-09-13 08:40:40 -04:00
for repo, prList := range prs {
fullPrompt += fmt.Sprintf("Repository: %s\n", repo)
2025-09-13 08:40:40 -04:00
for _, pr := range prList {
fullPrompt += fmt.Sprintf("- Title: %s\n", pr.Title)
fullPrompt += fmt.Sprintf(" Body: %s\n", pr.Body)
2025-09-13 08:40:40 -04:00
}
}
2025-11-10 14:30:17 -05:00
fullPrompt += "Issues:\n"
2025-09-13 08:40:40 -04:00
for _, issue := range issues {
fullPrompt += fmt.Sprintf("Summary: %s\n", issue.Summary)
fullPrompt += fmt.Sprintf("Description: %s\n", issue.Description)
2025-11-10 14:30:17 -05:00
fullPrompt += "--------\n"
2025-09-13 08:40:40 -04:00
}
promptf, err := os.Create(fmt.Sprintf("prompt-%s-%d.json", employeename, time.Now().Unix()))
if err != nil {
fmt.Println(fmt.Errorf("error creating PR file: %w", err))
os.Exit(1)
}
promptf.WriteString(fullPrompt)
defer promptf.Close()
2025-09-13 08:40:40 -04:00
// Get OpenAI endpoint and token from environment variables
openaiEndpoint := os.Getenv("OPENAI_ENDPOINT")
openaiToken := os.Getenv("OPENAI_TOKEN")
2025-10-28 12:28:04 -04:00
openaiModel := os.Getenv("OPENAI_MODEL")
2025-09-13 08:40:40 -04:00
if openaiEndpoint == "" || openaiToken == "" {
return "", fmt.Errorf("OPENAI_ENDPOINT and OPENAI_TOKEN must be set in environment variables")
}
// Create a JSON payload for the OpenAI API
2025-10-28 12:28:04 -04:00
payload := struct {
Model string `json:"model"`
Messages []struct {
Role string `json:"role"`
Content string `json:"content"`
} `json:"messages"`
}{
Model: openaiModel,
Messages: []struct {
Role string `json:"role"`
Content string `json:"content"`
}{{Role: "system", Content: fullPrompt}},
2025-10-28 12:28:04 -04:00
}
2025-09-13 08:40:40 -04:00
jsonPayload, err := json.Marshal(payload)
2025-10-28 12:28:04 -04:00
fmt.Println(string(jsonPayload))
2025-09-13 08:40:40 -04:00
if err != nil {
return "", err
}
// Create a POST request to the OpenAI endpoint with JSON body
req, err := http.NewRequest("POST", openaiEndpoint, bytes.NewBuffer(jsonPayload))
if err != nil {
return "", err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", openaiToken))
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
2025-10-28 12:28:04 -04:00
//parts := strings.Split(body, "think")
2025-09-13 08:40:40 -04:00
return string(body), nil
}