-
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
1 parent
19e3253
commit a0cc4fb
Showing
3 changed files
with
116 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,22 @@ | ||
package config | ||
|
||
import m "github.com/garrettladley/mattress" | ||
|
||
type SearchSettings struct { | ||
URI string `env:"URI"` | ||
OpenAIApiKey *m.Secret[string] | ||
} | ||
|
||
type intermediateSearchSettings struct { | ||
OpenAIApiKey string `env:"OPENAI_API_KEY` | ||
} | ||
|
||
func (i *intermediateSearchSettings) into() (*SearchSettings, error) { | ||
openAiApiKey, err := m.NewSecret(i.OpenAIApiKey) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &SearchSettings{ | ||
OpenAIApiKey: openAiApiKey, | ||
}, 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
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,91 @@ | ||
package search | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"net/http" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/GenerateNU/sac/backend/config" | ||
"github.com/GenerateNU/sac/backend/entities/models" | ||
"github.com/GenerateNU/sac/backend/utilities" | ||
"github.com/goccy/go-json" | ||
"github.com/gofiber/fiber/v2" | ||
"gorm.io/gorm" | ||
) | ||
|
||
type CreateEmbeddingRequestBody struct { | ||
Input []string `json:"input"` | ||
Model string `json:"model"` | ||
} | ||
|
||
type CreateEmbeddingResponseBody struct { | ||
Data [][]float32 `json:"data"` | ||
} | ||
|
||
func UpsertClubEmbedding(db *gorm.DB, s *config.Settings, club *models.Club) error { | ||
embedding, err := | ||
createEmbedding(s, fmt.Sprintf("%s %s %s", club.Name, club.Preview, club.Description)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
embeddingArr := make([]string, len(embedding)) | ||
for i, f := range embedding { | ||
embeddingArr[i] = strconv.FormatFloat(float64(f), 'f', -1, 32) | ||
} | ||
embeddingStr := strings.Join(embeddingArr, ", ") | ||
|
||
queryString := fmt.Sprintf( | ||
"UPDATE clubs SET embedding = '[%s]' WHERE id = '%s'", embeddingStr, club.ID.String()) | ||
|
||
if err := db.Exec(queryString).Error; err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func createEmbedding(s *config.Settings, item string) ([]float32, error) { | ||
embeddingBody, err := json.Marshal( | ||
CreateEmbeddingRequestBody{ | ||
Input: []string{item}, | ||
Model: "text-embedding-ada-002", | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
req, err := http.NewRequest(fiber.MethodPost, | ||
"https://api.openai.com/v1/embeddings", | ||
bytes.NewBuffer(embeddingBody)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
req = utilities.ApplyModifiers(req, | ||
utilities.Authorization(s.Integrations.Search.OpenAIApiKey.Expose()), | ||
utilities.JSON(), | ||
) | ||
|
||
resp, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
defer resp.Body.Close() | ||
|
||
var embeddingResultBody CreateEmbeddingResponseBody | ||
|
||
err = json.NewDecoder(resp.Body).Decode(&embeddingResultBody) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if len(embeddingResultBody.Data) < 1 { | ||
return nil, err | ||
} | ||
|
||
return embeddingResultBody.Data[0], nil | ||
} |