parallelize queries
This commit is contained in:
parent
40223a12b5
commit
9086afcf12
1 changed files with 114 additions and 50 deletions
164
site/site.go
164
site/site.go
|
|
@ -6,8 +6,10 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
|
"log"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.chandlerswift.com/chandlerswift/svs-services-server/completion"
|
"git.chandlerswift.com/chandlerswift/svs-services-server/completion"
|
||||||
|
|
@ -82,7 +84,9 @@ func GetSiteData(subdomain string, cp completion.CompletionProvider) (site *Site
|
||||||
func GenerateSiteData(rawservice string, cp completion.CompletionProvider) (site *Site, err error) {
|
func GenerateSiteData(rawservice string, cp completion.CompletionProvider) (site *Site, err error) {
|
||||||
site = &Site{}
|
site = &Site{}
|
||||||
|
|
||||||
prompt := norm(`
|
ctx := context.TODO()
|
||||||
|
|
||||||
|
occupationPrompt := norm(`
|
||||||
Parse this subdomain for a company providing services into English words, if possible.
|
Parse this subdomain for a company providing services into English words, if possible.
|
||||||
For example, the subdomain "webdesign" should be parsed into the string "Web Design".
|
For example, the subdomain "webdesign" should be parsed into the string "Web Design".
|
||||||
If the string can't be parsed, just return the original string.
|
If the string can't be parsed, just return the original string.
|
||||||
|
|
@ -90,25 +94,13 @@ func GenerateSiteData(rawservice string, cp completion.CompletionProvider) (site
|
||||||
The string is "%s".
|
The string is "%s".
|
||||||
The parsed string is:
|
The parsed string is:
|
||||||
`)
|
`)
|
||||||
prompt = strings.TrimSpace(prompt)
|
|
||||||
prompt = whitespace.ReplaceAllString(prompt, " ")
|
|
||||||
|
|
||||||
site.Occupation, err = cp.Complete(context.TODO(), fmt.Sprintf(prompt, rawservice), nil)
|
log.Printf("Parsing service %s…", rawservice)
|
||||||
|
site.Occupation, err = cp.Complete(ctx, fmt.Sprintf(occupationPrompt, rawservice), nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("parsing service: %w", err)
|
return nil, fmt.Errorf("parsing service: %w", err)
|
||||||
}
|
}
|
||||||
|
log.Printf("Done parsing service %s.", site.Occupation)
|
||||||
prompt = norm(`
|
|
||||||
Generate a short, catchy tagline for a %v company.
|
|
||||||
The tagline should be no more than 7 words long, and should be slightly silly;
|
|
||||||
e.g. for a web design company, something like "Custom websites cooked to order".
|
|
||||||
Do not include quotes.
|
|
||||||
`)
|
|
||||||
site.Tagline, err = cp.Complete(context.TODO(), fmt.Sprintf(prompt, site.Occupation), nil)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("generating tagline: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
site.Theme = Theme{ // TODO
|
site.Theme = Theme{ // TODO
|
||||||
AccentColor: Color{
|
AccentColor: Color{
|
||||||
Hex: "#FF5733",
|
Hex: "#FF5733",
|
||||||
|
|
@ -122,20 +114,20 @@ func GenerateSiteData(rawservice string, cp completion.CompletionProvider) (site
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
prompt = norm(`
|
taglinePrompt := norm(`
|
||||||
|
Generate a short, catchy tagline for a %v company.
|
||||||
|
The tagline should be no more than 7 words long, and should be slightly silly;
|
||||||
|
e.g. for a web design company, something like "Custom websites cooked to order".
|
||||||
|
Do not include quotes.
|
||||||
|
`)
|
||||||
|
whatWeDoPrompt := norm(`
|
||||||
Generate a few short paragraphs (3-4 sentences each) describing what the offices of Swift, Villnow & Swift
|
Generate a few short paragraphs (3-4 sentences each) describing what the offices of Swift, Villnow & Swift
|
||||||
(colloquially \"SVS\"), a %v company, has to offer, in an over-the-top, slightly irreverent tone.
|
(colloquially \"SVS\"), a %v company, has to offer, in an over-the-top, slightly irreverent tone.
|
||||||
The paragraphs will go on the company's homesite to inform visitors about their services.
|
The paragraphs will go on the company's homesite to inform visitors about their services.
|
||||||
Markup should be in HTML, including paragraph breaks.
|
Markup should be in HTML, including paragraph breaks.
|
||||||
Do not wrap in any additional markup.
|
Do not wrap in any additional markup.
|
||||||
`)
|
`)
|
||||||
whatWeDo, err := cp.Complete(context.TODO(), fmt.Sprintf(prompt, site.Occupation), nil)
|
chandlerPrompt := norm(`
|
||||||
site.WhatWeDo = template.HTML(whatWeDo)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("generating what we do: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
prompt = norm(`
|
|
||||||
Generate a short bio (2-4 sentences) for Chandler Swift, a co-founder of SVS, a %v company.
|
Generate a short bio (2-4 sentences) for Chandler Swift, a co-founder of SVS, a %v company.
|
||||||
Chandler went to high school in Glencoe, Minnesota with Eric Villnow and Isaac Swift, the other two co-founders.
|
Chandler went to high school in Glencoe, Minnesota with Eric Villnow and Isaac Swift, the other two co-founders.
|
||||||
Chandler has always enjoyed tinkering with projects.
|
Chandler has always enjoyed tinkering with projects.
|
||||||
|
|
@ -150,13 +142,7 @@ func GenerateSiteData(rawservice string, cp completion.CompletionProvider) (site
|
||||||
Emphasize the relevance of all the above information to SVS's line of work.
|
Emphasize the relevance of all the above information to SVS's line of work.
|
||||||
Do not shoehorn all the above information; only include each piece if relevant.
|
Do not shoehorn all the above information; only include each piece if relevant.
|
||||||
`)
|
`)
|
||||||
bio, err := cp.Complete(context.TODO(), fmt.Sprintf(prompt, site.Occupation), nil)
|
ericPrompt := norm(`
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("generating Chandler bio: %w", err)
|
|
||||||
}
|
|
||||||
site.ChandlerBio = template.HTML(bio)
|
|
||||||
|
|
||||||
prompt = norm(`
|
|
||||||
Generate a short, satirical/irreverent bio (2-4 sentences) for Eric Villnow, a co-founder of SVS, a %v company.
|
Generate a short, satirical/irreverent bio (2-4 sentences) for Eric Villnow, a co-founder of SVS, a %v company.
|
||||||
Eric went to high school in Glencoe, Minnesota with Isaac Swift and Chandler Swift, the other two co-founders.
|
Eric went to high school in Glencoe, Minnesota with Isaac Swift and Chandler Swift, the other two co-founders.
|
||||||
Eric has a degree in Computer Science from Bemidji State University with a minor in Business Administration.
|
Eric has a degree in Computer Science from Bemidji State University with a minor in Business Administration.
|
||||||
|
|
@ -171,13 +157,7 @@ func GenerateSiteData(rawservice string, cp completion.CompletionProvider) (site
|
||||||
Emphasize the relevance of all the above information to SVS's line of work.
|
Emphasize the relevance of all the above information to SVS's line of work.
|
||||||
Do not include all the above information; only mention each piece if relevant.
|
Do not include all the above information; only mention each piece if relevant.
|
||||||
`)
|
`)
|
||||||
bio, err = cp.Complete(context.TODO(), fmt.Sprintf(prompt, site.Occupation), nil)
|
isaacPrompt := norm(`
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("generating Eric bio: %w", err)
|
|
||||||
}
|
|
||||||
site.EricBio = template.HTML(bio)
|
|
||||||
|
|
||||||
prompt = norm(`
|
|
||||||
Generate a short, satirical/irreverent bio (2-4 sentences) for Isaac Swift, a co-founder of SVS, a %v company.
|
Generate a short, satirical/irreverent bio (2-4 sentences) for Isaac Swift, a co-founder of SVS, a %v company.
|
||||||
Isaac went to high school in Glencoe, Minnesota with Eric Villnow and Chandler Swift, the other two co-founders.
|
Isaac went to high school in Glencoe, Minnesota with Eric Villnow and Chandler Swift, the other two co-founders.
|
||||||
Isaac studied Computer Science for a year at North Dakota State University.
|
Isaac studied Computer Science for a year at North Dakota State University.
|
||||||
|
|
@ -195,13 +175,7 @@ func GenerateSiteData(rawservice string, cp completion.CompletionProvider) (site
|
||||||
Emphasize the relevance of all the above information to SVS's line of work.
|
Emphasize the relevance of all the above information to SVS's line of work.
|
||||||
Do not include all the above information; only mention each piece if relevant.
|
Do not include all the above information; only mention each piece if relevant.
|
||||||
`)
|
`)
|
||||||
bio, err = cp.Complete(context.TODO(), fmt.Sprintf(prompt, site.Occupation), nil)
|
testimonialsPrompt := norm(`
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("generating Isaac bio: %w", err)
|
|
||||||
}
|
|
||||||
site.IsaacBio = template.HTML(bio)
|
|
||||||
|
|
||||||
prompt = norm(`
|
|
||||||
Generate three satirical testimonials from satisfied clients of SVS, a %v company.
|
Generate three satirical testimonials from satisfied clients of SVS, a %v company.
|
||||||
Testimonials should be from well-known figures, possibly historical (say, a former US President).
|
Testimonials should be from well-known figures, possibly historical (say, a former US President).
|
||||||
When possible, the person quoted should have some tenuous connection to the services provided by SVS.
|
When possible, the person quoted should have some tenuous connection to the services provided by SVS.
|
||||||
|
|
@ -242,12 +216,102 @@ func GenerateSiteData(rawservice string, cp completion.CompletionProvider) (site
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
raw_testimonials, err := cp.Complete(context.TODO(), fmt.Sprintf(prompt, site.Occupation), schema)
|
|
||||||
if err != nil {
|
var (
|
||||||
return nil, fmt.Errorf("generating testimonials: %w", err)
|
taglineResult string
|
||||||
|
whatWeDoResult string
|
||||||
|
chandlerBioResult string
|
||||||
|
ericBioResult string
|
||||||
|
isaacBioResult string
|
||||||
|
rawTestimonials string
|
||||||
|
)
|
||||||
|
|
||||||
|
errCh := make(chan error, 6)
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
wg.Add(6)
|
||||||
|
|
||||||
|
go func(p string) {
|
||||||
|
defer wg.Done()
|
||||||
|
var e error
|
||||||
|
log.Println("Generating tagline...")
|
||||||
|
taglineResult, e = cp.Complete(ctx, fmt.Sprintf(p, site.Occupation), nil)
|
||||||
|
if e != nil {
|
||||||
|
errCh <- fmt.Errorf("generating tagline: %w", e)
|
||||||
}
|
}
|
||||||
fmt.Println("Raw testimonials: %s\n", raw_testimonials)
|
log.Printf("Generated tagline: %s", taglineResult)
|
||||||
err = json.Unmarshal([]byte(raw_testimonials), &site.Testimonials)
|
}(taglinePrompt)
|
||||||
|
|
||||||
|
go func(p string) {
|
||||||
|
defer wg.Done()
|
||||||
|
var e error
|
||||||
|
log.Println("Generating what we do...")
|
||||||
|
whatWeDoResult, e = cp.Complete(ctx, fmt.Sprintf(p, site.Occupation), nil)
|
||||||
|
if e != nil {
|
||||||
|
errCh <- fmt.Errorf("generating what we do: %w", e)
|
||||||
|
}
|
||||||
|
}(whatWeDoPrompt)
|
||||||
|
|
||||||
|
go func(p string) {
|
||||||
|
defer wg.Done()
|
||||||
|
var e error
|
||||||
|
log.Println("Generating Chandler bio...")
|
||||||
|
chandlerBioResult, e = cp.Complete(ctx, fmt.Sprintf(p, site.Occupation), nil)
|
||||||
|
if e != nil {
|
||||||
|
errCh <- fmt.Errorf("generating Chandler bio: %w", e)
|
||||||
|
}
|
||||||
|
}(chandlerPrompt)
|
||||||
|
|
||||||
|
go func(p string) {
|
||||||
|
defer wg.Done()
|
||||||
|
var e error
|
||||||
|
log.Println("Generating Eric bio...")
|
||||||
|
ericBioResult, e = cp.Complete(ctx, fmt.Sprintf(p, site.Occupation), nil)
|
||||||
|
if e != nil {
|
||||||
|
errCh <- fmt.Errorf("generating Eric bio: %w", e)
|
||||||
|
}
|
||||||
|
}(ericPrompt)
|
||||||
|
|
||||||
|
go func(p string) {
|
||||||
|
defer wg.Done()
|
||||||
|
var e error
|
||||||
|
log.Println("Generating Isaac bio...")
|
||||||
|
isaacBioResult, e = cp.Complete(ctx, fmt.Sprintf(p, site.Occupation), nil)
|
||||||
|
if e != nil {
|
||||||
|
errCh <- fmt.Errorf("generating Isaac bio: %w", e)
|
||||||
|
}
|
||||||
|
}(isaacPrompt)
|
||||||
|
|
||||||
|
go func(p string) {
|
||||||
|
defer wg.Done()
|
||||||
|
var e error
|
||||||
|
log.Println("Generating testimonials...")
|
||||||
|
rawTestimonials, e = cp.Complete(ctx, fmt.Sprintf(p, site.Occupation), schema)
|
||||||
|
if e != nil {
|
||||||
|
errCh <- fmt.Errorf("generating testimonials: %w", e)
|
||||||
|
}
|
||||||
|
}(testimonialsPrompt)
|
||||||
|
|
||||||
|
wg.Wait()
|
||||||
|
close(errCh)
|
||||||
|
|
||||||
|
var firstErr error
|
||||||
|
for e := range errCh {
|
||||||
|
if firstErr == nil && e != nil {
|
||||||
|
firstErr = e
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if firstErr != nil {
|
||||||
|
return nil, firstErr
|
||||||
|
}
|
||||||
|
|
||||||
|
site.Tagline = taglineResult
|
||||||
|
site.WhatWeDo = template.HTML(whatWeDoResult)
|
||||||
|
site.ChandlerBio = template.HTML(chandlerBioResult)
|
||||||
|
site.EricBio = template.HTML(ericBioResult)
|
||||||
|
site.IsaacBio = template.HTML(isaacBioResult)
|
||||||
|
|
||||||
|
fmt.Println("Raw testimonials: %s\n", rawTestimonials)
|
||||||
|
err = json.Unmarshal([]byte(rawTestimonials), &site.Testimonials)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("unmarshaling testimonials: %w", err)
|
return nil, fmt.Errorf("unmarshaling testimonials: %w", err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue