Wrapper around OpenAI Chat large language models. 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 n
| 116 | |
| 117 | |
| 118 | class ChatOpenAI(BaseLanguageModel): |
| 119 | """Wrapper around OpenAI Chat large language models. |
| 120 | |
| 121 | To use, you should have the ``openai`` python package installed, and the |
| 122 | environment variable ``OPENAI_API_KEY`` set with your API key. |
| 123 | |
| 124 | Any parameters that are valid to be passed to the openai.create call can be passed |
| 125 | in, even if not explicitly saved on this class. |
| 126 | |
| 127 | Example: |
| 128 | .. code-block:: python |
| 129 | |
| 130 | from autochain.models.chat_openai import ChatOpenAI |
| 131 | openai = ChatOpenAI() |
| 132 | """ |
| 133 | |
| 134 | client: Any #: :meta private: |
| 135 | model_name: str = "gpt-3.5-turbo" |
| 136 | """Model name to use.""" |
| 137 | temperature: float = 0 |
| 138 | """What sampling temperature to use.""" |
| 139 | model_kwargs: Dict[str, Any] = Field(default_factory=dict) |
| 140 | """Holds any model parameters valid for `create` call not explicitly specified.""" |
| 141 | openai_api_key: Optional[str] = None |
| 142 | openai_organization: Optional[str] = None |
| 143 | api_type: Optional[str] = None |
| 144 | """OpenAI API type, it can be `openai` or `azure`.""" |
| 145 | api_base: Optional[str] = None |
| 146 | """The OpenAI API base url or Azure OpenAI API base url.""" |
| 147 | azure_api_version: Optional[str] = None |
| 148 | """Azure API version.""" |
| 149 | azure_deployment_name: Optional[str] = None |
| 150 | """Azure deployment name.""" |
| 151 | request_timeout: Optional[Union[float, Tuple[float, float]]] = None |
| 152 | """Timeout for requests to OpenAI completion API. Default is 600 seconds.""" |
| 153 | max_retries: int = 6 |
| 154 | """Maximum number of retries to make when generating.""" |
| 155 | # TODO: support streaming |
| 156 | # streaming: bool = False |
| 157 | # """Whether to stream the results or not.""" |
| 158 | # n: int = 1 |
| 159 | # """Number of chat completions to generate for each prompt.""" |
| 160 | max_tokens: Optional[int] = None |
| 161 | """Maximum number of tokens to generate.""" |
| 162 | |
| 163 | class Config: |
| 164 | """Configuration for this pydantic object.""" |
| 165 | |
| 166 | extra = Extra.ignore |
| 167 | |
| 168 | @root_validator() |
| 169 | def validate_environment(cls, values: Dict) -> Dict: |
| 170 | """Validate that api key and python package exists in environment.""" |
| 171 | openai_api_key = os.environ["OPENAI_API_KEY"] |
| 172 | openai_api_type = os.environ.get("OPENAI_API_TYPE", "open_ai") |
| 173 | openai_api_base = os.environ.get("OPENAI_API_BASE", None) |
| 174 | try: |
| 175 | import openai |
no outgoing calls