From 1deff36f0cd8243836e5e1966b7813c8d2db86e5 Mon Sep 17 00:00:00 2001 From: rapsealk Date: Tue, 15 Oct 2024 16:13:10 +0900 Subject: [PATCH 1/3] refactor: Use `yarl` for safe URL manipulation --- src/llmperf/models.py | 2 +- src/llmperf/ray_clients/openai_chat_completions_client.py | 8 ++------ 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/src/llmperf/models.py b/src/llmperf/models.py index be0d7ea..0c2e1b5 100644 --- a/src/llmperf/models.py +++ b/src/llmperf/models.py @@ -1,4 +1,4 @@ -from typing import Any, Dict, List, Optional, Tuple +from typing import Any, Dict, Optional, Tuple from pydantic import BaseModel diff --git a/src/llmperf/ray_clients/openai_chat_completions_client.py b/src/llmperf/ray_clients/openai_chat_completions_client.py index f2e0a91..1ffe990 100644 --- a/src/llmperf/ray_clients/openai_chat_completions_client.py +++ b/src/llmperf/ray_clients/openai_chat_completions_client.py @@ -5,6 +5,7 @@ import ray import requests +import yarl from llmperf.ray_llm_client import LLMClient from llmperf.models import RequestConfig @@ -54,14 +55,9 @@ def llm_request(self, request_config: RequestConfig) -> Dict[str, Any]: if not key: raise ValueError("the environment variable OPENAI_API_KEY must be set.") headers = {"Authorization": f"Bearer {key}"} - if not address: - raise ValueError("No host provided.") - if not address.endswith("/"): - address = address + "/" - address += "chat/completions" try: with requests.post( - address, + str(yarl.URL(address).with_path("/chat/completions")), json=body, stream=True, timeout=180, From 9559959a003f794145b525a0cdff68745234e085 Mon Sep 17 00:00:00 2001 From: rapsealk Date: Tue, 15 Oct 2024 16:17:17 +0900 Subject: [PATCH 2/3] chore: `requests` internally stringifies url --- src/llmperf/ray_clients/openai_chat_completions_client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/llmperf/ray_clients/openai_chat_completions_client.py b/src/llmperf/ray_clients/openai_chat_completions_client.py index 1ffe990..9012183 100644 --- a/src/llmperf/ray_clients/openai_chat_completions_client.py +++ b/src/llmperf/ray_clients/openai_chat_completions_client.py @@ -57,7 +57,7 @@ def llm_request(self, request_config: RequestConfig) -> Dict[str, Any]: headers = {"Authorization": f"Bearer {key}"} try: with requests.post( - str(yarl.URL(address).with_path("/chat/completions")), + yarl.URL(address).with_path("/chat/completions"), json=body, stream=True, timeout=180, From d10e90b18dc3977ed165d4f5a7d4353b7369a3ed Mon Sep 17 00:00:00 2001 From: rapsealk Date: Mon, 21 Oct 2024 17:32:06 +0900 Subject: [PATCH 3/3] chore: Import `URL` only --- src/llmperf/ray_clients/openai_chat_completions_client.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/llmperf/ray_clients/openai_chat_completions_client.py b/src/llmperf/ray_clients/openai_chat_completions_client.py index 9012183..2ee9931 100644 --- a/src/llmperf/ray_clients/openai_chat_completions_client.py +++ b/src/llmperf/ray_clients/openai_chat_completions_client.py @@ -5,7 +5,7 @@ import ray import requests -import yarl +from yarl import URL from llmperf.ray_llm_client import LLMClient from llmperf.models import RequestConfig @@ -57,7 +57,7 @@ def llm_request(self, request_config: RequestConfig) -> Dict[str, Any]: headers = {"Authorization": f"Bearer {key}"} try: with requests.post( - yarl.URL(address).with_path("/chat/completions"), + URL(address).with_path("/chat/completions"), json=body, stream=True, timeout=180,