From e2a5f653e02328a65bb7819805a3c0296b151a4c Mon Sep 17 00:00:00 2001 From: lucast2021 Date: Mon, 10 Jun 2024 11:27:41 -0700 Subject: [PATCH 1/5] updated ChatGroq docstring; note that usage_metadata returns None for this model; no mention of image input in the documentation --- .../groq/langchain_groq/chat_models.py | 212 +++++++++++++++++- 1 file changed, 210 insertions(+), 2 deletions(-) diff --git a/libs/partners/groq/langchain_groq/chat_models.py b/libs/partners/groq/langchain_groq/chat_models.py index a41da6e1d3101..3a5df80e63b3b 100644 --- a/libs/partners/groq/langchain_groq/chat_models.py +++ b/libs/partners/groq/langchain_groq/chat_models.py @@ -85,8 +85,8 @@ class ChatGroq(BaseChatModel): To use, you should have the environment variable ``GROQ_API_KEY`` set with your API key. - Any parameters that are valid to be passed to the groq.create call can be passed - in, even if not explicitly saved on this class. + Any parameters that are valid to be passed to the groq.create call + can be passed in, even if not explicitly saved on this class. Example: .. code-block:: python @@ -95,6 +95,214 @@ class ChatGroq(BaseChatModel): model = ChatGroq(model_name="mixtral-8x7b-32768") """ + """ + Setup: + Install ``langchain-groq`` and set environment variable + ``GROQ_API_KEY``. + + .. code-block:: bash + + pip install -U langchain-groq + export GROQ_API_KEY="your-api-key" + + Key init args — completion params: + model_name: str + Name of Groq model to use. E.g. "mixtral-8x7b-32768". + temperature: float + Sampling temperature. Ranges from 0.0 to 1.0. + max_tokens: Optional[int] + Max number of tokens to generate. + + Key init args — client params: + cache: Union[BaseCache, bool, None] + Whether to cache the response. Uses global cache if true, does not + use a cache if false, uses global cache (if it is set) if None, + and uses the provided cache if is instance of BaseCache. + callbacks: Callbacks + Callbacks to add to the run trace. + custom_get_token_ids: Optional[Callable[[str], List[int]]] + Optional encoder to use for counting tokens. + groq_api_base: Optional[str] + Base URL path for API requests, leave blank if not using a proxy + or service emulator. + metadata: Optional[Dict[str, Any]] + Metadata to add to the run trace. + model_kwargs: Dict[str, Any] + Holds any model parameters valid for create call not + explicitly specified. + streaming: bool = False + Whether to stream the results or not. + + See full list of supported init args and their descriptions in the params + section. + + Instantiate: + .. code-block:: python + + from langchain_groq import ChatGroq + + model = ChatGroq( + model_name="mixtral-8x7b-32768", + temperature=0.0, + max_retries=2, + # streaming=..., + # other params... + ) + + Invoke: + .. code-block:: python + + messages = [ + ("system", "You are a helpful translator. Translate the user + sentence to French."), + ("human", "I love programming."), + ] + model.invoke(messages) + + .. code-block:: python + + AIMessage(content='The English sentence "I love programming" can + be translated to French as "J\'aime programmer". The word + "programming" is translated as "programmer" in French.', + response_metadata={'token_usage': {'completion_tokens': 38, + 'prompt_tokens': 28, 'total_tokens': 66, 'completion_time': + 0.057975474, 'prompt_time': 0.005366091, 'queue_time': None, + 'total_time': 0.063341565}, 'model_name': 'mixtral-8x7b-32768', + 'system_fingerprint': 'fp_c5f20b5bb1', 'finish_reason': 'stop', + 'logprobs': None}, id='run-ecc71d70-e10c-4b69-8b8c-b8027d95d4b8-0') + + Stream: + .. code-block:: python + + for chunk in model.stream(messages): + print(chunk) + + .. code-block:: python + + content='' id='run-4e9f926b-73f5-483b-8ef5-09533d925853' + content='The' id='run-4e9f926b-73f5-483b-8ef5-09533d925853' + content=' English' id='run-4e9f926b-73f5-483b-8ef5-09533d925853' + content=' sentence' id='run-4e9f926b-73f5-483b-8ef5-09533d925853' + ... + content=' program' id='run-4e9f926b-73f5-483b-8ef5-09533d925853' + content='".' id='run-4e9f926b-73f5-483b-8ef5-09533d925853' + content='' response_metadata={'finish_reason': 'stop'} + id='run-4e9f926b-73f5-483b-8ef5-09533d925853 + + .. code-block:: python + + stream = model.stream(messages) + full = next(stream) + for chunk in stream: + full += chunk + full + + .. code-block:: python + + AIMessageChunk(content='The English sentence "I love programming" + can be translated to French as "J\'aime programmer". + Here\'s the breakdown of the sentence:\n\n* "J\'aime" is the + French equivalent of "I love"\n* "programmer" is the French + infinitive for "to program"\n\nSo, the literal translation + is "I love to program". However, in English we often omit the + "to" when talking about activities we love, and the same applies + to French. Therefore, "J\'aime programmer" is the correct and + natural way to express "I love programming" in French.', + response_metadata={'finish_reason': 'stop'}, + id='run-a3c35ac4-0750-4d08-ac55-bfc63805de76') + + Async: + .. code-block:: python + + await model.ainvoke(messages) + + .. code-block:: python + + AIMessage(content='The English sentence "I love programming" can + be translated to French as "J\'aime programmer". The word + "programming" is translated as "programmer" in French. I hope + this helps! Let me know if you have any other questions.', + response_metadata={'token_usage': {'completion_tokens': 53, + 'prompt_tokens': 28, 'total_tokens': 81, 'completion_time': + 0.083623752, 'prompt_time': 0.007365126, 'queue_time': None, + 'total_time': 0.090988878}, 'model_name': 'mixtral-8x7b-32768', + 'system_fingerprint': 'fp_c5f20b5bb1', 'finish_reason': 'stop', + 'logprobs': None}, id='run-897f3391-1bea-42e2-82e0-686e2367bcf8-0') + + Tool calling: + .. code-block:: python + + from langchain_core.pydantic_v1 import BaseModel, Field + + class GetWeather(BaseModel): + '''Get the current weather in a given location''' + + location: str = Field(..., description="The city and state, + e.g. San Francisco, CA") + + class GetPopulation(BaseModel): + '''Get the current population in a given location''' + + location: str = Field(..., description="The city and state, + e.g. San Francisco, CA") + + model_with_tools = model.bind_tools([GetWeather, GetPopulation]) + ai_msg = model_with_tools.invoke("What is the population of NY?") + ai_msg.tool_calls + + .. code-block:: python + + [{'name': 'GetPopulation', + 'args': {'location': 'NY'}, + 'id': 'call_bb8d'}] + + See ``ChatGroq.bind_tools()`` method for more. + + Structured output: + .. code-block:: python + + from typing import Optional + + from langchain_core.pydantic_v1 import BaseModel, Field + + class Joke(BaseModel): + '''Joke to tell user.''' + + setup: str = Field(description="The setup of the joke") + punchline: str = Field(description="The punchline to the joke") + rating: Optional[int] = Field(description="How funny the joke + is, from 1 to 10") + + structured_model = model.with_structured_output(Joke) + structured_model.invoke("Tell me a joke about cats") + + .. code-block:: python + + Joke(setup="Why don't cats play poker in the jungle?", + punchline='Too many cheetahs!', rating=None) + + See ``ChatGroq.with_structured_output()`` for more. + + Response metadata + .. code-block:: python + + ai_msg = model.invoke(messages) + ai_msg.response_metadata + + .. code-block:: python + + {'token_usage': {'completion_tokens': 70, + 'prompt_tokens': 28, + 'total_tokens': 98, + 'completion_time': 0.111956391, + 'prompt_time': 0.007518279, + 'queue_time': None, + 'total_time': 0.11947467}, + 'model_name': 'mixtral-8x7b-32768', + 'system_fingerprint': 'fp_c5f20b5bb1', + 'finish_reason': 'stop', + 'logprobs': None} + """ client: Any = Field(default=None, exclude=True) #: :meta private: async_client: Any = Field(default=None, exclude=True) #: :meta private: From ad7d1c2f17d7a974092e56798388e2e6812542a7 Mon Sep 17 00:00:00 2001 From: lucast2021 Date: Mon, 10 Jun 2024 11:33:49 -0700 Subject: [PATCH 2/5] updated ChatGroq docstring; note that usage_metadata returns None for this model; no mention of image input in the documentation --- libs/partners/groq/langchain_groq/chat_models.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/libs/partners/groq/langchain_groq/chat_models.py b/libs/partners/groq/langchain_groq/chat_models.py index 3a5df80e63b3b..3b3a91a1cbe87 100644 --- a/libs/partners/groq/langchain_groq/chat_models.py +++ b/libs/partners/groq/langchain_groq/chat_models.py @@ -94,8 +94,7 @@ class ChatGroq(BaseChatModel): from langchain_groq import ChatGroq model = ChatGroq(model_name="mixtral-8x7b-32768") - """ - """ + Setup: Install ``langchain-groq`` and set environment variable ``GROQ_API_KEY``. From 2e34fea97c13e5d72d6a12ce1dd988d08de4ff0b Mon Sep 17 00:00:00 2001 From: lucast2021 Date: Mon, 10 Jun 2024 13:32:13 -0700 Subject: [PATCH 3/5] reorganized ChatGroq docstring according to baskaryan's comments --- libs/partners/groq/langchain_groq/chat_models.py | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/libs/partners/groq/langchain_groq/chat_models.py b/libs/partners/groq/langchain_groq/chat_models.py index 3b3a91a1cbe87..0fb64f5e5fe82 100644 --- a/libs/partners/groq/langchain_groq/chat_models.py +++ b/libs/partners/groq/langchain_groq/chat_models.py @@ -111,14 +111,11 @@ class ChatGroq(BaseChatModel): Sampling temperature. Ranges from 0.0 to 1.0. max_tokens: Optional[int] Max number of tokens to generate. + model_kwargs: Dict[str, Any] + Holds any model parameters valid for create call not + explicitly specified. Key init args — client params: - cache: Union[BaseCache, bool, None] - Whether to cache the response. Uses global cache if true, does not - use a cache if false, uses global cache (if it is set) if None, - and uses the provided cache if is instance of BaseCache. - callbacks: Callbacks - Callbacks to add to the run trace. custom_get_token_ids: Optional[Callable[[str], List[int]]] Optional encoder to use for counting tokens. groq_api_base: Optional[str] @@ -126,11 +123,6 @@ class ChatGroq(BaseChatModel): or service emulator. metadata: Optional[Dict[str, Any]] Metadata to add to the run trace. - model_kwargs: Dict[str, Any] - Holds any model parameters valid for create call not - explicitly specified. - streaming: bool = False - Whether to stream the results or not. See full list of supported init args and their descriptions in the params section. From c66590a292a73c565449a330f2d899140ffdd9db Mon Sep 17 00:00:00 2001 From: lucast2021 Date: Mon, 10 Jun 2024 13:39:29 -0700 Subject: [PATCH 4/5] tried to get rid of trailing whitespace as per original test failure in the PR --- .../groq/langchain_groq/chat_models.py | 78 +++++++++---------- 1 file changed, 39 insertions(+), 39 deletions(-) diff --git a/libs/partners/groq/langchain_groq/chat_models.py b/libs/partners/groq/langchain_groq/chat_models.py index 0fb64f5e5fe82..33e18fbd117f9 100644 --- a/libs/partners/groq/langchain_groq/chat_models.py +++ b/libs/partners/groq/langchain_groq/chat_models.py @@ -85,7 +85,7 @@ class ChatGroq(BaseChatModel): To use, you should have the environment variable ``GROQ_API_KEY`` set with your API key. - Any parameters that are valid to be passed to the groq.create call + Any parameters that are valid to be passed to the groq.create call can be passed in, even if not explicitly saved on this class. Example: @@ -96,7 +96,7 @@ class ChatGroq(BaseChatModel): model = ChatGroq(model_name="mixtral-8x7b-32768") Setup: - Install ``langchain-groq`` and set environment variable + Install ``langchain-groq`` and set environment variable ``GROQ_API_KEY``. .. code-block:: bash @@ -112,14 +112,14 @@ class ChatGroq(BaseChatModel): max_tokens: Optional[int] Max number of tokens to generate. model_kwargs: Dict[str, Any] - Holds any model parameters valid for create call not + Holds any model parameters valid for create call not explicitly specified. Key init args — client params: custom_get_token_ids: Optional[Callable[[str], List[int]]] Optional encoder to use for counting tokens. groq_api_base: Optional[str] - Base URL path for API requests, leave blank if not using a proxy + Base URL path for API requests, leave blank if not using a proxy or service emulator. metadata: Optional[Dict[str, Any]] Metadata to add to the run trace. @@ -144,7 +144,7 @@ class ChatGroq(BaseChatModel): .. code-block:: python messages = [ - ("system", "You are a helpful translator. Translate the user + ("system", "You are a helpful translator. Translate the user sentence to French."), ("human", "I love programming."), ] @@ -152,14 +152,14 @@ class ChatGroq(BaseChatModel): .. code-block:: python - AIMessage(content='The English sentence "I love programming" can - be translated to French as "J\'aime programmer". The word - "programming" is translated as "programmer" in French.', - response_metadata={'token_usage': {'completion_tokens': 38, - 'prompt_tokens': 28, 'total_tokens': 66, 'completion_time': - 0.057975474, 'prompt_time': 0.005366091, 'queue_time': None, - 'total_time': 0.063341565}, 'model_name': 'mixtral-8x7b-32768', - 'system_fingerprint': 'fp_c5f20b5bb1', 'finish_reason': 'stop', + AIMessage(content='The English sentence "I love programming" can + be translated to French as "J\'aime programmer". The word + "programming" is translated as "programmer" in French.', + response_metadata={'token_usage': {'completion_tokens': 38, + 'prompt_tokens': 28, 'total_tokens': 66, 'completion_time': + 0.057975474, 'prompt_time': 0.005366091, 'queue_time': None, + 'total_time': 0.063341565}, 'model_name': 'mixtral-8x7b-32768', + 'system_fingerprint': 'fp_c5f20b5bb1', 'finish_reason': 'stop', 'logprobs': None}, id='run-ecc71d70-e10c-4b69-8b8c-b8027d95d4b8-0') Stream: @@ -177,7 +177,7 @@ class ChatGroq(BaseChatModel): ... content=' program' id='run-4e9f926b-73f5-483b-8ef5-09533d925853' content='".' id='run-4e9f926b-73f5-483b-8ef5-09533d925853' - content='' response_metadata={'finish_reason': 'stop'} + content='' response_metadata={'finish_reason': 'stop'} id='run-4e9f926b-73f5-483b-8ef5-09533d925853 .. code-block:: python @@ -190,16 +190,16 @@ class ChatGroq(BaseChatModel): .. code-block:: python - AIMessageChunk(content='The English sentence "I love programming" - can be translated to French as "J\'aime programmer". - Here\'s the breakdown of the sentence:\n\n* "J\'aime" is the - French equivalent of "I love"\n* "programmer" is the French - infinitive for "to program"\n\nSo, the literal translation - is "I love to program". However, in English we often omit the - "to" when talking about activities we love, and the same applies - to French. Therefore, "J\'aime programmer" is the correct and - natural way to express "I love programming" in French.', - response_metadata={'finish_reason': 'stop'}, + AIMessageChunk(content='The English sentence "I love programming" + can be translated to French as "J\'aime programmer". + Here\'s the breakdown of the sentence:\n\n* "J\'aime" is the + French equivalent of "I love"\n* "programmer" is the French + infinitive for "to program"\n\nSo, the literal translation + is "I love to program". However, in English we often omit the + "to" when talking about activities we love, and the same applies + to French. Therefore, "J\'aime programmer" is the correct and + natural way to express "I love programming" in French.', + response_metadata={'finish_reason': 'stop'}, id='run-a3c35ac4-0750-4d08-ac55-bfc63805de76') Async: @@ -209,15 +209,15 @@ class ChatGroq(BaseChatModel): .. code-block:: python - AIMessage(content='The English sentence "I love programming" can - be translated to French as "J\'aime programmer". The word - "programming" is translated as "programmer" in French. I hope - this helps! Let me know if you have any other questions.', - response_metadata={'token_usage': {'completion_tokens': 53, - 'prompt_tokens': 28, 'total_tokens': 81, 'completion_time': - 0.083623752, 'prompt_time': 0.007365126, 'queue_time': None, - 'total_time': 0.090988878}, 'model_name': 'mixtral-8x7b-32768', - 'system_fingerprint': 'fp_c5f20b5bb1', 'finish_reason': 'stop', + AIMessage(content='The English sentence "I love programming" can + be translated to French as "J\'aime programmer". The word + "programming" is translated as "programmer" in French. I hope + this helps! Let me know if you have any other questions.', + response_metadata={'token_usage': {'completion_tokens': 53, + 'prompt_tokens': 28, 'total_tokens': 81, 'completion_time': + 0.083623752, 'prompt_time': 0.007365126, 'queue_time': None, + 'total_time': 0.090988878}, 'model_name': 'mixtral-8x7b-32768', + 'system_fingerprint': 'fp_c5f20b5bb1', 'finish_reason': 'stop', 'logprobs': None}, id='run-897f3391-1bea-42e2-82e0-686e2367bcf8-0') Tool calling: @@ -228,13 +228,13 @@ class ChatGroq(BaseChatModel): class GetWeather(BaseModel): '''Get the current weather in a given location''' - location: str = Field(..., description="The city and state, + location: str = Field(..., description="The city and state, e.g. San Francisco, CA") class GetPopulation(BaseModel): '''Get the current population in a given location''' - location: str = Field(..., description="The city and state, + location: str = Field(..., description="The city and state, e.g. San Francisco, CA") model_with_tools = model.bind_tools([GetWeather, GetPopulation]) @@ -243,8 +243,8 @@ class GetPopulation(BaseModel): .. code-block:: python - [{'name': 'GetPopulation', - 'args': {'location': 'NY'}, + [{'name': 'GetPopulation', + 'args': {'location': 'NY'}, 'id': 'call_bb8d'}] See ``ChatGroq.bind_tools()`` method for more. @@ -261,7 +261,7 @@ class Joke(BaseModel): setup: str = Field(description="The setup of the joke") punchline: str = Field(description="The punchline to the joke") - rating: Optional[int] = Field(description="How funny the joke + rating: Optional[int] = Field(description="How funny the joke is, from 1 to 10") structured_model = model.with_structured_output(Joke) @@ -269,7 +269,7 @@ class Joke(BaseModel): .. code-block:: python - Joke(setup="Why don't cats play poker in the jungle?", + Joke(setup="Why don't cats play poker in the jungle?", punchline='Too many cheetahs!', rating=None) See ``ChatGroq.with_structured_output()`` for more. From baadb69f6a00a31a79f9060b346443e1500ddfe0 Mon Sep 17 00:00:00 2001 From: Bagatur Date: Thu, 13 Jun 2024 20:05:10 -0700 Subject: [PATCH 5/5] fmt --- .../groq/langchain_groq/chat_models.py | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/libs/partners/groq/langchain_groq/chat_models.py b/libs/partners/groq/langchain_groq/chat_models.py index 33e18fbd117f9..f8890af4de7a5 100644 --- a/libs/partners/groq/langchain_groq/chat_models.py +++ b/libs/partners/groq/langchain_groq/chat_models.py @@ -105,7 +105,7 @@ class ChatGroq(BaseChatModel): export GROQ_API_KEY="your-api-key" Key init args — completion params: - model_name: str + model: str Name of Groq model to use. E.g. "mixtral-8x7b-32768". temperature: float Sampling temperature. Ranges from 0.0 to 1.0. @@ -116,13 +116,17 @@ class ChatGroq(BaseChatModel): explicitly specified. Key init args — client params: - custom_get_token_ids: Optional[Callable[[str], List[int]]] - Optional encoder to use for counting tokens. - groq_api_base: Optional[str] + timeout: Union[float, Tuple[float, float], Any, None] + Timeout for requests. + max_retries: int + Max number of retries. + api_key: Optional[str] + Groq API key. If not passed in will be read from env var GROQ_API_KEY. + base_url: Optional[str] Base URL path for API requests, leave blank if not using a proxy or service emulator. - metadata: Optional[Dict[str, Any]] - Metadata to add to the run trace. + custom_get_token_ids: Optional[Callable[[str], List[int]]] + Optional encoder to use for counting tokens. See full list of supported init args and their descriptions in the params section. @@ -133,10 +137,9 @@ class ChatGroq(BaseChatModel): from langchain_groq import ChatGroq model = ChatGroq( - model_name="mixtral-8x7b-32768", + model="mixtral-8x7b-32768", temperature=0.0, max_retries=2, - # streaming=..., # other params... ) @@ -161,7 +164,7 @@ class ChatGroq(BaseChatModel): 'total_time': 0.063341565}, 'model_name': 'mixtral-8x7b-32768', 'system_fingerprint': 'fp_c5f20b5bb1', 'finish_reason': 'stop', 'logprobs': None}, id='run-ecc71d70-e10c-4b69-8b8c-b8027d95d4b8-0') - + Stream: .. code-block:: python @@ -304,7 +307,7 @@ class Joke(BaseModel): model_kwargs: Dict[str, Any] = Field(default_factory=dict) """Holds any model parameters valid for `create` call not explicitly specified.""" groq_api_key: Optional[SecretStr] = Field(default=None, alias="api_key") - """Automatically inferred from env var `groq_API_KEY` if not provided.""" + """Automatically inferred from env var `GROQ_API_KEY` if not provided.""" groq_api_base: Optional[str] = Field(default=None, alias="base_url") """Base URL path for API requests, leave blank if not using a proxy or service emulator."""