Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
ili16 committed Mar 11, 2024
2 parents c76c4e7 + 6d172a7 commit 2b9cf42
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 11 deletions.
12 changes: 7 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,21 @@ func main() {

redis.InitRedis()

router := gin.Default()
router := gin.New()

router.Use(gin.LoggerWithConfig(gin.LoggerConfig{
SkipPaths: []string{"/weaviate/promptcount", "/weaviate"},
}))
router.Use(gin.Recovery())

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

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

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

count, err := weaviate.RetrievePromptCount(decodedQuery)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
Expand Down Expand Up @@ -203,6 +204,7 @@ func main() {

router.POST("/add-instruct", redis.AddInstruct)
router.POST("/del-instruct", redis.DeleteInstruct)
router.GET("/get-all-sets", redis.GetAllSets)

err = router.Run(":8080")
if err != nil {
Expand Down
6 changes: 4 additions & 2 deletions ollama/ollama.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ func GenerateResponse(prompt map[string]interface{}) (weaviate.ResponseData, err
url := os.Getenv("OLLAMA_URL") + "/api/generate"

set, ok := prompt["instructType"].(string)

log.Printf("instruction type: %s\n", set)

if !ok {
set = "default"
}
Expand All @@ -30,7 +33,6 @@ func GenerateResponse(prompt map[string]interface{}) (weaviate.ResponseData, err

instruct, ok := prompt["instruct"].(string)

log.Printf("ok: %v", ok)
if !ok {
instruct, _ = redis.GetSetMember(set)
}
Expand All @@ -48,7 +50,7 @@ func GenerateResponse(prompt map[string]interface{}) (weaviate.ResponseData, err

model, ok := prompt["model"].(string)
if !ok {
model = "codellama:13b-instruct"
model = "codellama:34b-instruct"
}

requestBody := map[string]interface{}{
Expand Down
83 changes: 79 additions & 4 deletions redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,27 @@ func InitRedis() {
"Explain me this:",
"How does the following code work?",
"Explain me step by step how this code works:",
"How would you handle errors in this code?",
"What testing strategies would you use to test this code?",
"How would you handle concurrency in this code?",
"Explain me how you would refactor this code to make it more readable:",
"Explain me how you would refactor this code to make it more performant:",
"Explain me how you would refactor this code to make it more maintainable:",
"Explain me how you would refactor this code to make it more testable:",
"Can you explain the design decisions behind this code?",
}

rdb.SAdd(ctx, "default", members...)

members = []interface{}{
"What security considerations should be taken into account when using this code?",
"Are there any security problems in this code:",
"What encryption algorithms are used to secure sensitive data?",
"Explain me how you would secure this code against SQL injection attacks:",
"Explain me how you would secure this code against XSS attacks:",
"How would you ensure compliance with security standards in this code?",
"Does this code comply with GDPR?",
"What authentication and authorization mechanisms are used in this code?",
}

rdb.SAdd(ctx, "security", members...)
Expand All @@ -48,6 +62,39 @@ func InitRedis() {

rdb.SAdd(ctx, "funny", members...)

members = []interface{}{
"What is the overall architecture of this code?",
"What technologies and frameworks are used in this code?",
"How will this code handle scalability?",
"What design patterns or architectural patterns are used in this code?",
"What are the trade-offs of using this code?",
"What considerations have been made for future maintenance and updates?",
}

rdb.SAdd(ctx, "architecture", members...)

members = []interface{}{
"What business impact does this code have?",
"What are the business requirements for this code?",
"How will you prioritize tasks and allocate workload among team members?",
}

rdb.SAdd(ctx, "project-management", members...)

members = []interface{}{
"What is the purpose of this function, and does it adhere to the Single Responsibility Principle (SRP)?",
"What dependencies does this function have, and can they be minimized or eliminated?",
"Does this function exhibit any code smells, such as long parameter lists or excessive branching?",
"What level of technical debt does this function carry, and how can it be reduced?",
"Are there any performance bottlenecks or inefficiencies in this function?",
"Does this function handle error and exception cases effectively?",
"Is this function well-documented, and does it have sufficient unit test coverage?",
"What design patterns or architectural principles can be applied to improve this function?",
"Can this function be optimized for concurrency or parallelism?",
"How can this function be modularized or decoupled to promote reusability and maintainability?",
}

rdb.SAdd(ctx, "modernisation", members...)
}

func AddInstruct(c *gin.Context) {
Expand All @@ -63,13 +110,11 @@ func AddInstruct(c *gin.Context) {
return
}

// Set the default list name to "default" if not provided.
listName := requestData.List
if listName == "" {
listName = "default"
}

// Add the item to the specified redis set.
ctx := context.Background()
if err := rdb.SAdd(ctx, listName, requestData.Item).Err(); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
Expand Down Expand Up @@ -106,7 +151,6 @@ func DeleteInstruct(c *gin.Context) {
c.Status(http.StatusOK)
}

// GetSet retrieves the whole set from Redis
func GetSet(setName string) ([]string, error) {
rdb := loadClient()

Expand All @@ -123,7 +167,6 @@ func GetSet(setName string) ([]string, error) {
return vals, nil
}

// GetSetMember retrieves a single member from Redis
func GetSetMember(setName string) (string, error) {
rdb := loadClient()

Expand All @@ -139,3 +182,35 @@ func GetSetMember(setName string) (string, error) {

return val, nil
}

func GetAllSets(c *gin.Context) {
rdb := loadClient()

ctx := c.Request.Context()
keysCmd := rdb.Keys(ctx, "*") // Get all keys matching the pattern "*"

keys, err := keysCmd.Result()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}

var sets []string
for _, key := range keys {
typeCmd := rdb.Type(ctx, key) // Get the type of the key

keyType, err := typeCmd.Result()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}

if keyType == "set" {
sets = append(sets, key)
}
}

c.JSON(http.StatusOK, sets)

return
}

0 comments on commit 2b9cf42

Please sign in to comment.