From a1614b88ac6d83d66eabb16dc5a177ac859442c8 Mon Sep 17 00:00:00 2001 From: back2nix <143707086+back2nix@users.noreply.github.com> Date: Thu, 25 Apr 2024 00:58:03 +0300 Subject: [PATCH] groq[patch]: groq proxy support (#20758) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Proxy Fix for Groq Class :bug: :rocket: ## Description This PR fixes a bug related to proxy settings in the `Groq` class, allowing users to connect to LangChain services via a proxy. ## Changes Made - ✅ FIX support for specifying proxy settings in the `Groq` class. - ✅ Resolved the bug causing issues with proxy settings. - ❌ Did not include unit tests and documentation updates. - ❌ Did not run make format, make lint, and make test to ensure code quality and functionality because I couldn't get it to run, so I don't program in Python and couldn't run `ruff`. - ❔ Ensured that the changes are backwards compatible. - ✅ No additional dependencies were added to `pyproject.toml`. ### Error Before Fix ```python Traceback (most recent call last): File "/home/bg/Documents/code/github.com/back2nix/test/groq/main.py", line 9, in chat = ChatGroq( ^^^^^^^^^ File "/home/bg/Documents/code/github.com/back2nix/test/groq/venv310/lib/python3.11/site-packages/langchain_core/load/serializable.py", line 120, in __init__ super().__init__(**kwargs) File "/home/bg/Documents/code/github.com/back2nix/test/groq/venv310/lib/python3.11/site-packages/pydantic/v1/main.py", line 341, in __init__ raise validation_error pydantic.v1.error_wrappers.ValidationError: 1 validation error for ChatGroq __root__ Invalid `http_client` argument; Expected an instance of `httpx.AsyncClient` but got (type=type_error) ``` ### Example usage after fix ```python3 import os import httpx from langchain_core.prompts import ChatPromptTemplate from langchain_groq import ChatGroq chat = ChatGroq( temperature=0, groq_api_key=os.environ.get("GROQ_API_KEY"), model_name="mixtral-8x7b-32768", http_client=httpx.Client( proxies="socks5://127.0.0.1:1080", transport=httpx.HTTPTransport(local_address="0.0.0.0"), ), http_async_client=httpx.AsyncClient( proxies="socks5://127.0.0.1:1080", transport=httpx.HTTPTransport(local_address="0.0.0.0"), ), ) system = "You are a helpful assistant." human = "{text}" prompt = ChatPromptTemplate.from_messages([("system", system), ("human", human)]) chain = prompt | chat out = chain.invoke({"text": "Explain the importance of low latency LLMs"}) print(out) ``` --------- Co-authored-by: Bagatur <22008038+baskaryan@users.noreply.github.com> Co-authored-by: Bagatur --- libs/partners/groq/langchain_groq/chat_models.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/libs/partners/groq/langchain_groq/chat_models.py b/libs/partners/groq/langchain_groq/chat_models.py index 86db80e8e22a7..8820897af1c53 100644 --- a/libs/partners/groq/langchain_groq/chat_models.py +++ b/libs/partners/groq/langchain_groq/chat_models.py @@ -126,6 +126,9 @@ class ChatGroq(BaseChatModel): # [httpx documentation](https://www.python-httpx.org/api/#client) for more details. http_client: Union[Any, None] = None """Optional httpx.Client.""" + http_async_client: Union[Any, None] = None + """Optional httpx.AsyncClient. Only used for async invocations. Must specify + http_client as well if you'd like a custom client for sync invocations.""" class Config: """Configuration for this pydantic object.""" @@ -182,17 +185,20 @@ def validate_environment(cls, values: Dict) -> Dict: "max_retries": values["max_retries"], "default_headers": values["default_headers"], "default_query": values["default_query"], - "http_client": values["http_client"], } try: import groq + sync_specific = {"http_client": values["http_client"]} if not values.get("client"): - values["client"] = groq.Groq(**client_params).chat.completions + values["client"] = groq.Groq( + **client_params, **sync_specific + ).chat.completions if not values.get("async_client"): + async_specific = {"http_client": values["http_async_client"]} values["async_client"] = groq.AsyncGroq( - **client_params + **client_params, **async_specific ).chat.completions except ImportError: raise ImportError(