Skip to content

Commit f000d62

Browse files
authored
Merge pull request #282 from ServiceNow/remove-langchain-deps
Remove Langchain Dependency
2 parents b80d731 + c43d7cd commit f000d62

File tree

4 files changed

+38
-258
lines changed

4 files changed

+38
-258
lines changed

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ dependencies = [
3434
"browsergym>=0.7.1",
3535
"joblib>=1.2.0",
3636
"openai>=1.7,<2",
37-
"langchain_community",
3837
"tiktoken",
3938
"huggingface_hub",
4039
"contexttimer",

src/agentlab/llm/llm_utils.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import base64
22
import collections
3+
import importlib
34
import io
45
import json
56
import logging
@@ -16,11 +17,17 @@
1617
import openai
1718
import tiktoken
1819
import yaml
19-
from langchain.schema import BaseMessage as LangchainBaseMessage
20-
from langchain_community.adapters.openai import convert_message_to_dict
2120
from PIL import Image
2221
from transformers import AutoModel, AutoTokenizer
2322

23+
langchain_community = importlib.util.find_spec("langchain_community")
24+
if langchain_community is not None:
25+
from langchain.schema import BaseMessage as LangchainBaseMessage
26+
from langchain_community.adapters.openai import convert_message_to_dict
27+
else:
28+
LangchainBaseMessage = None
29+
convert_message_to_dict = None
30+
2431
if TYPE_CHECKING:
2532
from agentlab.llm.chat_api import ChatModel
2633

@@ -32,7 +39,7 @@ def messages_to_dict(messages: list[dict] | list[LangchainBaseMessage]) -> dict:
3239
new_messages.add_message(m)
3340
elif isinstance(m, str):
3441
new_messages.add_message({"role": "<unknown role>", "content": m})
35-
elif isinstance(m, LangchainBaseMessage):
42+
elif LangchainBaseMessage is not None and isinstance(m, LangchainBaseMessage):
3643
new_messages.add_message(convert_message_to_dict(m))
3744
else:
3845
raise ValueError(f"Unknown message type: {type(m)}")

src/agentlab/llm/tracking.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import importlib
12
import logging
23
import os
34
import re
@@ -9,7 +10,13 @@
910
from typing import Optional
1011

1112
import 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
1320
from litellm import completion_cost, get_model_info
1421

1522
TRACKER = threading.local()
@@ -103,7 +110,14 @@ def get_pricing_openrouter():
103110

104111
def 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

127141
def 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

Comments
 (0)