Skip to content

Commit

Permalink
Merge pull request #92 from basenana/chat_with_dir
Browse files Browse the repository at this point in the history
update: add chat api for friday client
  • Loading branch information
hyponet authored Mar 20, 2024
2 parents b6cf82a + 54a6657 commit ce330dd
Show file tree
Hide file tree
Showing 25 changed files with 757 additions and 290 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/aws/aws-sdk-go-v2/credentials v1.13.26
github.com/aws/aws-sdk-go-v2/service/s3 v1.36.0
github.com/aws/smithy-go v1.13.5
github.com/basenana/friday v0.0.0-20231219152848-7dd1f5bcfab3
github.com/basenana/friday v0.0.0-20240320144407-3c9cf9811d4b
github.com/blevesearch/bleve/v2 v2.3.10
github.com/bluele/gcache v0.0.2
github.com/getsentry/sentry-go v0.22.0
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ github.com/basenana/friday v0.0.0-20231216063336-c2c9735897d8 h1:jGuzKXoNJ4lIHe/
github.com/basenana/friday v0.0.0-20231216063336-c2c9735897d8/go.mod h1:OGCCDNAXqN/ZTqO8EsaGGUBt5GrVwb/BVdnDQA4KbZI=
github.com/basenana/friday v0.0.0-20231219152848-7dd1f5bcfab3 h1:uqBwvEBOt7kMgHuVsmik7gy52mNIqcLhuXOfU06TSHs=
github.com/basenana/friday v0.0.0-20231219152848-7dd1f5bcfab3/go.mod h1:7YHYOD5pGjvIVbif/JO02dwX02orbJRhE0vfKZdKEv8=
github.com/basenana/friday v0.0.0-20240319101022-a34e28c6e7b0 h1:60Zke2oTtbyeoAjByP3t9UEZ+w4sbFZOImJ1X+kTUOY=
github.com/basenana/friday v0.0.0-20240319101022-a34e28c6e7b0/go.mod h1:izPqWGUN5Kxz6mb7Xwhz3Zoe5GdOa/O+Ht6VhIHmjaA=
github.com/basenana/friday v0.0.0-20240320144407-3c9cf9811d4b h1:XexlIy3TglO1Ig/obLdAa2RPkCi4Kxem5sgNm0TAzmU=
github.com/basenana/friday v0.0.0-20240320144407-3c9cf9811d4b/go.mod h1:izPqWGUN5Kxz6mb7Xwhz3Zoe5GdOa/O+Ht6VhIHmjaA=
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
Expand Down
41 changes: 33 additions & 8 deletions pkg/friday/friday.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,18 @@ func IngestFile(ctx context.Context, entryId, parentId int64, fileName, content
ParentId: parentId,
Content: content,
}
return fridayClient.IngestFromFile(ctx, file)
result := friday.IngestState{}
f := fridayClient.WithContext(ctx).File(&file).Ingest(&result)
return result.Tokens, f.Error
}

func Question(ctx context.Context, q string) (answer string, usage map[string]int, err error) {
if fridayClient == nil {
return "", nil, fmt.Errorf("fridayClient is nil, can not use it")
}
return fridayClient.Question(ctx, 0, q)
result := friday.ChatState{}
f := fridayClient.WithContext(ctx).Question(q).Complete(&result)
return result.Answer, result.Tokens, f.Error
}

func SummaryFile(ctx context.Context, fileName, content string) (string, map[string]int, error) {
Expand All @@ -82,19 +86,40 @@ func SummaryFile(ctx context.Context, fileName, content string) (string, map[str
Name: fileName,
Content: content,
}
result, usage, err := fridayClient.SummaryFromFile(ctx, file, summary.MapReduce)
if err != nil {
return "", nil, err
result := friday.SummaryState{}
f := fridayClient.WithContext(ctx).File(&file).OfType(summary.MapReduce).Summary(&result)
if f.Error != nil {
return "", nil, f.Error
}
if result == nil || result[fileName] == "" {
if result.Summary == nil || result.Summary[fileName] == "" {
return "", nil, fmt.Errorf("fail to summary file %s", fileName)
}
return result[fileName], usage, nil
return result.Summary[fileName], result.Tokens, nil
}

func Keywords(ctx context.Context, content string) ([]string, map[string]int, error) {
if fridayClient == nil {
return nil, nil, fmt.Errorf("fridayClient is nil, can not use it")
}
return fridayClient.Keywords(ctx, content)
result := friday.KeywordsState{}
f := fridayClient.WithContext(ctx).Content(content).Keywords(&result)
return result.Keywords, result.Tokens, f.Error
}

func ChatInDir(ctx context.Context, dirId int64, history []map[string]string, response chan map[string]string) error {
if fridayClient == nil {
return fmt.Errorf("fridayClient is nil, can not use it")
}
result := friday.ChatState{Response: response}
f := fridayClient.WithContext(ctx).History(history).SearchIn(&models.DocQuery{ParentId: dirId}).Chat(&result)
return f.Error
}

func ChatInEntry(ctx context.Context, entryId int64, history []map[string]string, response chan map[string]string) error {
if fridayClient == nil {
return fmt.Errorf("fridayClient is nil, can not use it")
}
result := friday.ChatState{Response: response}
f := fridayClient.WithContext(ctx).History(history).SearchIn(&models.DocQuery{Oid: entryId}).Chat(&result)
return f.Error
}
9 changes: 6 additions & 3 deletions vendor/github.com/basenana/friday/config/config.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 64 additions & 1 deletion vendor/github.com/basenana/friday/pkg/friday/friday.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ce330dd

Please sign in to comment.