Update GenerateTitle

Show conversation and expect result back in JSON
This commit is contained in:
Matt Low 2024-05-28 07:37:09 +00:00
parent 0d66a49997
commit 0ad698a942
1 changed files with 34 additions and 25 deletions

View File

@ -2,6 +2,7 @@ package util
import ( import (
"context" "context"
"encoding/json"
"fmt" "fmt"
"os" "os"
"strings" "strings"
@ -149,36 +150,40 @@ func FormatForExternalPrompt(messages []model.Message, system bool) string {
} }
func GenerateTitle(ctx *lmcli.Context, messages []model.Message) (string, error) { func GenerateTitle(ctx *lmcli.Context, messages []model.Message) (string, error) {
const prompt = `Above is an excerpt from a conversation between a user and AI assistant. const systemPrompt = `You will be shown a conversation between a user and an AI assistant. Your task is to generate a short title (8 words or less) for the provided conversation that reflects the conversation's topic.
Reply with a short title (no more than 8 words) that reflects the topic of the conversation, read from the user's perspective.
Example conversation: Example conversation:
""" [{"role": "user", "content": "Can you help me with my math homework?"},{"role": "assistant", "content": "Sure, what topic are you struggling with?"}]
User:
Hello! Your response (response only with JSON in the format shown):
Assistant: {"title": "Help with math homework"}
Hello! How may I assist you?
"""
Your response:
"""
Title: A brief introduction
"""
Respond only as shown above.
` `
conversation := FormatForExternalPrompt(messages, false) type msg struct {
Role string
Content string
}
var msgs []msg
for _, m := range messages {
msgs = append(msgs, msg{string(m.Role), m.Content})
}
// Serialize the conversation to JSON
conversation, err := json.Marshal(msgs)
if err != nil {
return "", err
}
generateRequest := []model.Message{ generateRequest := []model.Message{
{
Role: model.MessageRoleSystem,
Content: systemPrompt,
},
{ {
Role: model.MessageRoleUser, Role: model.MessageRoleUser,
Content: fmt.Sprintf("\"\"\"\n%s\n\"\"\"\n\n%s", conversation, prompt), Content: string(conversation),
}, },
} }
@ -197,12 +202,16 @@ Respond only as shown above.
return "", err return "", err
} }
// TODO: validate response format // Parse the JSON response
response = strings.TrimPrefix(response, "Title:") var jsonResponse struct {
response = strings.TrimSpace(response) Title string `json:"title"`
response = strings.Trim(response, "\"") }
err = json.Unmarshal([]byte(response), &jsonResponse)
if err != nil {
return "", err
}
return response, nil return jsonResponse.Title, nil
} }
// ShowWaitAnimation prints an animated ellipses to stdout until something is // ShowWaitAnimation prints an animated ellipses to stdout until something is