Skip to content

Commit

Permalink
langchain[patch]: Fix scheduled testing (#14428)
Browse files Browse the repository at this point in the history
- integration tests in pyproject
- integration test fixes
  • Loading branch information
efriis authored Dec 8, 2023
1 parent 7be3eb6 commit 1d72532
Show file tree
Hide file tree
Showing 9 changed files with 316 additions and 140 deletions.
6 changes: 0 additions & 6 deletions .github/workflows/scheduled_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,6 @@ jobs:
run: |
echo "Running scheduled tests, installing dependencies with poetry..."
poetry install --with=test_integration
poetry run pip install google-cloud-aiplatform
poetry run pip install "boto3>=1.28.57"
if [[ ${{ matrix.python-version }} != "3.8" ]]
then
poetry run pip install fireworks-ai
fi
- name: Run tests
shell: bash
Expand Down
377 changes: 276 additions & 101 deletions libs/langchain/poetry.lock

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion libs/langchain/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ rspace_client = {version = "^2.5.0", optional = true}
upstash-redis = {version = "^0.15.0", optional = true}
azure-ai-textanalytics = {version = "^5.3.0", optional = true}
google-cloud-documentai = {version = "^2.20.1", optional = true}
fireworks-ai = {version = "^0.6.0", optional = true, python = ">=3.9,<4.0"}
fireworks-ai = {version = "^0.9.0", optional = true}
javelin-sdk = {version = "^0.1.8", optional = true}
hologres-vector = {version = "^0.0.6", optional = true}
praw = {version = "^7.7.1", optional = true}
Expand Down Expand Up @@ -205,6 +205,9 @@ python-dotenv = "^1.0.0"
cassio = "^0.1.0"
tiktoken = "^0.3.2"
anthropic = "^0.3.11"
fireworks-ai = "^0.9.0"
boto3 = ">=1.28.57,<2"
google-cloud-aiplatform = ">=1.37.0,<2"

[tool.poetry.group.lint]
optional = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@
OPENAI_API_VERSION = os.environ.get("AZURE_OPENAI_API_VERSION", "")
OPENAI_API_BASE = os.environ.get("AZURE_OPENAI_API_BASE", "")
OPENAI_API_KEY = os.environ.get("AZURE_OPENAI_API_KEY", "")
DEPLOYMENT_NAME = os.environ.get("AZURE_OPENAI_DEPLOYMENT_NAME", "")
DEPLOYMENT_NAME = os.environ.get(
"AZURE_OPENAI_DEPLOYMENT_NAME",
os.environ.get("AZURE_OPENAI_CHAT_DEPLOYMENT_NAME", ""),
)


def _get_llm(**kwargs: Any) -> AzureChatOpenAI:
return AzureChatOpenAI(
deployment_name=DEPLOYMENT_NAME,
openai_api_version=OPENAI_API_VERSION,
openai_api_base=OPENAI_API_BASE,
azure_endpoint=OPENAI_API_BASE,
openai_api_key=OPENAI_API_KEY,
**kwargs,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,32 +102,25 @@ def test_fireworks_batch(chat: ChatFireworks) -> None:
"""Test batch tokens from ChatFireworks."""
result = chat.batch(
[
"What is the weather in Redwood City, CA today",
"What is the weather in Redwood City, CA today",
"What is the weather in Redwood City, CA today",
"What is the weather in Redwood City, CA today",
"What is the weather in Redwood City, CA today",
"What is the weather in Redwood City, CA today",
"What is the weather in Redwood City, CA today?",
"What is the weather in Redwood City, CA today?",
"What is the weather in Redwood City, CA today?",
],
config={"max_concurrency": 5},
config={"max_concurrency": 2},
stop=[","],
)
for token in result:
assert isinstance(token.content, str)
assert token.content[-1] == ","
assert token.content[-1] == ",", token.content


@pytest.mark.scheduled
async def test_fireworks_abatch(chat: ChatFireworks) -> None:
"""Test batch tokens from ChatFireworks."""
result = await chat.abatch(
[
"What is the weather in Redwood City, CA today",
"What is the weather in Redwood City, CA today",
"What is the weather in Redwood City, CA today",
"What is the weather in Redwood City, CA today",
"What is the weather in Redwood City, CA today",
"What is the weather in Redwood City, CA today",
"What is the weather in Redwood City, CA today?",
"What is the weather in Redwood City, CA today?",
],
config={"max_concurrency": 5},
stop=[","],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ def test_chat_openai_generate() -> None:
assert isinstance(response, LLMResult)
assert len(response.generations) == 2
assert response.llm_output
assert "system_fingerprint" in response.llm_output
for generations in response.generations:
assert len(generations) == 2
for generation in generations:
Expand Down Expand Up @@ -178,7 +177,6 @@ async def test_async_chat_openai() -> None:
assert isinstance(response, LLMResult)
assert len(response.generations) == 2
assert response.llm_output
assert "system_fingerprint" in response.llm_output
for generations in response.generations:
assert len(generations) == 2
for generation in generations:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,26 @@

from langchain.embeddings import AzureOpenAIEmbeddings

OPENAI_API_VERSION = os.environ.get("AZURE_OPENAI_API_VERSION", "")
OPENAI_API_BASE = os.environ.get("AZURE_OPENAI_API_BASE", "")
OPENAI_API_KEY = os.environ.get("AZURE_OPENAI_API_KEY", "")
DEPLOYMENT_NAME = os.environ.get(
"AZURE_OPENAI_DEPLOYMENT_NAME",
os.environ.get("AZURE_OPENAI_EMBEDDINGS_DEPLOYMENT_NAME", ""),
)


def _get_embeddings(**kwargs: Any) -> AzureOpenAIEmbeddings:
return AzureOpenAIEmbeddings(
openai_api_version=os.environ.get("AZURE_OPENAI_API_VERSION", ""),
azure_deployment=DEPLOYMENT_NAME,
api_version=OPENAI_API_VERSION,
openai_api_base=OPENAI_API_BASE,
openai_api_key=OPENAI_API_KEY,
**kwargs,
)


@pytest.mark.scheduled
def test_azure_openai_embedding_documents() -> None:
"""Test openai embeddings."""
documents = ["foo bar"]
Expand All @@ -24,6 +36,7 @@ def test_azure_openai_embedding_documents() -> None:
assert len(output[0]) == 1536


@pytest.mark.scheduled
def test_azure_openai_embedding_documents_multiple() -> None:
"""Test openai embeddings."""
documents = ["foo bar", "bar foo", "foo"]
Expand All @@ -37,6 +50,7 @@ def test_azure_openai_embedding_documents_multiple() -> None:
assert len(output[2]) == 1536


@pytest.mark.scheduled
def test_azure_openai_embedding_documents_chunk_size() -> None:
"""Test openai embeddings."""
documents = ["foo bar"] * 20
Expand All @@ -49,6 +63,7 @@ def test_azure_openai_embedding_documents_chunk_size() -> None:
assert all([len(out) == 1536 for out in output])


@pytest.mark.scheduled
async def test_azure_openai_embedding_documents_async_multiple() -> None:
"""Test openai embeddings."""
documents = ["foo bar", "bar foo", "foo"]
Expand All @@ -61,6 +76,7 @@ async def test_azure_openai_embedding_documents_async_multiple() -> None:
assert len(output[2]) == 1536


@pytest.mark.scheduled
def test_azure_openai_embedding_query() -> None:
"""Test openai embeddings."""
document = "foo bar"
Expand All @@ -69,6 +85,7 @@ def test_azure_openai_embedding_query() -> None:
assert len(output) == 1536


@pytest.mark.scheduled
async def test_azure_openai_embedding_async_query() -> None:
"""Test openai embeddings."""
document = "foo bar"
Expand All @@ -94,11 +111,13 @@ def test_azure_openai_embedding_with_empty_string() -> None:
assert len(output[1]) == 1536


@pytest.mark.scheduled
def test_embed_documents_normalized() -> None:
output = _get_embeddings().embed_documents(["foo walked to the market"])
assert np.isclose(np.linalg.norm(output[0]), 1.0)


@pytest.mark.scheduled
def test_embed_query_normalized() -> None:
output = _get_embeddings().embed_query("foo walked to the market")
assert np.isclose(np.linalg.norm(output), 1.0)
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
OPENAI_API_VERSION = os.environ.get("AZURE_OPENAI_API_VERSION", "")
OPENAI_API_BASE = os.environ.get("AZURE_OPENAI_API_BASE", "")
OPENAI_API_KEY = os.environ.get("AZURE_OPENAI_API_KEY", "")
DEPLOYMENT_NAME = os.environ.get("AZURE_OPENAI_DEPLOYMENT_NAME", "")
DEPLOYMENT_NAME = os.environ.get(
"AZURE_OPENAI_DEPLOYMENT_NAME",
os.environ.get("AZURE_OPENAI_LLM_DEPLOYMENT_NAME", ""),
)


def _get_llm(**kwargs: Any) -> AzureOpenAI:
Expand All @@ -25,7 +28,6 @@ def _get_llm(**kwargs: Any) -> AzureOpenAI:
)


@pytest.mark.scheduled
@pytest.fixture
def llm() -> AzureOpenAI:
return _get_llm(
Expand Down
11 changes: 0 additions & 11 deletions libs/langchain/tests/integration_tests/llms/test_fireworks.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Test Fireworks AI API Wrapper."""
import sys
from typing import Generator

import pytest
Expand All @@ -13,9 +12,6 @@
from langchain.chains import LLMChain
from langchain.llms.fireworks import Fireworks

if sys.version_info < (3, 9):
pytest.skip("fireworks-ai requires Python > 3.8", allow_module_level=True)


@pytest.fixture
def llm() -> Fireworks:
Expand Down Expand Up @@ -71,14 +67,10 @@ async def test_fireworks_ainvoke(llm: Fireworks) -> None:
@pytest.mark.scheduled
def test_fireworks_batch(llm: Fireworks) -> None:
"""Tests completion with invoke"""
llm = Fireworks()
output = llm.batch(
[
"How is the weather in New York today?",
"How is the weather in New York today?",
"How is the weather in New York today?",
"How is the weather in New York today?",
"How is the weather in New York today?",
],
stop=[","],
)
Expand All @@ -94,9 +86,6 @@ async def test_fireworks_abatch(llm: Fireworks) -> None:
[
"How is the weather in New York today?",
"How is the weather in New York today?",
"How is the weather in New York today?",
"How is the weather in New York today?",
"How is the weather in New York today?",
],
stop=[","],
)
Expand Down

0 comments on commit 1d72532

Please sign in to comment.