mirror of https://github.com/hwchase17/langchain
langchain[patch],community[patch]: Move unit tests that depend on community to community (#21685)
parent
97a4ae50d2
commit
8607735b80
@ -1,14 +1,10 @@
|
||||
from pathlib import Path
|
||||
from tempfile import TemporaryDirectory
|
||||
|
||||
import pytest
|
||||
from langchain_core.language_models import FakeListLLM
|
||||
from langchain_core.tools import Tool
|
||||
|
||||
from langchain.agents.agent_types import AgentType
|
||||
from langchain.agents.initialize import initialize_agent, load_agent
|
||||
|
||||
pytest.importorskip("langchain_community")
|
||||
from langchain_core.language_models import FakeListLLM
|
||||
from langchain_core.tools import Tool
|
||||
|
||||
|
||||
def test_mrkl_serialization() -> None:
|
@ -0,0 +1,24 @@
|
||||
"""Verify proxy imports from langchain to community are behaving as expected."""
|
||||
|
||||
|
||||
def test_all_proxy_llms_are_llm_subclasses() -> None:
|
||||
"""Simple test to make sure all things are subclasses of BaseLLM."""
|
||||
from langchain import llms
|
||||
from langchain_core.language_models import BaseLLM
|
||||
|
||||
for cls in llms.__all__:
|
||||
assert issubclass(getattr(llms, cls), BaseLLM)
|
||||
|
||||
|
||||
def test_vectorstores() -> None:
|
||||
"""Simple test to make sure all things can be imported."""
|
||||
from langchain import vectorstores
|
||||
from langchain_core.vectorstores import VectorStore
|
||||
|
||||
for cls in vectorstores.__all__:
|
||||
if cls not in [
|
||||
"AlibabaCloudOpenSearchSettings",
|
||||
"ClickhouseSettings",
|
||||
"MyScaleSettings",
|
||||
]:
|
||||
assert issubclass(getattr(vectorstores, cls), VectorStore)
|
@ -0,0 +1,875 @@
|
||||
# serializer version: 1
|
||||
# name: test_person
|
||||
'''
|
||||
{
|
||||
"lc": 1,
|
||||
"type": "constructor",
|
||||
"id": [
|
||||
"tests",
|
||||
"unit_tests",
|
||||
"load",
|
||||
"test_dump",
|
||||
"Person"
|
||||
],
|
||||
"kwargs": {
|
||||
"secret": {
|
||||
"lc": 1,
|
||||
"type": "secret",
|
||||
"id": [
|
||||
"SECRET"
|
||||
]
|
||||
},
|
||||
"you_can_see_me": "hello"
|
||||
}
|
||||
}
|
||||
'''
|
||||
# ---
|
||||
# name: test_person.1
|
||||
'''
|
||||
{
|
||||
"lc": 1,
|
||||
"type": "constructor",
|
||||
"id": [
|
||||
"my",
|
||||
"special",
|
||||
"namespace",
|
||||
"SpecialPerson"
|
||||
],
|
||||
"kwargs": {
|
||||
"secret": {
|
||||
"lc": 1,
|
||||
"type": "secret",
|
||||
"id": [
|
||||
"SECRET"
|
||||
]
|
||||
},
|
||||
"you_can_see_me": "hello",
|
||||
"another_secret": {
|
||||
"lc": 1,
|
||||
"type": "secret",
|
||||
"id": [
|
||||
"ANOTHER_SECRET"
|
||||
]
|
||||
},
|
||||
"another_visible": "bye"
|
||||
}
|
||||
}
|
||||
'''
|
||||
# ---
|
||||
# name: test_person_with_kwargs
|
||||
'{"lc":1,"type":"constructor","id":["tests","unit_tests","load","test_dump","Person"],"kwargs":{"secret":{"lc":1,"type":"secret","id":["SECRET"]},"you_can_see_me":"hello"}}'
|
||||
# ---
|
||||
# name: test_serialize_llmchain
|
||||
'''
|
||||
{
|
||||
"lc": 1,
|
||||
"type": "constructor",
|
||||
"id": [
|
||||
"langchain",
|
||||
"chains",
|
||||
"llm",
|
||||
"LLMChain"
|
||||
],
|
||||
"kwargs": {
|
||||
"prompt": {
|
||||
"lc": 1,
|
||||
"type": "constructor",
|
||||
"id": [
|
||||
"langchain",
|
||||
"prompts",
|
||||
"prompt",
|
||||
"PromptTemplate"
|
||||
],
|
||||
"kwargs": {
|
||||
"input_variables": [
|
||||
"name"
|
||||
],
|
||||
"template": "hello {name}!",
|
||||
"template_format": "f-string"
|
||||
},
|
||||
"name": "PromptTemplate",
|
||||
"graph": {
|
||||
"nodes": [
|
||||
{
|
||||
"id": 0,
|
||||
"type": "schema",
|
||||
"data": "PromptInput"
|
||||
},
|
||||
{
|
||||
"id": 1,
|
||||
"type": "runnable",
|
||||
"data": {
|
||||
"id": [
|
||||
"langchain",
|
||||
"prompts",
|
||||
"prompt",
|
||||
"PromptTemplate"
|
||||
],
|
||||
"name": "PromptTemplate"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": 2,
|
||||
"type": "schema",
|
||||
"data": "PromptTemplateOutput"
|
||||
}
|
||||
],
|
||||
"edges": [
|
||||
{
|
||||
"source": 0,
|
||||
"target": 1
|
||||
},
|
||||
{
|
||||
"source": 1,
|
||||
"target": 2
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"llm": {
|
||||
"lc": 1,
|
||||
"type": "constructor",
|
||||
"id": [
|
||||
"langchain",
|
||||
"llms",
|
||||
"openai",
|
||||
"OpenAI"
|
||||
],
|
||||
"kwargs": {
|
||||
"model_name": "davinci",
|
||||
"temperature": 0.5,
|
||||
"max_tokens": 256,
|
||||
"top_p": 1,
|
||||
"n": 1,
|
||||
"best_of": 1,
|
||||
"openai_api_key": {
|
||||
"lc": 1,
|
||||
"type": "secret",
|
||||
"id": [
|
||||
"OPENAI_API_KEY"
|
||||
]
|
||||
},
|
||||
"openai_proxy": "",
|
||||
"batch_size": 20,
|
||||
"max_retries": 2,
|
||||
"disallowed_special": "all"
|
||||
},
|
||||
"name": "OpenAI",
|
||||
"graph": {
|
||||
"nodes": [
|
||||
{
|
||||
"id": 0,
|
||||
"type": "schema",
|
||||
"data": "OpenAIInput"
|
||||
},
|
||||
{
|
||||
"id": 1,
|
||||
"type": "runnable",
|
||||
"data": {
|
||||
"id": [
|
||||
"langchain",
|
||||
"llms",
|
||||
"openai",
|
||||
"OpenAI"
|
||||
],
|
||||
"name": "OpenAI"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": 2,
|
||||
"type": "schema",
|
||||
"data": "OpenAIOutput"
|
||||
}
|
||||
],
|
||||
"edges": [
|
||||
{
|
||||
"source": 0,
|
||||
"target": 1
|
||||
},
|
||||
{
|
||||
"source": 1,
|
||||
"target": 2
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"output_key": "text",
|
||||
"output_parser": {
|
||||
"lc": 1,
|
||||
"type": "constructor",
|
||||
"id": [
|
||||
"langchain",
|
||||
"schema",
|
||||
"output_parser",
|
||||
"StrOutputParser"
|
||||
],
|
||||
"kwargs": {},
|
||||
"name": "StrOutputParser",
|
||||
"graph": {
|
||||
"nodes": [
|
||||
{
|
||||
"id": 0,
|
||||
"type": "schema",
|
||||
"data": "StrOutputParserInput"
|
||||
},
|
||||
{
|
||||
"id": 1,
|
||||
"type": "runnable",
|
||||
"data": {
|
||||
"id": [
|
||||
"langchain",
|
||||
"schema",
|
||||
"output_parser",
|
||||
"StrOutputParser"
|
||||
],
|
||||
"name": "StrOutputParser"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": 2,
|
||||
"type": "schema",
|
||||
"data": "StrOutputParserOutput"
|
||||
}
|
||||
],
|
||||
"edges": [
|
||||
{
|
||||
"source": 0,
|
||||
"target": 1
|
||||
},
|
||||
{
|
||||
"source": 1,
|
||||
"target": 2
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"return_final_only": true
|
||||
},
|
||||
"name": "LLMChain",
|
||||
"graph": {
|
||||
"nodes": [
|
||||
{
|
||||
"id": 0,
|
||||
"type": "schema",
|
||||
"data": "ChainInput"
|
||||
},
|
||||
{
|
||||
"id": 1,
|
||||
"type": "runnable",
|
||||
"data": {
|
||||
"id": [
|
||||
"langchain",
|
||||
"chains",
|
||||
"llm",
|
||||
"LLMChain"
|
||||
],
|
||||
"name": "LLMChain"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": 2,
|
||||
"type": "schema",
|
||||
"data": "ChainOutput"
|
||||
}
|
||||
],
|
||||
"edges": [
|
||||
{
|
||||
"source": 0,
|
||||
"target": 1
|
||||
},
|
||||
{
|
||||
"source": 1,
|
||||
"target": 2
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
'''
|
||||
# ---
|
||||
# name: test_serialize_llmchain_chat
|
||||
'''
|
||||
{
|
||||
"lc": 1,
|
||||
"type": "constructor",
|
||||
"id": [
|
||||
"langchain",
|
||||
"chains",
|
||||
"llm",
|
||||
"LLMChain"
|
||||
],
|
||||
"kwargs": {
|
||||
"prompt": {
|
||||
"lc": 1,
|
||||
"type": "constructor",
|
||||
"id": [
|
||||
"langchain",
|
||||
"prompts",
|
||||
"chat",
|
||||
"ChatPromptTemplate"
|
||||
],
|
||||
"kwargs": {
|
||||
"input_variables": [
|
||||
"name"
|
||||
],
|
||||
"messages": [
|
||||
{
|
||||
"lc": 1,
|
||||
"type": "constructor",
|
||||
"id": [
|
||||
"langchain",
|
||||
"prompts",
|
||||
"chat",
|
||||
"HumanMessagePromptTemplate"
|
||||
],
|
||||
"kwargs": {
|
||||
"prompt": {
|
||||
"lc": 1,
|
||||
"type": "constructor",
|
||||
"id": [
|
||||
"langchain",
|
||||
"prompts",
|
||||
"prompt",
|
||||
"PromptTemplate"
|
||||
],
|
||||
"kwargs": {
|
||||
"input_variables": [
|
||||
"name"
|
||||
],
|
||||
"template": "hello {name}!",
|
||||
"template_format": "f-string"
|
||||
},
|
||||
"name": "PromptTemplate",
|
||||
"graph": {
|
||||
"nodes": [
|
||||
{
|
||||
"id": 0,
|
||||
"type": "schema",
|
||||
"data": "PromptInput"
|
||||
},
|
||||
{
|
||||
"id": 1,
|
||||
"type": "runnable",
|
||||
"data": {
|
||||
"id": [
|
||||
"langchain",
|
||||
"prompts",
|
||||
"prompt",
|
||||
"PromptTemplate"
|
||||
],
|
||||
"name": "PromptTemplate"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": 2,
|
||||
"type": "schema",
|
||||
"data": "PromptTemplateOutput"
|
||||
}
|
||||
],
|
||||
"edges": [
|
||||
{
|
||||
"source": 0,
|
||||
"target": 1
|
||||
},
|
||||
{
|
||||
"source": 1,
|
||||
"target": 2
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"name": "ChatPromptTemplate",
|
||||
"graph": {
|
||||
"nodes": [
|
||||
{
|
||||
"id": 0,
|
||||
"type": "schema",
|
||||
"data": "PromptInput"
|
||||
},
|
||||
{
|
||||
"id": 1,
|
||||
"type": "runnable",
|
||||
"data": {
|
||||
"id": [
|
||||
"langchain",
|
||||
"prompts",
|
||||
"chat",
|
||||
"ChatPromptTemplate"
|
||||
],
|
||||
"name": "ChatPromptTemplate"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": 2,
|
||||
"type": "schema",
|
||||
"data": "ChatPromptTemplateOutput"
|
||||
}
|
||||
],
|
||||
"edges": [
|
||||
{
|
||||
"source": 0,
|
||||
"target": 1
|
||||
},
|
||||
{
|
||||
"source": 1,
|
||||
"target": 2
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"llm": {
|
||||
"lc": 1,
|
||||
"type": "constructor",
|
||||
"id": [
|
||||
"langchain",
|
||||
"chat_models",
|
||||
"openai",
|
||||
"ChatOpenAI"
|
||||
],
|
||||
"kwargs": {
|
||||
"model_name": "davinci",
|
||||
"temperature": 0.5,
|
||||
"openai_api_key": {
|
||||
"lc": 1,
|
||||
"type": "secret",
|
||||
"id": [
|
||||
"OPENAI_API_KEY"
|
||||
]
|
||||
},
|
||||
"openai_proxy": "",
|
||||
"max_retries": 2,
|
||||
"n": 1
|
||||
},
|
||||
"name": "ChatOpenAI",
|
||||
"graph": {
|
||||
"nodes": [
|
||||
{
|
||||
"id": 0,
|
||||
"type": "schema",
|
||||
"data": "ChatOpenAIInput"
|
||||
},
|
||||
{
|
||||
"id": 1,
|
||||
"type": "runnable",
|
||||
"data": {
|
||||
"id": [
|
||||
"langchain",
|
||||
"chat_models",
|
||||
"openai",
|
||||
"ChatOpenAI"
|
||||
],
|
||||
"name": "ChatOpenAI"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": 2,
|
||||
"type": "schema",
|
||||
"data": "ChatOpenAIOutput"
|
||||
}
|
||||
],
|
||||
"edges": [
|
||||
{
|
||||
"source": 0,
|
||||
"target": 1
|
||||
},
|
||||
{
|
||||
"source": 1,
|
||||
"target": 2
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"output_key": "text",
|
||||
"output_parser": {
|
||||
"lc": 1,
|
||||
"type": "constructor",
|
||||
"id": [
|
||||
"langchain",
|
||||
"schema",
|
||||
"output_parser",
|
||||
"StrOutputParser"
|
||||
],
|
||||
"kwargs": {},
|
||||
"name": "StrOutputParser",
|
||||
"graph": {
|
||||
"nodes": [
|
||||
{
|
||||
"id": 0,
|
||||
"type": "schema",
|
||||
"data": "StrOutputParserInput"
|
||||
},
|
||||
{
|
||||
"id": 1,
|
||||
"type": "runnable",
|
||||
"data": {
|
||||
"id": [
|
||||
"langchain",
|
||||
"schema",
|
||||
"output_parser",
|
||||
"StrOutputParser"
|
||||
],
|
||||
"name": "StrOutputParser"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": 2,
|
||||
"type": "schema",
|
||||
"data": "StrOutputParserOutput"
|
||||
}
|
||||
],
|
||||
"edges": [
|
||||
{
|
||||
"source": 0,
|
||||
"target": 1
|
||||
},
|
||||
{
|
||||
"source": 1,
|
||||
"target": 2
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"return_final_only": true
|
||||
},
|
||||
"name": "LLMChain",
|
||||
"graph": {
|
||||
"nodes": [
|
||||
{
|
||||
"id": 0,
|
||||
"type": "schema",
|
||||
"data": "ChainInput"
|
||||
},
|
||||
{
|
||||
"id": 1,
|
||||
"type": "runnable",
|
||||
"data": {
|
||||
"id": [
|
||||
"langchain",
|
||||
"chains",
|
||||
"llm",
|
||||
"LLMChain"
|
||||
],
|
||||
"name": "LLMChain"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": 2,
|
||||
"type": "schema",
|
||||
"data": "ChainOutput"
|
||||
}
|
||||
],
|
||||
"edges": [
|
||||
{
|
||||
"source": 0,
|
||||
"target": 1
|
||||
},
|
||||
{
|
||||
"source": 1,
|
||||
"target": 2
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
'''
|
||||
# ---
|
||||
# name: test_serialize_llmchain_with_non_serializable_arg
|
||||
'''
|
||||
{
|
||||
"lc": 1,
|
||||
"type": "constructor",
|
||||
"id": [
|
||||
"langchain",
|
||||
"chains",
|
||||
"llm",
|
||||
"LLMChain"
|
||||
],
|
||||
"kwargs": {
|
||||
"prompt": {
|
||||
"lc": 1,
|
||||
"type": "constructor",
|
||||
"id": [
|
||||
"langchain",
|
||||
"prompts",
|
||||
"prompt",
|
||||
"PromptTemplate"
|
||||
],
|
||||
"kwargs": {
|
||||
"input_variables": [
|
||||
"name"
|
||||
],
|
||||
"template": "hello {name}!",
|
||||
"template_format": "f-string"
|
||||
},
|
||||
"name": "PromptTemplate",
|
||||
"graph": {
|
||||
"nodes": [
|
||||
{
|
||||
"id": 0,
|
||||
"type": "schema",
|
||||
"data": "PromptInput"
|
||||
},
|
||||
{
|
||||
"id": 1,
|
||||
"type": "runnable",
|
||||
"data": {
|
||||
"id": [
|
||||
"langchain",
|
||||
"prompts",
|
||||
"prompt",
|
||||
"PromptTemplate"
|
||||
],
|
||||
"name": "PromptTemplate"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": 2,
|
||||
"type": "schema",
|
||||
"data": "PromptTemplateOutput"
|
||||
}
|
||||
],
|
||||
"edges": [
|
||||
{
|
||||
"source": 0,
|
||||
"target": 1
|
||||
},
|
||||
{
|
||||
"source": 1,
|
||||
"target": 2
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"llm": {
|
||||
"lc": 1,
|
||||
"type": "constructor",
|
||||
"id": [
|
||||
"langchain",
|
||||
"llms",
|
||||
"openai",
|
||||
"OpenAI"
|
||||
],
|
||||
"kwargs": {
|
||||
"model_name": "davinci",
|
||||
"temperature": 0.5,
|
||||
"max_tokens": 256,
|
||||
"top_p": 1,
|
||||
"n": 1,
|
||||
"best_of": 1,
|
||||
"openai_api_key": {
|
||||
"lc": 1,
|
||||
"type": "secret",
|
||||
"id": [
|
||||
"OPENAI_API_KEY"
|
||||
]
|
||||
},
|
||||
"openai_proxy": "",
|
||||
"batch_size": 20,
|
||||
"max_retries": 2,
|
||||
"disallowed_special": "all"
|
||||
},
|
||||
"name": "OpenAI",
|
||||
"graph": {
|
||||
"nodes": [
|
||||
{
|
||||
"id": 0,
|
||||
"type": "schema",
|
||||
"data": "OpenAIInput"
|
||||
},
|
||||
{
|
||||
"id": 1,
|
||||
"type": "runnable",
|
||||
"data": {
|
||||
"id": [
|
||||
"langchain",
|
||||
"llms",
|
||||
"openai",
|
||||
"OpenAI"
|
||||
],
|
||||
"name": "OpenAI"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": 2,
|
||||
"type": "schema",
|
||||
"data": "OpenAIOutput"
|
||||
}
|
||||
],
|
||||
"edges": [
|
||||
{
|
||||
"source": 0,
|
||||
"target": 1
|
||||
},
|
||||
{
|
||||
"source": 1,
|
||||
"target": 2
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"output_key": "text",
|
||||
"output_parser": {
|
||||
"lc": 1,
|
||||
"type": "constructor",
|
||||
"id": [
|
||||
"langchain",
|
||||
"schema",
|
||||
"output_parser",
|
||||
"StrOutputParser"
|
||||
],
|
||||
"kwargs": {},
|
||||
"name": "StrOutputParser",
|
||||
"graph": {
|
||||
"nodes": [
|
||||
{
|
||||
"id": 0,
|
||||
"type": "schema",
|
||||
"data": "StrOutputParserInput"
|
||||
},
|
||||
{
|
||||
"id": 1,
|
||||
"type": "runnable",
|
||||
"data": {
|
||||
"id": [
|
||||
"langchain",
|
||||
"schema",
|
||||
"output_parser",
|
||||
"StrOutputParser"
|
||||
],
|
||||
"name": "StrOutputParser"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": 2,
|
||||
"type": "schema",
|
||||
"data": "StrOutputParserOutput"
|
||||
}
|
||||
],
|
||||
"edges": [
|
||||
{
|
||||
"source": 0,
|
||||
"target": 1
|
||||
},
|
||||
{
|
||||
"source": 1,
|
||||
"target": 2
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"return_final_only": true
|
||||
},
|
||||
"name": "LLMChain",
|
||||
"graph": {
|
||||
"nodes": [
|
||||
{
|
||||
"id": 0,
|
||||
"type": "schema",
|
||||
"data": "ChainInput"
|
||||
},
|
||||
{
|
||||
"id": 1,
|
||||
"type": "runnable",
|
||||
"data": {
|
||||
"id": [
|
||||
"langchain",
|
||||
"chains",
|
||||
"llm",
|
||||
"LLMChain"
|
||||
],
|
||||
"name": "LLMChain"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": 2,
|
||||
"type": "schema",
|
||||
"data": "ChainOutput"
|
||||
}
|
||||
],
|
||||
"edges": [
|
||||
{
|
||||
"source": 0,
|
||||
"target": 1
|
||||
},
|
||||
{
|
||||
"source": 1,
|
||||
"target": 2
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
'''
|
||||
# ---
|
||||
# name: test_serialize_openai_llm
|
||||
'''
|
||||
{
|
||||
"lc": 1,
|
||||
"type": "constructor",
|
||||
"id": [
|
||||
"langchain",
|
||||
"llms",
|
||||
"openai",
|
||||
"OpenAI"
|
||||
],
|
||||
"kwargs": {
|
||||
"model_name": "davinci",
|
||||
"temperature": 0.7,
|
||||
"max_tokens": 256,
|
||||
"top_p": 1,
|
||||
"n": 1,
|
||||
"best_of": 1,
|
||||
"openai_api_key": {
|
||||
"lc": 1,
|
||||
"type": "secret",
|
||||
"id": [
|
||||
"OPENAI_API_KEY"
|
||||
]
|
||||
},
|
||||
"openai_proxy": "",
|
||||
"batch_size": 20,
|
||||
"max_retries": 2,
|
||||
"disallowed_special": "all"
|
||||
},
|
||||
"name": "OpenAI",
|
||||
"graph": {
|
||||
"nodes": [
|
||||
{
|
||||
"id": 0,
|
||||
"type": "schema",
|
||||
"data": "OpenAIInput"
|
||||
},
|
||||
{
|
||||
"id": 1,
|
||||
"type": "runnable",
|
||||
"data": {
|
||||
"id": [
|
||||
"langchain",
|
||||
"llms",
|
||||
"openai",
|
||||
"OpenAI"
|
||||
],
|
||||
"name": "OpenAI"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": 2,
|
||||
"type": "schema",
|
||||
"data": "OpenAIOutput"
|
||||
}
|
||||
],
|
||||
"edges": [
|
||||
{
|
||||
"source": 0,
|
||||
"target": 1
|
||||
},
|
||||
{
|
||||
"source": 1,
|
||||
"target": 2
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
'''
|
||||
# ---
|
@ -0,0 +1,255 @@
|
||||
"""Test for Serializable base class"""
|
||||
|
||||
import json
|
||||
import os
|
||||
from typing import Any, Dict, List
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
from langchain.chains.llm import LLMChain
|
||||
from langchain_core.load.dump import dumps
|
||||
from langchain_core.load.serializable import Serializable
|
||||
from langchain_core.prompts.chat import ChatPromptTemplate, HumanMessagePromptTemplate
|
||||
from langchain_core.prompts.prompt import PromptTemplate
|
||||
from langchain_core.pydantic_v1 import Field, root_validator
|
||||
from langchain_core.tracers.langchain import LangChainTracer
|
||||
|
||||
|
||||
class Person(Serializable):
|
||||
secret: str
|
||||
|
||||
you_can_see_me: str = "hello"
|
||||
|
||||
@classmethod
|
||||
def is_lc_serializable(cls) -> bool:
|
||||
return True
|
||||
|
||||
@property
|
||||
def lc_secrets(self) -> Dict[str, str]:
|
||||
return {"secret": "SECRET"}
|
||||
|
||||
@property
|
||||
def lc_attributes(self) -> Dict[str, str]:
|
||||
return {"you_can_see_me": self.you_can_see_me}
|
||||
|
||||
|
||||
class SpecialPerson(Person):
|
||||
another_secret: str
|
||||
|
||||
another_visible: str = "bye"
|
||||
|
||||
@classmethod
|
||||
def get_lc_namespace(cls) -> List[str]:
|
||||
return ["my", "special", "namespace"]
|
||||
|
||||
# Gets merged with parent class's secrets
|
||||
@property
|
||||
def lc_secrets(self) -> Dict[str, str]:
|
||||
return {"another_secret": "ANOTHER_SECRET"}
|
||||
|
||||
# Gets merged with parent class's attributes
|
||||
@property
|
||||
def lc_attributes(self) -> Dict[str, str]:
|
||||
return {"another_visible": self.another_visible}
|
||||
|
||||
|
||||
class NotSerializable:
|
||||
pass
|
||||
|
||||
|
||||
def test_person(snapshot: Any) -> None:
|
||||
p = Person(secret="hello")
|
||||
assert dumps(p, pretty=True) == snapshot
|
||||
sp = SpecialPerson(another_secret="Wooo", secret="Hmm")
|
||||
assert dumps(sp, pretty=True) == snapshot
|
||||
assert Person.lc_id() == ["tests", "unit_tests", "load", "test_dump", "Person"]
|
||||
assert SpecialPerson.lc_id() == ["my", "special", "namespace", "SpecialPerson"]
|
||||
|
||||
|
||||
def test_typeerror() -> None:
|
||||
assert (
|
||||
dumps({(1, 2): 3})
|
||||
== """{"lc": 1, "type": "not_implemented", "id": ["builtins", "dict"], "repr": "{(1, 2): 3}"}""" # noqa: E501
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.requires("openai")
|
||||
def test_serialize_openai_llm(snapshot: Any) -> None:
|
||||
from langchain_community.llms.openai import OpenAI
|
||||
|
||||
with patch.dict(os.environ, {"LANGCHAIN_API_KEY": "test-api-key"}):
|
||||
llm = OpenAI( # type: ignore[call-arg]
|
||||
model="davinci",
|
||||
temperature=0.5,
|
||||
openai_api_key="hello",
|
||||
# This is excluded from serialization
|
||||
callbacks=[LangChainTracer()],
|
||||
)
|
||||
llm.temperature = 0.7 # this is reflected in serialization
|
||||
assert dumps(llm, pretty=True) == snapshot
|
||||
|
||||
|
||||
@pytest.mark.requires("openai")
|
||||
def test_serialize_llmchain(snapshot: Any) -> None:
|
||||
from langchain_community.llms.openai import OpenAI
|
||||
|
||||
llm = OpenAI(model="davinci", temperature=0.5, openai_api_key="hello") # type: ignore[call-arg]
|
||||
prompt = PromptTemplate.from_template("hello {name}!")
|
||||
chain = LLMChain(llm=llm, prompt=prompt)
|
||||
assert dumps(chain, pretty=True) == snapshot
|
||||
|
||||
|
||||
@pytest.mark.requires("openai")
|
||||
def test_serialize_llmchain_env() -> None:
|
||||
from langchain_community.llms.openai import OpenAI
|
||||
|
||||
llm = OpenAI(model="davinci", temperature=0.5, openai_api_key="hello") # type: ignore[call-arg]
|
||||
prompt = PromptTemplate.from_template("hello {name}!")
|
||||
chain = LLMChain(llm=llm, prompt=prompt)
|
||||
|
||||
import os
|
||||
|
||||
has_env = "OPENAI_API_KEY" in os.environ
|
||||
if not has_env:
|
||||
os.environ["OPENAI_API_KEY"] = "env_variable"
|
||||
|
||||
llm_2 = OpenAI(model="davinci", temperature=0.5) # type: ignore[call-arg]
|
||||
prompt_2 = PromptTemplate.from_template("hello {name}!")
|
||||
chain_2 = LLMChain(llm=llm_2, prompt=prompt_2)
|
||||
|
||||
assert dumps(chain_2, pretty=True) == dumps(chain, pretty=True)
|
||||
|
||||
if not has_env:
|
||||
del os.environ["OPENAI_API_KEY"]
|
||||
|
||||
|
||||
@pytest.mark.requires("openai")
|
||||
def test_serialize_llmchain_chat(snapshot: Any) -> None:
|
||||
from langchain_community.chat_models.openai import ChatOpenAI
|
||||
|
||||
llm = ChatOpenAI(model="davinci", temperature=0.5, openai_api_key="hello") # type: ignore[call-arg]
|
||||
prompt = ChatPromptTemplate.from_messages(
|
||||
[HumanMessagePromptTemplate.from_template("hello {name}!")]
|
||||
)
|
||||
chain = LLMChain(llm=llm, prompt=prompt)
|
||||
assert dumps(chain, pretty=True) == snapshot
|
||||
|
||||
import os
|
||||
|
||||
has_env = "OPENAI_API_KEY" in os.environ
|
||||
if not has_env:
|
||||
os.environ["OPENAI_API_KEY"] = "env_variable"
|
||||
|
||||
llm_2 = ChatOpenAI(model="davinci", temperature=0.5) # type: ignore[call-arg]
|
||||
prompt_2 = ChatPromptTemplate.from_messages(
|
||||
[HumanMessagePromptTemplate.from_template("hello {name}!")]
|
||||
)
|
||||
chain_2 = LLMChain(llm=llm_2, prompt=prompt_2)
|
||||
|
||||
assert dumps(chain_2, pretty=True) == dumps(chain, pretty=True)
|
||||
|
||||
if not has_env:
|
||||
del os.environ["OPENAI_API_KEY"]
|
||||
|
||||
|
||||
@pytest.mark.requires("openai")
|
||||
def test_serialize_llmchain_with_non_serializable_arg(snapshot: Any) -> None:
|
||||
from langchain_community.llms.openai import OpenAI
|
||||
|
||||
llm = OpenAI( # type: ignore[call-arg]
|
||||
model="davinci",
|
||||
temperature=0.5,
|
||||
openai_api_key="hello",
|
||||
client=NotSerializable,
|
||||
)
|
||||
prompt = PromptTemplate.from_template("hello {name}!")
|
||||
chain = LLMChain(llm=llm, prompt=prompt)
|
||||
assert dumps(chain, pretty=True) == snapshot
|
||||
|
||||
|
||||
def test_person_with_kwargs(snapshot: Any) -> None:
|
||||
person = Person(secret="hello")
|
||||
assert dumps(person, separators=(",", ":")) == snapshot
|
||||
|
||||
|
||||
def test_person_with_invalid_kwargs() -> None:
|
||||
person = Person(secret="hello")
|
||||
with pytest.raises(TypeError):
|
||||
dumps(person, invalid_kwarg="hello")
|
||||
|
||||
|
||||
class TestClass(Serializable):
|
||||
my_favorite_secret: str = Field(alias="my_favorite_secret_alias")
|
||||
my_other_secret: str = Field()
|
||||
|
||||
class Config:
|
||||
"""Configuration for this pydantic object."""
|
||||
|
||||
allow_population_by_field_name = True
|
||||
|
||||
@root_validator(pre=True)
|
||||
def get_from_env(cls, values: Dict) -> Dict:
|
||||
"""Get the values from the environment."""
|
||||
if "my_favorite_secret" not in values:
|
||||
values["my_favorite_secret"] = os.getenv("MY_FAVORITE_SECRET")
|
||||
if "my_other_secret" not in values:
|
||||
values["my_other_secret"] = os.getenv("MY_OTHER_SECRET")
|
||||
return values
|
||||
|
||||
@classmethod
|
||||
def is_lc_serializable(cls) -> bool:
|
||||
return True
|
||||
|
||||
@classmethod
|
||||
def get_lc_namespace(cls) -> List[str]:
|
||||
return ["my", "special", "namespace"]
|
||||
|
||||
@property
|
||||
def lc_secrets(self) -> Dict[str, str]:
|
||||
return {
|
||||
"my_favorite_secret": "MY_FAVORITE_SECRET",
|
||||
"my_other_secret": "MY_OTHER_SECRET",
|
||||
}
|
||||
|
||||
|
||||
def test_aliases_hidden() -> None:
|
||||
test_class = TestClass(my_favorite_secret="hello", my_other_secret="world") # type: ignore[call-arg]
|
||||
dumped = json.loads(dumps(test_class, pretty=True))
|
||||
expected_dump = {
|
||||
"lc": 1,
|
||||
"type": "constructor",
|
||||
"id": ["my", "special", "namespace", "TestClass"],
|
||||
"kwargs": {
|
||||
"my_favorite_secret": {
|
||||
"lc": 1,
|
||||
"type": "secret",
|
||||
"id": ["MY_FAVORITE_SECRET"],
|
||||
},
|
||||
"my_other_secret": {"lc": 1, "type": "secret", "id": ["MY_OTHER_SECRET"]},
|
||||
},
|
||||
}
|
||||
assert dumped == expected_dump
|
||||
# Check while patching the os environment
|
||||
with patch.dict(
|
||||
os.environ, {"MY_FAVORITE_SECRET": "hello", "MY_OTHER_SECRET": "world"}
|
||||
):
|
||||
test_class = TestClass() # type: ignore[call-arg]
|
||||
dumped = json.loads(dumps(test_class, pretty=True))
|
||||
|
||||
# Check by alias
|
||||
test_class = TestClass(my_favorite_secret_alias="hello", my_other_secret="world") # type: ignore[call-arg]
|
||||
dumped = json.loads(dumps(test_class, pretty=True))
|
||||
expected_dump = {
|
||||
"lc": 1,
|
||||
"type": "constructor",
|
||||
"id": ["my", "special", "namespace", "TestClass"],
|
||||
"kwargs": {
|
||||
"my_favorite_secret": {
|
||||
"lc": 1,
|
||||
"type": "secret",
|
||||
"id": ["MY_FAVORITE_SECRET"],
|
||||
},
|
||||
"my_other_secret": {"lc": 1, "type": "secret", "id": ["MY_OTHER_SECRET"]},
|
||||
},
|
||||
}
|
||||
assert dumped == expected_dump
|
@ -1,11 +1,8 @@
|
||||
import pytest
|
||||
from langchain.retrievers.ensemble import EnsembleRetriever
|
||||
from langchain_core.documents import Document
|
||||
from langchain_core.embeddings import FakeEmbeddings
|
||||
|
||||
from langchain.retrievers.ensemble import EnsembleRetriever
|
||||
|
||||
pytest.importorskip("langchain_community")
|
||||
|
||||
|
||||
@pytest.mark.requires("rank_bm25")
|
||||
def test_ensemble_retriever_get_relevant_docs() -> None:
|
@ -1,16 +0,0 @@
|
||||
import pytest
|
||||
from langchain_core.vectorstores import VectorStore
|
||||
|
||||
from langchain import vectorstores
|
||||
|
||||
|
||||
@pytest.mark.community
|
||||
def test_all_imports() -> None:
|
||||
"""Simple test to make sure all things can be imported."""
|
||||
for cls in vectorstores.__all__:
|
||||
if cls not in [
|
||||
"AlibabaCloudOpenSearchSettings",
|
||||
"ClickhouseSettings",
|
||||
"MyScaleSettings",
|
||||
]:
|
||||
assert issubclass(getattr(vectorstores, cls), VectorStore)
|
Loading…
Reference in New Issue