-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathai.go
216 lines (178 loc) · 6.33 KB
/
ai.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package main
import (
openai "github.com/sashabaranov/go-openai"
"context"
"encoding/json"
"fmt"
"os"
"os/exec"
"strings"
)
const SYSTEM_PROMPT = `
You are a case study consultancy bot. Your audience are experienced consultants.
Structure of your output
Description of the two companies, should be at least two long paragraphs. And include a relevant fact in regards to that case study.
1. Context:
Provide background information about the case.
Highlight key relevant details.
Explain the problem statement.
Justify why the case is relevant for the consultant.
2. Approach:
Describe the step-by-step process used to solve the problem.
Segment the approach by tangible key outputs.
3. Impact
Present key figures and quantitative outcomes.
Focus on the results stemming from the approach.
----
Use formal, objective, and professional language suitable for corporate, business, and government contexts.
Ensure the language is quantitative, provides clear evidence, and is direct and to the point.
Avoid repetitive insights/impacts to ensure varied outcomes while maintaining structural consistency.
Ensure that the cases are closely relevant to the consultant's question.
The three categories
Context:
- Describe the company and the industry it operates in.
- Outline the challenge or problem the company faced.
- Explain why this problem is significant.
Approach:
- Detail the actions taken to address the problem.
- Highlight key steps and strategies used.
- Segment the approach into clear, tangible actions.
Impact:
- Quantify the results achieved through the approach.
- Use specific metrics and figures to demonstrate success.
- Explain the broader impact on the company and its stakeholders.
Example Structure
---
Your response is always in json, do not surround your json response with anything, do not surround with (` + "```" + `).
Do not use references, it should be valid JSON. Here is an example:
{
"case_study": {
"title": "Case Study on Z area of Company X, Y",
"company_a_name": "Company X",
"company_a_summary": "Company X was founded in ... by ... It was ...",
"company_b_name": "Company B",
"company_b_summary": "Company B was founded in ... by ... It was ...",
"context": [
"Company X, a leading player in the Y industry, faced a significant challenge in Z area.",
"This problem was critical because..."
],
"approach": [
"Step 1: Detailed description of the first key action.",
"Step 2: Explanation of the second action, including any tools or strategies used.",
"Step 3: Further steps segmented by tangible outputs."
],
"impact": [
"Outcome 1: Specific figures showing improvement.",
"Outcome 2: Quantitative metrics demonstrating success.",
"Outcome 3: Broader impact on the organization, supported by data."
]
}
}
`
type Response struct {
CaseStudy CaseStudy `json:"case_study"`
}
type CaseStudy struct {
Title string `json:"title"`
CompanyAName string `json:"company_a_name"`
CompanyASummary string `json:"company_a_summary"`
CompanyBName string `json:"company_b_name"`
CompanyBSummary string `json:"company_b_summary"`
Context []string `json:"context"`
Approach []string `json:"approach"`
Impact []string `json:"impact"`
}
type ErrorResponse struct {
Error string `json:"error"`
}
func GetSummary(prompt string) (CaseStudy, error) {
if len(prompt) < 8 {
return CaseStudy{}, fmt.Errorf("Prompt is too short, please provide more information")
}
client := openai.NewClient(openaiKey)
resp, err := client.CreateChatCompletion(
context.Background(),
openai.ChatCompletionRequest{
Model: openai.GPT4o,
Messages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleSystem,
Content: SYSTEM_PROMPT,
},
{
Role: openai.ChatMessageRoleUser,
Content: prompt,
},
},
},
)
if err != nil {
fmt.Println(err)
return CaseStudy{}, fmt.Errorf("Failed to connect to AI model")
}
fmt.Println(resp.Choices[0].Message.Content)
// Serialise the response
var caseStudy Response
err = json.Unmarshal([]byte(resp.Choices[0].Message.Content), &caseStudy)
if err != nil {
var errorResponse ErrorResponse
err2 := json.Unmarshal([]byte(resp.Choices[0].Message.Content), &errorResponse)
if err2 != nil {
fmt.Println(err, err2)
return CaseStudy{}, fmt.Errorf("Invalid AI response, try again?")
}
return CaseStudy{}, fmt.Errorf(errorResponse.Error)
}
return caseStudy.CaseStudy, nil
}
func escape(s string) string {
// TODO?
return s
}
func generateSlides(caseStudy CaseStudy) (string, error) {
data, _ := slideshow.ReadFile("page.html")
tex := string(data)
tex = strings.ReplaceAll(tex, "@@TITLE@@", escape(caseStudy.Title))
tex = strings.ReplaceAll(tex, "@@AUTHOR@@", "AI")
tex = strings.ReplaceAll(tex, "@@COMPANYA@@", escape(caseStudy.CompanyAName))
tex = strings.ReplaceAll(tex, "@@COMPANYADESC@@", escape(caseStudy.CompanyASummary))
tex = strings.ReplaceAll(tex, "@@COMPANYB@@", escape(caseStudy.CompanyBName))
tex = strings.ReplaceAll(tex, "@@COMPANYBDESC@@", escape(caseStudy.CompanyBSummary))
context := ""
for _, c := range caseStudy.Context {
context += "<li>" + escape(c) + "</li>\n"
}
tex = strings.ReplaceAll(tex, "@@CONTEXT@@", context)
approach := ""
for _, a := range caseStudy.Approach {
approach += "<li>" + escape(a) + "</li>\n"
}
tex = strings.ReplaceAll(tex, "@@APPROACH@@", approach)
impact := ""
for _, i := range caseStudy.Impact {
impact += "<li>" + escape(i) + "</li>\n"
}
tex = strings.ReplaceAll(tex, "@@IMPACT@@", impact)
// Write to temp directory
dir, err := os.MkdirTemp("", "slides")
if err != nil {
fmt.Println(err)
return "", err
}
doc, _ := slideshow.ReadFile("doc.svg")
target, _ := slideshow.ReadFile("target.svg")
arrow, _ := slideshow.ReadFile("arrow.png")
os.WriteFile(dir+"/doc.svg", doc, 0600)
os.WriteFile(dir+"/target.svg", target, 0600)
os.WriteFile(dir+"/arrow.png", arrow, 0600)
os.WriteFile(dir+"/slides.html", []byte(tex), 0600)
// Run chromium --headless=new --print-to-pdf --window-size=1920,1080 --no-pdf-header-footer page.html
cmd := exec.Command("chromium", "--headless=new", "--print-to-pdf", "--window-size=1920,1080", "--no-pdf-header-footer", "slides.html")
cmd.Dir = dir
err = cmd.Run()
if err != nil {
fmt.Println(err)
return "", err
}
return dir + "/output.pdf", nil
}