Skip to content

Commit

Permalink
Merge branch 'main' into exportloopref-linter
Browse files Browse the repository at this point in the history
  • Loading branch information
Hoega committed Jan 15, 2024
2 parents 77b4a9b + 60e5c2c commit a18953b
Show file tree
Hide file tree
Showing 23 changed files with 463 additions and 846 deletions.
10 changes: 5 additions & 5 deletions apiserver/pkg/chat/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ import (
"github.com/kubeagi/arcadia/api/base/v1alpha1"
"github.com/kubeagi/arcadia/apiserver/pkg/auth"
"github.com/kubeagi/arcadia/apiserver/pkg/client"
"github.com/kubeagi/arcadia/pkg/application"
"github.com/kubeagi/arcadia/pkg/application/base"
"github.com/kubeagi/arcadia/pkg/application/retriever"
"github.com/kubeagi/arcadia/pkg/appruntime"
"github.com/kubeagi/arcadia/pkg/appruntime/base"
"github.com/kubeagi/arcadia/pkg/appruntime/retriever"
)

var (
Expand Down Expand Up @@ -99,12 +99,12 @@ func AppRun(ctx context.Context, req ChatReqBody, respStream chan string) (*Chat
Answer: "",
})
ctx = base.SetAppNamespace(ctx, req.AppNamespace)
appRun, err := application.NewAppOrGetFromCache(ctx, app, c)
appRun, err := appruntime.NewAppOrGetFromCache(ctx, c, app)
if err != nil {
return nil, err
}
klog.FromContext(ctx).Info("begin to run application", "appName", req.APPName, "appNamespace", req.AppNamespace)
out, err := appRun.Run(ctx, c, respStream, application.Input{Question: req.Query, NeedStream: req.ResponseMode.IsStreaming(), History: conversation.History})
out, err := appRun.Run(ctx, c, respStream, appruntime.Input{Question: req.Query, NeedStream: req.ResponseMode.IsStreaming(), History: conversation.History})
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion apiserver/pkg/chat/chat_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (

"github.com/tmc/langchaingo/memory"

"github.com/kubeagi/arcadia/pkg/application/retriever"
"github.com/kubeagi/arcadia/pkg/appruntime/retriever"
)

type ResponseMode string
Expand Down
6 changes: 6 additions & 0 deletions apiserver/pkg/common/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
apiprompt "github.com/kubeagi/arcadia/api/app-node/prompt/v1alpha1"
apiretriever "github.com/kubeagi/arcadia/api/app-node/retriever/v1alpha1"
"github.com/kubeagi/arcadia/api/base/v1alpha1"
evalv1alpha1 "github.com/kubeagi/arcadia/api/evaluation/v1alpha1"
)

var (
Expand Down Expand Up @@ -102,6 +103,11 @@ var (
Version: v1alpha1.GroupVersion.Version,
Resource: "embedders",
},
"rag": {
Group: evalv1alpha1.GroupVersion.Group,
Version: evalv1alpha1.GroupVersion.Version,
Resource: "rags",
},
}
)

Expand Down
18 changes: 3 additions & 15 deletions cmd/arctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ import (
"path/filepath"

"github.com/spf13/cobra"
"k8s.io/client-go/dynamic"

"github.com/kubeagi/arcadia/apiserver/pkg/client"
arctlPkg "github.com/kubeagi/arcadia/pkg/arctl"
)

Expand All @@ -32,37 +30,27 @@ var (
home string

namespace string

kubeClient dynamic.Interface
)

func NewCLI() *cobra.Command {
arctl := &cobra.Command{
Use: "arctl [usage]",
Short: "Command line tools for Arcadia",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if _, err := os.Stat(home); os.IsNotExist(err) {
if _, err = os.Stat(home); os.IsNotExist(err) {
if err := os.MkdirAll(home, 0700); err != nil {
return err
}
}

// initialize a kube client
kubeClient, err = client.GetClient(nil)
if err != nil {
return err
}

return nil
},
}

arctl.PersistentFlags().StringVar(&home, "home", filepath.Join(os.Getenv("HOME"), ".arcadia"), "home directory to use")
arctl.PersistentFlags().StringVarP(&namespace, "namespace", "n", "default", "namespace to use")

arctl.AddCommand(arctlPkg.NewDatasourceCmd(kubeClient, namespace))
arctl.AddCommand(arctlPkg.NewDatasetCmd(home))
arctl.AddCommand(arctlPkg.NewChatCmd(home))
arctl.AddCommand(arctlPkg.NewDatasourceCmd(&namespace))
arctl.AddCommand(arctlPkg.NewEvalCmd(&home, &namespace))

return arctl
}
Expand Down
6 changes: 3 additions & 3 deletions config/samples/arcadia_v1alpha1_worker_bge-large-zh-v1.5.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
\apiVersion: arcadia.kubeagi.k8s.com.cn/v1alpha1
apiVersion: arcadia.kubeagi.k8s.com.cn/v1alpha1
kind: Worker
metadata:
name: bge-large-zh
namespace: arcadia
namespace: kubeagi-system
spec:
displayName: BGE模型服务
description: "这是一个Embedding模型服务,由BGE提供"
type: "fastchat"
replicas: 1
model:
kind: "Models"
name: "bge-large-zh-v1.5"
name: "bge-large-zh-v1.5"
25 changes: 17 additions & 8 deletions pkg/application/app_run.go → pkg/appruntime/app_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package application
package appruntime

import (
"container/list"
Expand All @@ -28,12 +28,12 @@ import (
"k8s.io/utils/strings/slices"

arcadiav1alpha1 "github.com/kubeagi/arcadia/api/base/v1alpha1"
"github.com/kubeagi/arcadia/pkg/application/base"
"github.com/kubeagi/arcadia/pkg/application/chain"
"github.com/kubeagi/arcadia/pkg/application/knowledgebase"
"github.com/kubeagi/arcadia/pkg/application/llm"
"github.com/kubeagi/arcadia/pkg/application/prompt"
"github.com/kubeagi/arcadia/pkg/application/retriever"
"github.com/kubeagi/arcadia/pkg/appruntime/base"
"github.com/kubeagi/arcadia/pkg/appruntime/chain"
"github.com/kubeagi/arcadia/pkg/appruntime/knowledgebase"
"github.com/kubeagi/arcadia/pkg/appruntime/llm"
"github.com/kubeagi/arcadia/pkg/appruntime/prompt"
"github.com/kubeagi/arcadia/pkg/appruntime/retriever"
)

type Input struct {
Expand Down Expand Up @@ -62,10 +62,14 @@ type Application struct {
// return app.Namespace + "/" + app.Name
//}

func NewAppOrGetFromCache(ctx context.Context, app *arcadiav1alpha1.Application, cli dynamic.Interface) (*Application, error) {
func NewAppOrGetFromCache(ctx context.Context, cli dynamic.Interface, app *arcadiav1alpha1.Application) (*Application, error) {
if app == nil || app.Name == "" || app.Namespace == "" {
return nil, errors.New("app has no name or namespace")
}
// make sure namespace value exists in context
if base.GetAppNamespace(ctx) == "" {
ctx = base.SetAppNamespace(ctx, app.Namespace)
}
// TODO: disable cache for now.
// https://github.com/kubeagi/arcadia/issues/391
// a, ok := cache[cacheKey(app)]
Expand Down Expand Up @@ -150,6 +154,11 @@ func (a *Application) Init(ctx context.Context, cli dynamic.Interface) (err erro
}

func (a *Application) Run(ctx context.Context, cli dynamic.Interface, respStream chan string, input Input) (output Output, err error) {
// make sure ns value set
if base.GetAppNamespace(ctx) == "" {
ctx = base.SetAppNamespace(ctx, a.Namespace)
}

out := map[string]any{
"question": input.Question,
"_answer_stream": respStream,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ const (
)

func GetAppNamespace(ctx context.Context) string {
return ctx.Value(AppNamespaceContextKey).(string)
ns := ctx.Value(AppNamespaceContextKey)
if ns == nil {
return ""
}
return ns.(string)
}

func SetAppNamespace(ctx context.Context, ns string) context.Context {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
"k8s.io/klog/v2"

"github.com/kubeagi/arcadia/api/app-node/chain/v1alpha1"
"github.com/kubeagi/arcadia/pkg/application/base"
"github.com/kubeagi/arcadia/pkg/appruntime/base"
)

type LLMChain struct {
Expand Down Expand Up @@ -64,13 +64,14 @@ func (l *LLMChain) Run(ctx context.Context, cli dynamic.Interface, args map[stri
if !ok {
return args, errors.New("prompt not prompts.FormatPrompter")
}
v3, ok := args["_history"]
if !ok {
return args, errors.New("no history")
}
history, ok := v3.(langchaingoschema.ChatMessageHistory)
if !ok {
return args, errors.New("history not memory.ChatMessageHistory")
// _history is optional
// if set ,only ChatMessageHistory allowed
var history langchaingoschema.ChatMessageHistory
if v3, ok := args["_history"]; ok && v3 != nil {
history, ok = v3.(langchaingoschema.ChatMessageHistory)
if !ok {
return args, errors.New("history not memory.ChatMessageHistory")
}
}

ns := base.GetAppNamespace(ctx)
Expand All @@ -87,7 +88,9 @@ func (l *LLMChain) Run(ctx context.Context, cli dynamic.Interface, args map[stri
options := getChainOptions(instance.Spec.CommonChainConfig)

chain := chains.NewLLMChain(llm, prompt)
chain.Memory = getMemory(llm, instance.Spec.Memory, history)
if history != nil {
chain.Memory = getMemory(llm, instance.Spec.Memory, history)
}
l.LLMChain = *chain
var out string
if needStream, ok := args["_need_stream"].(bool); ok && needStream {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ import (
"k8s.io/klog/v2"

"github.com/kubeagi/arcadia/api/app-node/chain/v1alpha1"
"github.com/kubeagi/arcadia/pkg/application/base"
appretriever "github.com/kubeagi/arcadia/pkg/application/retriever"
"github.com/kubeagi/arcadia/pkg/appruntime/base"
appretriever "github.com/kubeagi/arcadia/pkg/appruntime/retriever"
)

type RetrievalQAChain struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (

"k8s.io/client-go/dynamic"

"github.com/kubeagi/arcadia/pkg/application/base"
"github.com/kubeagi/arcadia/pkg/appruntime/base"
)

type Knowledgebase struct {
Expand Down
2 changes: 1 addition & 1 deletion pkg/application/llm/llm.go → pkg/appruntime/llm/llm.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"k8s.io/client-go/dynamic"

"github.com/kubeagi/arcadia/api/base/v1alpha1"
"github.com/kubeagi/arcadia/pkg/application/base"
"github.com/kubeagi/arcadia/pkg/appruntime/base"
"github.com/kubeagi/arcadia/pkg/langchainwrap"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"k8s.io/client-go/dynamic"

"github.com/kubeagi/arcadia/api/app-node/prompt/v1alpha1"
"github.com/kubeagi/arcadia/pkg/application/base"
"github.com/kubeagi/arcadia/pkg/appruntime/base"
)

type Prompt struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package retriever

import (
"context"
"encoding/json"
"fmt"
"strconv"
"strings"
Expand All @@ -34,7 +35,7 @@ import (

apiretriever "github.com/kubeagi/arcadia/api/app-node/retriever/v1alpha1"
"github.com/kubeagi/arcadia/api/base/v1alpha1"
"github.com/kubeagi/arcadia/pkg/application/base"
"github.com/kubeagi/arcadia/pkg/appruntime/base"
"github.com/kubeagi/arcadia/pkg/langchainwrap"
pkgvectorstore "github.com/kubeagi/arcadia/pkg/vectorstore"
)
Expand All @@ -52,6 +53,14 @@ type Reference struct {
LineNumber int `json:"line_number" example:"7"`
}

func (reference Reference) String() string {
bytes, err := json.Marshal(&reference)
if err != nil {
return ""
}
return string(bytes)
}

type KnowledgeBaseRetriever struct {
langchaingoschema.Retriever
base.BaseNode
Expand Down
Loading

0 comments on commit a18953b

Please sign in to comment.