File size: 3,798 Bytes
6ee47c4
 
 
 
 
5075eeb
d9a24f0
6ee47c4
 
 
 
 
 
 
 
 
 
 
 
6b447a5
6ee47c4
6b447a5
6ee47c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6b447a5
 
6ee47c4
 
 
 
 
 
6b447a5
 
6ee47c4
 
 
 
6b447a5
 
 
6ee47c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6b447a5
6ee47c4
 
6b447a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
from langchain_openai import OpenAI
import openai
import sys
import os
import requests
from json import JSONDecodeError
import time

sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../../')))

from config import * 


class LLM_API_Call:
    def __init__(self, type) -> None:
        if type == "openai":
            self.llm = OpenAI_API_Call(api_key = LLM_CONFIG[""],
                                       model = LLM_CONFIG["model"])
        elif type == "gilas":
            self.llm = Gilas_API_Call(api_keys = GILAS_API_KEYS,
                                       model = GILAS_CONFIG["model"],
                                       base_url = GILAS_CONFIG["base_url"])
        else:
            self.llm = OpenAI(
                            **LLM_CONFIG
            )

    def get_LLM_response(self, prompt: str) -> str:
        return self.llm.invoke(prompt)


class OpenAI_API_Call:

    def __init__(self, api_key, model="gpt-4"):
        self.api_key = api_key
        openai.api_key = api_key
        self.model = model
        self.conversation = []

    def add_message(self, role, content):
        self.conversation.append({"role": role, "content": content})

    def get_response(self):
        response = openai.ChatCompletion.create(
            model=self.model,
            messages=self.conversation
        )
        return response['choices'][0]['message']['content']

    def invoke(self, user_input):
        self.add_message("user", user_input)

        response = self.get_response()

        self.add_message("assistant", response)

        return response


class Gilas_API_Call:
    def __init__(self, api_keys, base_url, model="gpt-4o-mini"):
        self.api_keys = api_keys
        self.base_url = base_url
        self.model = model
        self.headers = {
            "Content-Type": "application/json"
        }
        self.conversation = []
        self.retry_wait_time = 30


    def add_message(self, role, content):
        self.conversation.append({"role": role, "content": content})

    def get_response(self, api_key):
        self.headers["Authorization"] = f"Bearer {api_key}"

        data = {
            "model": self.model,
            "messages": self.conversation
        }

        response = requests.post(
            url=f"{self.base_url}/chat/completions",
            headers=self.headers,
            json=data
        )

        if response.status_code == 200:
            try:
                return response.json()['choices'][0]['message']['content']
            except (KeyError, IndexError, ValueError) as e:
                raise Exception(f"Unexpected API response format: {e}")
        else:
            raise Exception(f"Gilas API call failed: {response.status_code} - {response.text}")

    def invoke(self, user_input, max_retries=3):
        self.add_message("user", user_input)

        retries = 0
        while retries < max_retries:
            for i, api_key in enumerate(self.api_keys):
                try:
                    response = self.get_response(api_key)
                    self.add_message("assistant", response)
                    return response
                except (JSONDecodeError, Exception) as e:
                    print(f"Error encountered with API key {api_key}: {e}. Trying next key...")
                    # Sleep before trying next key
                    if i == len(self.api_keys) - 1:
                        print(f"All keys failed. Retrying oldest key after {self.retry_wait_time} seconds...")
                        time.sleep(self.retry_wait_time)
                        self.retry_wait_time += 30  # Increase wait time for next retry

            retries += 1

        raise Exception(f"Failed to get a valid response after {max_retries} retries.")