From c05af95c70d693e979eb9a936e2e69412f4f7349 Mon Sep 17 00:00:00 2001 From: JoyboyBrian Date: Tue, 31 Dec 2024 12:01:53 -0800 Subject: [PATCH] add action --- nexa/gguf/server/nexa_service.py | 65 ++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/nexa/gguf/server/nexa_service.py b/nexa/gguf/server/nexa_service.py index 878c309d..2678f229 100644 --- a/nexa/gguf/server/nexa_service.py +++ b/nexa/gguf/server/nexa_service.py @@ -230,6 +230,9 @@ class DownloadModelRequest(BaseModel): "protected_namespaces": () } +class ActionRequest(BaseModel): + prompt: str = "query_plane_ticket(year='2024', date='12-31', time='12:00', departure='SFO', destination='JFK')" + class StreamASRProcessor: def __init__(self, asr, task, language): self.asr = asr @@ -1424,6 +1427,68 @@ async def create_embedding(request: EmbeddingRequest): logging.error(f"Error in embedding generation: {e}") raise HTTPException(status_code=500, detail=str(e)) +@app.post("/v1/action", tags=["Actions"]) +async def action(request: ActionRequest): + try: + # Extract content between and + prompt = request.prompt + import re + + # Use regex to match pattern + match = re.match(r"(.*?)", prompt) + if not match: + raise ValueError("Invalid prompt format. Must be wrapped in and ") + + # Extract the function call content + function_content = match.group(1) + + # Parse function name and parameters + function_name = function_content[:function_content.index("(")] + params_str = function_content[function_content.index("(")+1:function_content.rindex(")")] + + # Parse parameters into dictionary + params = {} + for param in params_str.split(","): + if "=" in param: + key, value = param.split("=") + params[key.strip()] = value.strip().strip("'").strip('"') + + # Handle different function types + if function_name == "query_plane_ticket": + # Validate required parameters + required_params = ["year", "date", "time", "departure", "destination"] + for param in required_params: + if param not in params: + raise ValueError(f"Missing required parameter: {param}") + + # Construct the date string in required format + date_str = f"{params['date']}/{params['year']}" + + # Build the URL + url = (f"https://www.expedia.com/Flights-Search?" + f"leg1=from:{params['departure']},to:{params['destination']}," + f"departure:{date_str}T&" + f"passengers=adults:1&trip=oneway&mode=search") + + return { + "status": "success", + "function": function_name, + "parameters": params, + "url": url + } + else: + # Handle other function types in the future + return { + "status": "error", + "message": f"Unsupported function: {function_name}" + } + + except Exception as e: + return { + "status": "error", + "message": str(e) + } + if __name__ == "__main__": parser = argparse.ArgumentParser( description="Run the Nexa AI Text Generation Service"