Skip to content

Commit 5371791

Browse files
committed
Adds docs on passing custom headers to OAI model
1 parent 46f83ed commit 5371791

File tree

2 files changed

+77
-78
lines changed

2 files changed

+77
-78
lines changed

docs/core_docs/docs/integrations/chat/azure.ipynb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,48 @@
324324
"});\n"
325325
]
326326
},
327+
{
328+
"cell_type": "markdown",
329+
"id": "092e7a38",
330+
"metadata": {},
331+
"source": [
332+
"## Custom headers\n",
333+
"\n",
334+
"You can specify custom headers by passing in a `configuration` field:"
335+
]
336+
},
337+
{
338+
"cell_type": "code",
339+
"execution_count": null,
340+
"id": "43503a94",
341+
"metadata": {},
342+
"outputs": [],
343+
"source": [
344+
"import { AzureChatOpenAI } from \"@langchain/openai\";\n",
345+
"\n",
346+
"const llmWithCustomHeaders = new AzureChatOpenAI({\n",
347+
" azureOpenAIApiKey: process.env.AZURE_OPENAI_API_KEY, // In Node.js defaults to process.env.AZURE_OPENAI_API_KEY\n",
348+
" azureOpenAIApiInstanceName: process.env.AZURE_OPENAI_API_INSTANCE_NAME, // In Node.js defaults to process.env.AZURE_OPENAI_API_INSTANCE_NAME\n",
349+
" azureOpenAIApiDeploymentName: process.env.AZURE_OPENAI_API_DEPLOYMENT_NAME, // In Node.js defaults to process.env.AZURE_OPENAI_API_DEPLOYMENT_NAME\n",
350+
" azureOpenAIApiVersion: process.env.AZURE_OPENAI_API_VERSION, // In Node.js defaults to process.env.AZURE_OPENAI_API_VERSION\n",
351+
" configuration: {\n",
352+
" defaultHeaders: {\n",
353+
" \"x-custom-header\": `SOME_VALUE`,\n",
354+
" },\n",
355+
" },\n",
356+
"});\n",
357+
"\n",
358+
"await llmWithCustomHeaders.invoke(\"Hi there!\");"
359+
]
360+
},
361+
{
362+
"cell_type": "markdown",
363+
"id": "1a6b849d",
364+
"metadata": {},
365+
"source": [
366+
"The `configuration` field also accepts other `ClientOptions` parameters accepted by the official SDK."
367+
]
368+
},
327369
{
328370
"cell_type": "markdown",
329371
"id": "0ac0310c",

docs/core_docs/docs/integrations/chat/openai.ipynb

Lines changed: 35 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -272,13 +272,44 @@
272272
},
273273
{
274274
"cell_type": "markdown",
275-
"id": "013b6300",
275+
"id": "20b60ccb",
276276
"metadata": {},
277277
"source": [
278-
"You can also pass other `ClientOptions` parameters accepted by the official SDK here.\n",
278+
"The `configuration` field also accepts other `ClientOptions` parameters accepted by the official SDK.\n",
279279
"\n",
280280
"If you are hosting on Azure OpenAI, see the [dedicated page instead](/docs/integrations/chat/azure).\n",
281281
"\n",
282+
"## Custom headers\n",
283+
"\n",
284+
"You can specify custom headers in the same `configuration` field:"
285+
]
286+
},
287+
{
288+
"cell_type": "code",
289+
"execution_count": null,
290+
"id": "cd612609",
291+
"metadata": {},
292+
"outputs": [],
293+
"source": [
294+
"import { ChatOpenAI } from \"@langchain/openai\";\n",
295+
"\n",
296+
"const llmWithCustomHeaders = new ChatOpenAI({\n",
297+
" temperature: 0.9,\n",
298+
" configuration: {\n",
299+
" defaultHeaders: {\n",
300+
" \"Authorization\": `Bearer SOME_CUSTOM_VALUE`,\n",
301+
" },\n",
302+
" },\n",
303+
"});\n",
304+
"\n",
305+
"await llmWithCustomHeaders.invoke(\"Hi there!\");"
306+
]
307+
},
308+
{
309+
"cell_type": "markdown",
310+
"id": "013b6300",
311+
"metadata": {},
312+
"source": [
282313
"## Calling fine-tuned models\n",
283314
"\n",
284315
"You can call fine-tuned OpenAI models by passing in your corresponding `modelName` parameter.\n",
@@ -411,7 +442,7 @@
411442
},
412443
{
413444
"cell_type": "markdown",
414-
"id": "bc5ecebd",
445+
"id": "3a5bb5ca-c3ae-4a58-be67-2cd18574b9a3",
415446
"metadata": {},
416447
"source": [
417448
"## Tool calling\n",
@@ -420,82 +451,8 @@
420451
"\n",
421452
"- [How to: disable parallel tool calling](/docs/how_to/tool_calling_parallel/)\n",
422453
"- [How to: force a tool call](/docs/how_to/tool_choice/)\n",
423-
"- [How to: bind model-specific tool formats to a model](/docs/how_to/tool_calling#binding-model-specific-formats-advanced)."
424-
]
425-
},
426-
{
427-
"cell_type": "markdown",
428-
"id": "3392390e",
429-
"metadata": {},
430-
"source": [
431-
"### ``strict: true``\n",
432-
"\n",
433-
"```{=mdx}\n",
454+
"- [How to: bind model-specific tool formats to a model](/docs/how_to/tool_calling#binding-model-specific-formats-advanced).\n",
434455
"\n",
435-
":::info Requires ``@langchain/openai >= 0.2.6``\n",
436-
"\n",
437-
"As of Aug 6, 2024, OpenAI supports a `strict` argument when calling tools that will enforce that the tool argument schema is respected by the model. See more here: https://platform.openai.com/docs/guides/function-calling\n",
438-
"\n",
439-
"**Note**: If ``strict: true`` the tool definition will also be validated, and a subset of JSON schema are accepted. Crucially, schema cannot have optional args (those with default values). Read the full docs on what types of schema are supported here: https://platform.openai.com/docs/guides/structured-outputs/supported-schemas. \n",
440-
":::\n",
441-
"\n",
442-
"\n",
443-
"```"
444-
]
445-
},
446-
{
447-
"cell_type": "code",
448-
"execution_count": 1,
449-
"id": "90f0d465",
450-
"metadata": {},
451-
"outputs": [
452-
{
453-
"name": "stdout",
454-
"output_type": "stream",
455-
"text": [
456-
"[\n",
457-
" {\n",
458-
" name: 'get_current_weather',\n",
459-
" args: { location: 'Hanoi' },\n",
460-
" type: 'tool_call',\n",
461-
" id: 'call_aB85ybkLCoccpzqHquuJGH3d'\n",
462-
" }\n",
463-
"]\n"
464-
]
465-
}
466-
],
467-
"source": [
468-
"import { ChatOpenAI } from \"@langchain/openai\";\n",
469-
"import { tool } from \"@langchain/core/tools\";\n",
470-
"import { z } from \"zod\";\n",
471-
"\n",
472-
"const weatherTool = tool((_) => \"no-op\", {\n",
473-
" name: \"get_current_weather\",\n",
474-
" description: \"Get the current weather\",\n",
475-
" schema: z.object({\n",
476-
" location: z.string(),\n",
477-
" }),\n",
478-
"})\n",
479-
"\n",
480-
"const llmWithStrictTrue = new ChatOpenAI({\n",
481-
" model: \"gpt-4o\",\n",
482-
"}).bindTools([weatherTool], {\n",
483-
" strict: true,\n",
484-
" tool_choice: weatherTool.name,\n",
485-
"});\n",
486-
"\n",
487-
"// Although the question is not about the weather, it will call the tool with the correct arguments\n",
488-
"// because we passed `tool_choice` and `strict: true`.\n",
489-
"const strictTrueResult = await llmWithStrictTrue.invoke(\"What is 127862 times 12898 divided by 2?\");\n",
490-
"\n",
491-
"console.dir(strictTrueResult.tool_calls, { depth: null });"
492-
]
493-
},
494-
{
495-
"cell_type": "markdown",
496-
"id": "3a5bb5ca-c3ae-4a58-be67-2cd18574b9a3",
497-
"metadata": {},
498-
"source": [
499456
"## API reference\n",
500457
"\n",
501458
"For detailed documentation of all ChatOpenAI features and configurations head to the API reference: https://api.js.langchain.com/classes/langchain_openai.ChatOpenAI.html"

0 commit comments

Comments
 (0)