Created
May 15, 2023 00:20
-
-
Save aarushik93/65318d6bbb6b2642001cad77f80e59b0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import weaviate | |
import csv | |
import openai | |
from weaviate.util import generate_uuid5, get_valid_uuid | |
from uuid import uuid4 | |
OPENAI_API_KEY = "YOUR KEY" | |
WEAVIATE_URL = "YOUR URL" | |
openai.api_key = "YOUR KEY" | |
client = weaviate.Client( | |
url=WEAVIATE_URL, | |
additional_headers={ | |
'X-OpenAI-Api-Key': OPENAI_API_KEY, | |
'Authorization': 'Bearer "TOKEN"' | |
} | |
) | |
client.get_meta() | |
def convert_to_boolean(value): | |
if value.lower() == "yes": | |
return True | |
elif value.lower() == "no": | |
return False | |
else: | |
return None | |
def create_schemas(): | |
csv_file = 'zomato.csv' | |
list_of_dicts = [] | |
with open(csv_file, mode='r', encoding='utf-8') as csvfile: | |
reader = csv.DictReader(csvfile) | |
for row in reader: | |
row['Has Table booking'] = convert_to_boolean(row['Has Table booking']) | |
row['Has Online delivery'] = convert_to_boolean(row['Has Online delivery']) | |
row['Is delivering now'] = convert_to_boolean(row['Is delivering now']) | |
list_of_dicts.append(row) | |
client.schema.delete_all() | |
Restaurant = { | |
"classes": [ | |
{ | |
"class": "Restaurant", | |
"description": "An MunchMate Restaurant.", | |
"moduleConfig": { | |
"text2vec-openai": { | |
"skip": False, | |
"vectorizeClassName": False, | |
"vectorizePropertyName": False | |
} | |
}, | |
"vectorIndexType": "hnsw", | |
"vectorizer": "text2vec-openai", | |
"properties": [ | |
{ | |
"name": "description", | |
"dataType": ["text"], | |
"description": "The general description written by an LLM.", | |
"moduleConfig": { | |
"text2vec-openai": { | |
"skip": False, | |
"vectorizePropertyName": False, | |
"vectorizeClassName": False | |
} | |
} | |
}, | |
{ | |
"name": "restaurantName", | |
"dataType": ["string"], | |
"description": "The restaurant name", | |
"moduleConfig": { | |
"text2vec-openai": { | |
"skip": True, | |
"vectorizeClassName": False, | |
"vectorizePropertyName": False | |
} | |
} | |
}, | |
{ | |
"name": "city", | |
"dataType": ["string"], | |
"moduleConfig": { | |
"text2vec-openai": { | |
"skip": True, | |
"vectorizeClassName": False, | |
"vectorizePropertyName": False | |
} | |
} | |
}, | |
{ | |
"name": "address", | |
"dataType": ["string"], | |
"moduleConfig": { | |
"text2vec-openai": { | |
"skip": True, | |
"vectorizeClassName": False, | |
"vectorizePropertyName": False | |
} | |
} | |
}, | |
{ | |
"name": "locality", | |
"dataType": ["string"], | |
"moduleConfig": { | |
"text2vec-openai": { | |
"skip": True, | |
"vectorizeClassName": False, | |
"vectorizePropertyName": False | |
} | |
} | |
}, | |
{ | |
"name": "cuisines", | |
"dataType": ["string"], | |
"description": "The restaurant cuisines", | |
"moduleConfig": { | |
"text2vec-openai": { | |
"skip": True, | |
"vectorizeClassName": False, | |
"vectorizePropertyName": False | |
} | |
} | |
}, | |
{ | |
"name": "averageCostForTwo", | |
"dataType": ["int"], | |
"description": "The average cost for two people" | |
}, | |
{ | |
"name": "hasTableBooking", | |
"dataType": ["boolean"], | |
"description": "The table booking policy" | |
}, | |
{ | |
"name": "hasOnlineDelivery", | |
"dataType": ["boolean"], | |
"description": "The restaurants online delivery policy", | |
}, | |
{ | |
"name": "isDeliveringNow", | |
"dataType": ["boolean"], | |
"description": "If the restaurant is delivering right now", | |
"moduleConfig": { | |
"text2vec-openai": { | |
"skip": True, | |
"vectorizeClassName": False, | |
"vectorizePropertyName": False | |
} | |
} | |
}, | |
{ | |
"name": "priceRange", | |
"dataType": ["int"] | |
}, | |
{ | |
"name": "ratingText", | |
"dataType": ["string"] | |
} | |
] | |
} | |
] | |
} | |
client.schema.create(Restaurant) | |
client.batch.configure( | |
batch_size=5, | |
dynamic=True, | |
timeout_retries=3, | |
callback=None, | |
) | |
for obj in list_of_dicts: | |
data_properties = {} | |
data_properties["restaurantName"] = obj["Restaurant Name"] | |
data_properties["city"] = obj["City"] | |
data_properties["address"] = obj["Address"] | |
data_properties["locality"] = obj["Locality"] | |
data_properties["cuisines"] = obj["Cuisines"] | |
data_properties["averageCostForTwo"] = int(obj["Average Cost for two"]) | |
data_properties["hasTableBooking"] = obj["Has Table booking"] | |
data_properties["hasOnlineDelivery"] = obj["Has Online delivery"] | |
data_properties["isDeliveringNow"] = obj["Is delivering now"] | |
data_properties["priceRange"] = int(obj["Price range"]) | |
data_properties["ratingText"] = obj["Rating text"] | |
new_id = get_valid_uuid(uuid4()) | |
client.batch.add_data_object(data_properties, "Restaurant", uuid=new_id) | |
print("finished adding: ", data_properties["restaurantName"]) | |
print(client.query.get("Restaurant", ["restaurantName"]).do()) | |
push_schema = { | |
"classes": [ | |
{ | |
"class": "Push", | |
"description": "A push for MunchMate app.", | |
"properties": [ | |
{ | |
"dataType": ["text"], | |
"name": "content", | |
"description": "The push copy." | |
} | |
] | |
} | |
] | |
} | |
client.schema.create(push_schema) | |
push_hasPush_cref = { | |
"dataType": ["Push"], | |
"description": "The push for a restaurant", | |
"name": "hasPush" | |
} | |
client.schema.property.create("Restaurant", push_hasPush_cref) | |
def create_user(): | |
user_schema = { | |
"classes": [ | |
{ | |
"class": "User", | |
"description": "A user.", | |
"properties": [ | |
{ | |
"dataType": ["text"], | |
"name": "biography", | |
}, | |
{ | |
"dataType": ["text"], | |
"name": "name" | |
} | |
] | |
} | |
] | |
} | |
client.schema.create(user_schema) | |
new_user_id = get_valid_uuid(uuid4()) | |
user_properties = { | |
"biography": "Alex enjoys Mexican food, hates valentines day, loves the superbowl", | |
"name": "Alex" | |
} | |
client.data_object.create( | |
data_object=user_properties, | |
class_name="User", | |
uuid=new_user_id | |
) | |
new_user_id = get_valid_uuid(uuid4()) | |
user_properties = { | |
"biography": "Alice is a vegetarian, woman, loves Valentines day and the superbowl.", | |
"name": "Alice" | |
} | |
client.data_object.create( | |
data_object=user_properties, | |
class_name="User", | |
uuid=new_user_id | |
) | |
user_hasPushTarget_cref = { | |
"dataType": ["User"], | |
"description": "The push for a particular user.", | |
"name": "hasUserTarget" | |
} | |
client.schema.property.create("Push", user_hasPushTarget_cref) | |
def generate_description(): | |
generatePrompt = """ | |
Create a description, for a restaurant with the following details: | |
Restaurant Name: {restaurantName} | |
Cuisines: {cuisines} | |
Delivering now: {isDeliveringNow} | |
Table Booking Offered: {hasTableBooking} | |
City: {city} | |
Stick to the information provided. Do not make up any information about the restaurant in your description. | |
""" | |
generate_properties = ["restaurantName", "cuisines", "isDeliveringNow", "hasTableBooking", "city"] | |
# res = restaurants = client.query.get("Restaurant", generate_properties).with_additional('id').with_limit(5).do() | |
# print(res) | |
restaurants = \ | |
client.query.get("Restaurant", generate_properties).with_additional('id').with_limit(10).do()["data"]["Get"][ | |
"Restaurant"] | |
descriptions = [] | |
for restaurant in restaurants: | |
prompt = generatePrompt.format( | |
restaurantName=restaurant["restaurantName"], | |
cuisines=restaurant["cuisines"], | |
isDeliveringNow=restaurant["isDeliveringNow"], | |
hasTableBooking=restaurant["hasTableBooking"], | |
city=restaurant["city"] | |
) | |
response = openai.Completion.create( | |
engine="text-davinci-003", | |
prompt=prompt, | |
max_tokens=200, | |
n=1, | |
stop=None, | |
temperature=0.8, | |
) | |
description = response.choices[0].text.strip() | |
descriptions.append(description) | |
# | |
for i, restaurant in enumerate(restaurants): | |
update = { | |
"description": descriptions[i] | |
} | |
client.data_object.update(update, class_name="Restaurant", uuid=restaurant['_additional']['id']) | |
def generate_push(): | |
generatePrompt = """ | |
Write a tempting, short push notification, with a short heading for the following new Restaurant: | |
Description: {description}. | |
Do not make up any information in the push notification | |
Style: | |
Heading: | |
Content: | |
""" | |
generate_properties = ["description", "city"] | |
restaurants = client.query \ | |
.get("Restaurant", generate_properties) \ | |
.with_additional(["id"]) \ | |
.with_limit(5) \ | |
.do()["data"]["Get"]["Restaurant"] | |
pushes = [] | |
for restaurant in restaurants: | |
prompt = generatePrompt.format( | |
description=restaurant["description"], | |
city=restaurant["city"] | |
) | |
response = openai.Completion.create( | |
engine="text-davinci-003", | |
prompt=prompt, | |
max_tokens=200, | |
n=1, | |
stop=None, | |
temperature=0.8, | |
) | |
push_content = response.choices[0].text.strip() | |
new_push_properties = { | |
"content": push_content | |
} | |
new_push_id = get_valid_uuid(uuid4()) | |
client.data_object.create( | |
data_object=new_push_properties, | |
class_name="Push", | |
uuid=new_push_id | |
) | |
client.data_object.reference.add( | |
from_uuid=restaurant["_additional"]["id"], | |
from_property_name="hasPush", | |
to_uuid=new_push_id | |
) | |
def generate_push_for_events(): | |
events = ["valentines day", "super bowl", "international women's day"] | |
for e in events: | |
generatePrompt = """ | |
Write a tempting, short push notification, with a short heading for the following new Restaurant: | |
Description: {description}. | |
Do not make up any information in the push notification | |
Target the push towards: {event} | |
Style: | |
Heading: | |
Content: | |
""" | |
restaurants = client.query \ | |
.get("Restaurant", ["description"]) \ | |
.with_additional(["id"]) \ | |
.with_limit(10) \ | |
.do()["data"]["Get"]["Restaurant"] | |
for restaurant in restaurants: | |
prompt = generatePrompt.format( | |
description=restaurant["description"], | |
event=e | |
) | |
response = openai.Completion.create( | |
engine="text-davinci-003", | |
prompt=prompt, | |
max_tokens=200, | |
n=1, | |
stop=None, | |
temperature=0.8, | |
) | |
push_content = response.choices[0].text.strip() | |
print(push_content) | |
new_push_properties = { | |
"content": push_content, | |
"event": e | |
} | |
new_push_id = get_valid_uuid(uuid4()) | |
client.data_object.create( | |
data_object=new_push_properties, | |
class_name="Push", | |
uuid=new_push_id | |
) | |
client.data_object.reference.add( | |
from_uuid=restaurant["_additional"]["id"], | |
from_property_name="hasPush", | |
to_uuid=new_push_id | |
) | |
def generate_push_for_person(): | |
events = ["valentines day", "super bowl", "international women's day"] | |
users = client.query \ | |
.get("User", ["biography"]) \ | |
.with_additional(["id"]) \ | |
.with_limit(5) \ | |
.do()["data"]["Get"]["User"] | |
for e in events: | |
for u in users: | |
generatePrompt = """ | |
Write a short, targeted, clever, punchy push notification, with a short heading for the following new Restaurant: | |
Description: {description}. | |
The push notification should appeal to this user, based on their biography, likes and dislikes: {person}. | |
Base the push notification on today's event: {event}. | |
Use their name. | |
Do not make up any information in the push notification. | |
Style: | |
Heading: | |
Content: | |
""" | |
restaurants = client.query \ | |
.get("Restaurant", ["description"]) \ | |
.with_additional(["id"]) \ | |
.with_limit(10) \ | |
.do()["data"]["Get"]["Restaurant"] | |
for restaurant in restaurants: | |
prompt = generatePrompt.format( | |
description=restaurant["description"], | |
event=e, | |
person=u["biography"] | |
) | |
response = openai.Completion.create( | |
engine="text-davinci-003", | |
prompt=prompt, | |
max_tokens=200, | |
n=1, | |
stop=None, | |
temperature=0.8, | |
) | |
push_content = response.choices[0].text.strip() | |
new_push_properties = { | |
"content": push_content, | |
"event": e, | |
"user": u["biography"] | |
} | |
new_push_id = get_valid_uuid(uuid4()) | |
client.data_object.create( | |
data_object=new_push_properties, | |
class_name="Push", | |
uuid=new_push_id | |
) | |
client.data_object.reference.add( | |
from_uuid=restaurant["_additional"]["id"], | |
from_property_name="hasPush", | |
to_uuid=new_push_id | |
) | |
# create_schemas() | |
# | |
# create_user() | |
# | |
# generate_description() | |
# | |
# generate_push() | |
# | |
# generate_push_for_events() | |
# | |
# generate_push_for_person() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment