mirror of
https://github.com/hwchase17/langchain.git
synced 2026-06-09 10:17:00 +00:00
style: monorepo pass for refs (#33359)
* Delete some double backticks previously used by Sphinx (not done everywhere yet) * Fix some code blocks / dropdowns Ignoring CLI CI for now
This commit is contained in:
@@ -900,7 +900,7 @@ class AzureChatOpenAI(BaseChatOpenAI):
|
||||
If a model elects to call a
|
||||
tool, the resulting `AIMessage` in ``'raw'`` will include tool calls.
|
||||
|
||||
??? note "Example"
|
||||
??? example
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
|
||||
@@ -1791,7 +1791,7 @@ class BaseChatOpenAI(BaseChatModel):
|
||||
If a model elects to call a tool, the resulting `AIMessage` in `'raw'`
|
||||
will include tool calls.
|
||||
|
||||
??? note "Example"
|
||||
??? example
|
||||
|
||||
```python
|
||||
from langchain.chat_models import init_chat_model
|
||||
@@ -2015,7 +2015,7 @@ class BaseChatOpenAI(BaseChatModel):
|
||||
class ChatOpenAI(BaseChatOpenAI): # type: ignore[override]
|
||||
r"""Interface to OpenAI chat model APIs.
|
||||
|
||||
???+ note "Setup"
|
||||
???+ info "Setup"
|
||||
|
||||
Install `langchain-openai` and set environment variable `OPENAI_API_KEY`.
|
||||
|
||||
@@ -2030,42 +2030,32 @@ class ChatOpenAI(BaseChatOpenAI): # type: ignore[override]
|
||||
export OPENAI_API_KEY="your-api-key"
|
||||
```
|
||||
|
||||
??? note "Key init args — completion params"
|
||||
??? info "Key init args — completion params"
|
||||
|
||||
model: str
|
||||
Name of OpenAI model to use.
|
||||
temperature: float
|
||||
Sampling temperature.
|
||||
max_tokens: int | None
|
||||
Max number of tokens to generate.
|
||||
logprobs: bool | None
|
||||
Whether to return logprobs.
|
||||
stream_options: Dict
|
||||
Configure streaming outputs, like whether to return token usage when
|
||||
streaming (`{"include_usage": True}`).
|
||||
use_responses_api: bool | None
|
||||
Whether to use the responses API.
|
||||
| Param | Type | Description |
|
||||
| ------------------- | ------------- | ----------------------------------------------------------------------------------------------------------- |
|
||||
| `model` | `str` | Name of OpenAI model to use. |
|
||||
| `temperature` | `float` | Sampling temperature. |
|
||||
| `max_tokens` | `int | None` | Max number of tokens to generate. |
|
||||
| `logprobs` | `bool | None` | Whether to return logprobs. |
|
||||
| `stream_options` | `dict` | Configure streaming outputs, like whether to return token usage when streaming (`{"include_usage": True}`). |
|
||||
| `use_responses_api` | `bool | None` | Whether to use the responses API. |
|
||||
|
||||
See full list of supported init args and their descriptions in the params section.
|
||||
See full list of supported init args and their descriptions below.
|
||||
|
||||
??? note "Key init args — client params"
|
||||
??? info "Key init args — client params"
|
||||
|
||||
timeout: Union[float, Tuple[float, float], Any, None]
|
||||
Timeout for requests.
|
||||
max_retries: int | None
|
||||
Max number of retries.
|
||||
api_key: str | None
|
||||
OpenAI API key. If not passed in will be read from env var `OPENAI_API_KEY`.
|
||||
base_url: str | None
|
||||
Base URL for API requests. Only specify if using a proxy or service
|
||||
emulator.
|
||||
organization: str | None
|
||||
OpenAI organization ID. If not passed in will be read from env
|
||||
var `OPENAI_ORG_ID`.
|
||||
| Param | Type | Description |
|
||||
| -------------- | ------------------------------------------ | ----------------------------------------------------------------------------------- |
|
||||
| `timeout` | `float | Tuple[float, float] | Any | None` | Timeout for requests. |
|
||||
| `max_retries` | `int | None` | Max number of retries. |
|
||||
| `api_key` | `str | None` | OpenAI API key. If not passed in will be read from env var `OPENAI_API_KEY`. |
|
||||
| `base_url` | `str | None` | Base URL for API requests. Only specify if using a proxy or service emulator. |
|
||||
| `organization` | `str | None` | OpenAI organization ID. If not passed in will be read from env var `OPENAI_ORG_ID`. |
|
||||
|
||||
See full list of supported init args and their descriptions in the params section.
|
||||
See full list of supported init args and their descriptions below.
|
||||
|
||||
??? note "Instantiate"
|
||||
??? info "Instantiate"
|
||||
|
||||
Create a model instance with desired params. For example:
|
||||
|
||||
@@ -2107,7 +2097,7 @@ class ChatOpenAI(BaseChatOpenAI): # type: ignore[override]
|
||||
ChatOpenAI(...).invoke(..., frequency_penalty=0.2)
|
||||
```
|
||||
|
||||
??? note "Invoke"
|
||||
??? info "Invoke"
|
||||
|
||||
Generate a response from the model:
|
||||
|
||||
@@ -2143,7 +2133,7 @@ class ChatOpenAI(BaseChatOpenAI): # type: ignore[override]
|
||||
)
|
||||
```
|
||||
|
||||
??? note "Stream"
|
||||
??? info "Stream"
|
||||
|
||||
Stream a response from the model:
|
||||
|
||||
@@ -2187,7 +2177,7 @@ class ChatOpenAI(BaseChatOpenAI): # type: ignore[override]
|
||||
)
|
||||
```
|
||||
|
||||
??? note "Async"
|
||||
??? info "Async"
|
||||
|
||||
Asynchronous equivalents of `invoke`, `stream`, and `batch` are also available:
|
||||
|
||||
@@ -2227,9 +2217,9 @@ class ChatOpenAI(BaseChatOpenAI): # type: ignore[override]
|
||||
)
|
||||
```
|
||||
|
||||
For batched calls, results in a list of `AIMessage` responses.
|
||||
For batched calls, results in a `list[AIMessage]`.
|
||||
|
||||
??? note "Tool calling"
|
||||
??? info "Tool calling"
|
||||
|
||||
```python
|
||||
from pydantic import BaseModel, Field
|
||||
@@ -2314,17 +2304,7 @@ class ChatOpenAI(BaseChatOpenAI): # type: ignore[override]
|
||||
|
||||
See `bind_tools` for more.
|
||||
|
||||
??? note "Built-in (server-side) tools"
|
||||
|
||||
!!! version-added "Added in version 0.3.9"
|
||||
|
||||
!!! version-added "Added in version 0.3.26: Updated `AIMessage` format"
|
||||
`langchain-openai >= 0.3.26` allows users to opt-in to an updated
|
||||
`AIMessage` format when using the Responses API. Setting
|
||||
`ChatOpenAI(..., output_version="responses/v1")` will format output from
|
||||
reasoning summaries, built-in tool invocations, and other response items
|
||||
into the message's `content` field, rather than `additional_kwargs`. We
|
||||
recommend this format for new applications.
|
||||
??? info "Built-in (server-side) tools"
|
||||
|
||||
You can access [built-in tools](https://platform.openai.com/docs/guides/tools?api-mode=responses)
|
||||
supported by the OpenAI Responses API. See [LangChain docs](https://docs.langchain.com/oss/python/integrations/chat/openai#responses-api)
|
||||
@@ -2360,10 +2340,18 @@ class ChatOpenAI(BaseChatOpenAI): # type: ignore[override]
|
||||
]
|
||||
```
|
||||
|
||||
??? note "Managing conversation state"
|
||||
|
||||
!!! version-added "Added in version 0.3.9"
|
||||
|
||||
!!! version-added "Added in version 0.3.26: Updated `AIMessage` format"
|
||||
[`langchain-openai >= 0.3.26`](https://pypi.org/project/langchain-openai/#history)
|
||||
allows users to opt-in to an updated `AIMessage` format when using the
|
||||
Responses API. Setting `ChatOpenAI(..., output_version="responses/v1")` will
|
||||
format output from reasoning summaries, built-in tool invocations, and other
|
||||
response items into the message's `content` field, rather than
|
||||
`additional_kwargs`. We recommend this format for new applications.
|
||||
|
||||
??? info "Managing conversation state"
|
||||
|
||||
OpenAI's Responses API supports management of [conversation state](https://platform.openai.com/docs/guides/conversation-state?api-mode=responses).
|
||||
Passing in response IDs from previous messages will continue a conversational
|
||||
thread.
|
||||
@@ -2380,7 +2368,7 @@ class ChatOpenAI(BaseChatOpenAI): # type: ignore[override]
|
||||
response.text
|
||||
```
|
||||
|
||||
```python
|
||||
```txt
|
||||
"Hi Bob! How can I assist you today?"
|
||||
```
|
||||
|
||||
@@ -2392,12 +2380,13 @@ class ChatOpenAI(BaseChatOpenAI): # type: ignore[override]
|
||||
second_response.text
|
||||
```
|
||||
|
||||
```python
|
||||
```txt
|
||||
"Your name is Bob. How can I help you today, Bob?"
|
||||
```
|
||||
|
||||
!!! version-added "Added in version 0.3.26"
|
||||
!!! version-added "Added in version 0.3.9"
|
||||
|
||||
!!! version-added "Added in version 0.3.26"
|
||||
You can also initialize `ChatOpenAI` with `use_previous_response_id`.
|
||||
Input messages up to the most recent response will then be dropped from request
|
||||
payloads, and `previous_response_id` will be set using the ID of the most
|
||||
@@ -2407,15 +2396,7 @@ class ChatOpenAI(BaseChatOpenAI): # type: ignore[override]
|
||||
llm = ChatOpenAI(model="...", use_previous_response_id=True)
|
||||
```
|
||||
|
||||
??? note "Reasoning output"
|
||||
|
||||
!!! version-added "Added in version 0.3.26: Updated `AIMessage` format"
|
||||
`langchain-openai >= 0.3.26` allows users to opt-in to an updated
|
||||
`AIMessage` format when using the Responses API. Setting
|
||||
`ChatOpenAI(..., output_version="responses/v1")` will format output from
|
||||
reasoning summaries, built-in tool invocations, and other response items
|
||||
into the message's `content` field, rather than `additional_kwargs`. We
|
||||
recommend this format for new applications.
|
||||
??? info "Reasoning output"
|
||||
|
||||
OpenAI's Responses API supports [reasoning models](https://platform.openai.com/docs/guides/reasoning?api-mode=responses)
|
||||
that expose a summary of internal reasoning processes.
|
||||
@@ -2448,7 +2429,15 @@ class ChatOpenAI(BaseChatOpenAI): # type: ignore[override]
|
||||
Reasoning: The user wants to know...
|
||||
```
|
||||
|
||||
??? note "Structured output"
|
||||
!!! version-added "Added in version 0.3.26: Updated `AIMessage` format"
|
||||
[`langchain-openai >= 0.3.26`](https://pypi.org/project/langchain-openai/#history)
|
||||
allows users to opt-in to an updated `AIMessage` format when using the
|
||||
Responses API. Setting `ChatOpenAI(..., output_version="responses/v1")` will
|
||||
format output from reasoning summaries, built-in tool invocations, and other
|
||||
response items into the message's `content` field, rather than
|
||||
`additional_kwargs`. We recommend this format for new applications.
|
||||
|
||||
??? info "Structured output"
|
||||
|
||||
```python
|
||||
from pydantic import BaseModel, Field
|
||||
@@ -2478,7 +2467,7 @@ class ChatOpenAI(BaseChatOpenAI): # type: ignore[override]
|
||||
|
||||
See `with_structured_output` for more info.
|
||||
|
||||
??? note "JSON mode"
|
||||
??? info "JSON mode"
|
||||
|
||||
```python
|
||||
json_llm = llm.bind(response_format={"type": "json_object"})
|
||||
@@ -2492,7 +2481,7 @@ class ChatOpenAI(BaseChatOpenAI): # type: ignore[override]
|
||||
'\\n{\\n "random_ints": [23, 87, 45, 12, 78, 34, 56, 90, 11, 67]\\n}'
|
||||
```
|
||||
|
||||
??? note "Image input"
|
||||
??? info "Image input"
|
||||
|
||||
```python
|
||||
import base64
|
||||
@@ -2519,7 +2508,7 @@ class ChatOpenAI(BaseChatOpenAI): # type: ignore[override]
|
||||
"The weather in the image appears to be clear and pleasant. The sky is mostly blue with scattered, light clouds, suggesting a sunny day with minimal cloud cover. There is no indication of rain or strong winds, and the overall scene looks bright and calm. The lush green grass and clear visibility further indicate good weather conditions."
|
||||
```
|
||||
|
||||
??? note "Token usage"
|
||||
??? info "Token usage"
|
||||
|
||||
```python
|
||||
ai_msg = llm.invoke(messages)
|
||||
@@ -2543,7 +2532,7 @@ class ChatOpenAI(BaseChatOpenAI): # type: ignore[override]
|
||||
{"input_tokens": 28, "output_tokens": 5, "total_tokens": 33}
|
||||
```
|
||||
|
||||
??? note "Logprobs"
|
||||
??? info "Logprobs"
|
||||
|
||||
```python
|
||||
logprobs_llm = llm.bind(logprobs=True)
|
||||
@@ -2603,7 +2592,7 @@ class ChatOpenAI(BaseChatOpenAI): # type: ignore[override]
|
||||
}
|
||||
```
|
||||
|
||||
??? note "Response metadata"
|
||||
??? info "Response metadata"
|
||||
|
||||
```python
|
||||
ai_msg = llm.invoke(messages)
|
||||
@@ -2624,7 +2613,7 @@ class ChatOpenAI(BaseChatOpenAI): # type: ignore[override]
|
||||
}
|
||||
```
|
||||
|
||||
??? note "Flex processing"
|
||||
??? info "Flex processing"
|
||||
|
||||
OpenAI offers a variety of [service tiers](https://platform.openai.com/docs/guides/flex-processing?api-mode=responses).
|
||||
The "flex" tier offers cheaper pricing for requests, with the trade-off that
|
||||
@@ -2644,7 +2633,7 @@ class ChatOpenAI(BaseChatOpenAI): # type: ignore[override]
|
||||
See OpenAI [flex processing docs](https://platform.openai.com/docs/guides/flex-processing?api-mode=responses)
|
||||
for more detail.
|
||||
|
||||
??? note "OpenAI-compatible APIs"
|
||||
??? info "OpenAI-compatible APIs"
|
||||
|
||||
`ChatOpenAI` can be used with OpenAI-compatible APIs like
|
||||
[LM Studio](https://lmstudio.ai/), [vLLM](https://github.com/vllm-project/vllm),
|
||||
@@ -2673,7 +2662,7 @@ class ChatOpenAI(BaseChatOpenAI): # type: ignore[override]
|
||||
)
|
||||
```
|
||||
|
||||
??? note "`model_kwargs` vs `extra_body`"
|
||||
??? info "`model_kwargs` vs `extra_body`"
|
||||
|
||||
Use the correct parameter for different types of API arguments:
|
||||
|
||||
@@ -2725,7 +2714,7 @@ class ChatOpenAI(BaseChatOpenAI): # type: ignore[override]
|
||||
Always use `extra_body` for custom parameters, **not** `model_kwargs`.
|
||||
Using `model_kwargs` for non-OpenAI parameters will cause API errors.
|
||||
|
||||
??? note "Prompt caching optimization"
|
||||
??? info "Prompt caching optimization"
|
||||
|
||||
For high-volume applications with repetitive prompts, use `prompt_cache_key`
|
||||
per-invocation to improve cache hit rates and reduce costs:
|
||||
@@ -2749,7 +2738,6 @@ class ChatOpenAI(BaseChatOpenAI): # type: ignore[override]
|
||||
Cache keys help ensure requests with the same prompt prefix are routed to
|
||||
machines with existing cache, providing cost reduction and latency improvement on
|
||||
cached tokens.
|
||||
|
||||
""" # noqa: E501
|
||||
|
||||
max_tokens: Optional[int] = Field(default=None, alias="max_completion_tokens")
|
||||
@@ -2907,7 +2895,7 @@ class ChatOpenAI(BaseChatOpenAI): # type: ignore[override]
|
||||
If a model elects to call a
|
||||
tool, the resulting `AIMessage` in `'raw'` will include tool calls.
|
||||
|
||||
??? note "Example"
|
||||
??? example
|
||||
|
||||
```python
|
||||
from langchain.chat_models import init_chat_model
|
||||
|
||||
@@ -84,7 +84,7 @@ class TestOpenAIResponses(TestOpenAIStandard):
|
||||
return False
|
||||
|
||||
def test_openai_pdf_tool_messages(self, model: BaseChatModel) -> None:
|
||||
"""Test that the model can process PDF inputs in ToolMessages."""
|
||||
"""Test that the model can process PDF inputs in `ToolMessage` objects."""
|
||||
url = "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf"
|
||||
pdf_data = base64.b64encode(httpx.get(url).content).decode("utf-8")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user