-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_few_shot_prompting.py
More file actions
46 lines (36 loc) · 1.27 KB
/
Copy pathsimple_few_shot_prompting.py
File metadata and controls
46 lines (36 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from dotenv import load_dotenv
load_dotenv()
from langchain import PromptTemplate, FewShotPromptTemplate, LLMChain
from langchain.llms import OpenAI
# Initialize LLM
llm = OpenAI(model_name="text-davinci-003", temperature=0)
examples = [
{"color": "red", "emotion": "passion"},
{"color": "blue", "emotion": "serenity"},
{"color": "green", "emotion": "tranquility"},
]
example_formatter_template = """
Color: {color}
Emotion: {emotion}\n
"""
example_prompt = PromptTemplate(
input_variables=["color", "emotion"],
template=example_formatter_template,
)
few_shot_prompt = FewShotPromptTemplate(
examples=examples,
example_prompt=example_prompt,
prefix="Here are some examples of colors and the emotions associated with them:\n\n",
suffix="\n\nNow, given a new color, identify the emotion associated with it:\n\nColor: {input}\nEmotion:",
input_variables=["input"],
example_separator="\n",
)
formatted_prompt = few_shot_prompt.format(input="purple")
# Create the LLMChain for the prompt
chain = LLMChain(
llm=llm, prompt=PromptTemplate(template=formatted_prompt, input_variables=[])
)
# Run the LLMChain to get the AI-generated emotion associated with the input color
response = chain.run({})
print("Color: purple")
print("Emotion:", response)