Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
motica1 authored Sep 26, 2024
1 parent 46a5545 commit a5e2cc6
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 51 deletions.
125 changes: 74 additions & 51 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
- get_showtimes(): For movie times at specific locations.
- buy_ticket(): To assist with ticket purchases.
- get_reviews(): For recent reviews or audience reactions.
- confirm_ticket_purchase(): Confirm with the user, before making the purchase of the ticket.
3. Interaction: Be clear and concise. Ask for clarification if needed. Keep a friendly and helpful tone.
"""
Expand Down Expand Up @@ -59,63 +60,85 @@ async def generate_response(client, message_history, gen_kwargs):
async def on_message(message: cl.Message):
message_history = cl.user_session.get("message_history", [])
message_history.append({"role": "user", "content": message.content})


# Decide if we need to call a function for more context
function_response = None
functions = [
{
"name": "get_now_playing_movies",
"description": "Get a list of movies currently playing in theaters"
},
{
"name": "get_showtimes",
"description": "Get showtimes for a specific movie at a given location"
},
{
"name": "buy_ticket",
"description": "Purchase a ticket for a specific movie showing"
},
{
"name": "get_reviews",
"description": "Get recent reviews for a specific movie"
}
]

function_call_response = await client.chat.completions.create(
model="gpt-4",
messages=message_history,
functions=functions,
function_call="auto"
)

response_choice = function_call_response.choices[0]

if response_choice.message.function_call:
function_name = response_choice.message.function_call.name
function_args = json.loads(response_choice.message.function_call.arguments)
while True:
functions = [
{
"name": "get_now_playing_movies",
"description": "Get a list of movies currently playing in theaters"
},
{
"name": "get_showtimes",
"description": "Get showtimes for a specific movie at a given location"
},
{
"name": "buy_ticket",
"description": "Purchase a ticket for a specific movie showing"
},
{
"name": "get_reviews",
"description": "Get recent reviews for a specific movie"
},
{
"name":"confirm_ticket_purchase",
"description":"Confirm the details with the user before purchasing the ticket"
}
]

if hasattr(movie_functions, function_name):
function_to_call = getattr(movie_functions, function_name)
try:
function_call_response = await client.chat.completions.create(
model="gpt-4",
messages=message_history,
functions=functions,
function_call="auto"
)

# Adjust function arguments based on the actual function signature
expected_args = inspect.signature(function_to_call).parameters.keys()
adjusted_args = {k: v for k, v in function_args.items() if k in expected_args}
response_choice = function_call_response.choices[0]

if not response_choice.message.function_call:
break

function_name = response_choice.message.function_call.name
function_args = json.loads(response_choice.message.function_call.arguments)

# Check if the function is a coroutine
try:
if inspect.iscoroutinefunction(function_to_call):
function_response = await function_to_call(**adjusted_args)
else:
# Run synchronous functions in a thread pool
function_response = await asyncio.to_thread(function_to_call, **adjusted_args)
except Exception as e:
function_response = f"Error occurred while calling {function_name}: {str(e)}"
if hasattr(movie_functions, function_name):
function_to_call = getattr(movie_functions, function_name)

expected_args = inspect.signature(function_to_call).parameters.keys()
adjusted_args = {k: v for k, v in function_args.items() if k in expected_args}

try:
if inspect.iscoroutinefunction(function_to_call):
function_response = await function_to_call(**adjusted_args)
else:
function_response = function_to_call(**adjusted_args)

message_history.append({
"role": "function",
"name": function_name,
"content": function_response
})
except Exception as e:
error_message = f"Error calling function {function_name}: {str(e)}"
message_history.append({
"role": "function",
"name": function_name,
"content": error_message
})
else:
error_message = f"Function {function_name} not found"
message_history.append({
"role": "function",
"name": function_name,
"content": error_message
})
except Exception as e:
error_message = f"Error in function call loop: {str(e)}"
message_history.append({
"role": "function",
"name": function_name,
"content": function_response
"role": "system",
"content": error_message
})
break

response_message = await generate_response(client, message_history, gen_kwargs)

Expand Down
8 changes: 8 additions & 0 deletions movie_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ def get_showtimes(title, location):
def buy_ticket(theater, movie, showtime):
return f"Ticket purchased for {movie} at {theater} for {showtime}."

def confirm_ticket_purchase(theater, movie, showtime):
confirmation_message = f"Please confirm the following ticket purchase details:\n\n" \
f"Theater: {theater}\n" \
f"Movie: {movie}\n" \
f"Showtime: {showtime}\n\n" \
f"Is this information correct? (Yes/No)"
return confirmation_message

def get_reviews(movie_id):
url = f"https://api.themoviedb.org/3/movie/{movie_id}/reviews?language=en-US&page=1"
headers = {
Expand Down

0 comments on commit a5e2cc6

Please sign in to comment.