Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat[openai]: responses API #7831

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions docs/core_docs/docs/integrations/chat/openai.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,94 @@
"}]);"
]
},
{
"cell_type": "markdown",
"id": "7139f50e",
"metadata": {},
"source": [
"## Responses API\n",
"\n",
":::caution Compatibility\n",
"\n",
"The below points apply to `@langchain/openai>=0.4.5-rc.0`. Please see here for a [guide on upgrading](/docs/how_to/installation/#installing-integration-packages).\n",
"\n",
":::\n",
"\n",
"OpenAI supports a [Responses](https://platform.openai.com/docs/guides/responses-vs-chat-completions) API that is oriented toward building [agentic](/docs/concepts/agents/) applications. It includes a suite of [built-in tools](https://platform.openai.com/docs/guides/tools?api-mode=responses), including web and file search. It also supports management of [conversation state](https://platform.openai.com/docs/guides/conversation-state?api-mode=responses), allowing you to continue a conversational thread without explicitly passing in previous messages.\n",
"\n",
"`ChatOpenAI` will route to the Responses API if one of these features is used. You can also specify `useResponsesAPI: true` when instantiating `ChatOpenAI`.\n",
"\n",
"### Built-in tools\n",
"\n",
"Equipping `ChatOpenAI` with built-in tools will ground its responses with outside information, such as via context in files or the web. The [AIMessage](/docs/concepts/messages/#aimessage) generated from the model will include information about the built-in tool invocation.\n",
"\n",
"#### Web search\n",
"\n",
"To trigger a web search, pass `{\"type\": \"web_search_preview\"}` to the model as you would another tool.\n",
"\n",
":::tip\n",
"\n",
"You can also pass built-in tools as invocation params:\n",
"\n",
"```ts\n",
"llm.invoke(\"...\", { tools: [{ type: \"web_search_preview\" }] });\n",
"```\n",
"\n",
":::\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1b01f09a",
"metadata": {},
"outputs": [],
"source": [
"import { ChatOpenAI } from \"@langchain/openai\";\n",
"\n",
"const llm = new ChatOpenAI({ model: \"gpt-4o-mini\" }).bindTools([\n",
" { type: \"web_search_preview\" },\n",
"]);\n",
"\n",
"await llm.invoke(\"What was a positive news story from today?\");\n"
]
},
{
"cell_type": "markdown",
"id": "e62bae97",
"metadata": {},
"source": [
"Note that the response includes structured [content blocks](/docs/concepts/messages/#content-1) that include both the text of the response and OpenAI [annotations](https://platform.openai.com/docs/guides/tools-web-search?api-mode=responses#output-and-citations) citing its sources. The output message will also contain information from any tool invocations.\n",
"\n",
"#### File search\n",
"\n",
"To trigger a file search, pass a [file search tool](https://platform.openai.com/docs/guides/tools-file-search) to the model as you would another tool. You will need to populate an OpenAI-managed vector store and include the vector store ID in the tool definition. See [OpenAI documentation](https://platform.openai.com/docs/guides/tools-file-search) for more details.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b70c671c",
"metadata": {},
"outputs": [],
"source": [
"import { ChatOpenAI } from \"@langchain/openai\";\n",
"\n",
"const llm = new ChatOpenAI({ model: \"gpt-4o-mini\" }).bindTools([\n",
" { type: \"file_search\", vector_store_ids: [\"vs...\"] },\n",
"]);\n",
"\n",
"await llm.invoke(\"Is deep research by OpenAI?\");\n"
]
},
{
"cell_type": "markdown",
"id": "e2a8378c",
"metadata": {},
"source": [
"As with [web search](#web-search), the response will include content blocks with citations. It will also include information from the built-in tool invocations."
]
},
{
"cell_type": "markdown",
"id": "0194ec1f",
Expand Down
1 change: 1 addition & 0 deletions langchain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,7 @@
"openai": "^4.41.1",
"peggy": "^3.0.2",
"prettier": "^2.8.3",
"reflect-metadata": "^0.2.2",
"release-it": "^17.6.0",
"rimraf": "^5.0.1",
"rollup": "^3.19.1",
Expand Down
4 changes: 2 additions & 2 deletions libs/langchain-openai/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@langchain/openai",
"version": "0.4.4",
"version": "0.4.5-rc.0",
"description": "OpenAI integrations for LangChain.js",
"type": "module",
"engines": {
Expand Down Expand Up @@ -36,7 +36,7 @@
"license": "MIT",
"dependencies": {
"js-tiktoken": "^1.0.12",
"openai": "^4.77.0",
"openai": "^4.87.3",
"zod": "^3.22.4",
"zod-to-json-schema": "^3.22.3"
},
Expand Down
Loading
Loading