-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from thedadams/knowledge-ingest-tool
chore: use runs to ingest and delete knowledge
- Loading branch information
Showing
14 changed files
with
180 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package invoke | ||
|
||
import ( | ||
"context" | ||
|
||
v1 "github.com/gptscript-ai/otto/pkg/storage/apis/otto.gptscript.ai/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
func (i *Invoker) SystemAction(ctx context.Context, generateName, namespace, tool, input string, env ...string) (*Response, error) { | ||
thread := v1.Thread{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
GenerateName: generateName, | ||
Namespace: namespace, | ||
}, | ||
Spec: v1.ThreadSpec{ | ||
Input: input, | ||
}, | ||
} | ||
|
||
if err := i.storage.Create(ctx, &thread); err != nil { | ||
return nil, err | ||
} | ||
|
||
return i.createRunFromRemoteTool(ctx, &thread, tool, input, runOptions{ | ||
Env: env, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package knowledge | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/gptscript-ai/otto/pkg/invoke" | ||
"github.com/gptscript-ai/otto/pkg/workspace" | ||
) | ||
|
||
type Ingester struct { | ||
invoker *invoke.Invoker | ||
knowledgeTool string | ||
} | ||
|
||
func NewIngester(invoker *invoke.Invoker, knowledgeTool string) *Ingester { | ||
return &Ingester{ | ||
invoker: invoker, | ||
knowledgeTool: knowledgeTool, | ||
} | ||
} | ||
|
||
func (i *Ingester) IngestKnowledge(ctx context.Context, namespace, knowledgeWorkspaceID string) error { | ||
knowledgeTool, tag, ok := strings.Cut(i.knowledgeTool, "@") | ||
if ok { | ||
tag = "@" + tag | ||
} | ||
|
||
run, err := i.invoker.SystemAction(ctx, "ingest-", namespace, knowledgeTool+"/ingest.gpt"+tag, workspace.GetDir(knowledgeWorkspaceID), "GPTSCRIPT_DATASET="+workspace.KnowledgeIDFromWorkspaceID(knowledgeWorkspaceID)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
run.Wait() | ||
if run.Run.Status.Error != "" { | ||
return fmt.Errorf("failed to ingest knowledge: %s", run.Run.Status.Error) | ||
} | ||
return nil | ||
} | ||
|
||
func (i *Ingester) DeleteKnowledge(ctx context.Context, namespace, knowledgeWorkspaceID string) error { | ||
knowledgeTool, tag, ok := strings.Cut(i.knowledgeTool, "@") | ||
if ok { | ||
tag = "@" + tag | ||
} | ||
|
||
run, err := i.invoker.SystemAction(ctx, "ingest-delete-", namespace, knowledgeTool+"/delete.gpt"+tag, workspace.KnowledgeIDFromWorkspaceID(knowledgeWorkspaceID)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
run.Wait() | ||
if run.Run.Status.Error != "" { | ||
return fmt.Errorf("failed to delete knowledge: %s", run.Run.Status.Error) | ||
} | ||
return nil | ||
} |
Oops, something went wrong.