Skip to content
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

Experiment Reports and Level 1 Assertions @ Runtime #300

Merged
merged 7 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion app/pkg/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"strings"
"sync"

"github.com/jlewi/foyle/app/pkg/runme/ulid"

"github.com/jlewi/foyle/protos/go/foyle/v1alpha1/v1alpha1connect"

"google.golang.org/protobuf/encoding/protojson"
Expand Down Expand Up @@ -114,6 +116,8 @@ func (a *Agent) Generate(ctx context.Context, req *v1alpha1.GenerateRequest) (*v
return nil, err
}

log.Info(logs.Level1Assertion, "assertion", logs.BuildAssertion(v1alpha1.Assertion_AT_LEAST_ONE_BLOCK_POST_PROCESSED, len(postProcessed) > 0))

// Attach block ids to any blocks generated.
// N.B. This is kind of a last resort to make sure all blocks have an ID set. In general, we want to set blockIds
// earlier in the processing pipeline so that any log messages involving blocks has block ids set. BlockIDs
Expand Down Expand Up @@ -148,8 +152,9 @@ func (a *Agent) completeWithRetries(ctx context.Context, req *v1alpha1.GenerateR
})
}
for try := 0; try < maxTries; try++ {
docText := t.Text()
args := promptArgs{
Document: t.Text(),
Document: docText,
Examples: exampleArgs,
}

Expand All @@ -172,6 +177,29 @@ func (a *Agent) completeWithRetries(ctx context.Context, req *v1alpha1.GenerateR
return nil, errors.Wrapf(err, "CreateChatCompletion failed")
}

// Level1 assertion that docText is a non-empty string
assertion := &v1alpha1.Assertion{
Name: v1alpha1.Assertion_NON_EMPTY_DOC,
Result: v1alpha1.AssertResult_PASSED,
Id: ulid.GenerateID(),
}

if len(strings.TrimSpace(docText)) == 0 {
assertion.Result = v1alpha1.AssertResult_FAILED
}

log.Info(logs.Level1Assertion, "assertion", assertion)

assertBlocks := &v1alpha1.Assertion{
Name: v1alpha1.Assertion_AT_LEAST_ONE_BLOCK,
Result: v1alpha1.AssertResult_PASSED,
Id: ulid.GenerateID(),
}

if len(blocks) == 0 {
assertBlocks.Result = v1alpha1.AssertResult_FAILED
}
log.Info(logs.Level1Assertion, "assertion", assertion)
return blocks, nil
}
err := errors.Errorf("Failed to generate a chat completion after %d tries", maxTries)
Expand Down
12 changes: 11 additions & 1 deletion app/pkg/analyze/fsql/eval_query.sql
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,14 @@ WHERE id = ?;
SELECT * FROM results
WHERE (:cursor = '' OR time < :cursor)
ORDER BY time DESC
LIMIT :page_size;
LIMIT :page_size;

-- name: CountResults :one
-- Count the total number of results
SELECT COUNT(*) FROM results;

-- name: CountErrors :one
SELECT COUNT(*) FROM results WHERE json_extract(proto_json, '$.error') IS NOT NULL;

-- name: CountByCellsMatchResult :many
SELECT json_extract(proto_json, '$.cellsMatchResult') as match_result, COUNT(*) as count FROM results GROUP BY match_result;
55 changes: 55 additions & 0 deletions app/pkg/analyze/fsql/eval_query.sql.go

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

6 changes: 2 additions & 4 deletions app/pkg/eval/assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"github.com/jlewi/foyle/protos/go/foyle/v1alpha1"
)

// TODO(jeremy): A lot of this code is probably obsolete now that we are using protos.

// Assertion is an interface for evaluating AI generations.
type Assertion interface {
Assert(ctx context.Context, doc *v1alpha1.Doc, examples []*v1alpha1.Example, answer []*v1alpha1.Block) (*v1alpha1.Assertion, error)
Expand All @@ -14,7 +16,3 @@ type Assertion interface {
}

type AssertResult string

const AssertPassed AssertResult = "passed"
const AssertFailed AssertResult = "failed"
const AssertSkipped AssertResult = "skipped"
6 changes: 3 additions & 3 deletions app/pkg/eval/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type AssertCodeAfterMarkdown struct {

func (a *AssertCodeAfterMarkdown) Assert(ctx context.Context, doc *v1alpha1.Doc, examples []*v1alpha1.Example, answer []*v1alpha1.Block) (*v1alpha1.Assertion, error) {
assertion := &v1alpha1.Assertion{
Name: a.Name(),
Name: v1alpha1.Assertion_CODE_AFTER_MARKDOWN,
}

if len(doc.Blocks) == 0 {
Expand Down Expand Up @@ -62,7 +62,7 @@ type AssertOneCodeCell struct {

func (a *AssertOneCodeCell) Assert(ctx context.Context, doc *v1alpha1.Doc, examples []*v1alpha1.Example, answer []*v1alpha1.Block) (*v1alpha1.Assertion, error) {
assertion := &v1alpha1.Assertion{
Name: a.Name(),
Name: v1alpha1.Assertion_ONE_CODE_CELL,
}

if len(doc.Blocks) == 0 {
Expand Down Expand Up @@ -99,7 +99,7 @@ type AssertEndsWithCodeCell struct {

func (a *AssertEndsWithCodeCell) Assert(ctx context.Context, doc *v1alpha1.Doc, examples []*v1alpha1.Example, answer []*v1alpha1.Block) (*v1alpha1.Assertion, error) {
assertion := &v1alpha1.Assertion{
Name: a.Name(),
Name: v1alpha1.Assertion_ENDS_WITH_CODE_CELL,
}

if len(doc.Blocks) == 0 {
Expand Down
18 changes: 9 additions & 9 deletions app/pkg/eval/assertions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestAssertCodeAfterMarkdown(t *testing.T) {
examples: []*v1alpha1.Example{},
answer: []*v1alpha1.Block{},
expected: &v1alpha1.Assertion{
Name: "AssertCodeAfterMarkdown",
Name: v1alpha1.Assertion_CODE_AFTER_MARKDOWN,
Result: v1alpha1.AssertResult_SKIPPED,
},
},
Expand All @@ -43,7 +43,7 @@ func TestAssertCodeAfterMarkdown(t *testing.T) {
},
},
expected: &v1alpha1.Assertion{
Name: "AssertCodeAfterMarkdown",
Name: v1alpha1.Assertion_CODE_AFTER_MARKDOWN,
Result: v1alpha1.AssertResult_PASSED,
},
},
Expand All @@ -63,7 +63,7 @@ func TestAssertCodeAfterMarkdown(t *testing.T) {
},
},
expected: &v1alpha1.Assertion{
Name: "AssertCodeAfterMarkdown",
Name: v1alpha1.Assertion_CODE_AFTER_MARKDOWN,
Result: v1alpha1.AssertResult_FAILED,
},
},
Expand Down Expand Up @@ -91,7 +91,7 @@ func TestAssertOneCodeCell(t *testing.T) {
examples: []*v1alpha1.Example{},
answer: []*v1alpha1.Block{},
expected: &v1alpha1.Assertion{
Name: "AssertCodeAfterMarkdown",
Name: v1alpha1.Assertion_CODE_AFTER_MARKDOWN,
Result: v1alpha1.AssertResult_SKIPPED,
},
},
Expand All @@ -111,7 +111,7 @@ func TestAssertOneCodeCell(t *testing.T) {
},
},
expected: &v1alpha1.Assertion{
Name: CodeAfterMarkdownName,
Name: v1alpha1.Assertion_CODE_AFTER_MARKDOWN,
Result: v1alpha1.AssertResult_PASSED,
},
},
Expand All @@ -131,7 +131,7 @@ func TestAssertOneCodeCell(t *testing.T) {
},
},
expected: &v1alpha1.Assertion{
Name: OneCodeCellName,
Name: v1alpha1.Assertion_ONE_CODE_CELL,
Result: v1alpha1.AssertResult_FAILED,
},
},
Expand Down Expand Up @@ -159,7 +159,7 @@ func TestAssertEndsWithCodeCell(t *testing.T) {
examples: []*v1alpha1.Example{},
answer: []*v1alpha1.Block{},
expected: &v1alpha1.Assertion{
Name: EndsWithCodeCellName,
Name: v1alpha1.Assertion_ENDS_WITH_CODE_CELL,
Result: v1alpha1.AssertResult_SKIPPED,
},
},
Expand All @@ -179,7 +179,7 @@ func TestAssertEndsWithCodeCell(t *testing.T) {
},
},
expected: &v1alpha1.Assertion{
Name: EndsWithCodeCellName,
Name: v1alpha1.Assertion_ENDS_WITH_CODE_CELL,
Result: v1alpha1.AssertResult_PASSED,
},
},
Expand All @@ -202,7 +202,7 @@ func TestAssertEndsWithCodeCell(t *testing.T) {
},
},
expected: &v1alpha1.Assertion{
Name: EndsWithCodeCellName,
Name: v1alpha1.Assertion_ENDS_WITH_CODE_CELL,
Result: v1alpha1.AssertResult_FAILED,
},
},
Expand Down
Loading
Loading