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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can also do it all at once like this:
OUTPUT