Skip to content

Commit

Permalink
fix: switch langchain imports to core
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-kubica committed Jul 20, 2024
1 parent 5650693 commit e17e5ab
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 77 deletions.
13 changes: 7 additions & 6 deletions langfuse/callback/langchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

import pydantic

try: # Test that langchain is installed before proceeding
import langchain # noqa
try: # Test that langchain core is installed before proceeding
import langchain_core # noqa
except ImportError as e:
log = logging.getLogger("langfuse")
log.error(
Expand All @@ -24,11 +24,11 @@
from langfuse.utils.base_callback_handler import LangfuseBaseCallbackHandler

try:
from langchain.callbacks.base import (
from langchain_core.callbacks.base import (
BaseCallbackHandler as LangchainBaseCallbackHandler,
)
from langchain.schema.agent import AgentAction, AgentFinish
from langchain.schema.document import Document
from langchain_core.agents import AgentAction, AgentFinish
from langchain_core.documents import Document
from langchain_core.outputs import (
ChatGeneration,
LLMResult,
Expand All @@ -44,7 +44,8 @@
)
except ImportError:
raise ModuleNotFoundError(
"Please install langchain to use the Langfuse langchain integration: 'pip install langchain'"
"Please install 'langchain core' to use the Langfuse langchain integration:"
" 'pip install langchain-core'"
)


Expand Down
2 changes: 1 addition & 1 deletion langfuse/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

# Attempt to import Serializable
try:
from langchain.load.serializable import Serializable
from langchain_core.load.serializable import Serializable
except ImportError:
# If Serializable is not available, set it to NoneType
Serializable = type(None)
Expand Down
208 changes: 145 additions & 63 deletions poetry.lock

Large diffs are not rendered by default.

9 changes: 6 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pydantic = ">=1.10.7, <3.0"
backoff = ">=1.10.0"
openai = { version = ">=0.27.8", optional = true }
wrapt = "^1.14"
langchain = { version = ">=0.0.309", optional = true }
langchain-core = { version = ">=0.2.0", optional = true }
llama-index = {version = ">=0.10.12, <2.0.0", optional = true}
packaging = "^23.2"
idna = "^3.7"
Expand Down Expand Up @@ -48,13 +48,16 @@ bson = "^0.5.10"
langchain-anthropic = "^0.1.4"
langchain-groq = "^0.1.3"
langchain-aws = "^0.1.3"
langchain = "^0.2.9"
langchain-cohere = "^0.1.9"
langchain-community = "^0.2.7"

[tool.poetry.group.docs.dependencies]
pdoc = "^14.4.0"

[tool.poetry.extras]
openai = ["openai"]
langchain = ["langchain"]
langchain = ["langchain-core"]
llama-index = ["llama-index"]

[build-system]
Expand All @@ -67,4 +70,4 @@ log_cli = true
[tool.poetry_bumpversion.file."langfuse/version.py"]

[tool.poetry.scripts]
release = "scripts.release:main"
release = "scripts.release:main"
2 changes: 1 addition & 1 deletion tests/test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from concurrent.futures import ThreadPoolExecutor
import pytest

from langchain_community.chat_models import ChatOpenAI
from langchain_openai import ChatOpenAI
from langchain.prompts import ChatPromptTemplate
from langfuse.openai import AsyncOpenAI
from langfuse.decorators import langfuse_context, observe
Expand Down
2 changes: 1 addition & 1 deletion tests/test_langchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -1401,7 +1401,7 @@ def test_aws_bedrock_chain():
import os

import boto3
from langchain.llms.bedrock import Bedrock
from langchain_aws import Bedrock

api_wrapper = LangfuseAPI()
handler = CallbackHandler(debug=False)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_langchain_integration.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from langchain_openai import ChatOpenAI, OpenAI
from langchain.prompts import ChatPromptTemplate, PromptTemplate
from langchain.schema import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate, PromptTemplate
from langchain_core.output_parsers import StrOutputParser
import pytest
import types
from langfuse.callback import CallbackHandler
Expand Down

0 comments on commit e17e5ab

Please sign in to comment.