Suggest based on title from bookmarklet
This commit is contained in:
58
main.go
58
main.go
@@ -26,6 +26,7 @@ type ShortLinks struct {
|
||||
|
||||
domainAliases map[string]string
|
||||
writableDomains map[string]bool
|
||||
responseFormat *oaiResponseFormat
|
||||
}
|
||||
|
||||
type setResponse struct {
|
||||
@@ -34,6 +35,11 @@ type setResponse struct {
|
||||
URL string `json:"url"`
|
||||
}
|
||||
|
||||
type suggestRequest struct {
|
||||
Shorts []string `json:"shorts,omitempty"`
|
||||
Title string `json:"title,omitempty"`
|
||||
}
|
||||
|
||||
type suggestResponse struct {
|
||||
Shorts []string `json:"shorts"`
|
||||
Domain string `json:"domain"`
|
||||
@@ -96,6 +102,26 @@ func NewShortLinks(db *sql.DB, domainAliases map[string]string, writableDomains
|
||||
|
||||
domainAliases: domainAliases,
|
||||
writableDomains: writableDomains,
|
||||
responseFormat: &oaiResponseFormat{
|
||||
Type: "json_schema",
|
||||
JSONSchema: map[string]any{
|
||||
"name": "suggest_response",
|
||||
"strict": true,
|
||||
"schema": map[string]any{
|
||||
"type": "object",
|
||||
"properties": map[string]any{
|
||||
"shorts": map[string]any{
|
||||
"type": "array",
|
||||
"items": map[string]any{
|
||||
"type": "string",
|
||||
},
|
||||
},
|
||||
},
|
||||
"required": []string{"shorts"},
|
||||
"additionalProperties": false,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
sl.mux.HandleFunc("GET /{$}", sl.serveRoot)
|
||||
@@ -157,6 +183,7 @@ func (sl *ShortLinks) serveRootWithShort(w http.ResponseWriter, r *http.Request,
|
||||
"short": short,
|
||||
"host": sl.getDomain(r.Host),
|
||||
"long": r.Form.Get("long"),
|
||||
"title": r.Form.Get("title"),
|
||||
})
|
||||
if err != nil {
|
||||
sendError(w, http.StatusInternalServerError, "error executing template: %s", err)
|
||||
@@ -238,33 +265,32 @@ func (sl *ShortLinks) serveSuggest(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
if !r.Form.Has("shorts") {
|
||||
sendError(w, http.StatusBadRequest, "shorts= param required")
|
||||
if !r.Form.Has("shorts") && !r.Form.Has("title") {
|
||||
sendError(w, http.StatusBadRequest, "shorts= or title= param required")
|
||||
return
|
||||
}
|
||||
|
||||
user := strings.Join(r.Form["shorts"], "\n")
|
||||
in := suggestRequest{
|
||||
Shorts: r.Form["shorts"],
|
||||
Title: r.Form.Get("title"),
|
||||
}
|
||||
|
||||
comp, err := sl.oai.completeChat(
|
||||
"You are an assistant helping a user choose useful short names for a URL shortener. The request contains a list recents names chosen by the user, separated by newlines, with the most recent names first. Respond with only a list of possible suggestions for additional short names, separated by newlines. In descending order of preference, suggestions should include: plural/singular variations, 2 and 3 letter abbreivations, conceptual variations, other variations that are likely to be useful. Your bar for suggestions should be relatively high; responding with a shorter list of high quality suggestions is preferred.",
|
||||
user,
|
||||
out := &suggestResponse{}
|
||||
|
||||
err = sl.oai.completeChat(
|
||||
"You are an assistant helping a user choose useful short names for a URL shortener. The request contains JSON object where the optional `shorts` key contains a list of recent names chosen by the user, with the most recent names first, and the optional `title` key contains a title for the URL. Respond with only a JSON object where the `shorts` key contains a list of possible suggestions for additional short names. In descending order of preference, suggestions should include: plural/singular variations, 2 and 3 letter abbreivations, conceptual variations, other variations that are likely to be useful. Your bar for suggestions should be relatively high; responding with a shorter list of high quality suggestions is preferred.",
|
||||
in,
|
||||
sl.responseFormat,
|
||||
out,
|
||||
)
|
||||
if err != nil {
|
||||
sendError(w, http.StatusInternalServerError, "oai.completeChat: %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
shorts := []string{}
|
||||
for _, short := range strings.Split(comp, "\n") {
|
||||
if short != "" {
|
||||
shorts = append(shorts, strings.TrimSpace(short))
|
||||
}
|
||||
}
|
||||
out.Domain = sl.getDomain(r.Host)
|
||||
|
||||
sendJSON(w, suggestResponse{
|
||||
Shorts: shorts,
|
||||
Domain: sl.getDomain(r.Host),
|
||||
})
|
||||
sendJSON(w, out)
|
||||
}
|
||||
|
||||
func (sl *ShortLinks) serveHelp(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
46
openai.go
46
openai.go
@@ -15,8 +15,14 @@ type oaiClient struct {
|
||||
}
|
||||
|
||||
type oaiRequest struct {
|
||||
Model string `json:"model"`
|
||||
Messages []oaiMessage `json:"messages"`
|
||||
Model string `json:"model"`
|
||||
Messages []oaiMessage `json:"messages"`
|
||||
ResponseFormat *oaiResponseFormat `json:"response_format"`
|
||||
}
|
||||
|
||||
type oaiResponseFormat struct {
|
||||
Type string `json:"type"`
|
||||
JSONSchema map[string]interface{} `json:"json_schema"`
|
||||
}
|
||||
|
||||
type oaiMessage struct {
|
||||
@@ -48,23 +54,28 @@ func newOAIClientFromEnv() (*oaiClient, error) {
|
||||
return newOAIClient(apiKey), nil
|
||||
}
|
||||
|
||||
func (oai *oaiClient) completeChat(system, user string) (string, error) {
|
||||
buf := &bytes.Buffer{}
|
||||
err := json.NewEncoder(buf).Encode(&oaiRequest{
|
||||
func (oai *oaiClient) completeChat(system string, in any, responseFormat *oaiResponseFormat, out any) error {
|
||||
user, err := json.Marshal(in)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
reqBody, err := json.Marshal(&oaiRequest{
|
||||
Model: "gpt-4o",
|
||||
Messages: []oaiMessage{
|
||||
{Role: "system", Content: system},
|
||||
{Role: "user", Content: user},
|
||||
{Role: "user", Content: string(user)},
|
||||
},
|
||||
ResponseFormat: responseFormat,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return "", err
|
||||
return err
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", "https://api.openai.com/v1/chat/completions", buf)
|
||||
req, err := http.NewRequest("POST", "https://api.openai.com/v1/chat/completions", bytes.NewReader(reqBody))
|
||||
if err != nil {
|
||||
return "", err
|
||||
return err
|
||||
}
|
||||
|
||||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", oai.apiKey))
|
||||
@@ -72,23 +83,28 @@ func (oai *oaiClient) completeChat(system, user string) (string, error) {
|
||||
|
||||
resp, err := oai.c.Do(req)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return err
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != 200 {
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
return "", fmt.Errorf("%s", string(body))
|
||||
return fmt.Errorf("%s", string(body))
|
||||
}
|
||||
|
||||
dec := json.NewDecoder(resp.Body)
|
||||
var res oaiResponse
|
||||
res := &oaiResponse{}
|
||||
|
||||
err = dec.Decode(&res)
|
||||
err = dec.Decode(res)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return err
|
||||
}
|
||||
|
||||
return res.Choices[0].Message.Content, nil
|
||||
err = json.Unmarshal([]byte(res.Choices[0].Message.Content), out)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -211,6 +211,7 @@ a {
|
||||
<a href="javascript:(async function() {
|
||||
const params = new URLSearchParams();
|
||||
params.set('long', location.href);
|
||||
params.set('title', document.title);
|
||||
|
||||
window.open(`https://{{ .writeHost }}/?${params.toString()}`);
|
||||
})();">{{ .readHost }}</a>
|
||||
|
||||
@@ -62,6 +62,8 @@ sl-icon[name="check-square-fill"] {
|
||||
/>
|
||||
<script type="module" src="https://cdn.jsdelivr.net/npm/@shoelace-style/shoelace@2.18.0/cdn/shoelace-autoloader.js"></script>
|
||||
<script>
|
||||
let title = '{{ .title }}';
|
||||
|
||||
function setInputIcon(val, icon) {
|
||||
if (val.length > 0) {
|
||||
icon.setAttribute('name', 'square');
|
||||
@@ -171,6 +173,10 @@ async function set(short, long) {
|
||||
}
|
||||
}
|
||||
|
||||
suggest(shorts, title);
|
||||
}
|
||||
|
||||
async function suggest(shorts, title) {
|
||||
try {
|
||||
resp = await fetch('./', {
|
||||
method: 'QUERY',
|
||||
@@ -179,6 +185,7 @@ async function set(short, long) {
|
||||
},
|
||||
body: JSON.stringify({
|
||||
shorts: shorts,
|
||||
title: title,
|
||||
}),
|
||||
});
|
||||
} catch (err) {
|
||||
@@ -186,7 +193,9 @@ async function set(short, long) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (const short of (await resp.json()).shorts) {
|
||||
const data = await resp.json();
|
||||
|
||||
for (const short of data.shorts) {
|
||||
appendShortItem(short, long);
|
||||
}
|
||||
}
|
||||
@@ -275,6 +284,7 @@ document.addEventListener('DOMContentLoaded', async () => {
|
||||
clearAlerts();
|
||||
setInputIcons();
|
||||
document.getElementById('tree').replaceChildren();
|
||||
title = '';
|
||||
|
||||
if (longPaste) {
|
||||
longPaste = false;
|
||||
@@ -310,6 +320,14 @@ document.addEventListener('DOMContentLoaded', async () => {
|
||||
}
|
||||
|
||||
setInputIcons();
|
||||
|
||||
const short = document.getElementById('short').value;
|
||||
|
||||
if (short != '') {
|
||||
suggest([short], title);
|
||||
} else if (title != '') {
|
||||
suggest([], title);
|
||||
}
|
||||
});
|
||||
</script>
|
||||
</head>
|
||||
|
||||
Reference in New Issue
Block a user