Skip to content

Commit

Permalink
Updated TestCases
Browse files Browse the repository at this point in the history
Signed-off-by: Yogesh <[email protected]>
  • Loading branch information
yogeshmpandey committed Jan 7, 2025
1 parent f47b70f commit 732cef8
Show file tree
Hide file tree
Showing 12 changed files with 943 additions and 938 deletions.
1 change: 1 addition & 0 deletions comps/integrations/langchain/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__pycache__
Binary file not shown.
Binary file modified comps/integrations/langchain/dist/langchain_opea-0.1.0.tar.gz
Binary file not shown.
24 changes: 23 additions & 1 deletion comps/integrations/langchain/langchain_opea/embeddings.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,29 @@
import openai

class OPEAEmbeddings(OpenAIEmbeddings):
"""`OPEA` OPENAI Compatible Embeddings API."""
"""`OPEA` OPENAI Compatible Embeddings API.
Instantiate:
.. code-block:: python
from langchain_opea import OPEAEmbeddings
embeddings = OPEAEmbeddings(
model="text-embedding-3-large"
opea_api_base="https://<your-opea-endpoint>",
opea_api_keyy=... # Can provide an API key directly.
)
Embed single text:
.. code-block:: python
input_text = "The meaning of life is 42"
vector = embed.embed_query(input_text)
print(vector[:3])
.. code-block:: python
[-0.024603435769677162, -0.007543657906353474, 0.0039630369283258915]
"""

model_name: str = Field(alias="model")
"""Model name to use."""
Expand Down
6 changes: 4 additions & 2 deletions comps/integrations/langchain/langchain_opea/llms.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class OPEALLM(BaseOpenAI): # type: ignore[override]
OPEALLM is a class to interact with OPEA OpenAI compatible large
language model endpoints.
To use, you should have the environment variable ``OPEA_API_TOKEN`` set
To use, you should have the environment variable ``opea_api_key`` set
with your API token, or pass it as a named parameter to the constructor.
Example:
Expand Down Expand Up @@ -45,6 +45,8 @@ class OPEALLM(BaseOpenAI): # type: ignore[override]
opea_api_base: str = Field(default="http://localhost:8080/v1/")
"""Base URL path for API requests."""

top_p: float = 0.6

@classmethod
def get_lc_namespace(cls) -> List[str]:
"""Get the namespace of the langchain object."""
Expand Down Expand Up @@ -93,7 +95,7 @@ def validate_environment(self) -> Self:

@property
def _invocation_params(self) -> Dict[str, Any]:
openai_params = {"model": self.model_name}
openai_params = {"model": self.model_name, "top_p": self.top_p if self.top_p else 0.6}
return {**openai_params, **super()._invocation_params}

@property
Expand Down
1,735 changes: 818 additions & 917 deletions comps/integrations/langchain/poetry.lock

Large diffs are not rendered by default.

54 changes: 50 additions & 4 deletions comps/integrations/langchain/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,70 @@ build-backend = "poetry.core.masonry.api"
[tool.poetry]
name = "langchain-opea"
version = "0.1.0"
description = "An integration package connecting OPEA Microservices and LangChain"
description = "An integration package connecting OPEA and LangChain"
authors = []
readme = "README.md"
repository = "https://github.com/opea-project"
repository = "https://github.com/langchain-ai/langchain"
license = "MIT"

[tool.mypy]
disallow_untyped_defs = "True"

[tool.poetry.urls]
"Source Code" = "https://github.com/langchain-ai/langchain/tree/master/libs/partners/opea"
"Release Notes" = "https://github.com/langchain-ai/langchain/releases?q=tag%3A%22opea%3D%3D0%22&expanded=true"

[tool.poetry.dependencies]
python = ">=3.9,<4.0"
langchain-openai = ">=0.2,<0.3"
langchain-openai = ">=0.2.14,<0.3"
langchain-core = ">=0.3.29,<0.4"
requests = ">=2,<3"
aiohttp = ">=3.9.1,<4"

[tool.ruff.lint]
select = ["E", "F", "I", "D"]
select = ["E", "F", "I", "T201"]

[tool.coverage.run]
omit = ["tests/*"]

[tool.pytest.ini_options]
addopts = "--strict-markers --strict-config --durations=5"
markers = [
"compile: mark placeholder test used to compile integration tests without running them",
]
asyncio_mode = "auto"

[tool.poetry.group.test]
optional = true

[tool.poetry.group.codespell]
optional = true

[tool.poetry.group.test_integration]
optional = true

[tool.poetry.group.lint]
optional = true

[tool.poetry.group.dev]
optional = true

[tool.poetry.group.dev.dependencies]

[tool.poetry.group.test.dependencies]
pytest = "^7.4.3"
pytest-asyncio = "^0.23.2"
pytest-socket = "^0.7.0"
pytest-watcher = "^0.3.4"
langchain-tests = "^0.3.5"

[tool.poetry.group.codespell.dependencies]
codespell = "^2.2.6"

[tool.poetry.group.test_integration.dependencies]

[tool.poetry.group.lint.dependencies]
ruff = "^0.5"

[tool.poetry.group.typing.dependencies]
mypy = "^1.10"
4 changes: 2 additions & 2 deletions comps/integrations/langchain/scripts/check_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
SourceFileLoader("x", file).load_module()
except Exception:
has_failure = True
print(file)
print(file) # noqa: T201
traceback.print_exc()
print()
print() # noqa: T201

sys.exit(1 if has_failure else 0)
18 changes: 18 additions & 0 deletions comps/integrations/langchain/scripts/lint_imports.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash

set -eu

# Initialize a variable to keep track of errors
errors=0

# make sure not importing from langchain, langchain_experimental, or langchain_community
git --no-pager grep '^from langchain\.' . && errors=$((errors+1))
git --no-pager grep '^from langchain_experimental\.' . && errors=$((errors+1))
git --no-pager grep '^from langchain_community\.' . && errors=$((errors+1))

# Decide on an exit status based on the errors
if [ "$errors" -gt 0 ]; then
exit 1
else
exit 0
fi
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

from langchain_opea.chat_models import ChatOPEA

OPEA_API_BASE="http://localhost:8080/v1"
OPEA_API_BASE="http://localhost:9009/v1"
OPEA_API_KEY="my_secret_value"
MODEL_NAME="mistralai/Mistral-7B-Instruct-v0.3"
MODEL_NAME="Intel/neural-chat-7b-v3-3"

class TestChatOPEA(ChatModelIntegrationTests):
@property
Expand All @@ -27,7 +27,24 @@ def chat_model_params(self) -> dict:

@property
def supports_image_inputs(self) -> bool:
return True
return False

@property
def has_tool_calling(self) -> bool:
return False

@property
def supports_image_tool_message(self) -> bool:
return False

@property
def returns_usage_metadata(self) -> bool:
return False

@property
def supports_anthropic_inputs(self) -> bool:
return False
supports_anthropic_inputs

@pytest.mark.xfail(
reason=(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

from langchain_opea.llms import OPEALLM

OPEA_API_BASE="http://localhost:8080/v1"
OPEA_API_BASE="http://localhost:9009/v1"
OPEA_API_KEY="my_secret_value"
MODEL_NAME="mistralai/Mistral-7B-Instruct-v0.3"
MODEL_NAME="Intel/neural-chat-7b-v3-3"


def test_stream() -> None:
"""Test streaming tokens from OpenAI."""
llm = OPEALLM( opea_api_base=OPEA_API_BASE,
llm = OPEALLM(opea_api_base=OPEA_API_BASE,
opea_api_key=OPEA_API_KEY,
model_name=MODEL_NAME)

Expand All @@ -19,7 +19,7 @@ def test_stream() -> None:

async def test_astream() -> None:
"""Test streaming tokens from OpenAI."""
llm = OPEALLM( opea_api_base=OPEA_API_BASE,
llm = OPEALLM(opea_api_base=OPEA_API_BASE,
opea_api_key=OPEA_API_KEY,
model_name=MODEL_NAME)

Expand Down Expand Up @@ -64,7 +64,7 @@ def test_batch() -> None:

async def test_ainvoke() -> None:
"""Test invoke tokens from OPEALLM."""
llm = OPEALLM( opea_api_base=OPEA_API_BASE,
llm = OPEALLM(opea_api_base=OPEA_API_BASE,
opea_api_key=OPEA_API_KEY,
model_name=MODEL_NAME)

Expand All @@ -74,7 +74,7 @@ async def test_ainvoke() -> None:

def test_invoke() -> None:
"""Test invoke tokens from OPEALLM."""
llm = OPEALLM( opea_api_base=OPEA_API_BASE,
llm = OPEALLM(opea_api_base=OPEA_API_BASE,
opea_api_key=OPEA_API_KEY,
model_name=MODEL_NAME)

Expand Down
4 changes: 1 addition & 3 deletions comps/integrations/langchain/tests/unit_tests/test_llms.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ def test_initialize_opeallm_bad_path_without_api_base() -> None:



def test_initialize_opeallm_bad_path_without_api_key

() -> None:
def test_initialize_opeallm_bad_path_without_api_key() -> None:
try:
OPEALLM(
opea_api_base=OPEA_API_BASE,
Expand Down

0 comments on commit 732cef8

Please sign in to comment.