Các mẫu prompt LangChain (phần 5)

Các kĩ thuật tối ưu hóa prompt (tiếp tục)

Ví dụ về tối ưu hóa prompt qua học few-shot:

from langchain.prompts import FewShotPromptTemplate, PromptTemplate
# Define the example prompt template
example_prompt = PromptTemplate(

input_variables=[“input”, “output”],
template=”Input: {input}\nOutput: {output}”

)
# Define the few-shot prompt template
few_shot_prompt = FewShotPromptTemplate(

examples=[

{“input”: “This film was a complete waste of time. The plot was confusing and the acting was terrible?”, “output”: “Negative”},
{“input”: “I was blown away by the stunning visuals and compelling storyline. A must-see!”, “output”: “Positive”},

],

example_prompt=example_prompt,
prefix=”Answer the following questions:\n”,
suffix=”{input}\nOutput:”,
input_variables=[“input”],

)
# Use the few-shot prompt in your chain
chain = LLMChain(llm=llm, prompt=few_shot_prompt)
result = chain.invoke({“input”: “very bad decision to watch that movie”})
print(result)
output:
{‘input’: ‘very bad decision to watch that movie’, ‘text’: ‘ Negative’}

Code này minh họa một mẫu prompt few-shot cho phân tích sắc thái sử dụng LangChain. Nó định nghĩa các đầu vào
ví dụ và đầu ra cho movie reviews tích cực và tiêu cực, tạo một mẫu prompt cái hướng dẫn mô hình ngôn ngữ phân loại
các đầu vào movie review mới dựa trên các ví dụ này. Code thiết lập một xích cái sẽ đoán sắc thái của một movie review
text đã cho.

Chia sẻ