-
Notifications
You must be signed in to change notification settings - Fork 11
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 #316 from Southclaws/restructure-ai-packages
restructure gen/semdex features according to #314
- Loading branch information
Showing
44 changed files
with
742 additions
and
729 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
7 changes: 2 additions & 5 deletions
7
...es/semdex/semdexer/refhydrate/hydrator.go → app/resources/datagraph/hydrate/hydrator.go
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,42 @@ | ||
package generative | ||
|
||
import ( | ||
"context" | ||
|
||
"go.uber.org/fx" | ||
|
||
"github.com/Southclaws/storyden/app/resources/datagraph" | ||
"github.com/Southclaws/storyden/app/resources/tag/tag_ref" | ||
"github.com/Southclaws/storyden/internal/infrastructure/ai" | ||
) | ||
|
||
type Tagger interface { | ||
SuggestTags(ctx context.Context, content datagraph.Content, available tag_ref.Names) (tag_ref.Names, error) | ||
} | ||
|
||
type Summariser interface { | ||
Summarise(ctx context.Context, object datagraph.Item) (string, error) | ||
} | ||
|
||
var ( | ||
_ Tagger = &generator{} | ||
_ Summariser = &generator{} | ||
) | ||
|
||
type generator struct { | ||
prompter ai.Prompter | ||
} | ||
|
||
func newGenerator(prompter ai.Prompter) *generator { | ||
return &generator{prompter: prompter} | ||
} | ||
|
||
func Build() fx.Option { | ||
return fx.Provide( | ||
fx.Annotate( | ||
newGenerator, | ||
fx.As(new(Tagger)), | ||
fx.As(new(Summariser)), | ||
), | ||
) | ||
} |
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,48 @@ | ||
package generative | ||
|
||
import ( | ||
"context" | ||
"html/template" | ||
"strings" | ||
|
||
"github.com/Southclaws/fault" | ||
"github.com/Southclaws/fault/fctx" | ||
|
||
"github.com/Southclaws/storyden/app/resources/datagraph" | ||
) | ||
|
||
var SummarisePrompt = template.Must(template.New("").Parse(` | ||
Write a short few paragraphs that are somewhat engaging but remaining relatively neutral in tone in the style of a wikipedia introduction about "{{ .Name }}". Focus on providing unique insights and interesting details while keeping the tone conversational and approachable. Imagine this will be read by someone browsing a directory or knowledgebase. | ||
Be aware that the input to this may include broken HTML and other artifacts from the web and due to the nature of web scraping, there may be parts that do not make sense. | ||
- Ignore any HTML tags, malformed content, or text that does not contribute meaningfully to the main topic. | ||
- Based on the clear and coherent sections of the input, write short but engaging paragraphs. If the input lacks meaningful context, produce a neutral placeholder. | ||
- If the input content is too fragmented or lacks sufficient context to produce a coherent response, produce a neutral placeholder. | ||
- Do not describe the appearance of the input (e.g., broken HTML or artifacts). Instead, infer the main idea or purpose and expand on it creatively. | ||
- If key parts of the content are missing or ambiguous, use creativity to fill gaps while maintaining relevance to the topic. | ||
Output Format: Provide the output as a correctly formatted HTML document, you are free to use basic HTML formatting tags for emphasis, lists and headings. However, do not include the content title as a <h1> tag at the top. Start with a paragraph block immediately. | ||
Content: | ||
{{ .Content }} | ||
`)) | ||
|
||
func (g *generator) Summarise(ctx context.Context, object datagraph.Item) (string, error) { | ||
template := strings.Builder{} | ||
err := SummarisePrompt.Execute(&template, map[string]any{ | ||
"Name": object.GetName(), | ||
"Content": object.GetContent().Plaintext(), | ||
}) | ||
if err != nil { | ||
return "", fault.Wrap(err, fctx.With(ctx)) | ||
} | ||
|
||
result, err := g.prompter.Prompt(ctx, template.String()) | ||
if err != nil { | ||
return "", fault.Wrap(err, fctx.With(ctx)) | ||
} | ||
|
||
return result.Answer, nil | ||
} |
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,47 @@ | ||
package semdex | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/rs/xid" | ||
|
||
"github.com/Southclaws/storyden/app/resources/datagraph" | ||
"github.com/Southclaws/storyden/app/resources/pagination" | ||
"github.com/Southclaws/storyden/app/services/search/searcher" | ||
) | ||
|
||
type Disabled struct{} | ||
|
||
var _ Semdexer = &Disabled{} | ||
|
||
func (*Disabled) Index(ctx context.Context, object datagraph.Item) error { | ||
return nil | ||
} | ||
|
||
func (*Disabled) Delete(ctx context.Context, object xid.ID) error { | ||
return nil | ||
} | ||
|
||
func (*Disabled) Search(ctx context.Context, q string, p pagination.Parameters, opts searcher.Options) (*pagination.Result[datagraph.Item], error) { | ||
panic("semdex disabled: searcher switch bug") | ||
} | ||
|
||
func (*Disabled) SearchRefs(ctx context.Context, q string, p pagination.Parameters, opts searcher.Options) (*pagination.Result[*datagraph.Ref], error) { | ||
panic("semdex disabled: searcher switch bug") | ||
} | ||
|
||
func (*Disabled) Recommend(ctx context.Context, object datagraph.Item) (datagraph.ItemList, error) { | ||
return nil, nil | ||
} | ||
|
||
func (*Disabled) RecommendRefs(ctx context.Context, object datagraph.Item) (datagraph.RefList, error) { | ||
return nil, nil | ||
} | ||
|
||
func (*Disabled) ScoreRelevance(ctx context.Context, object datagraph.Item, idx ...xid.ID) (map[xid.ID]float64, error) { | ||
return nil, nil | ||
} | ||
|
||
func (*Disabled) GetMany(ctx context.Context, limit uint, ids ...xid.ID) (datagraph.RefList, error) { | ||
return nil, nil | ||
} |
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
Oops, something went wrong.