You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
langchain/langchain/llms/openai.py

122 lines
4.2 KiB
Python

2 years ago
"""Wrapper around OpenAI APIs."""
from typing import Any, Dict, List, Mapping, Optional
from pydantic import BaseModel, Extra, Field, root_validator
2 years ago
from langchain.llms.base import LLM
from langchain.utils import get_from_dict_or_env
2 years ago
class OpenAI(LLM, BaseModel):
"""Wrapper around OpenAI large language models.
2 years ago
To use, you should have the ``openai`` python package installed, and the
environment variable ``OPENAI_API_KEY`` set with your API key.
Any parameters that are valid to be passed to the openai.create call can be passed
in, even if not explicitly saved on this class.
Example:
.. code-block:: python
from langchain import OpenAI
openai = OpenAI(model="text-davinci-002")
"""
client: Any #: :meta private:
2 years ago
model_name: str = "text-davinci-002"
"""Model name to use."""
2 years ago
temperature: float = 0.7
"""What sampling temperature to use."""
2 years ago
max_tokens: int = 256
"""The maximum number of tokens to generate in the completion."""
2 years ago
top_p: int = 1
"""Total probability mass of tokens to consider at each step."""
2 years ago
frequency_penalty: int = 0
"""Penalizes repeated tokens according to frequency."""
2 years ago
presence_penalty: int = 0
"""Penalizes repeated tokens."""
2 years ago
n: int = 1
"""How many completions to generate for each prompt."""
2 years ago
best_of: int = 1
"""Generates best_of completions server-side and returns the "best"."""
model_kwargs: Dict[str, Any] = Field(default_factory=dict)
"""Holds any model parameters valid for `create` call not explicitly specified."""
openai_api_key: Optional[str] = None
2 years ago
class Config:
"""Configuration for this pydantic object."""
extra = Extra.forbid
@root_validator(pre=True)
def build_extra(cls, values: Dict[str, Any]) -> Dict[str, Any]:
"""Build extra kwargs from additional params that were passed in."""
all_required_field_names = {field.alias for field in cls.__fields__.values()}
extra = values.get("model_kwargs", {})
for field_name in list(values):
if field_name not in all_required_field_names:
if field_name in extra:
raise ValueError(f"Found {field_name} supplied twice.")
extra[field_name] = values.pop(field_name)
values["model_kwargs"] = extra
return values
2 years ago
@root_validator()
def validate_environment(cls, values: Dict) -> Dict:
"""Validate that api key and python package exists in environment."""
openai_api_key = get_from_dict_or_env(
values, "openai_api_key", "OPENAI_API_KEY"
)
2 years ago
try:
import openai
openai.api_key = openai_api_key
2 years ago
values["client"] = openai.Completion
except ImportError:
raise ValueError(
"Could not import openai python package. "
"Please it install it with `pip install openai`."
)
return values
@property
def _default_params(self) -> Mapping[str, Any]:
2 years ago
"""Get the default parameters for calling OpenAI API."""
normal_params = {
2 years ago
"temperature": self.temperature,
"max_tokens": self.max_tokens,
"top_p": self.top_p,
"frequency_penalty": self.frequency_penalty,
"presence_penalty": self.presence_penalty,
"n": self.n,
"best_of": self.best_of,
}
return {**normal_params, **self.model_kwargs}
2 years ago
@property
def _identifying_params(self) -> Mapping[str, Any]:
"""Get the identifying parameters."""
return {**{"model": self.model_name}, **self._default_params}
2 years ago
def __call__(self, prompt: str, stop: Optional[List[str]] = None) -> str:
"""Call out to OpenAI's create endpoint.
Args:
prompt: The prompt to pass into the model.
stop: Optional list of stop words to use when generating.
Returns:
The string generated by the model.
Example:
.. code-block:: python
response = openai("Tell me a joke.")
"""
2 years ago
response = self.client.create(
model=self.model_name, prompt=prompt, stop=stop, **self._default_params
2 years ago
)
return response["choices"][0]["text"]