langchain/libs/experimental/tests/unit_tests/test_smartllm.py

121 lines
4.4 KiB
Python
Raw Normal View History

Added SmartGPT workflow (issue #4463) (#4816) # Added SmartGPT workflow by providing SmartLLM wrapper around LLMs Edit: As @hwchase17 suggested, this should be a chain, not an LLM. I have adapted the PR. It is used like this: ``` from langchain.prompts import PromptTemplate from langchain.chains import SmartLLMChain from langchain.chat_models import ChatOpenAI hard_question = "I have a 12 liter jug and a 6 liter jug. I want to measure 6 liters. How do I do it?" hard_question_prompt = PromptTemplate.from_template(hard_question) llm = ChatOpenAI(model_name="gpt-4") prompt = PromptTemplate.from_template(hard_question) chain = SmartLLMChain(llm=llm, prompt=prompt, verbose=True) chain.run({}) ``` Original text: Added SmartLLM wrapper around LLMs to allow for SmartGPT workflow (as in https://youtu.be/wVzuvf9D9BU). SmartLLM can be used wherever LLM can be used. E.g: ``` smart_llm = SmartLLM(llm=OpenAI()) smart_llm("What would be a good company name for a company that makes colorful socks?") ``` or ``` smart_llm = SmartLLM(llm=OpenAI()) prompt = PromptTemplate( input_variables=["product"], template="What is a good name for a company that makes {product}?", ) chain = LLMChain(llm=smart_llm, prompt=prompt) chain.run("colorful socks") ``` SmartGPT consists of 3 steps: 1. Ideate - generate n possible solutions ("ideas") to user prompt 2. Critique - find flaws in every idea & select best one 3. Resolve - improve upon best idea & return it Fixes #4463 ## Who can review? Community members can review the PR once tests pass. Tag maintainers/contributors who might be interested: - @hwchase17 - @agola11 Twitter: [@UmerHAdil](https://twitter.com/@UmerHAdil) | Discord: RicChilligerDude#7589 --------- Co-authored-by: Bagatur <baskaryan@gmail.com>
2023-08-11 22:44:27 +00:00
"""Test SmartLLM."""
from langchain.chat_models import FakeListChatModel
from langchain.llms import FakeListLLM
from langchain.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