Skip to content

Commit

Permalink
Fix path
Browse files Browse the repository at this point in the history
  • Loading branch information
pseudotensor committed Oct 8, 2024
1 parent 01c7216 commit 0fce411
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 3 deletions.
4 changes: 2 additions & 2 deletions backend/function_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def call_function_server(host, port, function_name, args, kwargs, use_disk=False
if "error" in execute_result:
raise RuntimeError(execute_result['error'])
else:
if use_disk:
if use_disk or use_pickle:
file_path = execute_result["file_path"]
result_from_disk = read_result_from_disk(file_path, use_pickle, verbose=verbose)
return result_from_disk
Expand All @@ -65,7 +65,7 @@ def get_data_h2ogpt(file, file_type, file_path, verbose=False):
Simple function for Open Web UI
"""
function_server_host = os.getenv('H2OGPT_FUNCTION_SERVER_HOST', '0.0.0.0')
function_server_port = int(os.getenv('H2OGPT_FUNCTION_SERVER_PORT', '5003'))
function_server_port = int(os.getenv('H2OGPT_FUNCTION_SERVER_PORT', '5002'))
function_api_key = os.getenv('H2OGPT_FUNCTION_SERVER_API_KEY', 'EMPTY')

# could set other things:
Expand Down
86 changes: 86 additions & 0 deletions backend/open_webui/apps/retrieval/function_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import os
import pickle

import requests
import json


def execute_function_on_server(host: str, port: int, function_name: str, args: tuple, kwargs: dict, use_disk: bool,
use_pickle: bool, function_api_key: str):
url = f"http://{host}:{port}/execute_function/"
payload = {
"function_name": function_name,
"args": args,
"kwargs": kwargs,
"use_disk": use_disk,
"use_pickle": use_pickle,
}
headers = {
"Authorization": f"Bearer {function_api_key}"
}
response = requests.post(url, json=payload, headers=headers)
if response.status_code == 200:
return response.json()
else:
return {"error": response.json()["detail"]}


def read_result_from_disk(file_path: str, use_pickle: bool, verbose=False):
if verbose:
print(f"Size of {file_path} is {os.path.getsize(file_path)}")
try:
if use_pickle:
with open(file_path, "rb") as f:
result = pickle.load(f)
else:
with open(file_path, "r") as f:
result = json.load(f)
except Exception as e:
raise IOError(f"Error reading file {file_path}: {e}")
finally:
try:
os.remove(file_path)
except OSError as e:
print(f"Error deleting file {file_path}: {e}")
return result


def call_function_server(host, port, function_name, args, kwargs, use_disk=False, use_pickle=False,
function_api_key='EMPTY', verbose=False):
execute_result = execute_function_on_server(host, port, function_name, args, kwargs, use_disk, use_pickle,
function_api_key)
if "error" in execute_result:
raise RuntimeError(execute_result['error'])
else:
if use_disk or use_pickle:
file_path = execute_result["file_path"]
result_from_disk = read_result_from_disk(file_path, use_pickle, verbose=verbose)
return result_from_disk
else:
return execute_result["result"]


def get_data_h2ogpt(file, file_type, file_path, verbose=False):
"""
Simple function for Open Web UI
"""
function_server_host = os.getenv('H2OGPT_FUNCTION_SERVER_HOST', '0.0.0.0')
function_server_port = int(os.getenv('H2OGPT_FUNCTION_SERVER_PORT', '5002'))
function_api_key = os.getenv('H2OGPT_FUNCTION_SERVER_API_KEY', 'EMPTY')

# could set other things:
# https://github.com/h2oai/h2ogpt/blob/d2fa3d7ce507e8fb141c78ff92a83a8e27cf8b31/src/gpt_langchain.py#L9498
simple_kwargs = {}
function_name = 'path_to_docs'
use_disk = False
use_pickle = True
sources = call_function_server(function_server_host,
function_server_port,
function_name,
(file_path,),
simple_kwargs,
use_disk=use_disk, use_pickle=use_pickle,
function_api_key=function_api_key,
verbose=verbose)
known_type = len(sources) > 0
return sources, known_type
2 changes: 1 addition & 1 deletion backend/open_webui/apps/retrieval/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,7 @@ def process_file(
file_path = file.meta.get("path", None)
if file_path:
if os.getenv('H2OGPT_LOADERS'):
from backend.function_client import get_data_h2ogpt
from backend.open_webui.apps.retrieval.function_client import get_data_h2ogpt
docs, known_type = get_data_h2ogpt(file.filename, file.meta.get("content_type"), file_path)
else:
loader = Loader(
Expand Down

0 comments on commit 0fce411

Please sign in to comment.