Skip to content

Commit

Permalink
Error handle function calls
Browse files Browse the repository at this point in the history
  • Loading branch information
Elehiggle committed May 24, 2024
1 parent 6f3f3eb commit 293c75f
Showing 1 changed file with 29 additions and 19 deletions.
48 changes: 29 additions & 19 deletions chatbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,48 +445,42 @@ def handle_text_generation(current_message, messages, channel_id, root_id):
raise Exception("Maximum amount of function calls reached")

if call.function.name == "get_stock_ticker_data":
arguments = json.loads(call.function.arguments)
ticket_symbol = arguments["ticker_symbol"]
stock_data = get_stock_ticker_data(ticket_symbol)
data = wrapper_function_call(get_stock_ticker_data, call.function.arguments)
func_response = {
"tool_call_id": call.id,
"role": "tool",
"name": call.function.name,
"content": json.dumps(stock_data),
"content": json.dumps(data),
}

tool_messages.append(func_response)
elif call.function.name == "get_cryptocurrency_data_by_market_cap":
arguments = json.loads(call.function.arguments)
num_currencies = arguments["num_currencies"] if "num_currencies" in arguments else 15
crypto_data = get_cryptocurrency_data_by_market_cap(num_currencies)
data = wrapper_function_call(get_cryptocurrency_data_by_market_cap, call.function.arguments)
func_response = {
"tool_call_id": call.id,
"role": "tool",
"name": call.function.name,
"content": json.dumps(crypto_data),
"content": json.dumps(data),
}

tool_messages.append(func_response)
elif call.function.name == "get_cryptocurrency_data_by_id":
arguments = json.loads(call.function.arguments)
crypto_id = arguments["crypto_id"]
crypto_data = get_cryptocurrency_data_by_id(crypto_id)
data = wrapper_function_call(get_cryptocurrency_data_by_id, call.function.arguments)
func_response = {
"tool_call_id": call.id,
"role": "tool",
"name": call.function.name,
"content": json.dumps(crypto_data),
"content": json.dumps(data),
}

tool_messages.append(func_response)
elif call.function.name == "get_exchange_rates":
exchange_rates = get_exchange_rates()
data = wrapper_function_call(get_exchange_rates, call.function.arguments)
func_response = {
"tool_call_id": call.id,
"role": "tool",
"name": call.function.name,
"content": json.dumps(exchange_rates),
"content": json.dumps(data),
}

tool_messages.append(func_response)
Expand Down Expand Up @@ -567,8 +561,21 @@ def handle_generation(current_message, messages, channel_id, root_id):
driver.posts.create_post({"channel_id": channel_id, "message": f"Error occurred: {str(e)}", "root_id": root_id})


def wrapper_function_call(func, call_input_arguments, *args, **kwargs):
try:
result = func(call_input_arguments, *args, **kwargs)
except Exception as e:
logger.error(f"Error calling function call function: {str(e)} {traceback.format_exc()}")
result = f"An error occurred: {str(e)}"
return result
return result


@timed_lru_cache(seconds=300, maxsize=100)
def get_stock_ticker_data(ticker_symbol):
def get_stock_ticker_data(arguments):
arguments = json.loads(arguments)
ticker_symbol = arguments["ticker_symbol"]

stock = yfinance.Ticker(ticker_symbol)

stock_data = {
Expand All @@ -586,7 +593,7 @@ def get_stock_ticker_data(ticker_symbol):


@timed_lru_cache(seconds=7200, maxsize=100)
def get_exchange_rates():
def get_exchange_rates(_arguments):
ecb_url = "https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml"

with httpx.Client() as client:
Expand All @@ -608,7 +615,9 @@ def get_exchange_rates():


@timed_lru_cache(seconds=180, maxsize=100)
def get_cryptocurrency_data_by_market_cap(num_currencies):
def get_cryptocurrency_data_by_market_cap(arguments):
arguments = json.loads(arguments)
num_currencies = arguments["num_currencies"] if "num_currencies" in arguments else 15
num_currencies = min(num_currencies, 20) # Limit to 20

url = "https://api.coingecko.com/api/v3/coins/markets" # possible alternatives: coincap.io, mobula.io
Expand All @@ -630,8 +639,9 @@ def get_cryptocurrency_data_by_market_cap(num_currencies):


@timed_lru_cache(seconds=180, maxsize=100)
def get_cryptocurrency_data_by_id(crypto_id):
crypto_id = crypto_id.lower()
def get_cryptocurrency_data_by_id(arguments):
arguments = json.loads(arguments)
crypto_id = arguments["crypto_id"].lower()

url = "https://api.coingecko.com/api/v3/coins/markets"
params = {
Expand Down

0 comments on commit 293c75f

Please sign in to comment.