-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat Implement index retriever (#130)
* feat: implement index retriever * docs: update README * docs: fix * fix interfaces * fix retriever
- Loading branch information
Showing
4 changed files
with
91 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package retriever | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/henomis/lingoose/document" | ||
"github.com/henomis/lingoose/index" | ||
indexoption "github.com/henomis/lingoose/index/option" | ||
) | ||
|
||
type Index interface { | ||
LoadFromDocuments(context.Context, []document.Document) error | ||
Query(context.Context, string, ...indexoption.Option) (index.SearchResults, error) | ||
} | ||
|
||
const ( | ||
defautTopK = 3 | ||
) | ||
|
||
type Retriever struct { | ||
index Index | ||
topK int | ||
documents *[]document.Document | ||
areDocumentsLoaded bool | ||
} | ||
|
||
func New(index Index, documents *[]document.Document) *Retriever { | ||
return &Retriever{ | ||
index: index, | ||
topK: defautTopK, | ||
documents: documents, | ||
} | ||
} | ||
|
||
func (r *Retriever) WithTopK(topK int) *Retriever { | ||
r.topK = topK | ||
return r | ||
} | ||
|
||
func (r *Retriever) Query(ctx context.Context, query string) ([]document.Document, error) { | ||
|
||
if r.documents == nil { | ||
return nil, fmt.Errorf("documents are not defined") | ||
} | ||
|
||
if !r.areDocumentsLoaded { | ||
err := r.index.LoadFromDocuments(context.Background(), *r.documents) | ||
if err != nil { | ||
return nil, err | ||
} | ||
r.areDocumentsLoaded = true | ||
} | ||
|
||
results, err := r.index.Query(context.Background(), query, indexoption.WithTopK(r.topK)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return results.ToDocuments(), 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