File size: 2,233 Bytes
9f9394b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#
# SPDX-FileCopyrightText: Hadad <hadad@linuxmail.org>
# SPDX-License-Identifier: Apache-2.0
#

import aiohttp
import json
from config import (
    ENDPOINT,
    API_KEY,
    MODEL,
    STREAM,
    AIOHTTP,
    RETRY
)
from ..tools.mapping import TOOLS

async def client(messages):
    async with aiohttp.ClientSession(
        connector=aiohttp.TCPConnector(**AIOHTTP),
        headers={
            "Content-Type": "application/json",
            "Authorization": f"Bearer {API_KEY}"
        }
    ) as session:
        for attempt in range(RETRY):
            async with session.post(
                ENDPOINT,
                json={
                    "model": MODEL,
                    "messages": messages,
                    "tools": TOOLS,
                    "tool_choice": "auto",
                    "stream": STREAM
                }
            ) as response:
                if response.status != 200:
                    if attempt == RETRY - 1:
                        error_message = await response.text()
                        raise Exception(f"Error ({response.status}): {error_message}")
                    continue
                
                buffer = ""
                
                async for parts in response.content.iter_any():
                    if not parts:
                        continue
                    
                    buffer += parts.decode('utf-8')
                    
                    while '\n' in buffer:
                        line, buffer = buffer.split('\n', 1)
                        data = line.strip()
                        
                        if not data:
                            continue
                        
                        if data.startswith("data: "):
                            data = data[6:]
                        
                        if data == "[DONE]":
                            return
                        
                        if data:
                            try:
                                chunk = json.loads(data)
                                yield chunk
                            except json.JSONDecodeError:
                                continue
                
                return