LangChain – Use a PromptTemplate with Chaining

See also the LangChain cookbook.

Below is an example of using a PromptTemplate in LangChain

from dotenv import load_dotenv
from langchain.chat_models import ChatOpenAI
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
from langchain.chains import SimpleSequentialChain

load_dotenv()

template = """
You are a senior programmer with an expertise in {lang}. 
Implement the function below in the {lang} programming language:

Find the first four prime numbers

"""

prompt = PromptTemplate(
    input_variables=["lang"],
    template=template,
)

second_prompt = PromptTemplate(
    input_variables=["lang"],
    template="Convert the function {lang} below to a textual representation",
)

llm = ChatOpenAI(model_name="gpt-4")

chain = LLMChain(llm=llm, prompt=prompt)

chain_two = LLMChain(llm=llm, prompt=second_prompt)

overall_chain = SimpleSequentialChain(chains=[chain, chain_two], verbose=True)

print(overall_chain.run("c#"))
Share

Leave a Reply

Your email address will not be published. Required fields are marked *