Create a chat completion using the OpenAI API Supports both GPT-4 and GPT-4V). Example Usage: image_path = "path_to_your_image.jpg" base64_image = encode_image(image_path) response, info = self.create_completion( model="gpt-4-vision-preview",
(
self,
messages: List[Dict[str, str]],
model: str | None = None,
temperature: float = 1.0,
seed: int | None = 42,
max_tokens: int = 4096,
)
| 254 | |
| 255 | |
| 256 | def create_completion( |
| 257 | self, |
| 258 | messages: List[Dict[str, str]], |
| 259 | model: str | None = None, |
| 260 | temperature: float = 1.0, |
| 261 | seed: int | None = 42, |
| 262 | max_tokens: int = 4096, |
| 263 | ) -> Tuple[str, Dict[str, int]]: |
| 264 | """Create a chat completion using the OpenAI API |
| 265 | |
| 266 | Supports both GPT-4 and GPT-4V). |
| 267 | |
| 268 | Example Usage: |
| 269 | image_path = "path_to_your_image.jpg" |
| 270 | base64_image = encode_image(image_path) |
| 271 | response, info = self.create_completion( |
| 272 | model="gpt-4-vision-preview", |
| 273 | messages=[ |
| 274 | { |
| 275 | "role": "user", |
| 276 | "content": [ |
| 277 | { |
| 278 | "type": "text", |
| 279 | "text": "What’s in this image?" |
| 280 | }, |
| 281 | { |
| 282 | "type": "image_url", |
| 283 | "image_url": { |
| 284 | "url": f"data:image/jpeg;base64,{base64_image}" |
| 285 | } |
| 286 | } |
| 287 | ] |
| 288 | } |
| 289 | ], |
| 290 | ) |
| 291 | """ |
| 292 | |
| 293 | if model is None: |
| 294 | model = self.llm_model |
| 295 | |
| 296 | # print(f"Creating chat completion with model {model}, temperature {temperature}, max_tokens {max_tokens}") |
| 297 | |
| 298 | @backoff.on_exception( |
| 299 | backoff.constant, |
| 300 | ( |
| 301 | APIError, |
| 302 | RateLimitError, |
| 303 | APITimeoutError), |
| 304 | max_tries=self.retries, |
| 305 | interval=10, |
| 306 | ) |
| 307 | def _generate_response_with_retry( |
| 308 | messages: List[Dict[str, str]], |
| 309 | model: str, |
| 310 | temperature: float, |
| 311 | seed: int | None, |
| 312 | max_tokens: int = 512, |
| 313 | ) -> Tuple[str, Dict[str, int]]: |
nothing calls this directly
no outgoing calls
no test coverage detected