Skip to content

Commit

Permalink
add langchain_community to optional imports and checks for use of ope…
Browse files Browse the repository at this point in the history
…nai endpoint (#1154)

* first

* adjust docs

* logic

* add langhain_community to pinecone reqs too

* adjust trubot example with new reqs

* more reqs

* remove extra text

* add langchain_community to a few imports in notebooks

---------

Co-authored-by: Piotr Mardziel <[email protected]>
Co-authored-by: Josh Reini <[email protected]>
  • Loading branch information
3 people authored May 24, 2024
1 parent cf8cd67 commit 8487b2c
Show file tree
Hide file tree
Showing 13 changed files with 41 additions and 36 deletions.
2 changes: 1 addition & 1 deletion docs/trulens_eval/all_tools.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"metadata": {},
"outputs": [],
"source": [
"# ! pip install trulens_eval openai langchain langchain-openai faiss-cpu bs4 tiktoken"
"# ! pip install trulens_eval openai langchain langchain-openai langchain_community faiss-cpu bs4 tiktoken"
]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"## Additional requirements:\n",
"\n",
"```bash\n",
"pip install humanize pdfreader url_normalize tabulate unstructured\n",
"pip install humanize pdfreader url_normalize tabulate unstructured langchain_community\n",
"```"
]
},
Expand All @@ -20,7 +20,7 @@
"metadata": {},
"outputs": [],
"source": [
"# ! pip install humanize pdfreader url_normalize tabulate unstructured"
"# ! pip install humanize pdfreader url_normalize tabulate unstructured langchain_community"
]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### Import from LlamaIndex and TruLens"
"### Import from _LangChain_ and TruLens"
]
},
{
Expand All @@ -28,7 +28,7 @@
"metadata": {},
"outputs": [],
"source": [
"# ! pip install trulens_eval==0.18.1 langchain>=0.0.342"
"# ! pip install trulens_eval==0.18.1 langchain>=0.0.342 langchain_community"
]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"metadata": {},
"outputs": [],
"source": [
"# ! pip install trulens_eval==0.11.0 langchain==0.0.283"
"# ! pip install trulens_eval==0.11.0 langchain==0.0.283 langchain_community"
]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"metadata": {},
"outputs": [],
"source": [
"# ! pip install trulens_eval==0.11.0 langchain==0.0.283"
"# ! pip install trulens_eval==0.11.0 langchain==0.0.283 langchain_community"
]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"metadata": {},
"outputs": [],
"source": [
"#!pip install langchain==0.0.354 ,langchain-community==0.0.20 ,langchain-core==0.1.23,trulens_eval"
"#!pip install 'langchain==0.0.354' 'langchain-community==0.0.20' 'langchain-core==0.1.23' 'trulens_eval'"
]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"metadata": {},
"outputs": [],
"source": [
"# ! pip install trulens_eval==0.14.0 langchain>=0.0.263"
"# ! pip install 'trulens_eval==0.14.0' 'langchain>=0.0.263' langchain_community"
]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"metadata": {},
"outputs": [],
"source": [
"!pip install -qU trulens-eval==0.16.0 langchain==0.0.315 openai==0.28.1 tiktoken==0.5.1 \"pinecone-client[grpc]==2.2.4\" pinecone-datasets==0.5.1 datasets==2.14.5"
"!pip install -qU trulens-eval==0.16.0 langchain==0.0.315 openai==0.28.1 tiktoken==0.5.1 \"pinecone-client[grpc]==2.2.4\" pinecone-datasets==0.5.1 datasets==2.14.5 langchain_community"
]
},
{
Expand Down
4 changes: 2 additions & 2 deletions trulens_eval/examples/quickstart/langchain_quickstart.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"metadata": {},
"outputs": [],
"source": [
"# ! pip install trulens_eval openai langchain langchain-openai faiss-cpu bs4 tiktoken"
"# ! pip install trulens_eval openai langchain langchain-openai langchain_community faiss-cpu bs4 tiktoken"
]
},
{
Expand Down Expand Up @@ -434,7 +434,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.14"
"version": "3.12.3"
},
"vscode": {
"interpreter": {
Expand Down
13 changes: 8 additions & 5 deletions trulens_eval/trulens_eval/Example_TruBot.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
os.environ['PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION'] = 'python'

from langchain.chains import ConversationalRetrievalChain
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.memory import ConversationSummaryBufferMemory
from langchain_community.callbacks import get_openai_callback
from langchain_community.llms import OpenAI
from langchain_community.vectorstores import Pinecone

import numpy as np
import streamlit as st

Expand All @@ -22,8 +19,14 @@

with OptionalImports(messages=REQUIREMENT_PINECONE):
import pinecone
import langchain_community
from langchain_community.callbacks import get_openai_callback
from langchain_community.llms import OpenAI
from langchain.embeddings.openai import OpenAIEmbeddings

OptionalImports(messages=REQUIREMENT_PINECONE).assert_installed(pinecone)
OptionalImports(messages=REQUIREMENT_PINECONE).assert_installed(
mods=[pinecone, langchain_community, OpenAIEmbeddings]
)

check_keys("OPENAI_API_KEY", "PINECONE_API_KEY", "PINECONE_ENV")

Expand Down
10 changes: 7 additions & 3 deletions trulens_eval/trulens_eval/feedback/provider/endpoint/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import pprint
from typing import Any, Callable, ClassVar, Dict, List, Optional, Union

from langchain.callbacks.openai_info import OpenAICallbackHandler
from langchain.schema import Generation
from langchain.schema import LLMResult
import pydantic
Expand All @@ -44,8 +43,13 @@
with OptionalImports(messages=REQUIREMENT_OPENAI):
import openai as oai

# check that oai is not a dummy:
OptionalImports(messages=REQUIREMENT_OPENAI).assert_installed(oai)
# This is also required for running openai endpoints in trulens_eval:
from langchain.callbacks.openai_info import OpenAICallbackHandler

# check that oai is not a dummy, also the langchain component required for handling openai endpoint
OptionalImports(messages=REQUIREMENT_OPENAI).assert_installed(
mods=[oai, OpenAICallbackHandler]
)

logger = logging.getLogger(__name__)

Expand Down
2 changes: 1 addition & 1 deletion trulens_eval/trulens_eval/requirements.optional.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ evaluate >= 0.4.0 # same

# Apps: LangChain (optional parts)
langchainhub >= 0.1.14 # example notebooks
langchain_community >= 0.0.17 # example notebooks, now required for Pinecone, others
langchain_community >= 0.0.17 # example notebooks, openai endpoint, now required for Pinecone, others
langchain_openai >= 0.0.5 # unit tests, sample apps

# Apps: LlamaIndex
Expand Down
28 changes: 13 additions & 15 deletions trulens_eval/trulens_eval/utils/imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@
from pathlib import Path
from pprint import PrettyPrinter
import sys
from typing import Any, Dict, Optional, Sequence, Type, Union
from typing import Any, Dict, Iterable, Optional, Sequence, Type, Union

from packaging import requirements
from packaging import version
from pip._internal.req import parse_requirements

from trulens_eval import __name__ as trulens_name
from trulens_eval.utils.text import retab

logger = logging.getLogger(__name__)
pp = PrettyPrinter()
Expand Down Expand Up @@ -338,7 +337,7 @@ def format_import_errors(

REQUIREMENT_PINECONE = format_import_errors(
# package name is "pinecone-client" but module is "pinecone"
'pinecone-client',
['pinecone-client', 'langchain_community'],
purpose="running TruBot"
)

Expand All @@ -355,7 +354,7 @@ def format_import_errors(
)

REQUIREMENT_OPENAI = format_import_errors(
'openai', purpose="using OpenAI models"
['openai', 'langchain_community'], purpose="using OpenAI models"
)

REQUIREMENT_GROUNDEDNESS = format_import_errors(
Expand Down Expand Up @@ -445,7 +444,7 @@ def __init__(self, name: str, *args, **kwargs):
self.original_exception = original_exception

def __call__(self, *args, **kwargs):
raise self.exception_class(self.message)
raise self.exception_class(self.message) from self.original_exception

def __instancecheck__(self, __instance: Any) -> bool:
"""Nothing is an instance of this dummy.
Expand All @@ -463,7 +462,7 @@ def __subclasscheck__(self, __subclass: type) -> bool:
return False

def _wasused(self, *args, **kwargs):
raise self.exception_class(self.message)
raise self.exception_class(self.message) from self.original_exception

# If someone tries to use dummy in an expression, raise our usage exception:
__add__ = _wasused
Expand Down Expand Up @@ -536,12 +535,15 @@ class OptionalImports(object):
specified message (unless llama_index is installed of course).
"""

def assert_installed(self, mod):
def assert_installed(self, mods: Union[Any, Iterable[Any]]):
"""
Check that the given module `mod` is not a dummy. If it is, show the
Check that the given modules `mods` are not dummies. If any is, show the
optional requirement message.
"""
if isinstance(mod, Dummy):
if not isinstance(mods, Iterable):
mods = [mods]

if any(isinstance(mod, Dummy) for mod in mods):
raise ModuleNotFoundError(self.messages.module_not_found)

def __init__(self, messages: ImportErrorMessages, fail: bool = False):
Expand Down Expand Up @@ -659,9 +661,7 @@ def __exit__(self, exc_type, exc_value, exc_tb):
raise exc_value

raise ModuleNotFoundError(
self.messages.module_not_found +
"\nError that caused this problem:\n\n" +
retab(tab=" ", s=repr(exc_value))
self.messages.module_not_found
) from exc_value

elif isinstance(exc_value, ImportError):
Expand All @@ -670,9 +670,7 @@ def __exit__(self, exc_type, exc_value, exc_tb):
raise exc_value

raise ImportError(
self.messages.import_error +
"\nError that caused this problem:\n\n" +
retab(tab=" ", s=repr(exc_value))
self.messages.import_error
) from exc_value

# Exception will be propagated unless we return True so we don't return it.

0 comments on commit 8487b2c

Please sign in to comment.