mirror of
https://github.com/hwchase17/langchain.git
synced 2025-06-27 17:08:47 +00:00
docs: Standardize OpenAI Docs (#25280)
- **Description:** Standardize OpenAI Docs - **Issue:** the issue #24803 --------- Co-authored-by: Chester Curme <chester.curme@gmail.com>
This commit is contained in:
parent
fd546196ef
commit
9cd608efb3
@ -605,21 +605,105 @@ class BaseOpenAI(BaseLLM):
|
|||||||
|
|
||||||
|
|
||||||
class OpenAI(BaseOpenAI):
|
class OpenAI(BaseOpenAI):
|
||||||
"""OpenAI large language models.
|
"""OpenAI completion model integration.
|
||||||
|
|
||||||
To use, you should have the environment variable ``OPENAI_API_KEY``
|
Setup:
|
||||||
set with your API key, or pass it as a named parameter to the constructor.
|
Install ``langchain-openai`` and set environment variable ``OPENAI_API_KEY``.
|
||||||
|
|
||||||
Any parameters that are valid to be passed to the openai.create call can be passed
|
.. code-block:: bash
|
||||||
in, even if not explicitly saved on this class.
|
|
||||||
|
|
||||||
Example:
|
pip install -U langchain-openai
|
||||||
|
export OPENAI_API_KEY="your-api-key"
|
||||||
|
|
||||||
|
Key init args — completion params:
|
||||||
|
model: str
|
||||||
|
Name of OpenAI model to use.
|
||||||
|
temperature: float
|
||||||
|
Sampling temperature.
|
||||||
|
max_tokens: Optional[int]
|
||||||
|
Max number of tokens to generate.
|
||||||
|
logprobs: Optional[bool]
|
||||||
|
Whether to return logprobs.
|
||||||
|
stream_options: Dict
|
||||||
|
Configure streaming outputs, like whether to return token usage when
|
||||||
|
streaming (``{"include_usage": True}``).
|
||||||
|
|
||||||
|
Key init args — client params:
|
||||||
|
timeout: Union[float, Tuple[float, float], Any, None]
|
||||||
|
Timeout for requests.
|
||||||
|
max_retries: int
|
||||||
|
Max number of retries.
|
||||||
|
api_key: Optional[str]
|
||||||
|
OpenAI API key. If not passed in will be read from env var OPENAI_API_KEY.
|
||||||
|
base_url: Optional[str]
|
||||||
|
Base URL for API requests. Only specify if using a proxy or service
|
||||||
|
emulator.
|
||||||
|
organization: Optional[str]
|
||||||
|
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.
|
||||||
|
|
||||||
|
Instantiate:
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
from langchain_openai import OpenAI
|
from langchain_openai import OpenAI
|
||||||
|
|
||||||
model = OpenAI(model_name="gpt-3.5-turbo-instruct")
|
llm = OpenAI(
|
||||||
"""
|
model="gpt-3.5-turbo-instruct",
|
||||||
|
temperature=0,
|
||||||
|
max_retries=2,
|
||||||
|
# api_key="...",
|
||||||
|
# base_url="...",
|
||||||
|
# organization="...",
|
||||||
|
# other params...
|
||||||
|
)
|
||||||
|
|
||||||
|
Invoke:
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
input_text = "The meaning of life is "
|
||||||
|
llm.invoke(input_text)
|
||||||
|
|
||||||
|
.. code-block:: none
|
||||||
|
|
||||||
|
"a philosophical question that has been debated by thinkers and scholars for centuries."
|
||||||
|
|
||||||
|
Stream:
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
for chunk in llm.stream(input_text):
|
||||||
|
print(chunk, end="|")
|
||||||
|
|
||||||
|
.. code-block:: none
|
||||||
|
|
||||||
|
a| philosophical| question| that| has| been| debated| by| thinkers| and| scholars| for| centuries|.
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
"".join(llm.stream(input_text))
|
||||||
|
|
||||||
|
.. code-block:: none
|
||||||
|
|
||||||
|
"a philosophical question that has been debated by thinkers and scholars for centuries."
|
||||||
|
|
||||||
|
Async:
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
await llm.ainvoke(input_text)
|
||||||
|
|
||||||
|
# stream:
|
||||||
|
# async for chunk in (await llm.astream(input_text)):
|
||||||
|
# print(chunk)
|
||||||
|
|
||||||
|
# batch:
|
||||||
|
# await llm.abatch([input_text])
|
||||||
|
|
||||||
|
.. code-block:: none
|
||||||
|
|
||||||
|
"a philosophical question that has been debated by thinkers and scholars for centuries."
|
||||||
|
|
||||||
|
""" # noqa: E501
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_lc_namespace(cls) -> List[str]:
|
def get_lc_namespace(cls) -> List[str]:
|
||||||
|
Loading…
Reference in New Issue
Block a user