This repository has been archived by the owner on Nov 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconclude.go
94 lines (76 loc) · 2.23 KB
/
conclude.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
90
91
92
93
94
// Copyright 2023 Schibsted. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package concluder
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
)
const openAIURL = "https://api.openai.com/v1/chat/completions"
type OpenAIRequest struct {
Model string `json:"model"`
Messages []interface{} `json:"messages"`
}
type OpenAIResponse struct {
Choices []struct {
Message struct {
Role string `json:"role"`
Content string `json:"content"`
} `json:"message"`
} `json:"choices"`
}
func generateText(prompt string) (string, error) {
reqBody := OpenAIRequest{
Model: "gpt-3.5-turbo",
Messages: []interface{}{
map[string]string{
"role": "user",
"content": prompt,
},
},
}
jsonData, err := json.Marshal(reqBody)
if err != nil {
return "", err
}
req, err := http.NewRequest("POST", openAIURL, bytes.NewBuffer(jsonData))
if err != nil {
return "", err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", Config.OpenAIKey))
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
responseBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
var openAIResp OpenAIResponse
err = json.Unmarshal(responseBody, &openAIResp)
if err != nil {
return "", err
}
if len(openAIResp.Choices) == 0 {
return "", fmt.Errorf("No text generated by the OpenAI API")
}
return openAIResp.Choices[0].Message.Content, nil
}
func Conclude(content string) (string, error) {
summaryPrompt := fmt.Sprintf("Summarize the following meeting transcription: \"%s\"", content)
summary, err := generateText(summaryPrompt)
if err != nil {
return "", fmt.Errorf("error generating summary: %v", err)
}
conclusionPrompt := fmt.Sprintf("Generate a conclusion for the following meeting transcription (and at the end add a sentence of solid advice, formulated as a colorful command): \"%s\"", content)
conclusion, err := generateText(conclusionPrompt)
if err != nil {
return "", fmt.Errorf("error generating conclusion: %v", err)
}
return fmt.Sprintf("%s\n\n%s\n", strings.TrimSpace(summary), strings.TrimSpace(conclusion)), nil
}