Skip to content

Commit

Permalink
Merge pull request #88 from gen-mind/develop
Browse files Browse the repository at this point in the history
develop
  • Loading branch information
apaladiychuk authored May 7, 2024
2 parents b936600 + d9b566e commit 8f285c3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
14 changes: 11 additions & 3 deletions backend/api/handler/persona.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package handler

import (
"cognix.ch/api/v2/core/ai"
"cognix.ch/api/v2/core/bll"
"cognix.ch/api/v2/core/parameters"
"cognix.ch/api/v2/core/security"
Expand All @@ -13,10 +14,11 @@ import (

type PersonaHandler struct {
personaBL bll.PersonaBL
aiBuilder *ai.Builder
}

func NewPersonaHandler(personaBL bll.PersonaBL) *PersonaHandler {
return &PersonaHandler{personaBL: personaBL}
func NewPersonaHandler(personaBL bll.PersonaBL, aiBuilder *ai.Builder) *PersonaHandler {
return &PersonaHandler{personaBL: personaBL, aiBuilder: aiBuilder}
}
func (h *PersonaHandler) Mount(route *gin.Engine, authMiddleware gin.HandlerFunc) {
handler := route.Group("/api/manage/personas")
Expand Down Expand Up @@ -112,13 +114,16 @@ func (h *PersonaHandler) Update(c *gin.Context, identity *security.Identity) err
return utils.ErrorBadRequest.New("id should be presented")
}
var param parameters.PersonaParam
if err := server.BindJsonAndValidate(c, &param); err != nil {
if err = server.BindJsonAndValidate(c, &param); err != nil {
return err
}
persona, err := h.personaBL.Update(c.Request.Context(), id, identity.User, &param)
if err != nil {
return err
}
if persona.LLM != nil {
h.aiBuilder.Invalidate(persona.LLM)
}
return server.JsonResult(c, http.StatusOK, persona)
}

Expand Down Expand Up @@ -146,5 +151,8 @@ func (h *PersonaHandler) Archive(c *gin.Context, identity *security.Identity) er
if err != nil {
return err
}
if persona.LLM != nil {
h.aiBuilder.Invalidate(persona.LLM)
}
return server.JsonResult(c, http.StatusOK, persona)
}
13 changes: 12 additions & 1 deletion backend/core/ai/builder.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
package ai

import "cognix.ch/api/v2/core/model"
import (
"cognix.ch/api/v2/core/model"
"sync"
)

type Builder struct {
clients map[int64]OpenAIClient
mx sync.Mutex
}

func NewBuilder() *Builder {
return &Builder{clients: make(map[int64]OpenAIClient)}
}

func (b *Builder) New(llm *model.LLM) OpenAIClient {
b.mx.Lock()
defer b.mx.Unlock()
if client, ok := b.clients[llm.ID.IntPart()]; ok {
return client
}
client := NewOpenAIClient(llm)
b.clients[llm.ID.IntPart()] = client
return client
}
func (b *Builder) Invalidate(llm *model.LLM) {
b.mx.Lock()
delete(b.clients, llm.ID.IntPart())
b.mx.Unlock()
}

0 comments on commit 8f285c3

Please sign in to comment.