mirror of
https://github.com/hwchase17/langchain.git
synced 2025-09-13 13:36:15 +00:00
Include placeholder value for all secrets, not just kwargs (#6421)
Mirror PR for https://github.com/hwchase17/langchainjs/pull/1696 Secrets passed via environment variables should be present in the serialised chain
This commit is contained in:
@@ -129,7 +129,13 @@
|
||||
"kwargs": {
|
||||
"model": "davinci",
|
||||
"temperature": 0.5,
|
||||
"openai_api_key": "hello"
|
||||
"openai_api_key": {
|
||||
"lc": 1,
|
||||
"type": "secret",
|
||||
"id": [
|
||||
"OPENAI_API_KEY"
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
"prompt": {
|
||||
|
@@ -80,6 +80,28 @@ def test_serialize_llmchain(snapshot: Any) -> None:
|
||||
assert dumps(chain, pretty=True) == snapshot
|
||||
|
||||
|
||||
@pytest.mark.requires("openai")
|
||||
def test_serialize_llmchain_env() -> None:
|
||||
llm = OpenAI(model="davinci", temperature=0.5, openai_api_key="hello")
|
||||
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)
|
||||
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:
|
||||
llm = ChatOpenAI(model="davinci", temperature=0.5, openai_api_key="hello")
|
||||
@@ -89,6 +111,23 @@ def test_serialize_llmchain_chat(snapshot: Any) -> None:
|
||||
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)
|
||||
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:
|
||||
|
@@ -39,6 +39,30 @@ def test_load_llmchain() -> None:
|
||||
assert isinstance(chain2.prompt, PromptTemplate)
|
||||
|
||||
|
||||
@pytest.mark.requires("openai")
|
||||
def test_load_llmchain_env() -> None:
|
||||
import os
|
||||
|
||||
has_env = "OPENAI_API_KEY" in os.environ
|
||||
if not has_env:
|
||||
os.environ["OPENAI_API_KEY"] = "env_variable"
|
||||
|
||||
llm = OpenAI(model="davinci", temperature=0.5)
|
||||
prompt = PromptTemplate.from_template("hello {name}!")
|
||||
chain = LLMChain(llm=llm, prompt=prompt)
|
||||
chain_string = dumps(chain)
|
||||
chain2 = loads(chain_string)
|
||||
|
||||
assert chain2 == chain
|
||||
assert dumps(chain2) == chain_string
|
||||
assert isinstance(chain2, LLMChain)
|
||||
assert isinstance(chain2.llm, OpenAI)
|
||||
assert isinstance(chain2.prompt, PromptTemplate)
|
||||
|
||||
if not has_env:
|
||||
del os.environ["OPENAI_API_KEY"]
|
||||
|
||||
|
||||
@pytest.mark.requires("openai")
|
||||
def test_load_llmchain_with_non_serializable_arg() -> None:
|
||||
llm = OpenAI(
|
||||
|
Reference in New Issue
Block a user