mirror of
https://github.com/hwchase17/langchain.git
synced 2025-08-14 15:16:21 +00:00
```python """python scripts/update_mypy_ruff.py""" import glob import tomllib from pathlib import Path import toml import subprocess import re ROOT_DIR = Path(__file__).parents[1] def main(): for path in glob.glob(str(ROOT_DIR / "libs/**/pyproject.toml"), recursive=True): print(path) with open(path, "rb") as f: pyproject = tomllib.load(f) try: pyproject["tool"]["poetry"]["group"]["typing"]["dependencies"]["mypy"] = ( "^1.10" ) pyproject["tool"]["poetry"]["group"]["lint"]["dependencies"]["ruff"] = ( "^0.5" ) except KeyError: continue with open(path, "w") as f: toml.dump(pyproject, f) cwd = "/".join(path.split("/")[:-1]) completed = subprocess.run( "poetry lock --no-update; poetry install --with typing; poetry run mypy . --no-color", cwd=cwd, shell=True, capture_output=True, text=True, ) logs = completed.stdout.split("\n") to_ignore = {} for l in logs: if re.match("^(.*)\:(\d+)\: error:.*\[(.*)\]", l): path, line_no, error_type = re.match( "^(.*)\:(\d+)\: error:.*\[(.*)\]", l ).groups() if (path, line_no) in to_ignore: to_ignore[(path, line_no)].append(error_type) else: to_ignore[(path, line_no)] = [error_type] print(len(to_ignore)) for (error_path, line_no), error_types in to_ignore.items(): all_errors = ", ".join(error_types) full_path = f"{cwd}/{error_path}" try: with open(full_path, "r") as f: file_lines = f.readlines() except FileNotFoundError: continue file_lines[int(line_no) - 1] = ( file_lines[int(line_no) - 1][:-1] + f" # type: ignore[{all_errors}]\n" ) with open(full_path, "w") as f: f.write("".join(file_lines)) subprocess.run( "poetry run ruff format .; poetry run ruff --select I --fix .", cwd=cwd, shell=True, capture_output=True, text=True, ) if __name__ == "__main__": main() ```
122 lines
4.4 KiB
Python
122 lines
4.4 KiB
Python
"""Test SmartLLM."""
|
|
|
|
from langchain_community.chat_models import FakeListChatModel
|
|
from langchain_community.llms import FakeListLLM
|
|
from langchain_core.prompts.prompt import PromptTemplate
|
|
|
|
from langchain_experimental.smart_llm import SmartLLMChain
|
|
|
|
|
|
def test_ideation() -> None:
|
|
# test that correct responses are returned
|
|
responses = ["Idea 1", "Idea 2", "Idea 3"]
|
|
llm = FakeListLLM(responses=responses)
|
|
prompt = PromptTemplate(
|
|
input_variables=["product"],
|
|
template="What is a good name for a company that makes {product}?",
|
|
)
|
|
chain = SmartLLMChain(llm=llm, prompt=prompt)
|
|
prompt_value, _ = chain.prep_prompts({"product": "socks"})
|
|
chain.history.question = prompt_value.to_string()
|
|
results = chain._ideate()
|
|
assert results == responses
|
|
|
|
# test that correct number of responses are returned
|
|
for i in range(1, 5):
|
|
responses = [f"Idea {j+1}" for j in range(i)]
|
|
llm = FakeListLLM(responses=responses)
|
|
chain = SmartLLMChain(llm=llm, prompt=prompt, n_ideas=i)
|
|
prompt_value, _ = chain.prep_prompts({"product": "socks"})
|
|
chain.history.question = prompt_value.to_string()
|
|
results = chain._ideate()
|
|
assert len(results) == i
|
|
|
|
|
|
def test_critique() -> None:
|
|
response = "Test Critique"
|
|
llm = FakeListLLM(responses=[response])
|
|
prompt = PromptTemplate(
|
|
input_variables=["product"],
|
|
template="What is a good name for a company that makes {product}?",
|
|
)
|
|
chain = SmartLLMChain(llm=llm, prompt=prompt, n_ideas=2)
|
|
prompt_value, _ = chain.prep_prompts({"product": "socks"})
|
|
chain.history.question = prompt_value.to_string()
|
|
chain.history.ideas = ["Test Idea 1", "Test Idea 2"]
|
|
result = chain._critique()
|
|
assert result == response
|
|
|
|
|
|
def test_resolver() -> None:
|
|
response = "Test resolution"
|
|
llm = FakeListLLM(responses=[response])
|
|
prompt = PromptTemplate(
|
|
input_variables=["product"],
|
|
template="What is a good name for a company that makes {product}?",
|
|
)
|
|
chain = SmartLLMChain(llm=llm, prompt=prompt, n_ideas=2)
|
|
prompt_value, _ = chain.prep_prompts({"product": "socks"})
|
|
chain.history.question = prompt_value.to_string()
|
|
chain.history.ideas = ["Test Idea 1", "Test Idea 2"]
|
|
chain.history.critique = "Test Critique"
|
|
result = chain._resolve()
|
|
assert result == response
|
|
|
|
|
|
def test_all_steps() -> None:
|
|
joke = "Why did the chicken cross the Mobius strip?"
|
|
response = "Resolution response"
|
|
ideation_llm = FakeListLLM(responses=["Ideation response" for _ in range(20)])
|
|
critique_llm = FakeListLLM(responses=["Critique response" for _ in range(20)])
|
|
resolver_llm = FakeListLLM(responses=[response for _ in range(20)])
|
|
prompt = PromptTemplate(
|
|
input_variables=["joke"],
|
|
template="Explain this joke to me: {joke}?",
|
|
)
|
|
chain = SmartLLMChain(
|
|
ideation_llm=ideation_llm,
|
|
critique_llm=critique_llm,
|
|
resolver_llm=resolver_llm,
|
|
prompt=prompt,
|
|
)
|
|
result = chain(joke)
|
|
assert result["joke"] == joke
|
|
assert result["resolution"] == response
|
|
|
|
|
|
def test_intermediate_output() -> None:
|
|
joke = "Why did the chicken cross the Mobius strip?"
|
|
llm = FakeListLLM(responses=[f"Response {i+1}" for i in range(5)])
|
|
prompt = PromptTemplate(
|
|
input_variables=["joke"],
|
|
template="Explain this joke to me: {joke}?",
|
|
)
|
|
chain = SmartLLMChain(llm=llm, prompt=prompt, return_intermediate_steps=True)
|
|
result = chain(joke)
|
|
assert result["joke"] == joke
|
|
assert result["ideas"] == [f"Response {i+1}" for i in range(3)]
|
|
assert result["critique"] == "Response 4"
|
|
assert result["resolution"] == "Response 5"
|
|
|
|
|
|
def test_all_steps_with_chat_model() -> None:
|
|
joke = "Why did the chicken cross the Mobius strip?"
|
|
response = "Resolution response"
|
|
|
|
ideation_llm = FakeListChatModel(responses=["Ideation response" for _ in range(20)])
|
|
critique_llm = FakeListChatModel(responses=["Critique response" for _ in range(20)])
|
|
resolver_llm = FakeListChatModel(responses=[response for _ in range(20)])
|
|
prompt = PromptTemplate(
|
|
input_variables=["joke"],
|
|
template="Explain this joke to me: {joke}?",
|
|
)
|
|
chain = SmartLLMChain(
|
|
ideation_llm=ideation_llm,
|
|
critique_llm=critique_llm,
|
|
resolver_llm=resolver_llm,
|
|
prompt=prompt,
|
|
)
|
|
result = chain(joke)
|
|
assert result["joke"] == joke
|
|
assert result["resolution"] == response
|