FewShotPromptTemplate
import os
from dotenv import load_dotenv
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain_core.prompts import PromptTemplate, FewShotPromptTemplate
load_dotenv()
api_key = os.getenv("GEMINI_API_KEY")
llm = ChatGoogleGenerativeAI(model="gemini-2.5-flash", api_key=api_key)
examples = [
{"question": "Who lived longer, Steve Jobs or Einstein?", "answer": "Einstein"},
{"question": "When was Naver's founder born?", "answer": "June 22, 1967"},
]
example_template = "Q:{question}\nA:{answer}\n"
few_shot_prompt = FewShotPromptTemplate(
examples=examples,
example_prompt=PromptTemplate.from_template(example_template),
prefix="You are a helpful assistant. Answer the question using examples below:\n",
suffix="Now answer this question: {new_question}",
input_variables=["new_question"]
)
final_prompt = few_shot_prompt.format(new_question="Who directed Parasite?")
response = llm.invoke(final_prompt)
print(response.content)Last updated