Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add action #332

Merged
merged 2 commits into from
Dec 31, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions nexa/gguf/server/nexa_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,9 @@ class DownloadModelRequest(BaseModel):
"protected_namespaces": ()
}

class ActionRequest(BaseModel):
prompt: str = ""

class StreamASRProcessor:
def __init__(self, asr, task, language):
self.asr = asr
Expand Down Expand Up @@ -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 <nexa_X> and <nexa_end>
prompt = request.prompt
import re

# Use regex to match <nexa_X> pattern
match = re.match(r"<nexa_\d+>(.*?)<nexa_end>", prompt)
if not match:
raise ValueError("Invalid prompt format. Must be wrapped in <nexa_X> and <nexa_end>")

# 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"
Expand Down