-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
restructure gen/semdex features according to #314 #316
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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)), | ||
), | ||
) | ||
} |
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 | ||
} | ||
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 | ||
} | ||
Comment on lines
+33
to
+47
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Consider returning ErrDisabled instead of nil The methods Consider adding an error variable and using it consistently: +var ErrSemdexDisabled = errors.New("semantic indexing is disabled")
func (*Disabled) Recommend(ctx context.Context, object datagraph.Item) (datagraph.ItemList, error) {
- return nil, nil
+ return nil, ErrSemdexDisabled
}
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Add context timeout and error context enrichment
The implementation looks solid but could benefit from additional error handling and context management.
Consider these improvements:
📝 Committable suggestion