You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
importosfromtypingimportAny, Dict, ListfromlangchainimportLLMChain, OpenAI, PromptTemplatefromlangchain.chains.baseimportChainos.environ['OPENAI_API_KEY'] =os.environ.get('OPENAI_API_KEY', 'sk-********')
classConcatenateChain(Chain):
chain_1: LLMChainchain_2: LLMChain@propertydefinput_keys(self) ->List[str]:
# Union of the input keys of the two chains.all_input_vars=set(self.chain_1.input_keys).union(
set(self.chain_2.input_keys)
)
returnlist(all_input_vars)
@propertydefoutput_keys(self) ->List[str]:
return ['concat_output']
def_call(self, inputs: Dict[str, str]) ->Dict[str, str]:
output_1=self.chain_1.run(inputs)
output_2=self.chain_2.run(inputs)
return {'concat_output': output_1+output_2}
llm=OpenAI(openai_api_key=os.environ['OPENAI_API_KEY'], temperature=0.7)
prompt_1=PromptTemplate(
input_variables=["product"],
template="What is a good name for a company that makes {product}?",
)
chain_1=LLMChain(llm=llm, prompt=prompt_1)
prompt_2=PromptTemplate(
input_variables=["product"],
template="What is a good slogan for a company that makes {product}?",
)
chain_2=LLMChain(llm=llm, prompt=prompt_2)
concat_chain=ConcatenateChain(chain_1=chain_1, chain_2=chain_2)
concat_output=concat_chain.run("colorful socks")
print(f"Concatenated output:\n{concat_output}")