aurelio-labs / semantic-router

Superfast AI decision making and intelligent processing of multi-modal data.
https://www.aurelio.ai/semantic-router
MIT License
1.74k stars 178 forks source link

Idea - Use OpenAI functions fine tunings for param parsing #115

Open hardcorebadger opened 6 months ago

hardcorebadger commented 6 months ago

Love the concept, but ran into issues with param parsing on dynamic routes with openAI, even with simple functions, when compared to running a completion on their functions fine tuning. There's likely an optimal way to parse function params using openAI's fine tuning and format, rather than having to prompt engineer 3.5-turbo into a JSON function call.

Obviously it's a bit less agnostic to foundational model, but could be dealt with via inheritance, and would make the "easy mode" work a lot better out of the box :)

bruvduroiu commented 4 months ago

Hi @hardcorebadger , thank you for raising this. We do something similar when using local models, by constraining output using GBNF.

For functions fine-tuning, is this example reflective of what you're referring to, where function signature is being passed to OpenAI as a Tool?

# Example dummy function hard coded to return the same weather
# In production, this could be your backend API or an external API
def get_current_weather(location, unit="fahrenheit"):
    """Get the current weather in a given location"""
    if "tokyo" in location.lower():
        return json.dumps({"location": "Tokyo", "temperature": "10", "unit": unit})
    elif "san francisco" in location.lower():
        return json.dumps({"location": "San Francisco", "temperature": "72", "unit": unit})
    elif "paris" in location.lower():
        return json.dumps({"location": "Paris", "temperature": "22", "unit": unit})
    else:
        return json.dumps({"location": location, "temperature": "unknown"})

def run_conversation():
    # Step 1: send the conversation and available functions to the model
    messages = [{"role": "user", "content": "What's the weather like in San Francisco, Tokyo, and Paris?"}]
    tools = [
        {
            "type": "function",
            "function": {
                "name": "get_current_weather",
                "description": "Get the current weather in a given location",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "location": {
                            "type": "string",
                            "description": "The city and state, e.g. San Francisco, CA",
                        },
                        "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
                    },
                    "required": ["location"],
                },
            },
        }
    ]
    response = client.chat.completions.create(
        model="gpt-3.5-turbo-0125",
        messages=messages,
        tools=tools,
        tool_choice="auto",  # auto is default, but we'll be explicit
    )
Siraj-Aizlewood commented 3 months ago

Looking into implementing this:

https://github.com/aurelio-labs/semantic-router/issues/115#issuecomment-2005441950

Siraj-Aizlewood commented 3 months ago

PR for this here: https://github.com/aurelio-labs/semantic-router/pull/258

hardcorebadger commented 2 months ago

Exciting stuff, will try it out!!