Skip to content

Commit

Permalink
add multiple retrieval functions including a sorted list of IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
ili16 committed Mar 2, 2024
1 parent 3716c48 commit af6d695
Show file tree
Hide file tree
Showing 7 changed files with 298 additions and 51 deletions.
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ toolchain go1.21.5
require (
github.com/gin-gonic/gin v1.9.1
github.com/go-redis/redis/v8 v8.11.5
github.com/weaviate/weaviate v1.23.3
github.com/weaviate/weaviate v1.24.1
github.com/weaviate/weaviate-go-client/v4 v4.12.1
)

Expand Down Expand Up @@ -46,14 +46,15 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/oklog/ulid v1.3.1 // indirect
github.com/pelletier/go-toml/v2 v2.1.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
go.mongodb.org/mongo-driver v1.13.1 // indirect
golang.org/x/arch v0.7.0 // indirect
golang.org/x/crypto v0.18.0 // indirect
golang.org/x/crypto v0.19.0 // indirect
golang.org/x/net v0.20.0 // indirect
golang.org/x/oauth2 v0.16.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/sys v0.17.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240108191215-35c7eff3a6b1 // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ github.com/onsi/gomega v1.18.1 h1:M1GfJqGRrBrrGGsbxzV5dqM2U2ApXefZCQpkukxYRLE=
github.com/onsi/gomega v1.18.1/go.mod h1:0q+aL8jAiMXy9hbwj2mr5GziHiwhAIQpFmmtT5hitRs=
github.com/pelletier/go-toml/v2 v2.1.1 h1:LWAJwfNvjQZCFIDKWYQaM62NcYeYViCmWIwmOStowAI=
github.com/pelletier/go-toml/v2 v2.1.1/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
Expand All @@ -126,6 +128,8 @@ github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65E
github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
github.com/weaviate/weaviate v1.23.3 h1:9nbPTzwheh2XYk6hPCwCJnkuBDq5Ob/dXZWbBuLztaY=
github.com/weaviate/weaviate v1.23.3/go.mod h1:afludwbcyIZa9HEBELvHNb8zjH+KcjcW/jb4SZ5C2T4=
github.com/weaviate/weaviate v1.24.1 h1:Cl/NnqgFlNfyC7KcjFtETf1bwtTQPLF3oz5vavs+Jq0=
github.com/weaviate/weaviate v1.24.1/go.mod h1:wcg1vJgdIQL5MWBN+871DFJQa+nI2WzyXudmGjJ8cG4=
github.com/weaviate/weaviate-go-client/v4 v4.12.1 h1:XFKL49BgSOcxrFs5IV+Q5pydLTsh0HQHuWbKNSLMWLU=
github.com/weaviate/weaviate-go-client/v4 v4.12.1/go.mod h1:r1PlU5sAZKFvAPgymEHQj0hjSAuEV9X77PJ/ffZ6cEo=
github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=
Expand All @@ -143,6 +147,8 @@ golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5y
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.18.0 h1:PGVlW0xEltQnzFZ55hkuX5+KLyrMYhHld1YHO4AKcdc=
golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1mg=
golang.org/x/crypto v0.19.0 h1:ENy+Az/9Y1vSrlrvBSyna3PITt4tiZLf7sgCjZBX7Wo=
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
Expand All @@ -164,6 +170,8 @@ golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down
63 changes: 63 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ func main() {
})

router.GET("/weaviate/retrieveresponse", func(c *gin.Context) {

//TODO: allow user annotation

searchQuery := c.Query("query")
upvoteStr := c.Query("best")
best := upvoteStr == "true"
Expand Down Expand Up @@ -76,6 +79,66 @@ func main() {
c.JSON(http.StatusOK, response)
})

router.GET("/weaviate/retrieveresponselist", func(c *gin.Context) {
searchQuery := c.Query("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)

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

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

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

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)

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

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

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

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)

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

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

router.POST("/generate", func(c *gin.Context) {
var requestBody map[string]interface{}
if err := c.BindJSON(&requestBody); err != nil {
Expand Down
49 changes: 26 additions & 23 deletions ollama/ollama.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,58 +12,59 @@ import (
"os"
)

type ResponseData struct {
Response string `json:"response"`
PromptID string `json:"promptID"`
Instruct string `json:"instruct"`
}

func GenerateResponse(prompt map[string]interface{}) (ResponseData, error) {
func GenerateResponse(prompt map[string]interface{}) (weaviate.ResponseData, error) {
url := os.Getenv("OLLAMA_URL") + "/api/generate"

set, ok := prompt["instructType"].(string)
if !ok {
set = "default"
}

instruct, err := redis.GetSetMember(set)
if err != nil {
return ResponseData{}, errors.New("no data available")
instruct, ok := prompt["instruct"].(string)

log.Printf("ok: %v", ok)
if !ok {
instruct, _ = redis.GetSetMember(set)
}

log.Printf("Prompt: %s\n", instruct)

code, ok := prompt["prompt"].(string)
if !ok {
return ResponseData{}, errors.New("prompt field is not a string")
return weaviate.ResponseData{}, errors.New("prompt field is not a string")
}

log.Printf("Code: %s\n", code)

completePrompt := instruct + " " + code

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

requestBody := map[string]interface{}{
"model": prompt["model"],
"model": model,
"prompt": completePrompt,
"stream": false,
}

jsonData, err := json.Marshal(requestBody)
if err != nil {
return ResponseData{}, err
return weaviate.ResponseData{}, err
}

req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
if err != nil {
return ResponseData{}, err
return weaviate.ResponseData{}, err
}

req.Header.Set("Content-Type", "application/json")

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return ResponseData{}, err
return weaviate.ResponseData{}, err
}
defer func(Body io.ReadCloser) {
err := Body.Close()
Expand All @@ -74,39 +75,41 @@ func GenerateResponse(prompt map[string]interface{}) (ResponseData, error) {

body, err := io.ReadAll(resp.Body)
if err != nil {
return ResponseData{}, err
return weaviate.ResponseData{}, err
}

var responseJSON map[string]interface{}
err = json.Unmarshal(body, &responseJSON)
if err != nil {
return ResponseData{}, err
return weaviate.ResponseData{}, err
}

response, ok := responseJSON["response"].(string)
log.Printf("Reponse: %s\n", response)

if !ok {
log.Println("Error: 'response' field is not a string array")
return ResponseData{}, errors.New("invalid response format")
return weaviate.ResponseData{}, errors.New("invalid response format")
}

log.Printf("Reponse: %s\n", response)

PromptID, err := weaviate.CreatePromptObject(instruct, code, "Prompt")
if err != nil {
return ResponseData{}, err
return weaviate.ResponseData{}, err
}

log.Printf("PromptID: %s\n", PromptID)

ResponseID, err := weaviate.CreateResponseObject(response, "Response")
if err != nil {
return ResponseData{}, err
return weaviate.ResponseData{}, err
}

err = weaviate.CreateReferences(PromptID, ResponseID)
if err != nil {
panic(err)
}

responseData := ResponseData{
responseData := weaviate.ResponseData{
Response: response,
PromptID: PromptID,
Instruct: instruct,
Expand Down
3 changes: 2 additions & 1 deletion redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func InitRedis() {
members := []interface{}{
"Explain me this:",
"How does the following code work?",
"Explain me step by step how this code works:",
}

rdb.SAdd(ctx, "default", members...)
Expand Down Expand Up @@ -75,7 +76,7 @@ func AddInstruct(c *gin.Context) {
return
}

c.Status(http.StatusOK)
c.JSON(http.StatusOK, "added Item to list: "+listName)
}

func DeleteInstruct(c *gin.Context) {
Expand Down
27 changes: 25 additions & 2 deletions weaviate/weaviate.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,29 @@ import (
"strings"
)

type Position struct {
Line int `json:"line"`
Character int `json:"character"`
}

type Range struct {
Start Position `json:"start"`
End Position `json:"end"`
}

type ResponseData struct {
Response string `json:"response"`
PromptID string `json:"promptID"`
Instruct string `json:"instruct"`
}

type PromptProperties struct {
Code string `json:"code"`
HasResponse string `json:"hasResponse"`
Instruct string `json:"instruct"`
Rank int `json:"rank"`
}

func InitSchema() error {

client, err := loadClient()
Expand Down Expand Up @@ -135,14 +158,14 @@ func createClass(className, description, vectorizer string, properties []*models
return nil
}

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

dataSchema := map[string]interface{}{
"instruct": prompt,
"instruct": instruct,
"code": code,
"rank": 1,
}
Expand Down
Loading

0 comments on commit af6d695

Please sign in to comment.