-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
156 additions
and
28 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,58 @@ | ||
package app | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/initialcapacity/ai-starter/internal/analyzer" | ||
"github.com/initialcapacity/ai-starter/pkg/ai" | ||
"log/slog" | ||
) | ||
|
||
type QueryService struct { | ||
embeddingsGateway *analyzer.EmbeddingsGateway | ||
aiClient aiClient | ||
} | ||
|
||
func NewQueryService(embeddingsGateway *analyzer.EmbeddingsGateway, aiClient aiClient) *QueryService { | ||
return &QueryService{embeddingsGateway: embeddingsGateway, aiClient: aiClient} | ||
} | ||
|
||
func (q *QueryService) FetchResponse(ctx context.Context, query string) (QueryResult, error) { | ||
embedding, err := q.aiClient.CreateEmbedding(ctx, query) | ||
if err != nil { | ||
slog.Error("unable to create embedding", err) | ||
return QueryResult{}, err | ||
} | ||
|
||
record, err := q.embeddingsGateway.FindSimilar(embedding) | ||
if err != nil { | ||
slog.Error("unable to find similar embedding", err) | ||
return QueryResult{}, err | ||
} | ||
|
||
response, err := q.aiClient.GetChatCompletion(ctx, []ai.ChatMessage{ | ||
{Role: ai.System, Content: "You are a reporter for a major world newspaper."}, | ||
{Role: ai.System, Content: "Write your response as if you were writing a short, high-quality news article for your paper. Limit your response to one paragraph."}, | ||
{Role: ai.System, Content: fmt.Sprintf("Use the following article for context: %s", record.Content)}, | ||
{Role: ai.User, Content: query}, | ||
}) | ||
if err != nil { | ||
slog.Error("unable fetch chat completion", err) | ||
return QueryResult{}, err | ||
} | ||
|
||
return QueryResult{ | ||
Source: record.Source, | ||
Response: response, | ||
}, nil | ||
} | ||
|
||
type QueryResult struct { | ||
Response chan string | ||
Source string | ||
} | ||
|
||
type aiClient interface { | ||
GetChatCompletion(ctx context.Context, messages []ai.ChatMessage) (chan string, error) | ||
CreateEmbedding(ctx context.Context, text string) ([]float32, error) | ||
} |
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,92 @@ | ||
package app_test | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"github.com/initialcapacity/ai-starter/internal/analyzer" | ||
"github.com/initialcapacity/ai-starter/internal/app" | ||
"github.com/initialcapacity/ai-starter/pkg/ai" | ||
"github.com/initialcapacity/ai-starter/pkg/testsupport" | ||
"github.com/pgvector/pgvector-go" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestQueryService_FetchResponse(t *testing.T) { | ||
testDb := testsupport.NewTestDb(t) | ||
defer testDb.Close() | ||
insertData(testDb) | ||
|
||
service := app.NewQueryService(analyzer.NewEmbeddingsGateway(testDb.DB), fakeAi{}) | ||
|
||
result, err := service.FetchResponse(context.Background(), "Does this sound good?") | ||
assert.NoError(t, err) | ||
message := <-result.Response | ||
|
||
assert.Equal(t, "https://example.com", result.Source) | ||
assert.Equal(t, "Sounds good", message) | ||
} | ||
|
||
func TestQueryService_FetchResponse_EmbeddingError(t *testing.T) { | ||
testDb := testsupport.NewTestDb(t) | ||
defer testDb.Close() | ||
insertData(testDb) | ||
service := app.NewQueryService(analyzer.NewEmbeddingsGateway(testDb.DB), fakeAi{embeddingError: errors.New("bad news")}) | ||
|
||
_, err := service.FetchResponse(context.Background(), "Does this sound good?") | ||
|
||
assert.EqualError(t, err, "bad news") | ||
} | ||
|
||
func TestQueryService_FetchResponse_NoEmbeddings(t *testing.T) { | ||
testDb := testsupport.NewTestDb(t) | ||
defer testDb.Close() | ||
service := app.NewQueryService(analyzer.NewEmbeddingsGateway(testDb.DB), fakeAi{}) | ||
|
||
_, err := service.FetchResponse(context.Background(), "Does this sound good?") | ||
|
||
assert.EqualError(t, err, "sql: no rows in result set") | ||
} | ||
|
||
func TestQueryService_FetchResponse_CompletionError(t *testing.T) { | ||
testDb := testsupport.NewTestDb(t) | ||
defer testDb.Close() | ||
insertData(testDb) | ||
service := app.NewQueryService(analyzer.NewEmbeddingsGateway(testDb.DB), fakeAi{completionError: errors.New("bad news")}) | ||
|
||
_, err := service.FetchResponse(context.Background(), "Does this sound good?") | ||
|
||
assert.EqualError(t, err, "bad news") | ||
} | ||
|
||
func insertData(testDb *testsupport.TestDb) { | ||
testDb.Execute("insert into data (id, source, content) values ('aaaaaaaa-2f3f-4bc9-8dba-ba397156cc16', 'https://example.com', 'some content')") | ||
testDb.Execute("insert into chunks (id, data_id, content) values ('bbbbbbbb-2f3f-4bc9-8dba-ba397156cc16', 'aaaaaaaa-2f3f-4bc9-8dba-ba397156cc16','a chunk')") | ||
testDb.Execute("insert into embeddings (chunk_id, embedding) values ('bbbbbbbb-2f3f-4bc9-8dba-ba397156cc16', $1)", pgvector.NewVector(testsupport.CreateVector(0))) | ||
|
||
} | ||
|
||
type fakeAi struct { | ||
embeddingError error | ||
completionError error | ||
} | ||
|
||
func (f fakeAi) GetChatCompletion(_ context.Context, _ []ai.ChatMessage) (chan string, error) { | ||
if f.embeddingError != nil { | ||
return nil, f.embeddingError | ||
} | ||
|
||
response := make(chan string) | ||
go func() { | ||
response <- "Sounds good" | ||
}() | ||
return response, nil | ||
} | ||
|
||
func (f fakeAi) CreateEmbedding(_ context.Context, _ string) ([]float32, error) { | ||
if f.completionError != nil { | ||
return nil, f.completionError | ||
} | ||
|
||
return testsupport.CreateVector(0), nil | ||
} |