web-ui
web-ui copied to clipboard
我的模型API地址和框架设置的不一样,如何在请求大模型时织入一个自定义的路由转发器?
我的模型API访问地址url后缀是/v1/completions但是框架自动拼接的是/chat/completions 因此我需要将/chat/completions转发到/v1/completions以避免连接异常. 我目前暂不想用修改本地库文件的方式来实现这个目标.
之前我debug时确认本项目使用httpx发起请求,于是尝试重写了main方法,在其中织入替换异步请求和同步请求方法的代码:
# 定义自定义 Transport
class RedirectTransport(httpx.AsyncBaseTransport):
def __init__(self, next_transport=None):
self.next_transport = next_transport or httpx.AsyncHTTPTransport()
async def handle_async_request(self, request: httpx.Request) -> httpx.Response:
# 检查并重定向 URL
if request.url.path == "/v1/completions/chat/completions":
new_url = request.url.copy_with(path="/v1/completions")
request.url = new_url
return await self.next_transport.handle_async_request(request)
def main():
parser = argparse.ArgumentParser(description="Gradio UI for Browser Agent")
parser.add_argument("--ip", type=str, default="127.0.0.1", help="IP address to bind to")
parser.add_argument("--port", type=int, default=7788, help="Port to listen on")
parser.add_argument("--theme", type=str, default="Ocean", choices=theme_map.keys(), help="Theme to use for the UI")
parser.add_argument("--dark-mode", action="store_true", help="Enable dark mode")
args = parser.parse_args()
config_dict = default_config()
pydevd_pycharm.settrace('localhost', port=6789, stdoutToServer=True, stderrToServer=True)
demo = create_ui(config_dict, theme_name=args.theme)
# 替换全局 httpx异步请求客户端
original_async_client = httpx.AsyncClient
httpx.AsyncClient = lambda *args, **kwargs: original_async_client(
transport=RedirectTransport(), *args, **kwargs)
# 保存原始的 httpx.Client.request 方法
original_request = httpx.Client.request
# 自定义请求逻辑
def custom_request(self, method, url, *args, **kwargs):
# 打印原始请求 URL
print(f"Original URL: {url}")
# 替换 URL 的逻辑
if "/v1/completions/chat/completions" in url:
url = url.replace("/v1/completions/chat/completions", "/v1/completions")
print(f"Redirected to: {url}")
# 调用原始的请求逻辑
return original_request(self, method, url, *args, **kwargs)
# 替换 httpx.Client.request
httpx.Client.request = custom_request
demo.launch(server_name=args.ip, server_port=args.port, share=False,
allowed_paths=['D:\\workboo\\web-ui'])
这个替换方式对于httpx请求是确认生效的 但是在服务器运行时仍然未生效.
请问我该如何解决此问题?