pythonnlprequestgoogle-cloud-vertex-aigoogle-gemini

How to make dynamic API calls based on user input in a Gemini application python nlp?


I'm working on a Gemini application where I need to make dynamic API calls based on user input. Specifically, I want to perform different API requests depending on the user's query. For example, if the user asks for the latest news, the application should make an API call to a news service. Similarly, if the user wants to know the current weather, the application should fetch data from a weather API.

Here's a basic outline of what I'm trying to achieve:

Capture the user's input. Determine the type of request based on the input (e.g., news or weather). Make the appropriate API call and return the data to the user. I want to avoid using third-party libraries like RAG for this purpose. How can I implement this functionality in a clean and efficient way within my Gemini application?

And i dont want to use a approach like this '

def handle_user_input(user_input):
if "news" in user_input:
    # Call news API
    pass
elif "weather" in user_input:
    # Call weather API
    pass
else:
    return "I can't handle that request."

Solution

  • I suggest using a dictionary, basically what you u are looking to do is something called a "strategy pattern". You want to choose a different "strategy" in your case api based on different input.

    so the way it will look is you have a dictionary {"news": "news-url", "weather": "weather-url"}

    and then your code will be very simple

    user_input = ""
    url = api_dict[user_input]
    #  make api call
    

    and at the start of your program you need to initialize your dict. having it in this structure makes it so that you could even write a json file, parse it and have it used as your dictionary meaning you dont even need to edit code to update your available API's

    At the end of the day when making a dynamic program, the options will always need to be inputted somehow, worst case is within the code like the if that you successfully understood is bad, as it makes the code long, unreadable and hard to modify.

    An improvement like I suggested is creating that dynamic options map as a file or a python dictionary on the start of the program and using that.

    the most dynamic option would be if the code is able to generate different responses to different input purely by the input. such an example would be gemini and chat-gpt where you can ask the some api route both "what is the weather in france" and "how much is 4 + 4" and it will provide a fitting answer (although not always correct)

    EDIT (based on last comment)

    Now I understand your question better. I cant say I understand your reasoning but the implementation is very simple, here is a chat I had with:

     chatGPT
    
    here are a few api routes
    http://test/weather
    http://test/stocks
    http://test/facts
    
    in the next message I will put user input and based on the input you need to output the appropriate url
    
    ChatGPT
    Got it! Please provide the user input in your next message, and I'll give you the appropriate URL.
    
    what should I wear tomorrow 
    ChatGPT
    For the query "what should I wear tomorrow," the appropriate URL is:
    
    http://test/weather
    

    you will need chatGPT to alaways know the context for your URLs, meaning always provide or from a longer session have it available.

    then you query with the user input and get your URL as a response

    key issues:

    1. chat gpt makes stuff up, it can easily make stuff up, best case it will provide no url result for such a case, but im sure some edge cases will make it generate a url that doesnt exist in your context
    2. expansive, using chat gpt to process user input simply to make a http request is expansive, it means a lot of requests, a lot of queries, a big context window (tokens) depending on your url count and input length. whatever you are building will be quite expansive to run