LLM
AsyncOpenAIModel
Bases: Model
A class to interface with OpenAI's language models using the Weave framework.
This class provides methods to create structured messages and generate predictions using AsyncOpenAI's chat completion API. It is designed to work with both single and multiple user prompts, and optionally includes a system prompt to guide the model's responses.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_name
|
str
|
The name of the OpenAI model to be used for predictions. |
'gpt-4o'
|
Source code in safeguards/llm.py
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
|
create_messages(user_prompts, system_prompt=None, messages=None)
Create a list of messages for the OpenAI chat completion API.
This function constructs a list of messages in the format required by the OpenAI chat completion API. It takes user prompts, an optional system prompt, and an optional list of existing messages, and combines them into a single list of messages.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
user_prompts
|
Union[str, list[str]]
|
A single user prompt or a list of user prompts to be included in the messages. |
required |
system_prompt
|
Optional[str]
|
An optional system prompt to guide the model's responses. If provided, it will be added at the beginning of the messages list. |
None
|
messages
|
Optional[list[dict]]
|
An optional list of existing messages to which the new prompts will be appended. If not provided, a new list will be created. |
None
|
Returns:
Type | Description |
---|---|
list[dict]
|
list[dict]: A list of messages formatted for the OpenAI chat completion API. |
Source code in safeguards/llm.py
predict(user_prompts, system_prompt=None, messages=None, **kwargs)
async
Generate a chat completion response using the AsyncOpenAI API.
This function takes user prompts, an optional system prompt, and an optional list of existing messages to create a list of messages formatted for the OpenAI chat completion API. It then sends these messages to the OpenAI API to generate a chat completion response.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
user_prompts
|
Union[str, list[str]]
|
A single user prompt or a list of user prompts to be included in the messages. |
required |
system_prompt
|
Optional[str]
|
An optional system prompt to guide the model's responses. If provided, it will be added at the beginning of the messages list. |
None
|
messages
|
Optional[list[dict]]
|
An optional list of existing messages to which the new prompts will be appended. If not provided, a new list will be created. |
None
|
**kwargs
|
Additional keyword arguments to be passed to the OpenAI API for chat completion. |
{}
|
Returns:
Name | Type | Description |
---|---|---|
ChatCompletion |
ChatCompletion
|
The chat completion response from the OpenAI API. |
Source code in safeguards/llm.py
OpenAIModel
Bases: Model
A class to interface with OpenAI's language models using the Weave framework.
This class provides methods to create structured messages and generate predictions using OpenAI's chat completion API. It is designed to work with both single and multiple user prompts, and optionally includes a system prompt to guide the model's responses.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_name
|
str
|
The name of the OpenAI model to be used for predictions. |
'gpt-4o'
|
Source code in safeguards/llm.py
create_messages(user_prompts, system_prompt=None, messages=None)
Create a list of messages for the OpenAI chat completion API.
This function constructs a list of messages in the format required by the OpenAI chat completion API. It takes user prompts, an optional system prompt, and an optional list of existing messages, and combines them into a single list of messages.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
user_prompts
|
Union[str, list[str]]
|
A single user prompt or a list of user prompts to be included in the messages. |
required |
system_prompt
|
Optional[str]
|
An optional system prompt to guide the model's responses. If provided, it will be added at the beginning of the messages list. |
None
|
messages
|
Optional[list[dict]]
|
An optional list of existing messages to which the new prompts will be appended. If not provided, a new list will be created. |
None
|
Returns:
Type | Description |
---|---|
list[dict]
|
list[dict]: A list of messages formatted for the OpenAI chat completion API. |
Source code in safeguards/llm.py
predict(user_prompts, system_prompt=None, messages=None, **kwargs)
Generate a chat completion response using the OpenAI API.
This function takes user prompts, an optional system prompt, and an optional list of existing messages to create a list of messages formatted for the OpenAI chat completion API. It then sends these messages to the OpenAI API to generate a chat completion response.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
user_prompts
|
Union[str, list[str]]
|
A single user prompt or a list of user prompts to be included in the messages. |
required |
system_prompt
|
Optional[str]
|
An optional system prompt to guide the model's responses. If provided, it will be added at the beginning of the messages list. |
None
|
messages
|
Optional[list[dict]]
|
An optional list of existing messages to which the new prompts will be appended. If not provided, a new list will be created. |
None
|
**kwargs
|
Additional keyword arguments to be passed to the OpenAI API for chat completion. |
{}
|
Returns:
Name | Type | Description |
---|---|---|
ChatCompletion |
ChatCompletion
|
The chat completion response from the OpenAI API. |