From 9086afcf1267832c438e429ec6583fe7d04b935a Mon Sep 17 00:00:00 2001 From: Chandler Swift Date: Sun, 28 Dec 2025 20:02:17 -0600 Subject: [PATCH] parallelize queries --- site/site.go | 164 +++++++++++++++++++++++++++++++++++---------------- 1 file changed, 114 insertions(+), 50 deletions(-) diff --git a/site/site.go b/site/site.go index 94547e9..f8ffe1c 100644 --- a/site/site.go +++ b/site/site.go @@ -6,8 +6,10 @@ import ( "encoding/json" "fmt" "html/template" + "log" "regexp" "strings" + "sync" "time" "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) { site = &Site{} - prompt := norm(` + ctx := context.TODO() + + occupationPrompt := norm(` 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". 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 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 { return nil, fmt.Errorf("parsing service: %w", err) } - - 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) - } - + log.Printf("Done parsing service %s.", site.Occupation) site.Theme = Theme{ // TODO AccentColor: Color{ 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 (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. Markup should be in HTML, including paragraph breaks. Do not wrap in any additional markup. `) - whatWeDo, err := cp.Complete(context.TODO(), fmt.Sprintf(prompt, site.Occupation), nil) - site.WhatWeDo = template.HTML(whatWeDo) - if err != nil { - return nil, fmt.Errorf("generating what we do: %w", err) - } - - prompt = norm(` + chandlerPrompt := norm(` 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 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. 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) - if err != nil { - return nil, fmt.Errorf("generating Chandler bio: %w", err) - } - site.ChandlerBio = template.HTML(bio) - - prompt = norm(` + ericPrompt := norm(` 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 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. 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) - if err != nil { - return nil, fmt.Errorf("generating Eric bio: %w", err) - } - site.EricBio = template.HTML(bio) - - prompt = norm(` + isaacPrompt := norm(` 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 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. 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) - if err != nil { - return nil, fmt.Errorf("generating Isaac bio: %w", err) - } - site.IsaacBio = template.HTML(bio) - - prompt = norm(` + testimonialsPrompt := norm(` 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). 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 { - return nil, fmt.Errorf("generating testimonials: %w", err) + + var ( + 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) + } + log.Printf("Generated tagline: %s", taglineResult) + }(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 + } } - fmt.Println("Raw testimonials: %s\n", raw_testimonials) - err = json.Unmarshal([]byte(raw_testimonials), &site.Testimonials) + 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 { return nil, fmt.Errorf("unmarshaling testimonials: %w", err) }