Skip to content
This repository has been archived by the owner on Oct 30, 2024. It is now read-only.

Commit

Permalink
add: get and list dataset cmds
Browse files Browse the repository at this point in the history
  • Loading branch information
iwilltry42 committed May 3, 2024
1 parent 22b5c39 commit b571fb4
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 6 deletions.
44 changes: 44 additions & 0 deletions pkg/cmd/get_dataset.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package cmd

import (
"encoding/json"
"fmt"
"github.com/spf13/cobra"
)

type ClientGetDataset struct {
Client
}

func (s *ClientGetDataset) Customize(cmd *cobra.Command) {
cmd.Use = "get-dataset <dataset-id>"
cmd.Short = "Get a dataset"
cmd.Args = cobra.ExactArgs(1)
}

func (s *ClientGetDataset) Run(cmd *cobra.Command, args []string) error {
c, err := s.getClient()
if err != nil {
return err
}

datasetID := args[0]

ds, err := c.GetDataset(cmd.Context(), datasetID)
if err != nil {
return fmt.Errorf("failed to get dataset: %w", err)
}

if ds == nil {
fmt.Println("dataset not found")
return fmt.Errorf("dataset not found")
}

jsonOutput, err := json.Marshal(ds)
if err != nil {
return fmt.Errorf("failed to marshal dataset: %w", err)
}

fmt.Println(string(jsonOutput))
return nil
}
42 changes: 42 additions & 0 deletions pkg/cmd/list_datasets.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package cmd

import (
"encoding/json"
"fmt"
"github.com/spf13/cobra"
)

type ClientListDatasets struct {
Client
}

func (s *ClientListDatasets) Customize(cmd *cobra.Command) {
cmd.Use = "list-datasets"
cmd.Short = "List existing datasets"
cmd.Args = cobra.NoArgs
}

func (s *ClientListDatasets) Run(cmd *cobra.Command, args []string) error {
c, err := s.getClient()
if err != nil {
return err
}

ds, err := c.ListDatasets(cmd.Context())
if err != nil {
return fmt.Errorf("failed to list datasets: %w", err)
}

if len(ds) == 0 {
fmt.Println("no datasets found")
return nil
}

jsonOutput, err := json.Marshal(ds)
if err != nil {
return fmt.Errorf("failed to marshal datasets: %w", err)
}

fmt.Println(string(jsonOutput))
return nil
}
11 changes: 10 additions & 1 deletion pkg/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,16 @@ import (
)

func New() *cobra.Command {
return cmd.Command(&Knowledge{}, new(Server), new(ClientCreateDataset), new(ClientIngest), new(ClientDeleteDataset), new(ClientRetrieve))
return cmd.Command(
&Knowledge{},
new(Server),
new(ClientCreateDataset),
new(ClientGetDataset),
new(ClientListDatasets),
new(ClientIngest),
new(ClientDeleteDataset),
new(ClientRetrieve),
)
}

type Knowledge struct{}
Expand Down
5 changes: 3 additions & 2 deletions pkg/datastore/dataset.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package datastore
import (
"context"
"errors"
"fmt"
"github.com/acorn-io/z"
"github.com/gptscript-ai/knowledge/pkg/index"
"github.com/gptscript-ai/knowledge/pkg/types"
Expand Down Expand Up @@ -50,13 +51,13 @@ func (s *Datastore) DeleteDataset(ctx context.Context, datasetID string) error {

func (s *Datastore) GetDataset(ctx context.Context, datasetID string) (*index.Dataset, error) {
// Get dataset with files and associated documents preloaded
var dataset *index.Dataset
dataset := &index.Dataset{}
tx := s.Index.WithContext(ctx).Preload("Files.Documents").First(dataset, "id = ?", datasetID)
if tx.Error != nil {
if errors.Is(tx.Error, gorm.ErrRecordNotFound) {
return nil, nil
}
return nil, tx.Error
return nil, fmt.Errorf("failed to get dataset %q from DB: %w", datasetID, tx.Error)
}

return dataset, nil
Expand Down
3 changes: 0 additions & 3 deletions pkg/datastore/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ import (
"log/slog"
)

// ErrDBDocumentNotFound is returned when a document is not found in the database.
var ErrDBDocumentNotFound = fmt.Errorf("document not found in database")

func (s *Datastore) DeleteDocument(ctx context.Context, documentID, datasetID string) error {
// Find in Database
var document index.Document
Expand Down
8 changes: 8 additions & 0 deletions pkg/datastore/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package datastore

import (
"errors"
)

// ErrDBDocumentNotFound is returned when a document is not found in the database.
var ErrDBDocumentNotFound = errors.New("document not found in database")

0 comments on commit b571fb4

Please sign in to comment.