File size: 8,397 Bytes
88435ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
from typing import Literal, cast

from openai import AzureOpenAI

from neollm.llm.abstract_llm import AbstractLLM
from neollm.llm.gpt.abstract_gpt import AbstractGPT
from neollm.types import (
    APIPricing,
    ClientSettings,
    LLMSettings,
    Messages,
    Response,
    StreamResponse,
)
from neollm.utils.utils import cprint, ensure_env_var, suport_unrecomended_env_var

suport_unrecomended_env_var(old_key="AZURE_API_BASE", new_key="AZURE_OPENAI_ENDPOINT")
suport_unrecomended_env_var(old_key="AZURE_API_VERSION", new_key="OPENAI_API_VERSION")
# 0613なし
suport_unrecomended_env_var(old_key="AZURE_ENGINE_GPT35", new_key="AZURE_ENGINE_GPT35T_0613")
suport_unrecomended_env_var(old_key="AZURE_ENGINE_GPT35_16k", new_key="AZURE_ENGINE_GPT35T_16K_0613")
suport_unrecomended_env_var(old_key="AZURE_ENGINE_GPT4", new_key="AZURE_ENGINE_GPT4_0613")
suport_unrecomended_env_var(old_key="AZURE_ENGINE_GPT4_32k", new_key="AZURE_ENGINE_GPT4_32K_0613")
# turbo抜け
suport_unrecomended_env_var(old_key="AZURE_ENGINE_GPT35_0613", new_key="AZURE_ENGINE_GPT35T_0613")
suport_unrecomended_env_var(old_key="AZURE_ENGINE_GPT35_16K_0613", new_key="AZURE_ENGINE_GPT35T_16K_0613")

# Pricing: https://azure.microsoft.com/en-us/pricing/details/cognitive-services/openai-service/

SUPPORTED_MODELS = Literal[
    "gpt-4o-2024-05-13",
    "gpt-4-turbo-2024-04-09",
    "gpt-3.5-turbo-0125",
    "gpt-4-turbo-0125",
    "gpt-3.5-turbo-1106",
    "gpt-4-turbo-1106",
    "gpt-4v-turbo-1106",
    "gpt-3.5-turbo-0613",
    "gpt-3.5-turbo-16k-0613",
    "gpt-4-0613",
    "gpt-4-32k-0613",
]


def get_azure_llm(model_name: SUPPORTED_MODELS | str, client_settings: ClientSettings) -> AbstractLLM:
    # 表記変更
    model_name = model_name.replace("gpt-35-turbo", "gpt-3.5-turbo")
    # Add 日付
    replace_map_for_nodate: dict[str, SUPPORTED_MODELS] = {
        "gpt-4o": "gpt-4o-2024-05-13",
        "gpt-3.5-turbo": "gpt-3.5-turbo-0613",
        "gpt-3.5-turbo-16k": "gpt-3.5-turbo-16k-0613",
        "gpt-4": "gpt-4-0613",
        "gpt-4-32k": "gpt-4-32k-0613",
        "gpt-4-turbo": "gpt-4-turbo-1106",
        "gpt-4v-turbo": "gpt-4v-turbo-1106",
    }
    if model_name in replace_map_for_nodate:
        cprint("WARNING: model_nameに日付を指定してください", color="yellow", background=True)
        print(f"model_name: {model_name} -> {replace_map_for_nodate[model_name]}")
        model_name = replace_map_for_nodate[model_name]

    # map to LLM
    supported_model_map: dict[SUPPORTED_MODELS, AbstractLLM] = {
        "gpt-4o-2024-05-13": AzureGPT4O_20240513(client_settings),
        "gpt-4-turbo-2024-04-09": AzureGPT4T_20240409(client_settings),
        "gpt-3.5-turbo-0125": AzureGPT35T_0125(client_settings),
        "gpt-4-turbo-0125": AzureGPT4T_0125(client_settings),
        "gpt-3.5-turbo-1106": AzureGPT35T_1106(client_settings),
        "gpt-4-turbo-1106": AzureGPT4T_1106(client_settings),
        "gpt-4v-turbo-1106": AzureGPT4VT_1106(client_settings),
        "gpt-3.5-turbo-0613": AzureGPT35T_0613(client_settings),
        "gpt-3.5-turbo-16k-0613": AzureGPT35T16k_0613(client_settings),
        "gpt-4-0613": AzureGPT4_0613(client_settings),
        "gpt-4-32k-0613": AzureGPT432k_0613(client_settings),
    }
    # 通常モデル
    if model_name in supported_model_map:
        model_name = cast(SUPPORTED_MODELS, model_name)
        return supported_model_map[model_name]
    # FTモデル
    return AzureGPT35FT(model_name, client_settings)


class AzureLLM(AbstractGPT):
    _engine_name_env_key: str | None = None

    @property
    def client(self) -> AzureOpenAI:
        client: AzureOpenAI = AzureOpenAI(**self.client_settings)
        # api_key: str | None = (None,)
        # timeout: httpx.Timeout(timeout=600.0, connect=5.0)
        # max_retries: int = 2
        return client

    @property
    def engine(self) -> str:
        return ensure_env_var(self._engine_name_env_key)

    def generate(self, messages: Messages, llm_settings: LLMSettings) -> Response:
        openai_response = self.client.chat.completions.create(
            model=self.engine,
            messages=self._convert_to_platform_messages(messages),
            stream=False,
            **llm_settings,
        )
        response = self._convert_to_response(openai_response)
        return response

    def generate_stream(self, messages: Messages, llm_settings: LLMSettings) -> StreamResponse:
        platform_stream_response = self.client.chat.completions.create(
            model=self.engine,
            messages=self._convert_to_platform_messages(messages),
            stream=True,
            **llm_settings,
        )
        stream_response = self._convert_to_streamresponse(platform_stream_response)
        return stream_response


# omni 2024-05-13 --------------------------------------------------------------------------------------------
class AzureGPT4O_20240513(AzureLLM):
    dollar_per_ktoken = APIPricing(input=0.005, output=0.015)  # 30倍/45倍
    model: str = "gpt-4o-2024-05-13"
    _engine_name_env_key: str = "AZURE_ENGINE_GPT4O_20240513"
    context_window: int = 128_000


# 2024-04-09 --------------------------------------------------------------------------------------------
class AzureGPT4T_20240409(AzureLLM):
    dollar_per_ktoken = APIPricing(input=0.01, output=0.03)
    model: str = "gpt-4-turbo-2024-04-09"
    _engine_name_env_key: str = "AZURE_ENGINE_GPT4T_20240409"
    context_window: int = 128_000


# 0125 --------------------------------------------------------------------------------------------
class AzureGPT35T_0125(AzureLLM):
    dollar_per_ktoken = APIPricing(input=0.0005, output=0.0015)
    model: str = "gpt-3.5-turbo-0125"
    _engine_name_env_key: str = "AZURE_ENGINE_GPT35T_0125"
    context_window: int = 16_385


class AzureGPT4T_0125(AzureLLM):
    dollar_per_ktoken = APIPricing(input=0.01, output=0.03)
    model: str = "gpt-4-turbo-0125"
    _engine_name_env_key: str = "AZURE_ENGINE_GPT4T_0125"
    context_window: int = 128_000


# 1106 --------------------------------------------------------------------------------------------
class AzureGPT35T_1106(AzureLLM):
    dollar_per_ktoken = APIPricing(input=0.001, output=0.002)
    model: str = "gpt-3.5-turbo-1106"
    _engine_name_env_key: str = "AZURE_ENGINE_GPT35T_1106"
    context_window: int = 16_385


class AzureGPT4VT_1106(AzureLLM):
    dollar_per_ktoken = APIPricing(input=0.01, output=0.03)  # 10倍/15倍
    model: str = "gpt-4-1106-vision-preview"
    _engine_name_env_key: str = "AZURE_ENGINE_GPT4VT_1106"
    context_window: int = 128_000


class AzureGPT4T_1106(AzureLLM):
    dollar_per_ktoken = APIPricing(input=0.01, output=0.03)
    model: str = "gpt-4-turbo-1106"
    _engine_name_env_key: str = "AZURE_ENGINE_GPT4T_1106"
    context_window: int = 128_000


# FT --------------------------------------------------------------------------------------------
class AzureGPT35FT(AzureLLM):
    dollar_per_ktoken = APIPricing(input=0.0005, output=0.0015)  # 1倍 + セッション稼働時間
    model: str = "gpt-3.5-turbo-ft"
    context_window: int = 4_096

    def __init__(self, model_name: str, client_setting: ClientSettings) -> None:
        super().__init__(client_setting)
        self._engine = model_name

    @property
    def engine(self) -> str:
        return self._engine


# 0613 --------------------------------------------------------------------------------------------
class AzureGPT35T_0613(AzureLLM):
    dollar_per_ktoken = APIPricing(input=0.0015, output=0.002)
    model: str = "gpt-3.5-turbo-0613"
    _engine_name_env_key: str = "AZURE_ENGINE_GPT35T_0613"
    context_window: int = 4_096


class AzureGPT35T16k_0613(AzureLLM):
    dollar_per_ktoken = APIPricing(input=0.003, output=0.004)  # 2倍
    model: str = "gpt-3.5-turbo-16k-0613"
    _engine_name_env_key: str = "AZURE_ENGINE_GPT35T_16K_0613"
    context_window: int = 16_385


class AzureGPT4_0613(AzureLLM):
    dollar_per_ktoken = APIPricing(input=0.03, output=0.06)  # 20倍/30倍
    model: str = "gpt-4-0613"
    _engine_name_env_key: str = "AZURE_ENGINE_GPT4_0613"
    context_window: int = 8_192


class AzureGPT432k_0613(AzureLLM):
    dollar_per_ktoken = APIPricing(input=0.06, output=0.12)  # 40倍/60倍
    model: str = "gpt-4-32k-0613"
    _engine_name_env_key: str = "AZURE_ENGINE_GPT4_32K_0613"
    context_window: int = 32_768