MCPcopy Create free account
hub / github.com/Forethought-Technologies/AutoChain / BaseLanguageModel

Class BaseLanguageModel

autochain/models/base.py:49–141  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

47
48
49class BaseLanguageModel(BaseModel):
50 """Wrapper around OpenAI Chat large language models.
51
52 To use, you should have the ``openai`` python package installed, and the
53 environment variable ``OPENAI_API_KEY`` set with your API key.
54
55 Any parameters that are valid to be passed to the openai.create call can be passed
56 in, even if not explicitly saved on this class.
57
58 Example:
59 .. code-block:: python
60
61 from autochain.models import ChatOpenAI
62 openai = ChatOpenAI(model_name="gpt-3.5-turbo")
63 """
64
65 client: Any #: :meta private:
66 model_name: str = "gpt-3.5-turbo"
67 """Model name to use."""
68 temperature: float = 0.7
69 """What sampling temperature to use."""
70 model_kwargs: Dict[str, Any] = Field(default_factory=dict)
71 """Holds any model parameters valid for `create` call not explicitly specified."""
72 openai_api_key: Optional[str] = None
73 openai_organization: Optional[str] = None
74 request_timeout: Optional[Union[float, Tuple[float, float]]] = None
75 """Timeout for requests to OpenAI completion API. Default is 600 seconds."""
76 max_retries: int = 6
77 """Maximum number of retries to make when generating."""
78 n: int = 1
79 """Number of chat completions to generate for each prompt."""
80 max_tokens: Optional[int] = None
81 """Maximum number of tokens to generate."""
82
83 class Config:
84 """Configuration for this pydantic object."""
85
86 extra = Extra.ignore
87
88 @property
89 def _default_params(self) -> Dict[str, Any]:
90 """Get the default parameters for calling OpenAI API."""
91 return {
92 "model": self.model_name,
93 "request_timeout": self.request_timeout,
94 "max_tokens": self.max_tokens,
95 "n": self.n,
96 "temperature": self.temperature,
97 **self.model_kwargs,
98 }
99
100 def _create_retry_decorator(self) -> Callable[[Any], Any]:
101 import openai
102
103 min_seconds = 1
104 max_seconds = 60
105 # Wait 2^x * 1 second between each retry starting with
106 # 4 seconds, then up to 10 seconds, then 10 seconds afterwards

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected