Skip to content

Commit

Permalink
Use langchain and py39 version gating correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
Yun-Kim committed Jul 27, 2024
1 parent e74c9f8 commit d35e50e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 116 deletions.
115 changes: 33 additions & 82 deletions tests/contrib/langchain/test_langchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
parse_version(_langchain.__version__) >= (0, 1, 0), reason="This module only tests langchain < 0.1"
)

PY39 = sys.version_info <= (3, 9)


@pytest.fixture(scope="session")
def request_vcr():
Expand All @@ -32,10 +34,7 @@ def test_global_tags(ddtrace_config_langchain, langchain, request_vcr, mock_metr
"""
llm = langchain.llms.OpenAI(model="text-davinci-003")
with override_global_config(dict(service="test-svc", env="staging", version="1234")):
if sys.version_info >= (3, 10):
cassette_name = "openai_completion_sync.yaml"
else:
cassette_name = "openai_completion_sync_39.yaml"
cassette_name = "openai_completion_sync_39.yaml" if PY39 else "openai_completion_sync.yaml"
with request_vcr.use_cassette(cassette_name):
llm("What does Nietzsche mean by 'God is dead'?")

Expand Down Expand Up @@ -75,23 +74,23 @@ def test_global_tags(ddtrace_config_langchain, langchain, request_vcr, mock_metr
)


@pytest.mark.skipif(sys.version_info < (3, 10), reason="Python 3.10+ specific test")
@pytest.mark.skipif(PY39, reason="Python 3.10+ specific test")
@pytest.mark.snapshot(ignores=["metrics.langchain.tokens.total_cost", "resource"])
def test_openai_llm_sync(langchain, request_vcr):
llm = langchain.llms.OpenAI(model="text-davinci-003")
with request_vcr.use_cassette("openai_completion_sync.yaml"):
llm("Can you explain what Descartes meant by 'I think, therefore I am'?")


@pytest.mark.skipif(sys.version_info >= (3, 10), reason="Python 3.9 specific test")
@pytest.mark.skipif(not PY39, reason="Python 3.9 specific test")
@pytest.mark.snapshot(ignores=["metrics.langchain.tokens.total_cost"])
def test_openai_llm_sync_39(langchain, request_vcr):
llm = langchain.llms.OpenAI(model="text-davinci-003")
with request_vcr.use_cassette("openai_completion_sync_39.yaml"):
llm("Can you explain what Descartes meant by 'I think, therefore I am'?")


@pytest.mark.skipif(sys.version_info < (3, 10), reason="Python 3.10+ specific test")
@pytest.mark.skipif(PY39, reason="Python 3.10+ specific test")
@pytest.mark.snapshot(ignores=["resource"])
def test_openai_llm_sync_multiple_prompts(langchain, request_vcr):
llm = langchain.llms.OpenAI(model="text-davinci-003")
Expand All @@ -104,7 +103,7 @@ def test_openai_llm_sync_multiple_prompts(langchain, request_vcr):
)


@pytest.mark.skipif(sys.version_info >= (3, 10), reason="Python 3.9 specific test")
@pytest.mark.skipif(not PY39, reason="Python 3.9 specific test")
@pytest.mark.snapshot
def test_openai_llm_sync_multiple_prompts_39(langchain, request_vcr):
llm = langchain.llms.OpenAI(model="text-davinci-003")
Expand All @@ -121,10 +120,7 @@ def test_openai_llm_sync_multiple_prompts_39(langchain, request_vcr):
@pytest.mark.snapshot(ignores=["resource", "langchain.request.openai.parameters.request_timeout"])
async def test_openai_llm_async(langchain, request_vcr):
llm = langchain.llms.OpenAI(model="text-davinci-003")
if sys.version_info >= (3, 10):
cassette_name = "openai_completion_async.yaml"
else:
cassette_name = "openai_completion_async_39.yaml"
cassette_name = "openai_completion_async_39.yaml" if PY39 else "openai_completion_async.yaml"
with request_vcr.use_cassette(cassette_name):
await llm.agenerate(["Which team won the 2019 NBA finals?"])

Expand Down Expand Up @@ -165,20 +161,14 @@ def test_huggingfacehub_llm_sync(langchain, request_vcr):
@pytest.mark.snapshot(ignores=["meta.langchain.response.completions.0.text", "resource"])
def test_ai21_llm_sync(langchain, request_vcr):
llm = langchain.llms.AI21(ai21_api_key=os.getenv("AI21_API_KEY", "<not-a-real-key>"))
if sys.version_info >= (3, 10):
cassette_name = "ai21_completion_sync.yaml"
else:
cassette_name = "ai21_completion_sync_39.yaml"
cassette_name = "ai21_completion_sync_39.yaml" if PY39 else "ai21_completion_sync.yaml"
with request_vcr.use_cassette(cassette_name):
llm("Why does everyone in Bikini Bottom hate Plankton?")


def test_openai_llm_metrics(langchain, request_vcr, mock_metrics, mock_logs, snapshot_tracer):
llm = langchain.llms.OpenAI(model="text-davinci-003")
if sys.version_info >= (3, 10):
cassette_name = "openai_completion_sync.yaml"
else:
cassette_name = "openai_completion_sync_39.yaml"
cassette_name = "openai_completion_sync_39.yaml" if PY39 else "openai_completion_sync.yaml"
with request_vcr.use_cassette(cassette_name):
llm("Can you explain what Descartes meant by 'I think, therefore I am'?")
expected_tags = [
Expand Down Expand Up @@ -210,10 +200,7 @@ def test_openai_llm_metrics(langchain, request_vcr, mock_metrics, mock_logs, sna
)
def test_llm_logs(langchain, ddtrace_config_langchain, request_vcr, mock_logs, mock_metrics, mock_tracer):
llm = langchain.llms.OpenAI(model="text-davinci-003")
if sys.version_info >= (3, 10):
cassette_name = "openai_completion_sync.yaml"
else:
cassette_name = "openai_completion_sync_39.yaml"
cassette_name = "openai_completion_sync_39.yaml" if PY39 else "openai_completion_sync.yaml"
with request_vcr.use_cassette(cassette_name):
llm("Can you explain what Descartes meant by 'I think, therefore I am'?")
span = mock_tracer.pop_traces()[0][0]
Expand All @@ -240,7 +227,7 @@ def test_llm_logs(langchain, ddtrace_config_langchain, request_vcr, mock_logs, m
mock_metrics.count.assert_not_called()


@pytest.mark.skipif(sys.version_info < (3, 10), reason="Python 3.10+ specific test")
@pytest.mark.skipif(PY39, reason="Python 3.10+ specific test")
@pytest.mark.snapshot(
token="tests.contrib.langchain.test_langchain.test_openai_chat_model_call",
ignores=["metrics.langchain.tokens.total_cost", "resource"],
Expand All @@ -251,15 +238,15 @@ def test_openai_chat_model_sync_call(langchain, request_vcr):
chat(messages=[langchain.schema.HumanMessage(content="When do you use 'whom' instead of 'who'?")])


@pytest.mark.skipif(sys.version_info >= (3, 10), reason="Python 3.9 specific test")
@pytest.mark.skipif(not PY39, reason="Python 3.9 specific test")
@pytest.mark.snapshot(ignores=["metrics.langchain.tokens.total_cost"])
def test_openai_chat_model_sync_call_39(langchain, request_vcr):
chat = langchain.chat_models.ChatOpenAI(temperature=0, max_tokens=256)
with request_vcr.use_cassette("openai_chat_completion_sync_call_39.yaml"):
chat([langchain.schema.HumanMessage(content="When do you use 'whom' instead of 'who'?")])


@pytest.mark.skipif(sys.version_info < (3, 10), reason="Python 3.10+ specific test")
@pytest.mark.skipif(PY39, reason="Python 3.10+ specific test")
@pytest.mark.snapshot(
token="tests.contrib.langchain.test_langchain.test_openai_chat_model_generate",
ignores=["metrics.langchain.tokens.total_cost", "resource"],
Expand All @@ -283,7 +270,7 @@ def test_openai_chat_model_sync_generate(langchain, request_vcr):
)


@pytest.mark.skipif(sys.version_info >= (3, 10), reason="Python 3.9 specific test")
@pytest.mark.skipif(not PY39, reason="Python 3.9 specific test")
@pytest.mark.snapshot(ignores=["metrics.langchain.tokens.total_cost"])
def test_openai_chat_model_sync_generate_39(langchain, request_vcr):
chat = langchain.chat_models.ChatOpenAI(temperature=0, max_tokens=256)
Expand Down Expand Up @@ -341,10 +328,7 @@ async def test_openai_chat_model_async_generate(langchain, request_vcr):

def test_chat_model_metrics(langchain, request_vcr, mock_metrics, mock_logs, snapshot_tracer):
chat = langchain.chat_models.ChatOpenAI(temperature=0, max_tokens=256)
if sys.version_info >= (3, 10):
cassette_name = "openai_chat_completion_sync_call.yaml"
else:
cassette_name = "openai_chat_completion_sync_call_39.yaml"
cassette_name = "openai_chat_completion_sync_call_39.yaml" if PY39 else "openai_chat_completion_sync_call.yaml"
with request_vcr.use_cassette(cassette_name):
chat([langchain.schema.HumanMessage(content="When do you use 'whom' instead of 'who'?")])
expected_tags = [
Expand Down Expand Up @@ -376,10 +360,7 @@ def test_chat_model_metrics(langchain, request_vcr, mock_metrics, mock_logs, sna
)
def test_chat_model_logs(langchain, ddtrace_config_langchain, request_vcr, mock_logs, mock_metrics, mock_tracer):
chat = langchain.chat_models.ChatOpenAI(temperature=0, max_tokens=256)
if sys.version_info >= (3, 10):
cassette_name = "openai_chat_completion_sync_call.yaml"
else:
cassette_name = "openai_chat_completion_sync_call_39.yaml"
cassette_name = "openai_chat_completion_sync_call_39.yaml" if PY39 else "openai_chat_completion_sync_call.yaml"
with request_vcr.use_cassette(cassette_name):
chat([langchain.schema.HumanMessage(content="When do you use 'whom' instead of 'who'?")])
span = mock_tracer.pop_traces()[0][0]
Expand Down Expand Up @@ -409,10 +390,7 @@ def test_chat_model_logs(langchain, ddtrace_config_langchain, request_vcr, mock_
@pytest.mark.snapshot
def test_openai_embedding_query(langchain, request_vcr):
embeddings = langchain.embeddings.OpenAIEmbeddings()
if sys.version_info >= (3, 10):
cassette_name = "openai_embedding_query.yaml"
else:
cassette_name = "openai_embedding_query_39.yaml"
cassette_name = "openai_embedding_query_39.yaml" if PY39 else "openai_embedding_query.yaml"
with request_vcr.use_cassette(cassette_name):
embeddings.embed_query("this is a test query.")

Expand All @@ -421,10 +399,7 @@ def test_openai_embedding_query(langchain, request_vcr):
@pytest.mark.snapshot
def test_openai_embedding_document(langchain, request_vcr):
embeddings = langchain.embeddings.OpenAIEmbeddings()
if sys.version_info >= (3, 10):
cassette_name = "openai_embedding_document.yaml"
else:
cassette_name = "openai_embedding_document_39.yaml"
cassette_name = "openai_embedding_document_39.yaml" if PY39 else "openai_embedding_document.yaml"
with request_vcr.use_cassette(cassette_name):
embeddings.embed_documents(["this is", "a test document."])

Expand All @@ -443,10 +418,7 @@ def test_fake_embedding_document(langchain):

def test_openai_embedding_metrics(langchain, request_vcr, mock_metrics, mock_logs, snapshot_tracer):
embeddings = langchain.embeddings.OpenAIEmbeddings()
if sys.version_info >= (3, 10):
cassette_name = "openai_embedding_query.yaml"
else:
cassette_name = "openai_embedding_query_39.yaml"
cassette_name = "openai_embedding_query_39.yaml" if PY39 else "openai_embedding_query.yaml"
with request_vcr.use_cassette(cassette_name):
embeddings.embed_query("this is a test query.")
expected_tags = [
Expand All @@ -472,10 +444,7 @@ def test_openai_embedding_metrics(langchain, request_vcr, mock_metrics, mock_log
)
def test_embedding_logs(langchain, ddtrace_config_langchain, request_vcr, mock_logs, mock_metrics, mock_tracer):
embeddings = langchain.embeddings.OpenAIEmbeddings()
if sys.version_info >= (3, 10):
cassette_name = "openai_embedding_query.yaml"
else:
cassette_name = "openai_embedding_query_39.yaml"
cassette_name = "openai_embedding_query_39.yaml" if PY39 else "openai_embedding_query.yaml"
with request_vcr.use_cassette(cassette_name):
embeddings.embed_query("this is a test query.")
span = mock_tracer.pop_traces()[0][0]
Expand Down Expand Up @@ -511,10 +480,7 @@ def test_openai_math_chain_sync(langchain, request_vcr):
the overall LLMMathChain, LLMChain, and underlying OpenAI interface.
"""
chain = langchain.chains.LLMMathChain(llm=langchain.llms.OpenAI(temperature=0))
if sys.version_info >= (3, 10):
cassette_name = "openai_math_chain_sync.yaml"
else:
cassette_name = "openai_math_chain_sync_39.yaml"
cassette_name = "openai_math_chain_sync_39.yaml" if PY39 else "openai_math_chain_sync.yaml"
with request_vcr.use_cassette(cassette_name):
chain.run("what is two raised to the fifty-fourth power?")

Expand Down Expand Up @@ -547,7 +513,7 @@ def test_cohere_math_chain_sync(langchain, request_vcr):
chain.run("what is thirteen raised to the .3432 power?")


@pytest.mark.skipif(sys.version_info < (3, 10), reason="Requires unnecessary cassette file for Python 3.9")
@pytest.mark.skipif(PY39, reason="Requires unnecessary cassette file for Python 3.9")
@pytest.mark.snapshot(
token="tests.contrib.langchain.test_langchain.test_openai_sequential_chain",
ignores=["metrics.langchain.tokens.total_cost", "resource"],
Expand Down Expand Up @@ -604,7 +570,7 @@ def _transform_func(inputs):
sequential_chain.run({"text": input_text, "style": "a 90s rapper"})


@pytest.mark.skipif(sys.version_info < (3, 10), reason="Requires unnecessary cassette file for Python 3.9")
@pytest.mark.skipif(PY39, reason="Requires unnecessary cassette file for Python 3.9")
@pytest.mark.snapshot(ignores=["langchain.tokens.total_cost", "resource"])
def test_openai_sequential_chain_with_multiple_llm_sync(langchain, request_vcr):
template = """Paraphrase this text:
Expand Down Expand Up @@ -663,10 +629,7 @@ async def test_openai_sequential_chain_with_multiple_llm_async(langchain, reques

def test_openai_chain_metrics(langchain, request_vcr, mock_metrics, mock_logs, snapshot_tracer):
chain = langchain.chains.LLMMathChain(llm=langchain.llms.OpenAI(temperature=0))
if sys.version_info >= (3, 10):
cassette_name = "openai_math_chain_sync.yaml"
else:
cassette_name = "openai_math_chain_sync_39.yaml"
cassette_name = "openai_math_chain_sync_39.yaml" if PY39 else "openai_math_chain_sync.yaml"
with request_vcr.use_cassette(cassette_name):
chain.run("what is two raised to the fifty-fourth power?")
expected_tags = [
Expand Down Expand Up @@ -698,10 +661,7 @@ def test_openai_chain_metrics(langchain, request_vcr, mock_metrics, mock_logs, s
)
def test_chain_logs(langchain, ddtrace_config_langchain, request_vcr, mock_logs, mock_metrics, mock_tracer):
chain = langchain.chains.LLMMathChain(llm=langchain.llms.OpenAI(temperature=0))
if sys.version_info >= (3, 10):
cassette_name = "openai_math_chain_sync.yaml"
else:
cassette_name = "openai_math_chain_sync_39.yaml"
cassette_name = "openai_math_chain_sync_39.yaml" if PY39 else "openai_math_chain_sync.yaml"
with request_vcr.use_cassette(cassette_name):
chain.run("what is two raised to the fifty-fourth power?")
traces = mock_tracer.pop_traces()
Expand Down Expand Up @@ -811,10 +771,7 @@ def test_pinecone_vectorstore_similarity_search(langchain, request_vcr):
"""
import pinecone

if sys.version_info >= (3, 10):
cassette_name = "openai_pinecone_similarity_search.yaml"
else:
cassette_name = "openai_pinecone_similarity_search_39.yaml"
cassette_name = "openai_pinecone_similarity_search_39.yaml" if PY39 else "openai_pinecone_similarity_search.yaml"
with request_vcr.use_cassette(cassette_name):
pinecone.init(
api_key=os.getenv("PINECONE_API_KEY", "<not-a-real-key>"),
Expand All @@ -826,7 +783,7 @@ def test_pinecone_vectorstore_similarity_search(langchain, request_vcr):
vectorstore.similarity_search("Who was Alan Turing?", 1)


@pytest.mark.skipif(sys.version_info < (3, 10), reason="Cassette specific to Python 3.10+")
@pytest.mark.skipif(PY39, reason="Cassette specific to Python 3.10+")
@pytest.mark.snapshot
def test_pinecone_vectorstore_retrieval_chain(langchain, request_vcr):
"""
Expand All @@ -851,7 +808,7 @@ def test_pinecone_vectorstore_retrieval_chain(langchain, request_vcr):
qa_with_sources("Who was Alan Turing?")


@pytest.mark.skipif(sys.version_info >= (3, 10), reason="Cassette specific to Python 3.9")
@pytest.mark.skipif(PY39, reason="Cassette specific to Python 3.9")
@pytest.mark.snapshot
def test_pinecone_vectorstore_retrieval_chain_39(langchain, request_vcr):
"""
Expand Down Expand Up @@ -879,10 +836,7 @@ def test_pinecone_vectorstore_retrieval_chain_39(langchain, request_vcr):
def test_vectorstore_similarity_search_metrics(langchain, request_vcr, mock_metrics, mock_logs, snapshot_tracer):
import pinecone

if sys.version_info >= (3, 10):
cassette_name = "openai_pinecone_similarity_search.yaml"
else:
cassette_name = "openai_pinecone_similarity_search_39.yaml"
cassette_name = "openai_pinecone_similarity_search_39.yaml" if PY39 else "openai_pinecone_similarity_search.yaml"
with request_vcr.use_cassette(cassette_name):
pinecone.init(
api_key=os.getenv("PINECONE_API_KEY", "<not-a-real-key>"),
Expand Down Expand Up @@ -916,10 +870,7 @@ def test_vectorstore_similarity_search_metrics(langchain, request_vcr, mock_metr
def test_vectorstore_logs(langchain, ddtrace_config_langchain, request_vcr, mock_logs, mock_metrics, mock_tracer):
import pinecone

if sys.version_info >= (3, 10):
cassette_name = "openai_pinecone_similarity_search.yaml"
else:
cassette_name = "openai_pinecone_similarity_search_39.yaml"
cassette_name = "openai_pinecone_similarity_search_39.yaml" if PY39 else "openai_pinecone_similarity_search.yaml"
with request_vcr.use_cassette(cassette_name):
pinecone.init(
api_key=os.getenv("PINECONE_API_KEY", "<not-a-real-key>"),
Expand Down Expand Up @@ -973,7 +924,7 @@ def test_vectorstore_logs(langchain, ddtrace_config_langchain, request_vcr, mock
mock_metrics.count.assert_not_called()


@pytest.mark.skipif(sys.version_info < (3, 10), reason="Requires unnecessary cassette file for Python 3.9")
@pytest.mark.skipif(PY39, reason="Requires unnecessary cassette file for Python 3.9")
@pytest.mark.snapshot(ignores=["metrics.langchain.tokens.total_cost", "resource"])
def test_openai_integration(langchain, request_vcr, ddtrace_run_python_code_in_subprocess):
env = os.environ.copy()
Expand Down Expand Up @@ -1005,7 +956,7 @@ def test_openai_integration(langchain, request_vcr, ddtrace_run_python_code_in_s
assert err == b""


@pytest.mark.skipif(sys.version_info < (3, 10), reason="Requires unnecessary cassette file for Python 3.9")
@pytest.mark.skipif(PY39, reason="Requires unnecessary cassette file for Python 3.9")
@pytest.mark.snapshot(ignores=["metrics.langchain.tokens.total_cost", "resource"])
@pytest.mark.parametrize("schema_version", [None, "v0", "v1"])
@pytest.mark.parametrize("service_name", [None, "mysvc"])
Expand Down
8 changes: 3 additions & 5 deletions tests/contrib/langchain/test_langchain_community.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@


pytestmark = pytest.mark.skipif(
# parse_version(langchain.__version__) < (0, 1, 0) or (sys.version_info[0] == 3 and sys.version_info[1] == 9),
parse_version(langchain.__version__) < (0, 1, 0),
parse_version(langchain.__version__) < (0, 1, 0) or sys.version_info < (3, 10),
reason="This module only tests langchain >= 0.1 and Python 3.10+",
)

Expand Down Expand Up @@ -44,7 +43,6 @@ def test_global_tags(ddtrace_config_langchain, langchain_openai, request_vcr, mo
The env should be used for all data
The version should be used for all data
"""
assert 0, sys.version_info
llm = langchain_openai.OpenAI()
with override_global_config(dict(service="test-svc", env="staging", version="1234")):
with request_vcr.use_cassette("openai_completion_sync.yaml"):
Expand Down Expand Up @@ -1157,7 +1155,7 @@ async def test_lcel_chain_simple_async(langchain_core, langchain_openai, request

@flaky(1735812000, reason="batch() is non-deterministic in which order it processes inputs")
@pytest.mark.snapshot(ignores=IGNORE_FIELDS)
@pytest.mark.skipif(sys.version_info >= (3, 11, 0), reason="Python <3.11 test")
@pytest.mark.skipif(sys.version_info >= (3, 11), reason="Python <3.11 test")
def test_lcel_chain_batch(langchain_core, langchain_openai, request_vcr):
"""
Test that invoking a chain with a batch of inputs will result in a 4-span trace,
Expand All @@ -1174,7 +1172,7 @@ def test_lcel_chain_batch(langchain_core, langchain_openai, request_vcr):

@flaky(1735812000, reason="batch() is non-deterministic in which order it processes inputs")
@pytest.mark.snapshot(ignores=IGNORE_FIELDS)
@pytest.mark.skipif(sys.version_info < (3, 11, 0), reason="Python 3.11+ required")
@pytest.mark.skipif(sys.version_info < (3, 11), reason="Python 3.11+ required")
def test_lcel_chain_batch_311(langchain_core, langchain_openai, request_vcr):
"""
Test that invoking a chain with a batch of inputs will result in a 4-span trace,
Expand Down
Loading

0 comments on commit d35e50e

Please sign in to comment.