haiku
the grass grows greener
summer evening sun amber
light wind gentle touch
haiku, unrhymed poetic form consisting of 17 syllables arranged in three lines of 5, 7, and 5 syllables respectively. Normally the current time of year should be reflected.
The traditional way to write a haiku, is to reach in to your inner poet and let its words come out. The haiku above is what I got.
And, then there is the way where you get someone else to do it, maybe you know a poet of some reputation? If not, consult a LLM somewhere.
The LLM needs to be given instructions / prompt on what it is expected to respond.
"Today's date is %s (Northern Hemisphere seasons apply).
Write one original haiku (5-7-5 syllables) whose imagery reflects the season this date falls in.
Use contemporary English-language haiku style: lowercase throughout except proper nouns, minimal or no end punctuation.
Respond with only the haiku, three lines, no title or commentary."haiku prompt
Basically, the same instructions could go to a consultant poet, he would probably think that you are a bit off though..
func generateHaiku(ctx context.Context) (string, error) {
today := time.Now().UTC().Format("January 2")
prompt := fmt.Sprintf(
"...",
today,
)
reqBody, err := json.Marshal(geminiRequest{
Contents: []geminiContent{
{
Parts: []geminiPart{
{Text: prompt},
},
},
},
})
if err != nil {
return "", err
}
url := fmt.Sprintf(geminiAPIURLFormat, geminiModel)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(reqBody))
if err != nil {
return "", err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("x-goog-api-key", geminiKey)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("gemini api returned %d: %s", resp.StatusCode, body)
}
var parsed geminiResponse
if err := json.Unmarshal(body, &parsed); err != nil {
return "", err
}
if len(parsed.Candidates) == 0 || len(parsed.Candidates[0].Content.Parts) == 0 {
return "", fmt.Errorf("gemini api returned no content")
}
return strings.TrimSpace(parsed.Candidates[0].Content.Parts[0].Text), nil
}
go func generateHaiku
Did the Format("January 2" )catch your eye? I really pondered a bit about that, but Google does have a good reason:
The reference time Mon Jan 2 15:04:05 MST 2006 was chosen so the numbers appear in increasing order: 1 (month), 2 (day), 3 (hour), 4 (minute), 5 (second), 6 (year). Easy to remember as "1 2 3 4 5 6" once you know the trick.`
Date and number formats are tricky. Any help with them is appreciated.
The haiku service is deployed to gcloud and lives at:
´https://haiku-service-671268857462.europe-west4.run.app´
To keep token count low, it is cached, so the LLM endpoint is only called once every 24h.
The haiku service is really living up to the site title, the context is so modest that its empty, only a prompt!
Feel free to use the service, and if your inner poet got inspired, grab a pen and pencil and go retro!
As long as the service is alive, todays haiku is shown below.