Skip to content

Commit

Permalink
CI passing
Browse files Browse the repository at this point in the history
  • Loading branch information
garrettladley committed Feb 17, 2024
1 parent 50484ea commit 45aa86d
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 62 deletions.
22 changes: 15 additions & 7 deletions backend/src/search/openai.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,25 @@ func NewOpenAIClient(settings config.OpenAISettings) *OpenAIClient {
return &OpenAIClient{Settings: settings}
}

type CreateEmbeddingRequestBody struct {
Input string `json:"input"`
Model string `json:"model"`
}

type Embedding struct {
Embedding []float32 `json:"embedding"`
}

type CreateEmbeddingResponseBody struct {
Data []struct {
Embedding []float32 `json:"embedding"`
} `json:"data"`
Data []Embedding `json:"data"`
}

func (c *OpenAIClient) CreateEmbedding(payload string) ([]float32, *errors.Error) {
embeddingBody, err := json.Marshal(map[string]interface{}{
"input": payload,
"model": "text-embedding-ada-002",
})
embeddingBody, err := json.Marshal(
CreateEmbeddingRequestBody{
Input: payload,
Model: "text-embedding-ada-002",
})
if err != nil {
return nil, &errors.FailedToCreateEmbedding
}
Expand Down
23 changes: 12 additions & 11 deletions backend/src/search/pinecone.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (c *PineconeClient) Upsert(item Searchable) *errors.Error {
return &errors.FailedToUpsertToPinecone
}

upsertBody, _ := json.Marshal(
upsertBody, err := json.Marshal(
PineconeUpsertRequestBody{
Vectors: []Vector{
{
Expand All @@ -65,6 +65,9 @@ func (c *PineconeClient) Upsert(item Searchable) *errors.Error {
},
Namespace: item.Namespace(),
})
if err != nil {
return &errors.FailedToUpsertToPinecone
}

req, err := http.NewRequest(fiber.MethodPost,
fmt.Sprintf("%s/vectors/upsert", c.Settings.IndexHost.Expose()),
Expand Down Expand Up @@ -141,13 +144,15 @@ type PineconeSearchRequestBody struct {
Namespace string `json:"namespace"`
}

type Match struct {
Id string `json:"id"`
Score float32 `json:"score"`
Values []float32 `json:"values"`
}

type PineconeSearchResponseBody struct {
Matches []struct {
Id string `json:"id"`
Score float32 `json:"score"`
Values []float32 `json:"values"`
} `json:"matches"`
Namespace string `json:"namespace"`
Matches []Match `json:"matches"`
Namespace string `json:"namespace"`
}

func (c *PineconeClient) Search(item Searchable, topK int) ([]string, *errors.Error) {
Expand Down Expand Up @@ -184,10 +189,6 @@ func (c *PineconeClient) Search(item Searchable, topK int) ([]string, *errors.Er

defer resp.Body.Close()

if err != nil {
return []string{}, &errors.FailedToSearchToPinecone
}

if resp.StatusCode != fiber.StatusOK {
return []string{}, &errors.FailedToSearchToPinecone
}
Expand Down
84 changes: 41 additions & 43 deletions backend/tests/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type mockConfig struct {
}

func newMockConfig() *mockConfig {
pineconeIndexHost := "indexHost"
pineconeIndexHost := "https://indexHost.com"
pineconeApiKey := "pinecone"

pineconeIndexHostSecret, err := m.NewSecret(pineconeIndexHost)
Expand Down Expand Up @@ -79,14 +79,14 @@ func TestPineconeUpsertWorks(t *testing.T) {
MatchHeader("accept", "application/json").
MatchHeader("content-type", "application/json").
MatchType("json").
JSON(map[string]interface{}{
"vectors": []interface{}{
map[string]interface{}{
"id": mockSearchId,
"values": mockValues,
JSON(search.PineconeUpsertRequestBody{
Vectors: []search.Vector{
{
ID: mockSearchId,
Values: mockValues,
},
},
"namespace": mockNamespace,
Namespace: mockNamespace,
}).
Reply(200)

Expand All @@ -95,15 +95,15 @@ func TestPineconeUpsertWorks(t *testing.T) {
MatchHeader("Authorization", "Bearer "+mockConfig.OpenAI.APIKey.Expose()).
MatchHeader("content-type", "application/json").
MatchType("json").
JSON(map[string]interface{}{
"input": mockSearchString,
"model": "text-embedding-ada-002",
JSON(search.CreateEmbeddingRequestBody{
Input: mockSearchString,
Model: "text-embedding-ada-002",
}).
Reply(200).
JSON(map[string]interface{}{
"data": []map[string]interface{}{
JSON(search.CreateEmbeddingResponseBody{
Data: []search.Embedding{
{
"embedding": []float32{1.0, 1.0, 1.0, 1.0},
Embedding: mockValues,
},
},
})
Expand All @@ -129,12 +129,10 @@ func TestPineconeDeleteWorks(t *testing.T) {
MatchHeader("accept", "application/json").
MatchHeader("content-type", "application/json").
MatchType("json").
JSON(map[string]interface{}{
"deleteAll": false,
"ids": []string{
mockSearchId,
},
"namespace": mockNamespace,
JSON(search.PineconeDeleteRequestBody{
Namespace: mockNamespace,
IDs: []string{mockSearchId},
DeleteAll: false,
}).
Reply(200)

Expand Down Expand Up @@ -162,39 +160,39 @@ func TestPineconeSearchWorks(t *testing.T) {
MatchHeader("accept", "application/json").
MatchHeader("content-type", "application/json").
MatchType("json").
JSON(map[string]interface{}{
"includeValues": false,
"includeMetadata": false,
"topK": topK,
"vector": mockValues,
"namespace": mockNamespace,
JSON(search.PineconeSearchRequestBody{
IncludeValues: false,
IncludeMetadata: false,
TopK: topK,
Vector: mockValues,
Namespace: mockNamespace,
}).
Reply(200).
JSON(map[string]interface{}{
"matches": []map[string]interface{}{
JSON(search.PineconeSearchResponseBody{
Matches: []search.Match{
{
"id": mockSearchId,
"score": 1.0,
"values": []float32{1.0, 1.0, 1.0, 1.0},
Id: mockSearchId,
Score: 1.0,
Values: mockValues,
},
},
"namespace": mockNamespace,
Namespace: mockNamespace,
})

gock.New("https://api.openai.com").
Post("/v1/embeddings").
MatchHeader("Authorization", "Bearer "+mockConfig.OpenAI.APIKey.Expose()).
MatchHeader("content-type", "application/json").
MatchType("json").
JSON(map[string]interface{}{
"input": mockSearchString,
"model": "text-embedding-ada-002",
JSON(search.CreateEmbeddingRequestBody{
Input: mockSearchString,
Model: "text-embedding-ada-002",
}).
Reply(200).
JSON(map[string]interface{}{
"data": []map[string]interface{}{
JSON(search.CreateEmbeddingResponseBody{
Data: []search.Embedding{
{
"embedding": []float32{1.0, 1.0, 1.0, 1.0},
Embedding: []float32{1.0, 1.0, 1.0, 1.0},
},
},
})
Expand All @@ -220,15 +218,15 @@ func TestOpenAIEmbeddingWorks(t *testing.T) {
MatchHeader("Authorization", "Bearer "+mockConfig.OpenAI.APIKey.Expose()).
MatchHeader("content-type", "application/json").
MatchType("json").
JSON(map[string]interface{}{
"input": testString,
"model": "text-embedding-ada-002",
JSON(search.CreateEmbeddingRequestBody{
Input: testString,
Model: "text-embedding-ada-002",
}).
Reply(200).
JSON(map[string]interface{}{
"data": []map[string]interface{}{
JSON(search.CreateEmbeddingResponseBody{
Data: []search.Embedding{
{
"embedding": []float32{1.0, 1.0, 1.0, 1.0},
Embedding: []float32{1.0, 1.0, 1.0, 1.0},
},
},
})
Expand Down
2 changes: 1 addition & 1 deletion config/.example_backend_env
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
SAC_PINECONE_INDEX_HOST="SAC_PINECONE_INDEX_HOST"
SAC_PINECONE_INDEX_HOST="https://SAC_PINECONE_INDEX_HOST.com"
SAC_PINECONE_API_KEY="SAC_PINECONE_API_KEY"
SAC_OPENAI_API_KEY="SAC_OPENAI_API_KEY"

0 comments on commit 45aa86d

Please sign in to comment.