Skip to content

Commit

Permalink
change schema and add references to prompt class
Browse files Browse the repository at this point in the history
  • Loading branch information
ili16 committed Feb 15, 2024
1 parent 513da7b commit 594aec3
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 45 deletions.
46 changes: 46 additions & 0 deletions create_schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import weaviate
import os
import requests
import json
import os
import weaviate.classes as wvc

# Connect to a local Weaviate instance
client = weaviate.connect_to_custom(
http_host="192.168.10.165",
http_port=8080,
http_secure=False,
grpc_host="192.168.10.165",
grpc_port=50051,
grpc_secure=False,
auth_credentials=weaviate.auth.AuthApiKey(
os.getenv("WEAVIATE_KEY")
), # Set this environment variable
)

try:
# Wrap in try/finally to ensure client is closed gracefully
# ===== define collection =====
questions = client.collections.create(
name="JeopardyCategory",
)

client.collections.create(
name="JeopardyQuestion",
description="A Jeopardy! question",
properties=[
wvc.config.Property(name="question", data_type=wvc.config.DataType.TEXT),
wvc.config.Property(name="answer", data_type=wvc.config.DataType.TEXT),
],
references=[
wvc.config.ReferenceProperty(
name="hasCategory",
target_collection="JeopardyCategory"
)
]

)


finally:
client.close() # Close client gracefully
17 changes: 13 additions & 4 deletions ollama/ollama.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,15 @@ func CreateEmbedding(prompt string) ([]float32, error) {
}
}

return embeddingVector, nil
return nil, nil
}

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

//TODO add possibility to differentiate between system prompt roles/creativity
//TODO add routes to show and add prompts

var promptDB = []string{
"Explain me this:",
"How does the following code work?",
Expand All @@ -96,12 +99,13 @@ func GenerateResponse(prompt map[string]interface{}) (string, error) {
"Consider how this code could be adapted for different use cases.",
"Examine the impact of different input data on the code's behavior.",
"Evaluate the scalability of this code for large-scale applications.",
"How my WTFs per minute does this code generate?",
"How many WTFs per minute does this code generate?",
"Explain this code as if you were a wizard casting a spell.",
"Pretend you're a detective solving a mystery related to this code.",
"Imagine this code as a recipe for a bizarre culinary dish.",
"Describe this code using only emojis and internet slang.",
"Interpret this code through the lens of a conspiracy theorist uncovering hidden messages.",
"As the responsible senior engineer, what technical debt does this code have?",
}

source := rand.NewSource(time.Now().UnixNano())
Expand Down Expand Up @@ -178,7 +182,7 @@ func GenerateResponse(prompt map[string]interface{}) (string, error) {
return "", err
}

err = weaviate.CreatePromptObject(vector, chosenPrompt, code, "Prompt")
PromptID, err := weaviate.CreatePromptObject(vector, chosenPrompt, code, "Prompt")
if err != nil {
return "", err
}
Expand All @@ -188,10 +192,15 @@ func GenerateResponse(prompt map[string]interface{}) (string, error) {
return "", err
}

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

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

return response, nil
}
100 changes: 59 additions & 41 deletions weaviate/weaviate.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,31 +43,26 @@ func InitSchema() error {
return err
}

exists, err := client.Schema().ClassExistenceChecker().WithClassName("Prompt").Do(context.Background())
exists, err := client.Schema().ClassExistenceChecker().WithClassName("Response").Do(context.Background())
if err != nil {
return err
}

if !exists {
classObj := &models.Class{
Class: "Prompt",
Description: "This class holds information regarding the prompt, code and count of queries regarding ones codebase",
Class: "Response",
Description: "This class contains the responses to prompts",
Vectorizer: "none",
Properties: []*models.Property{
{
DataType: []string{"text"},
Description: "The specific instruct or question prepended to the code",
Name: "instruct",
},
{
DataType: []string{"text"},
Description: "The code which is targeted in the prompt",
Name: "code",
Description: "The generated response by the LLM",
Name: "response",
},
{
DataType: []string{"int"},
Description: "number of times that this particular code has been referenced",
Name: "count",
Description: "The relative rank for this response against other ones regarding the same code",
Name: "rank",
},
},
}
Expand All @@ -76,42 +71,46 @@ func InitSchema() error {
if err != nil {
return err
}
log.Println("created class")
log.Println("created Response class")
} else {
log.Println("class already exists")
log.Println("Response class already exists")
}

exists, err = client.Schema().ClassExistenceChecker().WithClassName("Response").Do(context.Background())
exists, err = client.Schema().ClassExistenceChecker().WithClassName("Prompt").Do(context.Background())
if err != nil {
return err
}

if !exists {
classObj := &models.Class{
Class: "Response",
Description: "This class contains the responses to prompts",
Vectorizer: "none",
Class: "Prompt",
Description: "This class holds information regarding the prompt, code and count of queries regarding ones codebase",
Properties: []*models.Property{
{
DataType: []string{"text"},
Description: "The generated response by the LLM",
Name: "response",
Description: "The specific instruct or question prepended to the code",
Name: "instruct",
},
{
DataType: []string{"int"},
Description: "The relative rank for this response against other ones regarding the same code",
Name: "rank",
DataType: []string{"text"},
Description: "The code which is targeted in the prompt",
Name: "code",
},
{
DataType: []string{"Response"},
Name: "hasResponse",
},
},
Vectorizer: "none",
}

err = client.Schema().ClassCreator().WithClass(classObj).Do(context.Background())
if err != nil {
return err
}
log.Println("created class")
log.Println("created Prompt class")
} else {
log.Println("class already exists")
log.Println("Prompt class already exists")
}

return nil
Expand Down Expand Up @@ -140,52 +139,51 @@ func createClass(className, description, vectorizer string, properties []*models
return nil
}

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

dataSchema := map[string]interface{}{
strings.ToLower(class): prompt,
"code": code,
"count": 1,
"instruct": prompt,
"code": code,
}

_, err = client.Data().Creator().
weaviateObject, err := client.Data().Creator().
WithClassName(class).
WithProperties(dataSchema).
WithVector(vector).
Do(context.Background())

if err != nil {
return err
return "", err
}

return nil
return string(weaviateObject.Object.ID), nil
}

func CreateResponseObject(vector []float32, response string, class string) error {
func CreateResponseObject(vector []float32, response string, class string) (string, error) {
client, err := loadClient()
if err != nil {
return err
return "", err
}

dataSchema := map[string]interface{}{
strings.ToLower(class): response,
"rank": 1,
}

_, err = client.Data().Creator().
weaviateObject, err := client.Data().Creator().
WithClassName(class).
WithProperties(dataSchema).
WithVector(vector).
Do(context.Background())

if err != nil {
return err
return "", err
}

return nil
return string(weaviateObject.Object.ID), nil
}

func RetrievePromptCount(code string) (int, error) {
Expand Down Expand Up @@ -219,8 +217,6 @@ func RetrievePromptCount(code string) (int, error) {
return 0, errors.New(result.Errors[0].Message)
}

log.Printf("%v", result)

getPrompt, ok := result.Data["Aggregate"].(map[string]interface{})
if !ok {
return 0, errors.New("unexpected response format: 'Aggregate' field not found or not a map")
Expand Down Expand Up @@ -290,3 +286,25 @@ func loadClient() (*weaviate.Client, error) {
}
return client, nil
}

func CreateReferences(PromptID string, ResponseID string) error {
client, err := loadClient()
if err != nil {
return err
}

err = client.Data().ReferenceCreator().
WithClassName("Prompt").
WithID(PromptID).
WithReferenceProperty("hasResponse").
WithReference(client.Data().ReferencePayloadBuilder().
WithClassName("Response").
WithID(ResponseID).
Payload()).
Do(context.Background())
if err != nil {
return err
}

return nil
}

0 comments on commit 594aec3

Please sign in to comment.