thread2md/thread/thread.go

96 lines
2.4 KiB
Go

package thread
import (
"encoding/json"
"fmt"
"git.wntrmute.dev/kyle/goutils/die"
"os"
"path/filepath"
"strings"
"time"
)
type Thread struct {
ID string `json:"id"`
Object string `json:"object"`
Title string `json:"title"`
Assistants []struct {
AssistantID string `json:"assistant_id"`
AssistantName string `json:"assistant_name"`
Tools []struct {
Type string `json:"type"`
Enabled bool `json:"enabled"`
Settings struct {
TopK int `json:"top_k"`
ChunkSize int `json:"chunk_size"`
ChunkOverlap int `json:"chunk_overlap"`
RetrievalTemplate string `json:"retrieval_template"`
} `json:"settings"`
} `json:"tools"`
Model struct {
ID string `json:"id"`
Settings struct {
CtxLen int `json:"ctx_len"`
PromptTemplate string `json:"prompt_template"`
LlamaModelPath string `json:"llama_model_path"`
} `json:"settings"`
Parameters struct {
Temperature float64 `json:"temperature"`
TopP float64 `json:"top_p"`
Stream bool `json:"stream"`
MaxTokens int `json:"max_tokens"`
FrequencyPenalty int `json:"frequency_penalty"`
PresencePenalty int `json:"presence_penalty"`
} `json:"parameters"`
Engine string `json:"engine"`
} `json:"model"`
Instructions string `json:"instructions"`
} `json:"assistants"`
Created int64 `json:"created"`
Updated int64 `json:"updated"`
Metadata struct {
LastMessage string `json:"lastMessage"`
} `json:"metadata"`
Messages []*Message
}
func LoadThread(path string) (*Thread, error) {
realPath := filepath.Join(path, "thread.json")
file, err := os.Open(realPath)
die.If(err)
defer file.Close()
jsonParser := json.NewDecoder(file)
var thread Thread
err = jsonParser.Decode(&thread)
die.If(err)
thread.Messages, err = LoadMessages(path)
die.If(err)
return &thread, nil
}
func (t *Thread) Date() time.Time {
return time.Unix(int64(t.Created/1000), 0)
}
func (t *Thread) Header() string {
return fmt.Sprintf("# %s (%s)\n## %s\n", t.Title,
t.Assistants[0].Model.ID, t.Date().Format(timeFormat))
}
func (t *Thread) Body() string {
messages := []string{}
for _, m := range t.Messages {
messages = append(messages, m.String())
}
return strings.Join(messages, "\n")
}
func (t *Thread) String() string {
return t.Header() + "\n" + t.Body()
}