From 4c4f032a75896ee05053e54e9768d67fc5222c76 Mon Sep 17 00:00:00 2001 From: Krrish Dholakia Date: Fri, 19 Jul 2024 15:32:01 -0700 Subject: [PATCH 1/3] fix(factory.py): refactor factory to use httpx client --- litellm/llms/prompt_templates/factory.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/litellm/llms/prompt_templates/factory.py b/litellm/llms/prompt_templates/factory.py index 99ffcfbf41..a219e930a0 100644 --- a/litellm/llms/prompt_templates/factory.py +++ b/litellm/llms/prompt_templates/factory.py @@ -14,6 +14,7 @@ import litellm import litellm.types import litellm.types.llms import litellm.types.llms.vertex_ai +from litellm.llms.custom_httpx.http_handler import HTTPHandler from litellm.types.completion import ( ChatCompletionFunctionMessageParam, ChatCompletionMessageParam, @@ -1989,8 +1990,9 @@ def get_image_details(image_url) -> Tuple[str, str]: try: import base64 + client = HTTPHandler(concurrent_limit=1) # Send a GET request to the image URL - response = requests.get(image_url) + response = client.get(image_url) response.raise_for_status() # Raise an exception for HTTP errors # Check the response's content type to ensure it is an image From 757dedd4c8daeecb4975d712fcdc82b5150836da Mon Sep 17 00:00:00 2001 From: Krrish Dholakia Date: Fri, 19 Jul 2024 15:35:05 -0700 Subject: [PATCH 2/3] fix(factory.py): refactor factory to use httpx client --- litellm/llms/prompt_templates/factory.py | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/litellm/llms/prompt_templates/factory.py b/litellm/llms/prompt_templates/factory.py index a219e930a0..efe8124975 100644 --- a/litellm/llms/prompt_templates/factory.py +++ b/litellm/llms/prompt_templates/factory.py @@ -6,7 +6,6 @@ import xml.etree.ElementTree as ET from enum import Enum from typing import Any, List, Mapping, MutableMapping, Optional, Sequence, Tuple -import requests from jinja2 import BaseLoader, Template, exceptions, meta from jinja2.sandbox import ImmutableSandboxedEnvironment @@ -365,7 +364,8 @@ def hf_chat_template(model: str, messages: list, chat_template: Optional[Any] = f"https://huggingface.co/{hf_model_name}/raw/main/tokenizer_config.json" ) # Make a GET request to fetch the JSON data - response = requests.get(url) + client = HTTPHandler(concurrent_limit=1) + response = client.get(url) if response.status_code == 200: # Parse the JSON data tokenizer_config = json.loads(response.content) @@ -495,7 +495,8 @@ def claude_2_1_pt( def get_model_info(token, model): try: headers = {"Authorization": f"Bearer {token}"} - response = requests.get("https://api.together.xyz/models/info", headers=headers) + client = HTTPHandler(concurrent_limit=1) + response = client.get("https://api.together.xyz/models/info", headers=headers) if response.status_code == 200: model_info = response.json() for m in model_info: @@ -658,11 +659,11 @@ def construct_tool_use_system_prompt( def convert_url_to_base64(url): import base64 - import requests - + client = HTTPHandler(concurrent_limit=1) for _ in range(3): try: - response = requests.get(url) + + response = client.get(url) break except: pass @@ -1799,7 +1800,8 @@ def _load_image_from_url(image_url): try: # Send a GET request to the image URL - response = requests.get(image_url) + client = HTTPHandler(concurrent_limit=1) + response = client.get(image_url) response.raise_for_status() # Raise an exception for HTTP errors # Check the response's content type to ensure it is an image @@ -1812,8 +1814,6 @@ def _load_image_from_url(image_url): # Load the image from the response content return Image.open(BytesIO(response.content)) - except requests.RequestException as e: - raise Exception(f"Request failed: {e}") except Exception as e: raise e @@ -2012,8 +2012,6 @@ def get_image_details(image_url) -> Tuple[str, str]: return base64_bytes, mime_type - except requests.RequestException as e: - raise Exception(f"Request failed: {e}") except Exception as e: raise e From bd914c714c319b5b5519d8bab07494aa0333e786 Mon Sep 17 00:00:00 2001 From: Krrish Dholakia Date: Fri, 19 Jul 2024 15:48:22 -0700 Subject: [PATCH 3/3] fix(files/main.py): fix linting error --- litellm/files/main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/litellm/files/main.py b/litellm/files/main.py index 598bc48787..836f22f967 100644 --- a/litellm/files/main.py +++ b/litellm/files/main.py @@ -406,7 +406,7 @@ async def acreate_file( extra_headers: Optional[Dict[str, str]] = None, extra_body: Optional[Dict[str, str]] = None, **kwargs, -) -> Coroutine[Any, Any, FileObject]: +) -> FileObject: """ Async: Files are used to upload documents that can be used with features like Assistants, Fine-tuning, and Batch API. @@ -537,7 +537,7 @@ async def afile_content( extra_headers: Optional[Dict[str, str]] = None, extra_body: Optional[Dict[str, str]] = None, **kwargs, -) -> Coroutine[Any, Any, HttpxBinaryResponseContent]: +) -> HttpxBinaryResponseContent: """ Async: Get file contents