Skip to content

Commit

Permalink
fix: use system client to get config
Browse files Browse the repository at this point in the history
Signed-off-by: Abirdcfly <fp544037857@gmail.com>
  • Loading branch information
Abirdcfly committed Apr 7, 2024
1 parent f259628 commit b671987
Show file tree
Hide file tree
Showing 14 changed files with 143 additions and 33 deletions.
4 changes: 2 additions & 2 deletions apiserver/pkg/application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"reflect"
"strings"

"github.com/kubeagi/arcadia/pkg/systemconfig"
"github.com/minio/minio-go/v7"
"k8s.io/apimachinery/pkg/api/equality"
apierrors "k8s.io/apimachinery/pkg/api/errors"
Expand All @@ -42,7 +43,6 @@ import (
"github.com/kubeagi/arcadia/apiserver/graph/generated"
"github.com/kubeagi/arcadia/apiserver/pkg/common"
"github.com/kubeagi/arcadia/apiserver/pkg/utils"
"github.com/kubeagi/arcadia/pkg/config"
"github.com/kubeagi/arcadia/pkg/datasource"
pkgutils "github.com/kubeagi/arcadia/pkg/utils"
)
Expand Down Expand Up @@ -987,7 +987,7 @@ func UploadIcon(ctx context.Context, client client.Client, icon, appName, namesp
return "", err
}

system, err := config.GetSystemDatasource(ctx, client)
system, err := systemconfig.GetSystemDatasourceWithSystemClient(ctx)
if err != nil {
return "", err
}
Expand Down
4 changes: 2 additions & 2 deletions apiserver/pkg/chat/chat_docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"time"

"github.com/jackc/pgx/v5/pgconn"
"github.com/kubeagi/arcadia/pkg/systemconfig"
"github.com/minio/minio-go/v7"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/uuid"
Expand All @@ -39,7 +40,6 @@ import (
"github.com/kubeagi/arcadia/apiserver/pkg/chat/storage"
pkgclient "github.com/kubeagi/arcadia/apiserver/pkg/client"
"github.com/kubeagi/arcadia/apiserver/pkg/common"
"github.com/kubeagi/arcadia/pkg/config"
)

// ReceiveConversationDocs receive and process docs for a conversation
Expand Down Expand Up @@ -206,7 +206,7 @@ func (cs *ChatServer) BuildConversationKnowledgeBase(ctx context.Context, req Co
return err
}
// systemDatasource which stores the document
systemDatasource, err := config.GetSystemDatasource(ctx, cs.cli)
systemDatasource, err := systemconfig.GetSystemDatasourceWithSystemClient(ctx)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions apiserver/pkg/chat/chat_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"sync"
"time"

"github.com/kubeagi/arcadia/pkg/systemconfig"
"github.com/tmc/langchaingo/chains"
langchainllms "github.com/tmc/langchaingo/llms"
"github.com/tmc/langchaingo/memory"
Expand All @@ -47,7 +48,6 @@ import (
"github.com/kubeagi/arcadia/pkg/appruntime/knowledgebase"
"github.com/kubeagi/arcadia/pkg/appruntime/llm"
"github.com/kubeagi/arcadia/pkg/appruntime/retriever"
pkgconfig "github.com/kubeagi/arcadia/pkg/config"
"github.com/kubeagi/arcadia/pkg/datasource"
"github.com/kubeagi/arcadia/pkg/documentloaders"
)
Expand All @@ -68,7 +68,7 @@ func (cs *ChatServer) Storage() storage.Storage {
if cs.storage == nil {
cs.once.Do(func() {
ctx := context.TODO()
ds, err := pkgconfig.GetRelationalDatasource(ctx, cs.cli)
ds, err := systemconfig.GetRelationalDatasourceWithSystemClient(ctx)
if err != nil || ds == nil {
if err != nil {
klog.Infof("get relational datasource failed: %s, use memory storage for chat", err.Error())
Expand Down
9 changes: 5 additions & 4 deletions apiserver/pkg/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"strings"
"time"

"github.com/kubeagi/arcadia/pkg/systemconfig"

Check failure on line 27 in apiserver/pkg/common/common.go

View workflow job for this annotation

GitHub Actions / Lint Go code

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/kubeagi/arcadia) -s blank -s dot --custom-order (gci)
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
Expand Down Expand Up @@ -62,7 +63,7 @@ var (
)

func SystemDatasourceOSS(ctx context.Context, mgrClient client.Client) (*datasource.OSS, error) {
systemDatasource, err := config.GetSystemDatasource(ctx, mgrClient)
systemDatasource, err := systemconfig.GetSystemDatasourceWithSystemClient(ctx)
if err != nil {
return nil, err
}
Expand All @@ -77,7 +78,7 @@ func SystemDatasourceOSS(ctx context.Context, mgrClient client.Client) (*datasou
// Embedder and vectorstore are both required when generating a new embedding.That's why we call it a `EmbeddingSuit`
func SystemEmbeddingSuite(ctx context.Context, cli client.Client) (*v1alpha1.Embedder, *v1alpha1.VectorStore, error) {
// get the built-in system embedder
emd, err := config.GetEmbedder(ctx, cli)
emd, err := systemconfig.GetEmbedderWithSystemClient(ctx)
if err != nil {
return nil, nil, err
}
Expand All @@ -86,7 +87,7 @@ func SystemEmbeddingSuite(ctx context.Context, cli client.Client) (*v1alpha1.Emb
return nil, nil, err
}
// get the built-in system vectorstore
vs, err := config.GetVectorStore(ctx, cli)
vs, err := systemconfig.GetVectorStoreWithSystemClient(ctx)
if err != nil {
return nil, nil, err
}
Expand All @@ -100,7 +101,7 @@ func SystemEmbeddingSuite(ctx context.Context, cli client.Client) (*v1alpha1.Emb
// GetAPIServer returns the api server url to access arcadia's worker
// if external is true,then this func will return the external api server
func GetAPIServer(ctx context.Context, cli client.Client, external bool) (string, error) {
gateway, err := config.GetGateway(ctx, cli)
gateway, err := systemconfig.GetGatewayWithSystemClient(ctx)
if err != nil {
return "", err
}
Expand Down
4 changes: 2 additions & 2 deletions apiserver/pkg/knowledgebase/knowledgebase.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"
"time"

"github.com/kubeagi/arcadia/pkg/systemconfig"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/utils/pointer"
Expand All @@ -31,7 +32,6 @@ import (
"github.com/kubeagi/arcadia/apiserver/graph/generated"
"github.com/kubeagi/arcadia/apiserver/pkg/common"
graphqlutils "github.com/kubeagi/arcadia/apiserver/pkg/utils"
"github.com/kubeagi/arcadia/pkg/config"
)

func knowledgebase2modelConverter(ctx context.Context, c client.Client) func(obj client.Object) (generated.PageNode, error) {
Expand Down Expand Up @@ -179,7 +179,7 @@ func knowledgebase2model(ctx context.Context, c client.Client, knowledgebase *v1
func CreateKnowledgeBase(ctx context.Context, c client.Client, input generated.CreateKnowledgeBaseInput) (*generated.KnowledgeBase, error) {
var filegroups []v1alpha1.FileGroup
var vectorstore v1alpha1.TypedObjectReference
vector, _ := config.GetVectorStore(ctx, c)
vector, _ := systemconfig.GetVectorStoreWithSystemClient(ctx)
displayname, description, embedder := "", "", ""
if input.DisplayName != nil {
displayname = *input.DisplayName
Expand Down
4 changes: 2 additions & 2 deletions apiserver/pkg/ray/raycluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ import (
"context"
"strings"

"github.com/kubeagi/arcadia/pkg/systemconfig"

Check failure on line 23 in apiserver/pkg/ray/raycluster.go

View workflow job for this annotation

GitHub Actions / Lint Go code

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/kubeagi/arcadia) -s blank -s dot --custom-order (gci)
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/kubeagi/arcadia/apiserver/graph/generated"

Check failure on line 26 in apiserver/pkg/ray/raycluster.go

View workflow job for this annotation

GitHub Actions / Lint Go code

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/kubeagi/arcadia) -s blank -s dot --custom-order (gci)
"github.com/kubeagi/arcadia/pkg/config"
)

func ListRayClusters(ctx context.Context, c client.Client, input generated.ListCommonInput) (*generated.PaginatedResult, error) {
clusters, err := config.GetRayClusters(ctx, c)
clusters, err := systemconfig.GetRayClustersWithSystemClient(ctx)
if err != nil {
return nil, err
}
Expand Down
5 changes: 3 additions & 2 deletions controllers/base/knowledgebase_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,8 @@ func (r *KnowledgeBaseReconciler) reconcileFileGroup(
kb *arcadiav1alpha1.KnowledgeBase,
vectorStore *arcadiav1alpha1.VectorStore,
embedder *arcadiav1alpha1.Embedder,
groupIndex, fileIndex int) (err error) {
groupIndex, fileIndex int,
) (err error) {
defer func() {
if err != nil {
err = fmt.Errorf("failed to reconcile FileGroup: %w", err)
Expand Down Expand Up @@ -552,7 +553,7 @@ func (r *KnowledgeBaseReconciler) handleFile(ctx context.Context, log logr.Logge
return errVectorStoreNotReady
}
embeddingOptions := kb.EmbeddingOptions()
em, err := langchainwrap.GetLangchainEmbedder(ctx, embedder, r.Client, "", embeddings.WithBatchSize(embeddingOptions.BatchSize))
em, err := langchainwrap.GetLangchainEmbedder(ctx, embedder, r.Client, r.Client, "", embeddings.WithBatchSize(embeddingOptions.BatchSize))
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/appruntime/documentloader/documentloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"path/filepath"
"strings"

"github.com/kubeagi/arcadia/pkg/systemconfig"

Check failure on line 27 in pkg/appruntime/documentloader/documentloader.go

View workflow job for this annotation

GitHub Actions / Lint Go code

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/kubeagi/arcadia) -s blank -s dot --custom-order (gci)
"github.com/tmc/langchaingo/documentloaders"
"github.com/tmc/langchaingo/schema"
"github.com/tmc/langchaingo/textsplitter"
Expand All @@ -35,7 +36,6 @@ import (
"github.com/kubeagi/arcadia/api/app-node/documentloader/v1alpha1"
arcadiav1alpha1 "github.com/kubeagi/arcadia/api/base/v1alpha1"
"github.com/kubeagi/arcadia/pkg/appruntime/base"
"github.com/kubeagi/arcadia/pkg/config"
"github.com/kubeagi/arcadia/pkg/datasource"
arcadiadocumentloaders "github.com/kubeagi/arcadia/pkg/documentloaders"

Check failure on line 40 in pkg/appruntime/documentloader/documentloader.go

View workflow job for this annotation

GitHub Actions / Lint Go code

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/kubeagi/arcadia) -s blank -s dot --custom-order (gci)
)
Expand Down Expand Up @@ -75,7 +75,7 @@ func (dl *DocumentLoader) Run(ctx context.Context, cli client.Client, args map[s
if err := cli.Get(ctx, types.NamespacedName{Namespace: dl.RefNamespace(), Name: dl.Ref.Name}, dl.Instance); err != nil {
return args, fmt.Errorf("can't find the documentloader in cluster: %w", err)
}
system, err := config.GetSystemDatasource(ctx, cli)
system, err := systemconfig.GetSystemDatasourceWithSystemClient(ctx)
if err != nil {
return nil, err
}
Expand Down
7 changes: 6 additions & 1 deletion pkg/appruntime/llm/llm.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"fmt"

pkgclient "github.com/kubeagi/arcadia/apiserver/pkg/client"

Check failure on line 23 in pkg/appruntime/llm/llm.go

View workflow job for this annotation

GitHub Actions / Lint Go code

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/kubeagi/arcadia) -s blank -s dot --custom-order (gci)
langchainllms "github.com/tmc/langchaingo/llms"
"k8s.io/apimachinery/pkg/types"
"k8s.io/klog/v2"
Expand Down Expand Up @@ -47,7 +48,11 @@ func (z *LLM) Init(ctx context.Context, cli client.Client, _ map[string]any) err
if err := cli.Get(ctx, types.NamespacedName{Namespace: z.RefNamespace(), Name: z.Ref.Name}, instance); err != nil {
return fmt.Errorf("can't find the llm in cluster: %w", err)
}
llm, err := langchainwrap.GetLangchainLLM(ctx, instance, cli, "")
systemCli, err := pkgclient.GetClient(nil)
if err != nil {
return fmt.Errorf("can't get system client: %w", err)
}
llm, err := langchainwrap.GetLangchainLLM(ctx, instance, cli, systemCli, "")
if err != nil {
return fmt.Errorf("can't convert to langchain llm: %w", err)
}
Expand Down
7 changes: 6 additions & 1 deletion pkg/appruntime/retriever/knowledgebaseretriever.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"errors"
"fmt"

pkgclient "github.com/kubeagi/arcadia/apiserver/pkg/client"

Check failure on line 24 in pkg/appruntime/retriever/knowledgebaseretriever.go

View workflow job for this annotation

GitHub Actions / Lint Go code

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/kubeagi/arcadia) -s blank -s dot --custom-order (gci)
langchaingoschema "github.com/tmc/langchaingo/schema"
"github.com/tmc/langchaingo/vectorstores"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -115,7 +116,11 @@ func GenerateKnowledgebaseRetriever(ctx context.Context, cli client.Client, know
if err := cli.Get(ctx, types.NamespacedName{Namespace: embedderReq.GetNamespace(knowledgebaseNamespace), Name: embedderReq.Name}, embedder); err != nil {
return nil, nil, fmt.Errorf("can't find the embedder in cluster: %w", err)
}
em, err := langchainwrap.GetLangchainEmbedder(ctx, embedder, cli, "")
systemCli, err := pkgclient.GetClient(nil)
if err != nil {
return nil, nil, fmt.Errorf("can't get system client: %w", err)
}
em, err := langchainwrap.GetLangchainEmbedder(ctx, embedder, cli, systemCli, "")
if err != nil {
return nil, nil, fmt.Errorf("can't convert to langchain embedder: %w", err)
}
Expand Down
18 changes: 9 additions & 9 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,23 @@ func getDatasource(ctx context.Context, ref arcadiav1alpha1.TypedObjectReference
}

func GetSystemDatasource(ctx context.Context, c client.Client) (*arcadiav1alpha1.Datasource, error) {
config, err := GetConfig(ctx, c)
config, err := getConfig(ctx, c)
if err != nil {
return nil, err
}
return getDatasource(ctx, config.SystemDatasource, c)
}

func GetRelationalDatasource(ctx context.Context, c client.Client) (*arcadiav1alpha1.Datasource, error) {
config, err := GetConfig(ctx, c)
config, err := getConfig(ctx, c)
if err != nil {
return nil, err
}
return getDatasource(ctx, config.RelationalDatasource, c)
}

func GetGateway(ctx context.Context, c client.Client) (*Gateway, error) {
config, err := GetConfig(ctx, c)
config, err := getConfig(ctx, c)
if err != nil {
return nil, err
}
Expand All @@ -84,7 +84,7 @@ func GetGateway(ctx context.Context, c client.Client) (*Gateway, error) {
return config.Gateway, nil
}

func GetConfig(ctx context.Context, c client.Client) (config *Config, err error) {
func getConfig(ctx context.Context, c client.Client) (config *Config, err error) {
cmName := env.GetString(EnvConfigKey, EnvConfigDefaultValue)
if cmName == "" {
return nil, ErrNoConfigEnv
Expand All @@ -106,7 +106,7 @@ func GetConfig(ctx context.Context, c client.Client) (config *Config, err error)

// GetEmbedder get the default embedder from config
func GetEmbedder(ctx context.Context, c client.Client) (*arcadiav1alpha1.TypedObjectReference, error) {
config, err := GetConfig(ctx, c)
config, err := getConfig(ctx, c)
if err != nil {
return nil, err
}
Expand All @@ -118,7 +118,7 @@ func GetEmbedder(ctx context.Context, c client.Client) (*arcadiav1alpha1.TypedOb

// GetVectorStore get the default vector store from config
func GetVectorStore(ctx context.Context, c client.Client) (*arcadiav1alpha1.TypedObjectReference, error) {
config, err := GetConfig(ctx, c)
config, err := getConfig(ctx, c)
if err != nil {
return nil, err
}
Expand All @@ -130,7 +130,7 @@ func GetVectorStore(ctx context.Context, c client.Client) (*arcadiav1alpha1.Type

// Get the configuration of streamlit tool
func GetStreamlit(ctx context.Context, c client.Client) (*Streamlit, error) {
config, err := GetConfig(ctx, c)
config, err := getConfig(ctx, c)
if err != nil {
return nil, err
}
Expand All @@ -142,7 +142,7 @@ func GetStreamlit(ctx context.Context, c client.Client) (*Streamlit, error) {

// Get the ray cluster that can be used a resource pool
func GetRayClusters(ctx context.Context, c client.Client) ([]RayCluster, error) {
config, err := GetConfig(ctx, c)
config, err := getConfig(ctx, c)
if err != nil {
return nil, err
}
Expand All @@ -154,7 +154,7 @@ func GetRayClusters(ctx context.Context, c client.Client) ([]RayCluster, error)

// GetDefaultRerankModel gets the default reranking model which is recommended by kubeagi
func GetDefaultRerankModel(ctx context.Context, c client.Client) (*arcadiav1alpha1.TypedObjectReference, error) {
config, err := GetConfig(ctx, c)
config, err := getConfig(ctx, c)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/langchainwrap/embedder.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (
"github.com/kubeagi/arcadia/pkg/llms/zhipuai"
)

func GetLangchainEmbedder(ctx context.Context, e *v1alpha1.Embedder, c client.Client, model string, opts ...langchaingoembeddings.Option) (em langchaingoembeddings.Embedder, err error) {
func GetLangchainEmbedder(ctx context.Context, e *v1alpha1.Embedder, c client.Client, systemCli client.Client, model string, opts ...langchaingoembeddings.Option) (em langchaingoembeddings.Embedder, err error) {
switch e.Spec.Provider.GetType() {
case v1alpha1.ProviderType3rdParty:
switch e.Spec.Type { // nolint: gocritic
Expand Down Expand Up @@ -90,7 +90,7 @@ func GetLangchainEmbedder(ctx context.Context, e *v1alpha1.Embedder, c client.Cl
return langchaingoembeddings.NewEmbedder(llm, opts...)
}
case v1alpha1.ProviderTypeWorker:
gateway, err := config.GetGateway(ctx, c)
gateway, err := config.GetGateway(ctx, systemCli)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/langchainwrap/llm.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const (
GatewayUseExternalURLEnv = "GATEWAY_USE_EXTERNAL_URL"
)

func GetLangchainLLM(ctx context.Context, llm *v1alpha1.LLM, c client.Client, model string) (langchainllms.Model, error) {
func GetLangchainLLM(ctx context.Context, llm *v1alpha1.LLM, c client.Client, systemCli client.Client, model string) (langchainllms.Model, error) {
switch llm.Spec.Provider.GetType() {
case v1alpha1.ProviderType3rdParty:
apiKey, err := llm.AuthAPIKey(ctx, c)
Expand Down Expand Up @@ -78,7 +78,7 @@ func GetLangchainLLM(ctx context.Context, llm *v1alpha1.LLM, c client.Client, mo
return googleLLM, nil
}
case v1alpha1.ProviderTypeWorker:
gateway, err := config.GetGateway(ctx, c)
gateway, err := config.GetGateway(ctx, systemCli)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit b671987

Please sign in to comment.