Replies: 1 comment
-
Hey there, @komi786! I'm a bot here to help you with any issues you're experiencing. Let's work together to solve this problem! To resolve the error Here is an example of how to correctly set up and use the import { FewShotChatMessagePromptTemplate } from 'path-to-few_shot';
import { PromptTemplate } from 'path-to-prompt';
import { SemanticSimilarityExampleSelector } from 'path-to-example_selector';
import { OpenAIEmbeddings } from 'path-to-openai-embeddings';
import { HNSWLib } from 'path-to-hnswlib';
// Define the example prompt template
const examplePrompt = PromptTemplate.fromTemplate(
"Input: {input}\nOutput: {output}"
);
// Create an example selector
const exampleSelector = await SemanticSimilarityExampleSelector.fromExamples(
[
{ input: "happy", output: "sad" },
{ input: "tall", output: "short" },
{ input: "energetic", output: "lethargic" },
{ input: "sunny", output: "gloomy" },
{ input: "windy", output: "calm" },
],
new OpenAIEmbeddings(),
HNSWLib,
{ k: 1 }
);
// Create the FewShotChatMessagePromptTemplate
const dynamicPrompt = new FewShotChatMessagePromptTemplate({
exampleSelector,
examplePrompt,
prefix: "Give the antonym of every input",
suffix: "Input: {adjective}\nOutput:",
inputVariables: ["adjective"],
});
// Format the dynamic prompt with the input 'rainy'
console.log(await dynamicPrompt.format({ adjective: "rainy" })); Ensure that the |
Beta Was this translation helpful? Give feedback.
-
Checked other resources
Commit to Help
Example Code
Description
Hello, I'm new to using LangChain and currently experimenting with the SemanticSimilarityExampleSelector to improve efficiency by avoiding unnecessary examples in my setup. However, I've encountered an issue when trying to integrate an
ExampleSelector
with aChatPromptTemplate
.Here’s what I’m trying to achieve:
I want to use
ExampleSelector
withinChatPromptTemplate
to dynamically select relevant examples based on the input.The implementation works fine without the
ExampleSelector
, but when I add the selector, I encounter the following error:Error Message:
Error in prompt: 'input_text'
. It probably throws error before final prompt as It does not get printed.`
def extract_information(input_text, examples, model):
try:
active_model = LLMManager.get_instance(model=model)
print(f"Examples={examples}")
embedding = FastEmbedEmbeddings()
example_selector = SemanticSimilarityExampleSelector.from_examples(
examples,
embedding,
FAISS,
k=4
)
few_shot_prompt = FewShotChatMessagePromptTemplate(
example_selector=example_selector,
example_prompt=ChatPromptTemplate.from_messages(
[("human", "{input}"), ("ai", "{output}")]
)
)
print(f"loaded model")
base_prompt=f"""Role: You are a helpful assistant
Input Text: "{input_text}"\n,
output: """
System Info
linux
langchain-python
latest langchain package
Beta Was this translation helpful? Give feedback.
All reactions