-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e11d4c7
commit 8a76cbf
Showing
8 changed files
with
248 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../src/function_server.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import requests | ||
import json | ||
|
||
|
||
def execute_function_on_server(host: str, port: int, function_name: str, args: list, kwargs: dict, use_disk: bool): | ||
url = f"http://{host}:{port}/execute_function/" | ||
payload = { | ||
"function_name": function_name, | ||
"args": args, | ||
"kwargs": kwargs, | ||
"use_disk": use_disk, | ||
} | ||
response = requests.post(url, json=payload) | ||
if response.status_code == 200: | ||
return response.json() | ||
else: | ||
return {"error": response.json()["detail"]} | ||
|
||
|
||
def read_result_from_disk(file_path: str): | ||
with open(file_path, "r") as f: | ||
result = json.load(f) | ||
return result | ||
|
||
|
||
def function_client(host, port, function_name, args, kwargs, use_disk): | ||
execute_result = execute_function_on_server(host, port, function_name, args, kwargs, use_disk) | ||
if "error" in execute_result: | ||
print(f"Error: {execute_result['error']}") | ||
else: | ||
if use_disk: | ||
file_path = execute_result["file_path"] | ||
print(f"Result saved at: {file_path}") | ||
result_from_disk = read_result_from_disk(file_path) | ||
print("Result read from disk:", result_from_disk) | ||
else: | ||
print("Result received directly:", execute_result["result"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import os | ||
import sys | ||
import json | ||
import inspect | ||
import typing | ||
from traceback import print_exception | ||
from typing import Union | ||
|
||
from pydantic import BaseModel | ||
|
||
from fastapi import FastAPI, Header, HTTPException | ||
from fastapi.middleware.cors import CORSMiddleware | ||
from fastapi import Request, Depends | ||
from fastapi.responses import JSONResponse, Response, StreamingResponse | ||
from sse_starlette import EventSourceResponse | ||
from starlette.responses import PlainTextResponse | ||
|
||
sys.path.append('src') | ||
|
||
|
||
# similar to openai_server/server.py | ||
def verify_api_key(authorization: str = Header(None)) -> None: | ||
server_api_key = os.getenv('H2OGPT_OPENAI_API_KEY', 'EMPTY') | ||
if server_api_key == 'EMPTY': | ||
# dummy case since '' cannot be handled | ||
return | ||
if server_api_key and (authorization is None or authorization != f"Bearer {server_api_key}"): | ||
raise HTTPException(status_code=401, detail="Unauthorized") | ||
|
||
|
||
app = FastAPI() | ||
check_key = [Depends(verify_api_key)] | ||
app.add_middleware( | ||
CORSMiddleware, | ||
allow_origins=["*"], | ||
allow_credentials=True, | ||
allow_methods=["*"], | ||
allow_headers=["*"] | ||
) | ||
|
||
|
||
class InvalidRequestError(Exception): | ||
pass | ||
|
||
|
||
class FunctionRequest(BaseModel): | ||
function_name: str | ||
args: list | ||
kwargs: dict | ||
use_disk: bool = False | ||
|
||
|
||
# Example functions | ||
def example_function1(x, y): | ||
return x + y | ||
|
||
|
||
def example_function2(path: str): | ||
if not os.path.exists(path): | ||
raise ValueError("Path does not exist") | ||
if not os.path.isdir(path): | ||
raise ValueError("Path is not a directory") | ||
docs = [f for f in os.listdir(path) if f.endswith('.doc') or f.endswith('.docx')] | ||
return {"documents": docs} | ||
|
||
|
||
@app.get("/health") | ||
async def health() -> Response: | ||
"""Health check.""" | ||
return Response(status_code=200) | ||
|
||
|
||
@app.exception_handler(Exception) | ||
async def validation_exception_handler(request, exc): | ||
print_exception(exc) | ||
exc2 = InvalidRequestError(str(exc)) | ||
return PlainTextResponse(str(exc2), status_code=400) | ||
|
||
|
||
@app.options("/", dependencies=check_key) | ||
async def options_route(): | ||
return JSONResponse(content="OK") | ||
|
||
|
||
@app.post("/execute_function/") | ||
def execute_function(request: FunctionRequest): | ||
# Mapping of function names to function objects | ||
FUNCTIONS = { | ||
'example_function1': example_function1, | ||
'example_function2': example_function2, | ||
} | ||
try: | ||
# Fetch the function from the function map | ||
func = FUNCTIONS.get(request.function_name) | ||
if not func: | ||
raise ValueError("Function not found") | ||
|
||
# Call the function with args and kwargs | ||
result = func(*request.args, **request.kwargs) | ||
|
||
if request.use_disk: | ||
# Save the result to a file on the shared disk | ||
file_path = "/path/to/shared/disk/function_result.json" | ||
with open(file_path, "w") as f: | ||
json.dump(result, f) | ||
return {"status": "success", "file_path": file_path} | ||
else: | ||
# Return the result directly | ||
return {"status": "success", "result": result} | ||
except Exception as e: | ||
raise HTTPException(status_code=500, detail=str(e)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.