Skip to content

Instantly share code, notes, and snippets.

@djacobs7
Created February 23, 2023 19:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save djacobs7/a9e75da29160888cb4aef14cbc8ead73 to your computer and use it in GitHub Desktop.
Save djacobs7/a9e75da29160888cb4aef14cbc8ead73 to your computer and use it in GitHub Desktop.
langchain self critique example
# Example of a bad LLM
from langchain.llms import OpenAI
from langchain.prompts import PromptTemplate
from langchain.chains.llm import LLMChain
story_prompt = PromptTemplate(
template="""Write a story about: {story}""",
input_variables=["story"]
)
llm = OpenAI(temperature=0)
from langchain.chains.constitutional_ai.base import ConstitutionalChain
from langchain.chains.constitutional_ai.models import ConstitutionalPrinciple
principles = [
# ConstitutionalPrinciple(
# name="Ethical Principle",
# critique_request="The model should only talk about ethical and legal things.",
# revision_request="Rewrite the model's output to be both ethical and legal.",
# ),
ConstitutionalPrinciple(
name="Main character",
critique_request="The model should only use a teenager main character.",
revision_request="Rewrite the story from the perspective of a teenager.",
),
ConstitutionalPrinciple(
name="no explicit moral",
critique_request="Identify if this story has an explicit moral.",
revision_request="Rewrite the story so that there is no explicit moral.",
),
ConstitutionalPrinciple(
name="not persuasive",
critique_request="Identify if this story tries to persuade the reader of a certain point of view.",
revision_request="Rewrite the story so that it is not persuasive.",
),
ConstitutionalPrinciple(
name="hopeful ending",
critique_request="Identify if this story has a hopeful ending.",
revision_request="Rewrite the story so that there is a hopeful ending.",
)
]
story_chain = LLMChain(llm=llm, prompt=story_prompt)
constitutional_chain = ConstitutionalChain.from_llm(
chain=story_chain,
constitutional_principles=
principles
,
llm=llm,
verbose=True,
)
# story= "a girl who loses her magical balloon"
# x = story_chain.run(story=story)
# print(x)
# x2 = constitutional_chain.run(story=story)
# print(x2)
# x = llm( "Write a story about a girl who loses her magical balloon. The story should be for teenagers.")
# print(x)
x = llm( "Write a story about a girl who loses her magical balloon. The story should be for teenagers. The main character should be a teenager. There should be no explicit moral. The story should have a hopeful ending")
print(x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment