Skip to content

Commit

Permalink
stash
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-brennan2005 committed Jun 18, 2024
1 parent 19e3253 commit a0cc4fb
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 3 deletions.
19 changes: 18 additions & 1 deletion backend/config/search.go
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
}
9 changes: 7 additions & 2 deletions backend/config/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type intermediateSettings struct {
Calendar intermediateCalendarSettings `envPrefix:"SAC_CALENDAR_"`
Google intermediateGoogleOAuthSettings `envPrefix:"SAC_GOOGLE_OAUTH_"`
Microsft intermediateMicrosoftOAuthSetting `envPrefix:"SAC_MICROSOFT_OAUTH_"`
Search SearchSettings `envPrefix:"SAC_SEARCH_"`
Search intermediateSearchSettings `envPrefix:"SAC_SEARCH_"`
}

func (i *intermediateSettings) into() (*Settings, error) {
Expand Down Expand Up @@ -91,6 +91,11 @@ func (i *intermediateSettings) into() (*Settings, error) {
return nil, err
}

search, err := i.Search.into()
if err != nil {
return nil, err
}

return &Settings{
Application: i.Application,
DBCache: *dbCache,
Expand All @@ -104,7 +109,7 @@ func (i *intermediateSettings) into() (*Settings, error) {
Microsft: *microsoft,
AWS: *aws,
Resend: *resend,
Search: i.Search,
Search: *search,
},
}, nil
}
91 changes: 91 additions & 0 deletions backend/search/embeddings.go
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
}

0 comments on commit a0cc4fb

Please sign in to comment.