Skip to content

Commit

Permalink
feat: ability to delete chats
Browse files Browse the repository at this point in the history
  • Loading branch information
catpaladin committed Jul 12, 2024
1 parent 731f929 commit 05b9757
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 42 deletions.
25 changes: 25 additions & 0 deletions app/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package app

type ModelConfig struct {
Platform string
ModelID string

Temperature float64
TopP float64
TopK int
MaxTokens int

Region string // used for AWS models
}

func NewModelConfig(platform, modelID, region string, temp, topP float64, topK, maxTokens int) ModelConfig {
return ModelConfig{
Platform: platform,
ModelID: modelID,
Temperature: temp,
TopP: topP,
TopK: topK,
MaxTokens: maxTokens,
Region: region,
}
}
12 changes: 12 additions & 0 deletions app/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,18 @@ func createNewConversation() (int, error) {
return conversationID, nil
}

func deleteConversation(conversationID int) error {
// Delete associated messages first
_, err := db.Exec("DELETE FROM messages WHERE conversation_id = ?", conversationID)
if err != nil {
return err
}

// Delete the conversation
_, err = db.Exec("DELETE FROM conversations WHERE id = ?", conversationID)
return err
}

type Message struct {
ID int `json:"id"`
ConversationID int `json:"conversation_id"`
Expand Down
16 changes: 16 additions & 0 deletions app/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,19 @@ func getAllConversationsHandler(c *gin.Context) {
"Conversations": conversations,
})
}

func deleteChat(c *gin.Context) {
conversationID, err := strconv.Atoi(c.Param("id"))
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid conversation ID"})
return
}

err = deleteConversation(conversationID)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete conversation"})
return
}

c.JSON(http.StatusOK, gin.H{"status": "Conversation deleted"})
}
11 changes: 0 additions & 11 deletions app/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,17 +104,6 @@ func (mc *ModelConfig) runModel(ctx context.Context) gin.HandlerFunc {
})
}

// Fetch updated list of conversations
conversations, err := getAllConversations()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to retrieve conversations"})
return
}

c.HTML(http.StatusOK, "sidebar.html", gin.H{
"Conversations": conversations,
})

c.HTML(http.StatusOK, "chat.html", gin.H{
"ChatMessages": chatMessages,
"Platform": platform,
Expand Down
27 changes: 1 addition & 26 deletions app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,6 @@ import (
"github.com/gin-gonic/gin"
)

var completion string

type ModelConfig struct {
Platform string
ModelID string

Temperature float64
TopP float64
TopK int
MaxTokens int

Region string // used for AWS models
}

func NewModelConfig(platform, modelID, region string, temp, topP float64, topK, maxTokens int) ModelConfig {
return ModelConfig{
Platform: platform,
ModelID: modelID,
Temperature: temp,
TopP: topP,
TopK: topK,
MaxTokens: maxTokens,
Region: region,
}
}

func (mc *ModelConfig) Serve(ctx context.Context) {
router := gin.Default()
router.LoadHTMLGlob("templates/*")
Expand All @@ -50,6 +24,7 @@ func (mc *ModelConfig) Serve(ctx context.Context) {
router.POST("/select-model", selectModel)
router.POST("/run", mc.runModel(ctx))
router.POST("/new-conversation", createConversation)
router.DELETE("/conversation/:id/delete", deleteChat)

if err := router.Run(":31000"); err != nil {
log.Fatal(err)
Expand Down
2 changes: 1 addition & 1 deletion templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
hx-swap="innerHTML"
>
<div class="flex justify-between items-center text-white text-xl mb-4 space-x-4">
<span>{{.Message}}</span>
<span hx-get="/messages" hx-trigger="click">{{.Message}}</span>
<button
class="btn btn-square btn-outline right-4"
hx-post="/new-conversation"
Expand Down
29 changes: 25 additions & 4 deletions templates/sidebar.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,31 @@
{{define "sidebar.html"}}
<ul>
<h2 class="menu-title">Chats</h2>
<ul class="menu bg-base-200 rounded-box w-56">
{{range .Conversations}}
<li class="mb-2 text-gray-300 hover:text-white">
<a href="#" hx-get="/message/{{.ID}}/render" hx-target="#messages" hx-swap="innerHTML">
{{if gt (len .LatestHuman) 25}}{{slice .LatestHuman 0 25}}...{{else}}{{.LatestHuman}}{{end}}
<li class="flex flex-row justify-between items-center mb-2 text-gray-300 hover:text-white">
<a href="#" hx-get="/message/{{.ID}}/render" hx-target="#messages" hx-swap="innerHTML" class="flex-grow truncate">
{{if gt (len .LatestHuman) 16}}{{slice .LatestHuman 0 16}}...{{else}}{{.LatestHuman}}{{end}}
</a>
<button
class="btn btn-square btn-outline ml-2 flex-shrink-0"
hx-delete="/conversation/{{.ID}}/delete"
hx-target="closest li"
hx-swap="outerHTML"
style="width: 2rem; height: 2rem; min-width: unset; display: flex; justify-content: center; align-items: center;"
>
<svg
xmlns="http://www.w3.org/2000/svg"
class="h-4 w-4"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor">
<path
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
d="M6 18L18 6M6 6l12 12" />
</svg>
</button>
</li>
{{end}}
</ul>
Expand Down

0 comments on commit 05b9757

Please sign in to comment.