forked from microsoft/UFO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.py
68 lines (60 loc) · 2.25 KB
/
base.py
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
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
import abc
from importlib import import_module
class BaseService(abc.ABC):
@abc.abstractmethod
def __init__(self, *args, **kwargs):
pass
@abc.abstractmethod
def chat_completion(self, *args, **kwargs):
pass
@staticmethod
def get_service(name):
service_map = {
"openai": "OpenAIService",
"aoai": "OpenAIService",
"azure_ad": "OpenAIService",
"qwen": "QwenService",
"ollama": "OllamaService",
"gemini": "GeminiService",
"placeholder": "PlaceHolderService",
}
service_name = service_map.get(name, None)
if service_name:
if name in ["aoai", "azure_ad"]:
module = import_module(".openai", package="ufo.llm")
else:
module = import_module("." + name.lower(), package="ufo.llm")
else:
raise ValueError(f"Service {name} not found.")
return getattr(module, service_name)
def get_cost_estimator(
self, api_type, model, prices, prompt_tokens, completion_tokens
) -> float:
"""
Calculates the cost estimate for using a specific model based on the number of prompt tokens and completion tokens.
Args:
model (str): The name of the model.
prices (dict): A dictionary containing the prices for different models.
prompt_tokens (int): The number of prompt tokens used.
completion_tokens (int): The number of completion tokens used.
Returns:
float: The estimated cost for using the model.
"""
if api_type.lower() == "openai":
name = str(api_type + "/" + model)
elif api_type.lower() in ["aoai", "azure_ad"]:
name = str("azure/" + model)
elif api_type.lower() == "qwen":
name = str("qwen/" + model)
elif api_type.lower() == "gemini":
name = str("gemini/" + model)
if name in prices:
cost = (
prompt_tokens * prices[name]["input"] / 1000
+ completion_tokens * prices[name]["output"] / 1000
)
else:
return 0
return cost