Feat: a bit of progress with r1

This commit is contained in:
Grail Finder
2025-03-10 23:38:33 +03:00
parent 53e9dff387
commit 4c994282b0
5 changed files with 77 additions and 45 deletions

1
.gitignore vendored
View File

@@ -7,3 +7,4 @@ history/
config.toml config.toml
sysprompts/* sysprompts/*
history_bak/ history_bak/
.aider*

41
bot.go
View File

@@ -16,6 +16,7 @@ import (
"net/http" "net/http"
"os" "os"
"path" "path"
"strconv"
"strings" "strings"
"time" "time"
@@ -121,24 +122,30 @@ func fetchDSBalance() *models.DSBalance {
func sendMsgToLLM(body io.Reader) { func sendMsgToLLM(body io.Reader) {
choseChunkParser() choseChunkParser()
bodyBytes, _ := io.ReadAll(body)
ok := json.Valid(bodyBytes)
if !ok {
panic("invalid json")
}
// nolint // nolint
req, err := http.NewRequest("POST", cfg.CurrentAPI, body) req, err := http.NewRequest("POST", cfg.CurrentAPI, bytes.NewReader(bodyBytes))
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", "Bearer "+cfg.DeepSeekToken)
// nolint
// resp, err := httpClient.Post(cfg.CurrentAPI, "application/json", body)
if err != nil { if err != nil {
logger.Error("llamacpp api", "error", err) logger.Error("newreq error", "error", err)
if err := notifyUser("error", "apicall failed:"+err.Error()); err != nil { if err := notifyUser("error", "apicall failed:"+err.Error()); err != nil {
logger.Error("failed to notify", "error", err) logger.Error("failed to notify", "error", err)
} }
streamDone <- true streamDone <- true
return return
} }
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", "Bearer "+cfg.DeepSeekToken)
req.Header.Set("Content-Length", strconv.Itoa(len(bodyBytes)))
req.Header.Set("Accept-Encoding", "gzip")
// nolint
// resp, err := httpClient.Post(cfg.CurrentAPI, "application/json", body)
resp, err := httpClient.Do(req) resp, err := httpClient.Do(req)
if err != nil { if err != nil {
bodyBytes, _ := io.ReadAll(body)
logger.Error("llamacpp api", "error", err, "body", string(bodyBytes)) logger.Error("llamacpp api", "error", err, "body", string(bodyBytes))
if err := notifyUser("error", "apicall failed:"+err.Error()); err != nil { if err := notifyUser("error", "apicall failed:"+err.Error()); err != nil {
logger.Error("failed to notify", "error", err) logger.Error("failed to notify", "error", err)
@@ -164,12 +171,13 @@ func sendMsgToLLM(body io.Reader) {
} }
line, err := reader.ReadBytes('\n') line, err := reader.ReadBytes('\n')
if err != nil { if err != nil {
logger.Error("error reading response body", "error", err, "line", string(line)) logger.Error("error reading response body", "error", err, "line", string(line),
if err.Error() != "EOF" { "reqbody", string(bodyBytes), "user_role", cfg.UserRole, "parser", chunkParser, "link", cfg.CurrentAPI)
streamDone <- true // if err.Error() != "EOF" {
break streamDone <- true
} break
continue // }
// continue
} }
if len(line) <= 1 { if len(line) <= 1 {
if interruptResp { if interruptResp {
@@ -192,8 +200,8 @@ func sendMsgToLLM(body io.Reader) {
break break
} }
// Handle error messages in response content // Handle error messages in response content
if content != "" && strings.Contains(strings.ToLower(content), "error") { if string(line) != "" && strings.Contains(strings.ToLower(string(line)), "error") {
logger.Error("API error response detected", "content", content, "url", cfg.CurrentAPI) logger.Error("API error response detected", "line", line, "url", cfg.CurrentAPI)
streamDone <- true streamDone <- true
break break
} }
@@ -274,6 +282,7 @@ func chatRound(userMsg, role string, tv *tview.TextView, regen, resume bool) {
return return
} }
} }
choseChunkParser()
reader, err := chunkParser.FormMsg(userMsg, role, resume) reader, err := chunkParser.FormMsg(userMsg, role, resume)
if reader == nil || err != nil { if reader == nil || err != nil {
logger.Error("empty reader from msgs", "role", role, "error", err) logger.Error("empty reader from msgs", "role", role, "error", err)

44
llm.go
View File

@@ -18,12 +18,20 @@ func choseChunkParser() {
switch cfg.CurrentAPI { switch cfg.CurrentAPI {
case "http://localhost:8080/completion": case "http://localhost:8080/completion":
chunkParser = LlamaCPPeer{} chunkParser = LlamaCPPeer{}
logger.Debug("chosen llamacppeer", "link", cfg.CurrentAPI)
return
case "http://localhost:8080/v1/chat/completions": case "http://localhost:8080/v1/chat/completions":
chunkParser = OpenAIer{} chunkParser = OpenAIer{}
logger.Debug("chosen openair", "link", cfg.CurrentAPI)
return
case "https://api.deepseek.com/beta/completions": case "https://api.deepseek.com/beta/completions":
chunkParser = DeepSeekerCompletion{} chunkParser = DeepSeekerCompletion{}
logger.Debug("chosen deepseekercompletio", "link", cfg.CurrentAPI)
return
case "https://api.deepseek.com/chat/completions": case "https://api.deepseek.com/chat/completions":
chunkParser = DeepSeekerChat{} chunkParser = DeepSeekerChat{}
logger.Debug("chosen deepseekerchat", "link", cfg.CurrentAPI)
return
default: default:
chunkParser = LlamaCPPeer{} chunkParser = LlamaCPPeer{}
} }
@@ -45,6 +53,7 @@ type DeepSeekerChat struct {
} }
func (lcp LlamaCPPeer) FormMsg(msg, role string, resume bool) (io.Reader, error) { func (lcp LlamaCPPeer) FormMsg(msg, role string, resume bool) (io.Reader, error) {
logger.Debug("formmsg llamacppeer", "link", cfg.CurrentAPI)
if msg != "" { // otherwise let the bot to continue if msg != "" { // otherwise let the bot to continue
newMsg := models.RoleMsg{Role: role, Content: msg} newMsg := models.RoleMsg{Role: role, Content: msg}
chatBody.Messages = append(chatBody.Messages, newMsg) chatBody.Messages = append(chatBody.Messages, newMsg)
@@ -124,6 +133,7 @@ func (op OpenAIer) ParseChunk(data []byte) (string, bool, error) {
} }
func (op OpenAIer) FormMsg(msg, role string, resume bool) (io.Reader, error) { func (op OpenAIer) FormMsg(msg, role string, resume bool) (io.Reader, error) {
logger.Debug("formmsg openaier", "link", cfg.CurrentAPI)
if cfg.ToolUse && !resume { if cfg.ToolUse && !resume {
// prompt += "\n" + cfg.ToolRole + ":\n" + toolSysMsg // prompt += "\n" + cfg.ToolRole + ":\n" + toolSysMsg
// add to chat body // add to chat body
@@ -168,6 +178,7 @@ func (ds DeepSeekerCompletion) ParseChunk(data []byte) (string, bool, error) {
} }
func (ds DeepSeekerCompletion) FormMsg(msg, role string, resume bool) (io.Reader, error) { func (ds DeepSeekerCompletion) FormMsg(msg, role string, resume bool) (io.Reader, error) {
logger.Debug("formmsg deepseekercompletion", "link", cfg.CurrentAPI)
if msg != "" { // otherwise let the bot to continue if msg != "" { // otherwise let the bot to continue
newMsg := models.RoleMsg{Role: role, Content: msg} newMsg := models.RoleMsg{Role: role, Content: msg}
chatBody.Messages = append(chatBody.Messages, newMsg) chatBody.Messages = append(chatBody.Messages, newMsg)
@@ -212,21 +223,25 @@ func (ds DeepSeekerCompletion) FormMsg(msg, role string, resume bool) (io.Reader
} }
func (ds DeepSeekerChat) ParseChunk(data []byte) (string, bool, error) { func (ds DeepSeekerChat) ParseChunk(data []byte) (string, bool, error) {
llmchunk := models.DSCompletionResp{} llmchunk := models.DSChatStreamResp{}
if err := json.Unmarshal(data, &llmchunk); err != nil { if err := json.Unmarshal(data, &llmchunk); err != nil {
logger.Error("failed to decode", "error", err, "line", string(data)) logger.Error("failed to decode", "error", err, "line", string(data))
return "", false, err return "", false, err
} }
if llmchunk.Choices[0].FinishReason != "" { if llmchunk.Choices[0].FinishReason != "" {
if llmchunk.Choices[0].Text != "" { if llmchunk.Choices[0].Delta.Content != "" {
logger.Error("text inside of finish llmchunk", "chunk", llmchunk) logger.Error("text inside of finish llmchunk", "chunk", llmchunk)
} }
return llmchunk.Choices[0].Text, true, nil return llmchunk.Choices[0].Delta.Content, true, nil
} }
return llmchunk.Choices[0].Text, false, nil if llmchunk.Choices[0].Delta.ReasoningContent != "" {
return llmchunk.Choices[0].Delta.ReasoningContent, false, nil
}
return llmchunk.Choices[0].Delta.Content, false, nil
} }
func (ds DeepSeekerChat) FormMsg(msg, role string, resume bool) (io.Reader, error) { func (ds DeepSeekerChat) FormMsg(msg, role string, resume bool) (io.Reader, error) {
logger.Debug("formmsg deepseekerchat", "link", cfg.CurrentAPI)
if cfg.ToolUse && !resume { if cfg.ToolUse && !resume {
// prompt += "\n" + cfg.ToolRole + ":\n" + toolSysMsg // prompt += "\n" + cfg.ToolRole + ":\n" + toolSysMsg
// add to chat body // add to chat body
@@ -247,17 +262,24 @@ func (ds DeepSeekerChat) FormMsg(msg, role string, resume bool) (io.Reader, erro
} }
} }
// Create copy of chat body with standardized user role // Create copy of chat body with standardized user role
modifiedBody := *chatBody // modifiedBody := *chatBody
modifiedBody.Messages = make([]models.RoleMsg, len(chatBody.Messages)) bodyCopy := &models.ChatBody{
Messages: make([]models.RoleMsg, len(chatBody.Messages)),
Model: chatBody.Model,
Stream: chatBody.Stream,
}
// modifiedBody.Messages = make([]models.RoleMsg, len(chatBody.Messages))
for i, msg := range chatBody.Messages { for i, msg := range chatBody.Messages {
if msg.Role == cfg.UserRole { logger.Debug("checking roles", "#", i, "role", msg.Role)
modifiedBody.Messages[i].Role = "user" if msg.Role == cfg.UserRole || i == 1 {
bodyCopy.Messages[i].Role = "user"
logger.Debug("replaced role in body", "#", i)
} else { } else {
modifiedBody.Messages[i] = msg bodyCopy.Messages[i] = msg
} }
} }
models.NewDSCharReq(&modifiedBody) dsBody := models.NewDSCharReq(*bodyCopy)
data, err := json.Marshal(chatBody) data, err := json.Marshal(dsBody)
if err != nil { if err != nil {
logger.Error("failed to form a msg", "error", err) logger.Error("failed to form a msg", "error", err)
return nil, err return nil, err

View File

@@ -123,7 +123,7 @@ type DSChatReq struct {
// TopLogprobs any `json:"top_logprobs"` // TopLogprobs any `json:"top_logprobs"`
} }
func NewDSCharReq(cb *ChatBody) DSChatReq { func NewDSCharReq(cb ChatBody) DSChatReq {
return DSChatReq{ return DSChatReq{
Messages: cb.Messages, Messages: cb.Messages,
Model: cb.Model, Model: cb.Model,
@@ -223,6 +223,23 @@ type DSChatResp struct {
} `json:"usage"` } `json:"usage"`
} }
type DSChatStreamResp struct {
ID string `json:"id"`
Object string `json:"object"`
Created int `json:"created"`
Model string `json:"model"`
SystemFingerprint string `json:"system_fingerprint"`
Choices []struct {
Index int `json:"index"`
Delta struct {
Content string `json:"content"`
ReasoningContent string `json:"reasoning_content"`
} `json:"delta"`
Logprobs any `json:"logprobs"`
FinishReason string `json:"finish_reason"`
} `json:"choices"`
}
type EmbeddingResp struct { type EmbeddingResp struct {
Embedding []float32 `json:"embedding"` Embedding []float32 `json:"embedding"`
Index uint32 `json:"index"` Index uint32 `json:"index"`

View File

@@ -24,39 +24,32 @@ func WriteToPng(metadata *models.CharCardSpec, sourcePath, outfile string) error
if err != nil { if err != nil {
return err return err
} }
jsonData, err := json.Marshal(metadata) jsonData, err := json.Marshal(metadata)
if err != nil { if err != nil {
return err return err
} }
base64Data := base64.StdEncoding.EncodeToString(jsonData) base64Data := base64.StdEncoding.EncodeToString(jsonData)
embedData := PngEmbed{ embedData := PngEmbed{
Key: "elefant", // Replace with appropriate key constant Key: "elefant", // Replace with appropriate key constant
Value: base64Data, Value: base64Data,
} }
var outputBuffer bytes.Buffer var outputBuffer bytes.Buffer
if _, err := outputBuffer.Write([]byte(pngHeader)); err != nil { if _, err := outputBuffer.Write([]byte(pngHeader)); err != nil {
return err return err
} }
chunks, iend, err := processChunks(pngData[8:]) chunks, iend, err := processChunks(pngData[8:])
if err != nil { if err != nil {
return err return err
} }
for _, chunk := range chunks { for _, chunk := range chunks {
outputBuffer.Write(chunk) outputBuffer.Write(chunk)
} }
newChunk, err := createTextChunk(embedData) newChunk, err := createTextChunk(embedData)
if err != nil { if err != nil {
return err return err
} }
outputBuffer.Write(newChunk) outputBuffer.Write(newChunk)
outputBuffer.Write(iend) outputBuffer.Write(iend)
return os.WriteFile(outfile, outputBuffer.Bytes(), 0666) return os.WriteFile(outfile, outputBuffer.Bytes(), 0666)
} }
@@ -67,7 +60,6 @@ func processChunks(data []byte) ([][]byte, []byte, error) {
iendChunk []byte iendChunk []byte
reader = bytes.NewReader(data) reader = bytes.NewReader(data)
) )
for { for {
var chunkLength uint32 var chunkLength uint32
if err := binary.Read(reader, binary.BigEndian, &chunkLength); err != nil { if err := binary.Read(reader, binary.BigEndian, &chunkLength); err != nil {
@@ -76,22 +68,18 @@ func processChunks(data []byte) ([][]byte, []byte, error) {
} }
return nil, nil, fmt.Errorf("error reading chunk length: %w", err) return nil, nil, fmt.Errorf("error reading chunk length: %w", err)
} }
chunkType := make([]byte, 4) chunkType := make([]byte, 4)
if _, err := reader.Read(chunkType); err != nil { if _, err := reader.Read(chunkType); err != nil {
return nil, nil, fmt.Errorf("error reading chunk type: %w", err) return nil, nil, fmt.Errorf("error reading chunk type: %w", err)
} }
chunkData := make([]byte, chunkLength) chunkData := make([]byte, chunkLength)
if _, err := reader.Read(chunkData); err != nil { if _, err := reader.Read(chunkData); err != nil {
return nil, nil, fmt.Errorf("error reading chunk data: %w", err) return nil, nil, fmt.Errorf("error reading chunk data: %w", err)
} }
crc := make([]byte, 4) crc := make([]byte, 4)
if _, err := reader.Read(crc); err != nil { if _, err := reader.Read(crc); err != nil {
return nil, nil, fmt.Errorf("error reading CRC: %w", err) return nil, nil, fmt.Errorf("error reading CRC: %w", err)
} }
fullChunk := bytes.NewBuffer(nil) fullChunk := bytes.NewBuffer(nil)
if err := binary.Write(fullChunk, binary.BigEndian, chunkLength); err != nil { if err := binary.Write(fullChunk, binary.BigEndian, chunkLength); err != nil {
return nil, nil, fmt.Errorf("error writing chunk length: %w", err) return nil, nil, fmt.Errorf("error writing chunk length: %w", err)
@@ -105,7 +93,6 @@ func processChunks(data []byte) ([][]byte, []byte, error) {
if _, err := fullChunk.Write(crc); err != nil { if _, err := fullChunk.Write(crc); err != nil {
return nil, nil, fmt.Errorf("error writing CRC: %w", err) return nil, nil, fmt.Errorf("error writing CRC: %w", err)
} }
switch string(chunkType) { switch string(chunkType) {
case "IEND": case "IEND":
iendChunk = fullChunk.Bytes() iendChunk = fullChunk.Bytes()
@@ -116,7 +103,6 @@ func processChunks(data []byte) ([][]byte, []byte, error) {
chunks = append(chunks, fullChunk.Bytes()) chunks = append(chunks, fullChunk.Bytes())
} }
} }
return nil, nil, errors.New("IEND chunk not found") return nil, nil, errors.New("IEND chunk not found")
} }
@@ -126,12 +112,10 @@ func createTextChunk(embed PngEmbed) ([]byte, error) {
content.WriteString(embed.Key) content.WriteString(embed.Key)
content.WriteByte(0) // Null separator content.WriteByte(0) // Null separator
content.WriteString(embed.Value) content.WriteString(embed.Value)
data := content.Bytes() data := content.Bytes()
crc := crc32.NewIEEE() crc := crc32.NewIEEE()
crc.Write([]byte(textChunkType)) crc.Write([]byte(textChunkType))
crc.Write(data) crc.Write(data)
chunk := bytes.NewBuffer(nil) chunk := bytes.NewBuffer(nil)
if err := binary.Write(chunk, binary.BigEndian, uint32(len(data))); err != nil { if err := binary.Write(chunk, binary.BigEndian, uint32(len(data))); err != nil {
return nil, fmt.Errorf("error writing chunk length: %w", err) return nil, fmt.Errorf("error writing chunk length: %w", err)
@@ -145,6 +129,5 @@ func createTextChunk(embed PngEmbed) ([]byte, error) {
if err := binary.Write(chunk, binary.BigEndian, crc.Sum32()); err != nil { if err := binary.Write(chunk, binary.BigEndian, crc.Sum32()); err != nil {
return nil, fmt.Errorf("error writing CRC: %w", err) return nil, fmt.Errorf("error writing CRC: %w", err)
} }
return chunk.Bytes(), nil return chunk.Bytes(), nil
} }