Spaces:
Sleeping
Sleeping
File size: 1,820 Bytes
ed83bfd |
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 |
import os
from dotenv import load_dotenv
from typing import Optional
from langchain_openai import ChatOpenAI
import inspect
load_dotenv(os.path.join(os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) , '.env'))
class MyChatOpenAI:
@classmethod
def from_model(
cls,
model: str = 'gpt-4o-mini',
*,
langsmith_project: str = 'default',
temperature: float = 0.7,
max_tokens: Optional[int] = 4096,
max_retries: int = 1,
**kwargs
)-> ChatOpenAI:
os.environ['LANGCHAIN_PROJECT'] = langsmith_project
if model in ['gpt-4o', 'GPT-4o', 'GPT-4O', 'gpt-4O', 'gpt4o', 'GPT4o', 'GPT4O', 'gpt4O']:
model = 'gpt-4o'
elif model in ['gpt-4o-mini', 'GPT-4o-mini', 'GPT-4O-mini', 'gpt-4O-mini', 'gpt4o-mini', 'GPT4o-mini', 'GPT4O-mini', 'gpt4O-mini', 'gpt4omini', 'GPT4omini', 'GPT4Omini', 'gpt4Omini']:
model = 'gpt-4o-mini'
else:
raise ValueError(f"Model {model} is currently not supported. Supported models are: ['gpt-4o', 'gpt-4o-mini']")
return ChatOpenAI(
openai_api_key=os.getenv("OPENAI_API_KEY"),
model=model,
temperature=temperature,
max_tokens=max_tokens,
max_retries=max_retries,
**kwargs
)
@classmethod
def get_model_price(cls)-> dict:
# Dictionary to store the cost of input and output tokens for each model
supported_models = {'gpt-4o' : (5, 15)} # gpt-4o model: input cost = $5 per 1M tokens, output cost = $15 per 1M tokens
supported_models.update({'gpt-4o-mini' : (0.15, 0.6)}) # gpt-4o-mini model: input cost = $0.15 per 1M tokens, output cost = $0.6 per 1M tokens
return supported_models
|