Debugging Dynamic Few Shot Langchain Code

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code># Import necessary modules and classes from langchain_community and langchain_core
from langchain_community.vectorstores import Chroma
from langchain_core.example_selectors import SemanticSimilarityExampleSelector
from langchain_openai import OpenAIEmbeddings ## paid
from langchain.embeddings import HuggingFaceEmbeddings #free
## setup vector db
# Create an instance of Chroma vector store
vectorstore = Chroma()
# Delete any existing collection in the vector store
# vectorstore.delete_collection()
# Create a SemanticSimilarityExampleSelector instance using examples, OpenAI embeddings, and the vector store
example_selector = SemanticSimilarityExampleSelector.from_examples(
examples, # List of example queries and inputs
HuggingFaceEmbeddings(), #HuggingFaceEmbeddings() OpenAIEmbeddings() # OpenAI Embeddings for generating vector representations
vectorstore, # Chroma vector store for storing and querying vector representations
k=2, # Number of similar examples to retrieve
input_keys=["input"], # Define the input keys to consider for semantic similarity
)
</code>
<code># Import necessary modules and classes from langchain_community and langchain_core from langchain_community.vectorstores import Chroma from langchain_core.example_selectors import SemanticSimilarityExampleSelector from langchain_openai import OpenAIEmbeddings ## paid from langchain.embeddings import HuggingFaceEmbeddings #free ## setup vector db # Create an instance of Chroma vector store vectorstore = Chroma() # Delete any existing collection in the vector store # vectorstore.delete_collection() # Create a SemanticSimilarityExampleSelector instance using examples, OpenAI embeddings, and the vector store example_selector = SemanticSimilarityExampleSelector.from_examples( examples, # List of example queries and inputs HuggingFaceEmbeddings(), #HuggingFaceEmbeddings() OpenAIEmbeddings() # OpenAI Embeddings for generating vector representations vectorstore, # Chroma vector store for storing and querying vector representations k=2, # Number of similar examples to retrieve input_keys=["input"], # Define the input keys to consider for semantic similarity ) </code>
# Import necessary modules and classes from langchain_community and langchain_core
from langchain_community.vectorstores import Chroma
from langchain_core.example_selectors import SemanticSimilarityExampleSelector
from langchain_openai import OpenAIEmbeddings   ## paid
from langchain.embeddings import HuggingFaceEmbeddings   #free


## setup vector db
# Create an instance of Chroma vector store
vectorstore = Chroma()

# Delete any existing collection in the vector store
# vectorstore.delete_collection()

# Create a SemanticSimilarityExampleSelector instance using examples, OpenAI embeddings, and the vector store
example_selector = SemanticSimilarityExampleSelector.from_examples(
    examples,  # List of example queries and inputs
    HuggingFaceEmbeddings(), #HuggingFaceEmbeddings() OpenAIEmbeddings() # OpenAI Embeddings for generating vector representations
    vectorstore,  # Chroma vector store for storing and querying vector representations
    k=2,  # Number of similar examples to retrieve
    input_keys=["input"],  # Define the input keys to consider for semantic similarity
)

This is my code, its showing an error “name ‘examples’ is not defined”. How to solve it ?

I tried to debug, but i failed. Could anyone hepl me out.

Does examples get defined? Here’s an example of SemanticSimilarityExampleSelector.from_examples

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>example_prompt = PromptTemplate(
input_variables=["input", "output"],
template="Input: {input}nOutput: {output}",
)
# Examples of a pretend task of creating antonyms.
examples = [
{"input": "happy", "output": "sad"},
{"input": "tall", "output": "short"},
{"input": "energetic", "output": "lethargic"},
{"input": "sunny", "output": "gloomy"},
{"input": "windy", "output": "calm"},
]
example_selector = SemanticSimilarityExampleSelector.from_examples(
# The list of examples available to select from.
examples,
# The embedding class used to produce embeddings which are used to measure semantic similarity.
OpenAIEmbeddings(),
# The VectorStore class that is used to store the embeddings and do a similarity search over.
Chroma,
# The number of examples to produce.
k=1,
)
similar_prompt = FewShotPromptTemplate(
# We provide an ExampleSelector instead of examples.
example_selector=example_selector,
example_prompt=example_prompt,
prefix="Give the antonym of every input",
suffix="Input: {adjective}nOutput:",
input_variables=["adjective"],
)
</code>
<code>example_prompt = PromptTemplate( input_variables=["input", "output"], template="Input: {input}nOutput: {output}", ) # Examples of a pretend task of creating antonyms. examples = [ {"input": "happy", "output": "sad"}, {"input": "tall", "output": "short"}, {"input": "energetic", "output": "lethargic"}, {"input": "sunny", "output": "gloomy"}, {"input": "windy", "output": "calm"}, ] example_selector = SemanticSimilarityExampleSelector.from_examples( # The list of examples available to select from. examples, # The embedding class used to produce embeddings which are used to measure semantic similarity. OpenAIEmbeddings(), # The VectorStore class that is used to store the embeddings and do a similarity search over. Chroma, # The number of examples to produce. k=1, ) similar_prompt = FewShotPromptTemplate( # We provide an ExampleSelector instead of examples. example_selector=example_selector, example_prompt=example_prompt, prefix="Give the antonym of every input", suffix="Input: {adjective}nOutput:", input_variables=["adjective"], ) </code>
example_prompt = PromptTemplate(
    input_variables=["input", "output"],
    template="Input: {input}nOutput: {output}",
)

# Examples of a pretend task of creating antonyms.
examples = [
    {"input": "happy", "output": "sad"},
    {"input": "tall", "output": "short"},
    {"input": "energetic", "output": "lethargic"},
    {"input": "sunny", "output": "gloomy"},
    {"input": "windy", "output": "calm"},
]

example_selector = SemanticSimilarityExampleSelector.from_examples(
    # The list of examples available to select from.
    examples,
    # The embedding class used to produce embeddings which are used to measure semantic similarity.
    OpenAIEmbeddings(),
    # The VectorStore class that is used to store the embeddings and do a similarity search over.
    Chroma,
    # The number of examples to produce.
    k=1,
)
similar_prompt = FewShotPromptTemplate(
    # We provide an ExampleSelector instead of examples.
    example_selector=example_selector,
    example_prompt=example_prompt,
    prefix="Give the antonym of every input",
    suffix="Input: {adjective}nOutput:",
    input_variables=["adjective"],
)

Take a look at the reference > https://python.langchain.com/v0.2/docs/how_to/example_selectors_mmr/

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật