Use HyperNet.Insight to generate insights

This commit is contained in:
2025-01-30 01:43:05 +08:00
parent 7a6dfeb9e6
commit e90458c049
4 changed files with 164 additions and 27 deletions

View File

@ -0,0 +1,71 @@
package api
import (
"context"
"strconv"
"strings"
localCache "git.solsynth.dev/hypernet/interactive/pkg/internal/cache"
"git.solsynth.dev/hypernet/interactive/pkg/internal/database"
"git.solsynth.dev/hypernet/interactive/pkg/internal/models"
"git.solsynth.dev/hypernet/interactive/pkg/internal/services"
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
authm "git.solsynth.dev/hypernet/passport/pkg/authkit/models"
"github.com/eko/gocache/lib/v4/cache"
"github.com/eko/gocache/lib/v4/marshaler"
"github.com/gofiber/fiber/v2"
)
func getPostInsight(c *fiber.Ctx) error {
if err := sec.EnsureAuthenticated(c); err != nil {
return err
}
user := c.Locals("user").(authm.Account)
id := c.Params("postId")
var item models.Post
var err error
tx := services.FilterPostDraft(database.C)
if user, authenticated := c.Locals("user").(authm.Account); authenticated {
tx = services.FilterPostWithUserContext(tx, &user)
} else {
tx = services.FilterPostWithUserContext(tx, nil)
}
if numericId, paramErr := strconv.Atoi(id); paramErr == nil {
item, err = services.GetPost(tx, uint(numericId))
} else {
segments := strings.Split(id, ":")
if len(segments) != 2 {
return fiber.NewError(fiber.StatusBadRequest, "invalid post id, must be a number or a string with two segment divided by a colon")
}
area := segments[0]
alias := segments[1]
item, err = services.GetPostByAlias(tx, alias, area)
}
if err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error())
}
cacheManager := cache.New[any](localCache.S)
marshal := marshaler.New(cacheManager)
contx := context.Background()
var response string
if val, err := marshal.Get(contx, services.GetPostInsightCacheKey(item.ID), new(string)); err == nil {
response = *(val.(*string))
} else {
response, err = services.GeneratePostInsights(item, user.ID)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
}
return c.JSON(fiber.Map{
"response": response,
})
}

View File

@ -0,0 +1,48 @@
package services
import (
"context"
"fmt"
"strings"
"time"
iproto "git.solsynth.dev/hypernet/insight/pkg/proto"
"git.solsynth.dev/hypernet/interactive/pkg/internal/gap"
"git.solsynth.dev/hypernet/interactive/pkg/internal/models"
)
func GetPostInsightCacheKey(postId uint) string {
return fmt.Sprintf("post-insight-%d", postId)
}
func GeneratePostInsights(post models.Post, user uint) (string, error) {
var compactBuilder []string
if val, ok := post.Body["title"].(string); ok && len(val) > 0 {
compactBuilder = append(compactBuilder, "Title: "+val)
}
if val, ok := post.Body["description"].(string); ok && len(val) > 0 {
compactBuilder = append(compactBuilder, "Description: "+val)
}
if val, ok := post.Body["content"].(string); ok && len(val) > 0 {
compactBuilder = append(compactBuilder, val)
}
compact := strings.Join(compactBuilder, "\n")
conn, err := gap.Nx.GetClientGrpcConn("ai")
if err != nil {
return "", fmt.Errorf("failed to connect Insight: %v", err)
}
ic := iproto.NewInsightServiceClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute)
defer cancel()
resp, err := ic.GenerateInsight(ctx, &iproto.InsightRequest{
Source: compact,
UserId: uint64(user),
})
if err != nil {
return resp.Response, err
}
return resp.Response, nil
}