1+ import importlib
12import logging
23import os
34import re
910from typing import Optional
1011
1112import requests
12- from langchain_community .callbacks import bedrock_anthropic_callback , openai_info
13+
14+ langchain_community = importlib .util .find_spec ("langchain_community" )
15+ if langchain_community is not None :
16+ from langchain_community .callbacks import bedrock_anthropic_callback , openai_info
17+ else :
18+ bedrock_anthropic_callback = None
19+ openai_info = None
1320from litellm import completion_cost , get_model_info
1421
1522TRACKER = threading .local ()
@@ -103,7 +110,14 @@ def get_pricing_openrouter():
103110
104111def get_pricing_openai ():
105112 """Returns a dictionary of model pricing for OpenAI models."""
106- cost_dict = openai_info .MODEL_COST_PER_1K_TOKENS
113+ try :
114+ cost_dict = openai_info .MODEL_COST_PER_1K_TOKENS
115+ except Exception as e :
116+ logging .warning (
117+ f"Failed to get OpenAI pricing: { e } . "
118+ "Please install langchain-community or use LiteLLM API for pricing information."
119+ )
120+ return {}
107121 cost_dict = {k : v / 1000 for k , v in cost_dict .items ()}
108122 res = {}
109123 for k in cost_dict :
@@ -126,8 +140,15 @@ def _remove_version_suffix(model_name):
126140
127141def get_pricing_anthropic ():
128142 """Returns a dictionary of model pricing for Anthropic models."""
129- input_cost_dict = bedrock_anthropic_callback .MODEL_COST_PER_1K_INPUT_TOKENS
130- output_cost_dict = bedrock_anthropic_callback .MODEL_COST_PER_1K_OUTPUT_TOKENS
143+ try :
144+ input_cost_dict = bedrock_anthropic_callback .MODEL_COST_PER_1K_INPUT_TOKENS
145+ output_cost_dict = bedrock_anthropic_callback .MODEL_COST_PER_1K_OUTPUT_TOKENS
146+ except Exception as e :
147+ logging .warning (
148+ f"Failed to get Anthropic pricing: { e } . "
149+ "Please install langchain-community or use LiteLLM API for pricing information."
150+ )
151+ return {}
131152
132153 res = {}
133154 for k , v in input_cost_dict .items ():
0 commit comments