File size: 1,657 Bytes
1daa44a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import requests,json
from config import Response
class ChatCompletionAPI():
    def __init__(self):
        pass
    def make_request(

        self,

        method='POST',

        stream=True,

        handle_stream=False,

        json=None,

        url=None,

        messages=None,

        headers=None ):
        self.headers = headers
        self.messages = messages
        response = requests.request(
            url=url,
            json=json,
            stream=stream,
            headers=self.headers,
            method=method)
        self.ai = ''
        if(response.status_code!=200):
            return f'an error occured\napi status_code: {response.status_code}\napi response: {response.text}',500
        if(handle_stream):
            return self.handle_stream(response=response)
        else:
            self.ai = response.text
            self.messages.append({"role":"assistant","content":self.ai})    
            return ai

    def handle_stream(self,response):
        def generator():
            for data in response.iter_lines():
                data = data.decode()
                data = data[6:]

                try:
                    data = json.loads(data)
                    chunk = data['choices'][0]['delta']['content']
                    self.ai+=chunk
                    yield chunk
                except json.JSONDecodeError:
                    pass
                except Exception as e:
                    yield str(data)
                    return
            self.messages.append({"role":"assistant","content":self.ai})
        return Response(generator())