Skip to content

Commit

Permalink
add reset function, add instructtype tags, add similar meaning function
Browse files Browse the repository at this point in the history
  • Loading branch information
ili16 committed Mar 20, 2024
1 parent 3799037 commit ac8eaff
Show file tree
Hide file tree
Showing 5 changed files with 242 additions and 30 deletions.
71 changes: 68 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"log"
"net/http"
"net/url"
"os"
"strconv"

"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -82,6 +83,7 @@ func main() {

router.GET("/weaviate/retrieveresponselist", func(c *gin.Context) {
searchQuery := c.Query("query")
instructType := c.Query("instructType")

decodedQuery, err := url.QueryUnescape(searchQuery)
if err != nil {
Expand All @@ -91,7 +93,7 @@ func main() {

log.Printf("Decoded Query: %s", decodedQuery)

responseList, err := weaviate.ResponseList(decodedQuery)
responseList, err := weaviate.ResponseList(decodedQuery, instructType)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
Expand Down Expand Up @@ -130,6 +132,24 @@ func main() {
c.JSON(http.StatusOK, response)
})

router.GET("/get-similar-meaning", func(c *gin.Context) {
searchQuery := c.Query("meaning")

decodedQuery, err := url.QueryUnescape(searchQuery)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid search query"})
return
}

response, err := SemanticSimilarityByMeaning(decodedQuery)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}

c.JSON(http.StatusOK, response)
})

router.GET("/get-similar-code", func(c *gin.Context) {
searchQuery := c.Query("code")

Expand All @@ -139,7 +159,25 @@ func main() {
return
}

response, err := SemanticSimilarity(decodedQuery)
response, err := SemanticSimilarityByCode(decodedQuery)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}

c.JSON(http.StatusOK, response)
})

router.GET("/get-instructtype", func(c *gin.Context) {
searchQuery := c.Query("code")

decodedQuery, err := url.QueryUnescape(searchQuery)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid search query"})
return
}

response, err := weaviate.GetInstructTypes(decodedQuery)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
Expand Down Expand Up @@ -217,14 +255,24 @@ func main() {
router.POST("/add-instruct", redis.AddInstruct)
router.POST("/del-instruct", redis.DeleteInstruct)
router.GET("/get-all-sets", redis.GetAllSets)
router.GET("/delete-db", func(c *gin.Context) {
secretkey := c.Query("key")

if secretkey == os.Getenv("delete_key") {
ResetDB()
c.JSON(http.StatusOK, "OK")
} else {
c.JSON(http.StatusUnauthorized, "Unauthorized")
}
})

err = router.Run(":8080")
if err != nil {
return
}
}

func SemanticSimilarity(code string) ([]string, error) {
func SemanticSimilarityByCode(code string) ([]string, error) {
PromptExists, exists := weaviate.RetrieveHasSemanticMeaning(code)
if !exists {
SemanticMeaning := ollama.SemanticMeaning("", code, false)
Expand All @@ -242,5 +290,22 @@ func SemanticSimilarity(code string) ([]string, error) {

return similarCode, err
}
}

func SemanticSimilarityByMeaning(meaning string) ([]string, error) {
similarCode, err := weaviate.GetSimilarSemanticMeaning(meaning)
if err != nil {
return nil, err
}
return similarCode, err
}

func ResetDB() {
redis.DeleteAllSets()
redis.InitRedis()
weaviate.DeleteAllClasses()
err := weaviate.InitSchema()
if err != nil {
return
}
}
2 changes: 1 addition & 1 deletion ollama/ollama.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func GenerateResponse(prompt map[string]interface{}) (weaviate.ResponseData, err
return weaviate.ResponseData{}, errors.New("invalid response format")
}

PromptID, err := weaviate.CreatePromptObject(instruct, code, "Prompt", gitURL)
PromptID, err := weaviate.CreatePromptObject(instruct, set, code, "Prompt", gitURL)
if err != nil {
return weaviate.ResponseData{}, err
}
Expand Down
24 changes: 24 additions & 0 deletions redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,3 +265,27 @@ func GetAllSets(c *gin.Context) {

return
}

func DeleteAllSets() {
rdb := loadClient()

keysCmd := rdb.Keys(context.Background(), "*") // Get all keys matching the pattern "*"

keys, err := keysCmd.Result()
if err != nil {
return
}

for _, key := range keys {
typeCmd := rdb.Type(context.Background(), key) // Get the type of the key

keyType, err := typeCmd.Result()
if err != nil {
return
}

if keyType == "set" {
rdb.Del(context.Background(), key)
}
}
}
36 changes: 31 additions & 5 deletions weaviate/weaviate.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,16 @@ func InitSchema() error {
},
},
},
{
DataType: []string{"text"},
Description: "instruct type",
Name: "instructType",
ModuleConfig: map[string]interface{}{
"text2vec-transformers": map[string]interface{}{
"skip": true,
},
},
},
{
DataType: []string{"text"},
Description: "The code which is targeted in the prompt",
Expand Down Expand Up @@ -226,17 +236,18 @@ func createClass(className, description, vectorizer string, properties []*models
return nil
}

func CreatePromptObject(instruct string, code string, class string, gitURL string) (string, error) {
func CreatePromptObject(instruct string, instructType string, code string, class string, gitURL string) (string, error) {
client, err := loadClient()
if err != nil {
return "", err
}

dataSchema := map[string]interface{}{
"instruct": instruct,
"code": code,
"rank": 1,
"gitURL": gitURL,
"instruct": instruct,
"code": code,
"rank": 1,
"gitURL": gitURL,
"instructType": instructType,
}

weaviateObject, err := client.Data().Creator().
Expand Down Expand Up @@ -438,3 +449,18 @@ func CreateReferenceSemanticMeaningToPrompt(semanticMeaningID string, PromptID s

return nil
}

func DeleteAllClasses() {
client, err := loadClient()
if err != nil {
log.Printf("Error loading client: %v\n", err)
return
}

classes := []string{"Prompt", "Response", "SemanticMeaning"}

for _, ch := range classes {
err = client.Schema().ClassDeleter().WithClassName(ch).Do(context.Background())

}
}
Loading

0 comments on commit ac8eaff

Please sign in to comment.