83
83
84
84
85
85
class ChatGroq (BaseChatModel ):
86
- """` Groq` Chat large language models API.
86
+ """Groq Chat large language models API.
87
87
88
88
To use, you should have the
89
89
environment variable ``GROQ_API_KEY`` set with your API key.
@@ -102,17 +102,27 @@ class ChatGroq(BaseChatModel):
102
102
103
103
Key init args β completion params:
104
104
model: str
105
- Name of Groq model to use. E .g. " llama-3.1-8b-instant" .
105
+ Name of Groq model to use, e .g. `` llama-3.1-8b-instant`` .
106
106
temperature: float
107
- Sampling temperature. Ranges from 0.0 to 1.0.
107
+ Sampling temperature. Ranges from `` 0.0`` to `` 1.0`` .
108
108
max_tokens: Optional[int]
109
109
Max number of tokens to generate.
110
110
reasoning_format: Optional[Literal["parsed", "raw", "hidden]]
111
- The format for reasoning output.
112
-
113
- - ``parsed``: Separates reasoning into a dedicated field while keeping the response concise.
114
- - ``raw``: Includes reasoning within think tags in the content.
115
- - ``hidden``: Returns only the final answer.
111
+ The format for reasoning output. Groq will default to ``raw`` if left
112
+ undefined.
113
+
114
+ - ``'parsed'``: Separates reasoning into a dedicated field while keeping the
115
+ response concise. Reasoning will be returned in the
116
+ ``additional_kwargs.reasoning_content`` field of the response.
117
+ - ``'raw'``: Includes reasoning within think tags (e.g.
118
+ ``<think>{reasoning_content}</think>``).
119
+ - ``'hidden'``: Returns only the final answer content. Note: this only
120
+ supresses reasoning content in the response; the model will still perform
121
+ reasoning unless overridden in ``reasoning_effort``.
122
+
123
+ See the `Groq documentation
124
+ <https://console.groq.com/docs/reasoning#reasoning>`__ for more
125
+ details and a list of supported reasoning models.
116
126
model_kwargs: Dict[str, Any]
117
127
Holds any model parameters valid for create call not
118
128
explicitly specified.
@@ -123,7 +133,7 @@ class ChatGroq(BaseChatModel):
123
133
max_retries: int
124
134
Max number of retries.
125
135
api_key: Optional[str]
126
- Groq API key. If not passed in will be read from env var GROQ_API_KEY.
136
+ Groq API key. If not passed in will be read from env var `` GROQ_API_KEY`` .
127
137
base_url: Optional[str]
128
138
Base URL path for API requests, leave blank if not using a proxy
129
139
or service emulator.
@@ -168,11 +178,9 @@ class ChatGroq(BaseChatModel):
168
178
'logprobs': None}, id='run-ecc71d70-e10c-4b69-8b8c-b8027d95d4b8-0')
169
179
170
180
Stream:
171
-
172
- Streaming `text` for each content chunk received:
173
-
174
181
.. code-block:: python
175
182
183
+ # Streaming `text` for each content chunk received
176
184
for chunk in llm.stream(messages):
177
185
print(chunk.text(), end="")
178
186
@@ -188,10 +196,9 @@ class ChatGroq(BaseChatModel):
188
196
content='' response_metadata={'finish_reason': 'stop'}
189
197
id='run-4e9f926b-73f5-483b-8ef5-09533d925853
190
198
191
- Reconstructing a full response:
192
-
193
199
.. code-block:: python
194
200
201
+ # Reconstructing a full response
195
202
stream = llm.stream(messages)
196
203
full = next(stream)
197
204
for chunk in stream:
@@ -283,7 +290,7 @@ class Joke(BaseModel):
283
290
284
291
See ``ChatGroq.with_structured_output()`` for more.
285
292
286
- Response metadata
293
+ Response metadata:
287
294
.. code-block:: python
288
295
289
296
ai_msg = llm.invoke(messages)
@@ -302,7 +309,7 @@ class Joke(BaseModel):
302
309
'system_fingerprint': 'fp_c5f20b5bb1',
303
310
'finish_reason': 'stop',
304
311
'logprobs': None}
305
- """ # noqa: E501
312
+ """
306
313
307
314
client : Any = Field (default = None , exclude = True ) #: :meta private:
308
315
async_client : Any = Field (default = None , exclude = True ) #: :meta private:
@@ -312,23 +319,44 @@ class Joke(BaseModel):
312
319
"""What sampling temperature to use."""
313
320
stop : Optional [Union [list [str ], str ]] = Field (default = None , alias = "stop_sequences" )
314
321
"""Default stop sequences."""
315
- reasoning_format : Optional [Literal ["parsed" , "raw" , "hidden" ]] = None
316
- """The format for reasoning output.
317
-
318
- - ``parsed``: Separates reasoning into a dedicated field while keeping the response concise.
319
- - ``raw``: Includes reasoning within think tags in the content.
320
- - ``hidden``: Returns only the final answer.
321
- """ # noqa: E501
322
+ reasoning_format : Optional [Literal ["parsed" , "raw" , "hidden" ]] = Field (default = None )
323
+ """The format for reasoning output. Groq will default to raw if left undefined.
324
+
325
+ - ``'parsed'``: Separates reasoning into a dedicated field while keeping the
326
+ response concise. Reasoning will be returned in the
327
+ ``additional_kwargs.reasoning_content`` field of the response.
328
+ - ``'raw'``: Includes reasoning within think tags (e.g.
329
+ ``<think>{reasoning_content}</think>``).
330
+ - ``'hidden'``: Returns only the final answer content. Note: this only supresses
331
+ reasoning content in the response; the model will still perform reasoning unless
332
+ overridden in ``reasoning_effort``.
333
+
334
+ See the `Groq documentation <https://console.groq.com/docs/reasoning#reasoning>`__
335
+ for more details and a list of supported reasoning models.
336
+ """
337
+ reasoning_effort : Optional [Literal ["none" , "default" ]] = Field (default = None )
338
+ """The level of effort the model will put into reasoning. Groq will default to
339
+ enabling reasoning if left undefined. If set to ``none``, ``reasoning_format`` will
340
+ not apply and ``reasoning_content`` will not be returned.
341
+
342
+ - ``'none'``: Disable reasoning. The model will not use any reasoning tokens when
343
+ generating a response.
344
+ - ``'default'``: Enable reasoning.
345
+
346
+ See the `Groq documentation
347
+ <https://console.groq.com/docs/reasoning#options-for-reasoning-effort>`__ for more
348
+ details and a list of models that support setting a reasoning effort.
349
+ """
322
350
model_kwargs : dict [str , Any ] = Field (default_factory = dict )
323
351
"""Holds any model parameters valid for `create` call not explicitly specified."""
324
352
groq_api_key : Optional [SecretStr ] = Field (
325
353
alias = "api_key" , default_factory = secret_from_env ("GROQ_API_KEY" , default = None )
326
354
)
327
- """Automatically inferred from env var `GROQ_API_KEY` if not provided."""
355
+ """Automatically inferred from env var `` GROQ_API_KEY` ` if not provided."""
328
356
groq_api_base : Optional [str ] = Field (
329
357
alias = "base_url" , default_factory = from_env ("GROQ_API_BASE" , default = None )
330
358
)
331
- """Base URL path for API requests, leave blank if not using a proxy or service
359
+ """Base URL path for API requests. Leave blank if not using a proxy or service
332
360
emulator."""
333
361
# to support explicit proxy for Groq
334
362
groq_proxy : Optional [str ] = Field (
@@ -426,11 +454,11 @@ def validate_environment(self) -> Self:
426
454
self .async_client = groq .AsyncGroq (
427
455
** client_params , ** async_specific
428
456
).chat .completions
429
- except ImportError :
457
+ except ImportError as exc :
430
458
raise ImportError (
431
459
"Could not import groq python package. "
432
460
"Please install it with `pip install groq`."
433
- )
461
+ ) from exc
434
462
return self
435
463
436
464
#
@@ -624,6 +652,7 @@ def _default_params(self) -> dict[str, Any]:
624
652
"temperature" : self .temperature ,
625
653
"stop" : self .stop ,
626
654
"reasoning_format" : self .reasoning_format ,
655
+ "reasoning_effort" : self .reasoning_effort ,
627
656
** self .model_kwargs ,
628
657
}
629
658
if self .max_tokens is not None :
@@ -1227,7 +1256,7 @@ def _convert_dict_to_message(_dict: Mapping[str, Any]) -> BaseMessage:
1227
1256
for raw_tool_call in raw_tool_calls :
1228
1257
try :
1229
1258
tool_calls .append (parse_tool_call (raw_tool_call , return_id = True ))
1230
- except Exception as e :
1259
+ except Exception as e : # pylint: disable=broad-except
1231
1260
invalid_tool_calls .append (
1232
1261
make_invalid_tool_call (raw_tool_call , str (e ))
1233
1262
)
0 commit comments