File size: 793 Bytes
2874322
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81f2388
2874322
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import os

import numpy as np
import requests

from neovision.utils import compose_payload


API_KEY = os.getenv('OPENAI_API_KEY')


class OpanAIConnector:

    def __init__(self, api_key: str = API_KEY):
        if api_key is None:
            raise ValueError("API_KEY is not set")
        self.api_key = api_key
    

    def simple_prompt(self, image: np.ndarray, prompt: str) -> str:
        headers = {
            "Content-Type": "application/json",
            "Authorization": f"Bearer {self.api_key}"
        }
        payload = compose_payload(image=image, prompt=prompt)
        response = requests.post("https://api.openai.com/v1/chat/completions",
                                 headers=headers, json=payload).json()

        return response['choices'][0]['message']['content']