Last active
October 29, 2023 07:10
-
-
Save ranfysvalle02/d214fe6b0abfeaf6d4c7cea352087153 to your computer and use it in GitHub Desktop.
ActionWeaver+MongoDB
This file contains hidden or 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 openai | |
from actionweaver.llms.openai.chat import OpenAIChatCompletion | |
from actionweaver import action, RequireNext | |
openai.api_key = "sk-test" #os.getenv("OPENAI_API_KEY") | |
from pymongo import MongoClient | |
connection_string="mongodb+srv://test:[email protected]/?retryWrites=true&w=majority" | |
mongodb_client = MongoClient(connection_string) | |
@action(name="query_airbnb",orch_expr=RequireNext(['query_airbnb'])) | |
def query_airbnb(bathrooms:int,bedrooms:int,country:str): | |
""" | |
Use this when the provided query asks for information about AirBnb listings | |
:input: An integer with the number of bathrooms referenced in the provided query, An integer with the number of bedrooms referenced in the provided query and a string with the desired Country | |
:return: A string with the results of the MongoDB query performed on the sample_airbnb collection | |
""" | |
print("BA="+str(bathrooms)) | |
print("BR="+str(bedrooms)) | |
client = mongodb_client | |
cursor = client['sample_airbnb']['listingsAndReviews'].find({'address.country':country,'bedrooms': bedrooms,'bathrooms':bathrooms}, {'listing_url': 1, '_id': 0, 'name':1, 'summary':1, 'address.country':1}).sort("name",-1).limit(5) | |
listings = [] | |
for listing in cursor: | |
print("<start-airbnb-listing>") | |
print(listing) | |
print("<end-airbnb-listing>") | |
listings.append(listing) | |
return f"Here are the results of from the sample_airbnb collection {listings}" | |
@action(name="query_movies",orch_expr=RequireNext(['query_movies'])) | |
def query_movies(yr:int,genre:str): | |
""" | |
Use this when the provided query asks for information about movies. | |
:input: An integer with the year referenced in the provided query, and a string with the desired genre | |
:return: A string with the results of the MongoDB query performed on the sample_mflix movies collection | |
""" | |
print("YR="+str(yr)) | |
print("GENRE="+str(genre)) | |
client = mongodb_client | |
cursor = client['sample_mflix']['movies'].find({'year': yr,'genres':genre}, {'title': 1, '_id': 0, 'year':1, 'genres':1, 'plot':1, 'rating':"$tomatoes.critic.rating"}).sort("rating",-1).limit(5) | |
movies = [] | |
for movie in cursor: | |
print("<start-movie>") | |
print(movie) | |
print("<end-movie>") | |
movies.append(movie) | |
return f"Here are the results of from the sample_mflix collection {movies}" | |
chat = OpenAIChatCompletion("gpt-3.5-turbo") | |
QUESTION1 = "What are some 'Animation' movies in 1996? Respond with a list including name, rating, title, year, and a short concise summary of the plot." | |
QUESTION2 = "What are some AirBnb listings in United States with 2 bedrooms and 3 bathrooms. Respond with a list in valid markdown with the most important details." | |
ANSWER1 = chat.create([{"role": "user", "content": QUESTION1}], actions = [query_movies,query_airbnb]) | |
ANSWER2 = chat.create([{"role": "user", "content": QUESTION2}], actions = [query_movies,query_airbnb]) | |
print(QUESTION1) | |
print(ANSWER1) | |
print(QUESTION2) | |
print(ANSWER2) |
Author
ranfysvalle02
commented
Oct 29, 2023
You can also do it all at once like this:
@action(name="query_airbnb",orch_expr=SelectOne(['query_airbnb']))
def query_airbnb(bathrooms:int,bedrooms:int,country:str):
"""
Use this when the provided query asks for information about AirBnb listings
:input: An integer with the number of bathrooms referenced in the provided query, An integer with the number of bedrooms referenced in the provided query and a string with the desired Country
:return: A string with the results of the MongoDB query performed on the sample_airbnb collection
"""
print("BA="+str(bathrooms))
print("BR="+str(bedrooms))
client = mongodb_client
cursor = client['sample_airbnb']['listingsAndReviews'].find({'address.country':country,'bedrooms': bedrooms,'bathrooms':bathrooms}, {'listing_url': 1, '_id': 0, 'name':1, 'summary':1, 'address.country':1}).sort("name",-1).limit(5)
movies = []
for movie in cursor:
print("<start-airbnb-listing>")
print(movie)
print("<end-airbnb-listing>")
movies.append(movie)
return f"Here are the results of from the sample_airbnb collection {movies}"
@action(name="query_movies",orch_expr=RequireNext(['query_movies','query_airbnb']))
def query_movies(yr:int,genre:str):
"""
Use this when the provided query asks for information about movies.
:input: An integer with the year referenced in the provided query, and a string with the desired genre
:return: A string with the results of the MongoDB query performed on the sample_mflix movies collection
"""
print("YR="+str(yr))
print("GENRE="+str(genre))
client = mongodb_client
cursor = client['sample_mflix']['movies'].find({'year': yr,'genres':genre}, {'title': 1, '_id': 0, 'year':1, 'genres':1, 'plot':1, 'rating':"$tomatoes.critic.rating"}).sort("rating",-1).limit(5)
movies = []
for movie in cursor:
print("<start-movie>")
print(movie)
print("<end-movie>")
movies.append(movie)
return f"Here are the results of from the sample_mflix collection {movies}"
@action(name="query_multi",orch_expr=SelectOne(['query_multi','query_movies']))
def query_multi(instruction:str)->str:
"""
Use this when you are asked a list of questions separated by a newline
Args:
instruction (str): The user's instruction
Returns:
str: The response to the user's question
"""
return f"Here is some context to answer:{instruction}"
chat = OpenAIChatCompletion("gpt-3.5-turbo")
QUESTION1 = "What are some 'Animation' movies in 1996? Respond with a list including name, rating, title, year, and a short concise summary of the plot."
QUESTION2 = "What are some AirBnb listings in United States with 2 bedrooms and 3 bathrooms. Respond with a list in valid markdown with the most important details."
ANSWER3 = chat.create([{"role": "user", "content": "Answer the below in valid markdown and list format with the most important details in each bullet in the bullet list. \n"+QUESTION1+"\n"+QUESTION2}],
actions = [query_multi,query_movies,query_airbnb])
print(ANSWER3)
OUTPUT
Here are some 'Animation' movies from 1996:
- **James and the Giant Peach**
- Rating: 7.2
- Year: 1996
- Plot: An orphan with terrible aunts for guardians befriends human-like bugs who live inside a giant peach. They take the boy on a journey to New York City.
- **The Land Before Time IV: Journey Through the Mists**
- Year: 1996
- Plot: Littlefoot's grandfather is dreadfully ill, and a golden flower is the only hope to cure him. But it lies within the land of the Mists, so Littlefoot and his friends embark on a journey to find it.
- **Beavis and Butt-Head Do America**
- Rating: 6.2
- Year: 1996
- Plot: The dimwitted teen duo of Beavis and Butt-Head travel across America in search of their stolen television set.
- **All Dogs Go to Heaven 2**
- Year: 1996
- Plot: Charlie and Itchy return to Earth to find Gabriel's Horn. Along the way, they meet a young boy named David who ran away from home.
- **The Hunchback of Notre Dame**
- Rating: 7.1
- Year: 1996
- Plot: A deformed bell-ringer must assert his independence from a vicious government minister in order to help his friend, a gypsy dancer.
Here are some AirBnb listings in the United States with 2 bedrooms and 3 bathrooms:
- [Villas Of Kamalii 20: Near Shops, Restaurants, And Beaches](https://www.airbnb.com/rooms/8104448)
- Summary: Beautiful two-story townhouse with a stunning interior, two master bedrooms, A/C in gated community. Pool, hot tub, bbq. Free midsize rental car.
- [Stunning Panoramic Ocean Views!](https://www.airbnb.com/rooms/6694877)
- Summary: When the Ocean Becomes Your Front Lawn..2 bedroom 3 bath Beauty with 180 degree ocean views.
- [Ocean & Beach View Condo WBR H204](https://www.airbnb.com/rooms/8386906)
- Summary: Ocean View Waipouli Beach Resort & Spa H204 is a 2 bedroom, 3 bathroom located in coveted Oceanfront H building, the closest building to the ocean. This Exquisite Luxury Condo offers stunning views from every room! Accommodates 6 guests comfortably! This condo has a fully equipped gourmet kitchen, central AC, and a washer/dryer inside for you! The price includes HI tax (14.42%), out clean, and resort fees!
- [Kapalua Ridge 511-13. Spacious villa offering one-of-a kind ocean, coastal, island AND golf views!](https://www.airbnb.com/rooms/18237857)
- Summary: Located in one of the most premier resorts in all Hawaii The Kapalua® Resort, this lovely two bedroom, three bath Ridge Villa offers guests luxury living accommodations at an affordable price. 13.416% HI state tax included in price.
- [Kapalua Bay Villa 20g2 Beachfront!](https://www.airbnb.com/rooms/6526827)
- Summary: This beachfront villa overlooks the pristine Oneloa Bay with views that will take your breath away. Two spacious master bedrooms each with its own en-suite offer luxurious comfort steps from the ocean.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment