Skip to content

Commit

Permalink
Merge branch 'openai-omg'
Browse files Browse the repository at this point in the history
  • Loading branch information
djpiper28 committed Mar 3, 2024
2 parents a4d5383 + 0275bb3 commit 3ee4347
Show file tree
Hide file tree
Showing 5 changed files with 281 additions and 31 deletions.
14 changes: 7 additions & 7 deletions kube_cache/aiStuff/ai.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ func (ai *KubeAi) GenerateKubeRecipe(kubeName1, kubeName2 string) (string, error
Role: "system",
Content: baseRequest,
},
{
Role: "user",
Content: ai.generateAiPrompt(kubeName1, kubeName2),
},
{
Role: "user",
Content: ai.generateAiPrompt(kubeName1, kubeName2),
},
},
MaxTokens: maxTokens,
Temperature: temperature,
Expand Down Expand Up @@ -125,16 +125,16 @@ func (ai *KubeAi) GenerateKubeRecipe(kubeName1, kubeName2 string) (string, error
return "", err
}

var aiResponse openaiResp
var aiResponse openaiResp
err = json.Unmarshal([]byte(body), &aiResponse)
if err != nil {
log.Printf("The silly server sent %s, this is very bad %s", body, err)
return string(body), nil
}

actualLegitMessage := aiResponse.Choices[0].Message.Content
actualLegitMessage := aiResponse.Choices[0].Message.Content

var aiResp2 aiResp
var aiResp2 aiResp
err = json.Unmarshal([]byte(actualLegitMessage), &aiResp2)
if err != nil {
log.Printf("The silly ai sent %s, this is very bad %s", actualLegitMessage, err)
Expand Down
10 changes: 5 additions & 5 deletions kube_cache/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ func main() {
log.Println("Reading config...")
err := godotenv.Load()
if err != nil {
log.Fatal(err)
log.Println(err)
}

log.Println("Using configuration for database...")
database := model.New(os.Getenv("DATABASE_URL"))
database := model.New(os.Getenv("DATABASE_URL"))

log.Println("Creating AI client...")
ai := aiStuff.New(os.Getenv("OPENAI_ENDPOINT"),
Expand All @@ -44,8 +44,8 @@ func main() {
},
}))

log.Println("Start up the API")
server := server.New(database, ai)
server.Use(router)
log.Println("Start up the API")
server := server.New(database, ai)
server.Use(router)
log.Fatal(router.Run("0.0.0.0:8080"))
}
88 changes: 71 additions & 17 deletions kube_cache/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,34 @@ import (
"gorm.io/gorm"
)

func SortKubesStr(kube1, kube2 *string) {
if *kube1 > *kube2 {
*kube1, *kube2 = *kube2, *kube1
}
}

func SortKubesUuid(kube1, kube2 *uuid.UUID) {
if kube1.String() > kube2.String() {
*kube1, *kube2 = *kube2, *kube1
}
}

type Kube struct {
Name string `json:"name"`
Name string `gorm:"unique;not null" json:"name"`
Id uuid.UUID `gorm:"primaryKey" json:"id"`
// Image []byte `json:"image"`
}

type KubeRecipe struct {
Id uuid.UUID `gorm:"primaryKey" json:"id"`
Output uuid.UUID `json:"outputId" gorm:"index:idx_output_id"`
Output uuid.UUID `json:"outputId" gorm:"index:idx_output_id;not null"`
OutputKube *Kube `json:"outputKube" gorm:"foreignKey:Output;references:Id"`
Ingredients []KubeRecipeLines `json:"ingredients"`
}

type KubeRecipeLines struct {
KubeRecipeId uuid.UUID `json:"kube_recipe_id" gorm:"index:idx_kube_recipe_id"`
KubeId string `json:"kube_id" gorm:"index:idx_kube_id"`
Kube *Kube `json:"kube" gorm:"foreignKey:KubeId;references:Id"`
Kube1Id uuid.UUID `json:"kube1_id" gorm:"index:idx_kube1_id;not null"`
Kube1 *Kube `json:"-" gorm:"foreignKey:Kube1Id;references:Id"`

Kube2Id uuid.UUID `json:"kube2_id" gorm:"index:idx_kube2_id;not null"`
Kube2 *Kube `json:"-" gorm:"foreignKey:Kube2Id;references:Id"`
}

type Database struct {
Expand All @@ -47,8 +58,12 @@ func New(url string) *Database {
log.Fatal(err)
}

database := &Database{Db: db}
log.Println("Seeding database...")
database.seed()

log.Println("Migrated database successfully")
return &Database{Db: db}
return database
}

func (db *Database) GetKube(id string) (Kube, error) {
Expand All @@ -57,18 +72,57 @@ func (db *Database) GetKube(id string) (Kube, error) {
return kube, result.Error
}

func (db *Database) SetKube(kube Kube) error {
result := db.Db.Create(&kube)
return result.Error
func (db *Database) GetAllKubeRecipes() ([]KubeRecipe, error) {
var recipes []KubeRecipe
result := db.Db.Find(&recipes)
return recipes, result.Error
}

func (db *Database) GetAllKubes() ([]Kube, error) {
var kubes []Kube
result := db.Db.Find(&kubes)
return kubes, result.Error
}

func (db *Database) GetKubeRecipe(id string) (KubeRecipe, error) {
func (db *Database) GetKubeRecipe(kube1, kube2 string) (KubeRecipe, error) {
SortKubesStr(&kube1, &kube2)

var recipe KubeRecipe
result := db.Db.Preload("OutputKube").Preload("Ingredients.Kube").First(&recipe, "id = ?", id)
result := db.Db.First(&recipe, "kube1_id = ? AND kube2_id = ?", kube1, kube2)
return recipe, result.Error
}

func (db *Database) SetKubeRecipe(recipe KubeRecipe) error {
result := db.Db.Create(&recipe)
return result.Error
func (db *Database) SetKubeRecipe(kube1, kube2 Kube, newKube string) error {
kube1Id := kube1.Id
kube2Id := kube2.Id
SortKubesUuid(&kube1Id, &kube2Id)

log.Printf("Setting kube recipe: %s + %s = %s", kube1.Name, kube2.Name, newKube)
err := db.Db.Transaction(func(tx *gorm.DB) error {
newKubeObject := Kube{Name: newKube, Id: uuid.New()}
err := tx.Create(&newKubeObject).Error
if err != nil {
log.Printf("Cannot create new kube: %s", err)
return err
}

recipe := KubeRecipe{
Id: uuid.New(),
Output: newKubeObject.Id,
Kube1Id: kube1Id,
Kube2Id: kube2Id,
}
err = tx.Create(&recipe).Error
if err != nil {
log.Printf("Cannot create new kube recipe: %s", err)
return err
}
return nil
})

if err != nil {
log.Print("Saving the kube recipe failed")
return err
}
return nil
}
115 changes: 115 additions & 0 deletions kube_cache/model/seed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package model

import (
"github.com/google/uuid"
"gorm.io/gorm"
"log"
)

func insertKube(name string, tx *gorm.DB) error {
log.Printf("Inserting kube: %s", name)
kube := Kube{Name: name, Id: uuid.New()}
return tx.Create(&kube).Error
}

func insertKubeRecipe(kube1, kube2, output string, tx *gorm.DB) error {
log.Printf("Inserting kube recipe: %s + %s = %s", kube1, kube2, output)
kube1Row := Kube{}
kube2Row := Kube{}
outputRow := Kube{}

tx.First(&kube1Row, "name = ?", kube1)
tx.First(&kube2Row, "name = ?", kube2)

kube1Id := kube1Row.Id
kube2Id := kube2Row.Id
SortKubesUuid(&kube1Id, &kube2Id)

err := insertKube(output, tx)
if err != nil {
return err
}
tx.First(&outputRow, "name = ?", output)

kubeRecipe := KubeRecipe{
Id: uuid.New(),
Output: outputRow.Id,
Kube1Id: kube1Id,
Kube2Id: kube2Id,
}

return tx.Create(&kubeRecipe).Error
}

type recipe struct {
kube1 string
kube2 string
output string
}

func (d *Database) seed() {
err := d.Db.Transaction(func(tx *gorm.DB) error {
kubes := []string{
"hydrogen",
"oxygen",
"nitrogen",
"calcium",
"iron",
"aluminium",
"uranium",
"sodium",
"chlorine",
"light",
"time",
"silicon",
}

for _, kube := range kubes {
if err := insertKube(kube, tx); err != nil {
return err
}
}

recipes := []recipe{
{"hydrogen", "oxygen", "water"},
{"water", "chlorine", "tap water"},
{"sodium", "chlorine", "salt"},
{"water", "salt", "sea water"},
{"nitrogen", "oxygen", "air"},
{"iron", "water", "rust"},
{"silicon", "aluminium", "feldspar"},
{"feldspar", "silicon", "sand"},
{"sand", "water", "dirt"},
{"sand", "sea water", "beach"},
{"dirt", "water", "earth"},
{"earth", "air", "life"},
{"life", "time", "age"},
{"uranium", "water", "energy"},
{"sand", "time", "rock"},
{"rock", "energy", "fire"},
{"fire", "sand", "glass"},
}

for _, recipe := range recipes {
if err := insertKubeRecipe(recipe.kube1, recipe.kube2, recipe.output, tx); err != nil {
return err
}
}
return nil
}).Error

if err != nil {
log.Printf("Seeding database failed: %s", err)
}

var kubes, recipes int64
d.Db.Model(&Kube{}).Count(&kubes)
d.Db.Model(&KubeRecipe{}).Count(&recipes)

log.Printf("There are %d kubes and %d recipes in the database", kubes, recipes)
if kubes == 0 || recipes == 0 {
log.Fatal("Seeding database failed - no kubes or recipes")
}

log.Println("Database successfully seeded")
}
Loading

0 comments on commit 3ee4347

Please sign in to comment.