🎉 Initial Commit
Some checks failed
release-nightly / build-docker (push) Has been cancelled
Some checks failed
release-nightly / build-docker (push) Has been cancelled
This commit is contained in:
24
pkg/internal/services/cleaner.go
Normal file
24
pkg/internal/services/cleaner.go
Normal file
@ -0,0 +1,24 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
database2 "git.solsynth.dev/hypernet/insight/pkg/internal/database"
|
||||
"time"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func DoAutoDatabaseCleanup() {
|
||||
deadline := time.Now().Add(60 * time.Minute)
|
||||
log.Debug().Time("deadline", deadline).Msg("Now cleaning up entire database...")
|
||||
|
||||
var count int64
|
||||
for _, model := range database2.AutoMaintainRange {
|
||||
tx := database2.C.Unscoped().Delete(model, "deleted_at >= ?", deadline)
|
||||
if tx.Error != nil {
|
||||
log.Error().Err(tx.Error).Msg("An error occurred when running auth context cleanup...")
|
||||
}
|
||||
count += tx.RowsAffected
|
||||
}
|
||||
|
||||
log.Debug().Int64("affected", count).Msg("Clean up entire database accomplished.")
|
||||
}
|
59
pkg/internal/services/ollama.go
Normal file
59
pkg/internal/services/ollama.go
Normal file
@ -0,0 +1,59 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
"github.com/tmc/langchaingo/llms"
|
||||
"github.com/tmc/langchaingo/llms/ollama"
|
||||
"github.com/tmc/langchaingo/prompts"
|
||||
)
|
||||
|
||||
func PingOllama() error {
|
||||
host := viper.GetString("ollama.url")
|
||||
resp, err := http.Get(host + "/api/version")
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to ping ollama: %v", err)
|
||||
}
|
||||
if resp.StatusCode != 200 {
|
||||
return fmt.Errorf("ollama returned status code %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
var LargeModel *ollama.LLM
|
||||
|
||||
func ConnectOllama() error {
|
||||
model := viper.GetString("ollama.model")
|
||||
llm, err := ollama.New(ollama.WithModel(model))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
LargeModel = llm
|
||||
return nil
|
||||
}
|
||||
|
||||
func GenerateInsights(source string) (string, error) {
|
||||
prompt := prompts.NewPromptTemplate(
|
||||
"Summerize this post on Solar Network below: {{.content}}",
|
||||
[]string{"content"},
|
||||
)
|
||||
inPrompt, err := prompt.Format(map[string]any{
|
||||
"content": source,
|
||||
})
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to format prompt: %v", err)
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute)
|
||||
defer cancel()
|
||||
completion, err := LargeModel.Call(ctx, inPrompt,
|
||||
llms.WithTemperature(0.8),
|
||||
)
|
||||
|
||||
return completion, err
|
||||
}
|
69
pkg/internal/services/payment.go
Normal file
69
pkg/internal/services/payment.go
Normal file
@ -0,0 +1,69 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"git.solsynth.dev/hypernet/insight/pkg/internal/gap"
|
||||
wproto "git.solsynth.dev/hypernet/wallet/pkg/proto"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
|
||||
// PlaceOrder create a transaction if needed for user
|
||||
// Pricing is 128 words input cost 10 source points, round up.
|
||||
func PlaceOrder(user uint, inputLength int) error {
|
||||
amount := float64(inputLength+128-1) / 128
|
||||
|
||||
conn, err := gap.Nx.GetClientGrpcConn("wa")
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to connect wallet: %v", err)
|
||||
}
|
||||
|
||||
wc := wproto.NewPaymentServiceClient(conn)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
|
||||
defer cancel()
|
||||
resp, err := wc.MakeTransactionWithAccount(ctx, &wproto.MakeTransactionWithAccountRequest{
|
||||
PayerAccountId: lo.ToPtr(uint64(user)),
|
||||
Amount: amount,
|
||||
Remark: "Insight Thinking Fee",
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Info().
|
||||
Uint64("transaction", resp.Id).Float64("amount", amount).
|
||||
Msg("Order placed for charge insight thinking fee...")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// MakeRefund to user who got error in generating insight
|
||||
func MakeRefund(user uint, inputLength int) error {
|
||||
amount := float64(inputLength+128-1) / 128
|
||||
|
||||
conn, err := gap.Nx.GetClientGrpcConn("wa")
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to connect wallet: %v", err)
|
||||
}
|
||||
|
||||
wc := wproto.NewPaymentServiceClient(conn)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
|
||||
defer cancel()
|
||||
resp, err := wc.MakeTransactionWithAccount(ctx, &wproto.MakeTransactionWithAccountRequest{
|
||||
PayeeAccountId: lo.ToPtr(uint64(user)),
|
||||
Amount: amount,
|
||||
Remark: "Insight Thinking Failed - Refund",
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Info().
|
||||
Uint64("transaction", resp.Id).Float64("amount", amount).
|
||||
Msg("Refund placed for insight thinking fee...")
|
||||
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user