Skip to content

Commit ccd0b4b

Browse files
allohamorajacoblee93
andauthoredSep 18, 2024··
fix(groq): Feature/groq response format improvements (#6754)
Co-authored-by: jacoblee93 <jacoblee93@gmail.com>
1 parent 3bb7bce commit ccd0b4b

File tree

2 files changed

+52
-7
lines changed

2 files changed

+52
-7
lines changed
 

‎docs/core_docs/docs/integrations/chat/groq.ipynb

+50-6
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,11 @@
145145
],
146146
"source": [
147147
"const aiMsg = await llm.invoke([\n",
148-
" [\n",
149-
" \"system\",\n",
150-
" \"You are a helpful assistant that translates English to French. Translate the user sentence.\",\n",
151-
" ],\n",
152-
" [\"human\", \"I love programming.\"],\n",
148+
" {\n",
149+
" role: \"system\",\n",
150+
" content: \"You are a helpful assistant that translates English to French. Translate the user sentence.\",\n",
151+
" },\n",
152+
" { role: \"user\", content: \"I love programming.\" },\n",
153153
"])\n",
154154
"aiMsg"
155155
]
@@ -174,6 +174,50 @@
174174
"console.log(aiMsg.content)"
175175
]
176176
},
177+
{
178+
"cell_type": "markdown",
179+
"id": "ce0414fe",
180+
"metadata": {},
181+
"source": [
182+
"## Json invocation"
183+
]
184+
},
185+
{
186+
"cell_type": "code",
187+
"execution_count": 4,
188+
"id": "3f0a7a2a",
189+
"metadata": {},
190+
"outputs": [
191+
{
192+
"name": "stdout",
193+
"output_type": "stream",
194+
"text": [
195+
"{\n",
196+
" aiInvokeMsgContent: '{\\n\"result\": 6\\n}',\n",
197+
" aiBindMsg: '{\\n\"result\": 6\\n}'\n",
198+
"}\n"
199+
]
200+
}
201+
],
202+
"source": [
203+
"const messages = [\n",
204+
" {\n",
205+
" role: \"system\",\n",
206+
" content: \"You are a math tutor that handles math exercises and makes output in json in format { result: number }.\",\n",
207+
" },\n",
208+
" { role: \"user\", content: \"2 + 2 * 2\" },\n",
209+
"];\n",
210+
"\n",
211+
"const aiInvokeMsg = await llm.invoke(messages, { response_format: { type: \"json_object\" } });\n",
212+
"\n",
213+
"// if you want not to pass response_format in every invoke, you can bind it to the instance\n",
214+
"const llmWithResponseFormat = llm.bind({ response_format: { type: \"json_object\" } });\n",
215+
"const aiBindMsg = await llmWithResponseFormat.invoke(messages);\n",
216+
"\n",
217+
"// they are the same\n",
218+
"console.log({ aiInvokeMsgContent: aiInvokeMsg.content, aiBindMsg: aiBindMsg.content });"
219+
]
220+
},
177221
{
178222
"cell_type": "markdown",
179223
"id": "18e2bfc0-7e78-4528-a73f-499ac150dca8",
@@ -186,7 +230,7 @@
186230
},
187231
{
188232
"cell_type": "code",
189-
"execution_count": 4,
233+
"execution_count": 5,
190234
"id": "e197d1d7-a070-4c96-9f8a-a0e86d046e0b",
191235
"metadata": {},
192236
"outputs": [

‎libs/langchain-groq/src/chat_models.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import {
4242
ChatCompletionCreateParams,
4343
ChatCompletionCreateParamsNonStreaming,
4444
ChatCompletionCreateParamsStreaming,
45+
CompletionCreateParams,
4546
} from "groq-sdk/resources/chat/completions";
4647
import {
4748
Runnable,
@@ -73,7 +74,7 @@ export interface ChatGroqCallOptions extends BaseChatModelCallOptions {
7374
headers?: Record<string, string>;
7475
tools?: ChatGroqToolType[];
7576
tool_choice?: OpenAIClient.ChatCompletionToolChoiceOption | "any" | string;
76-
response_format?: { type: "json_object" };
77+
response_format?: CompletionCreateParams.ResponseFormat;
7778
}
7879

7980
export interface ChatGroqInput extends BaseChatModelParams {

0 commit comments

Comments
 (0)
Please sign in to comment.