LangChain Reference home pageLangChain ReferenceLangChain Reference
  • GitHub
  • Main Docs
Deep Agents
LangChain
LangGraph
Integrations
LangSmith
LangChain
  • Universal
  • Hub
  • Node
  • Load
  • Serializable
  • Encoder Backed
  • File System
  • In Memory
LangChain Core
  • Agents
  • Caches
  • Base
  • Dispatch
  • Web
  • Manager
  • Promises
  • Chat History
  • Context
  • Base
  • Langsmith
  • Documents
  • Embeddings
  • Errors
  • Example Selectors
  • Indexing
  • Base
  • Chat Models
  • Llms
  • Profile
  • Load
  • Serializable
  • Memory
  • Messages
  • Tool
  • Output Parsers
  • Openai Functions
  • Openai Tools
  • Outputs
  • Prompt Values
  • Prompts
  • Retrievers
  • Document Compressors
  • Runnables
  • Graph
  • Singletons
  • Stores
  • Structured Query
  • Tools
  • Base
  • Console
  • Log Stream
  • Run Collector
  • Tracer Langchain
  • Stream
  • Async Caller
  • Chunk Array
  • Context
  • Env
  • Event Source Parse
  • Format
  • Function Calling
  • Hash
  • Json Patch
  • Json Schema
  • Math
  • Ssrf
  • Stream
  • Testing
  • Tiktoken
  • Types
  • Vectorstores
Text Splitters
MCP Adapters
⌘I

LangChain Assistant

Ask a question to get started

Enter to send•Shift+Enter new line

Menu

LangChain
UniversalHubNodeLoadSerializableEncoder BackedFile SystemIn Memory
LangChain Core
AgentsCachesBaseDispatchWebManagerPromisesChat HistoryContextBaseLangsmithDocumentsEmbeddingsErrorsExample SelectorsIndexingBaseChat ModelsLlmsProfileLoadSerializableMemoryMessagesToolOutput ParsersOpenai FunctionsOpenai ToolsOutputsPrompt ValuesPromptsRetrieversDocument CompressorsRunnablesGraphSingletonsStoresStructured QueryToolsBaseConsoleLog StreamRun CollectorTracer LangchainStreamAsync CallerChunk ArrayContextEnvEvent Source ParseFormatFunction CallingHashJson PatchJson SchemaMathSsrfStreamTestingTiktokenTypesVectorstores
Text Splitters
MCP Adapters
Language
Theme
JavaScript@langchain/coreexample_selectorsSemanticSimilarityExampleSelector
Classā—Since v1.0

SemanticSimilarityExampleSelector

Class that selects examples based on semantic similarity. It extends the BaseExampleSelector class.

Copy
class SemanticSimilarityExampleSelector

Bases

BaseExampleSelector

Example

Copy
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 },
);
const dynamicPrompt = new FewShotPromptTemplate({
  exampleSelector,
  examplePrompt: PromptTemplate.fromTemplate(
    "Input: {input}\nOutput: {output}",
  ),
  prefix: "Give the antonym of every input",
  suffix: "Input: {adjective}\nOutput:",
  inputVariables: ["adjective"],
});
console.log(await dynamicPrompt.format({ adjective: "rainy" }));

Constructors

constructor
constructor

Properties

property
exampleKeys: string[]
property
inputKeys: string[]
property
lc_kwargs: SerializedFields
property
lc_namespace: string[]

A path to the module that contains the class, eg. ["langchain", "llms"] Usually should be the same as the entrypoint the class is exported from.

property
lc_serializable: boolean
property
vectorStoreRetriever: VectorStoreRetrieverInterface<V>
property
lc_aliases: __type | undefined
property
lc_attributes: __type | undefined
property
lc_id: string[]
property
lc_secrets: __type | undefined
property
lc_serializable_keys: string[] | undefined

Methods

method
addExample→ Promise<string | void>

Adds an example to the example selector.

method
selectExamples→ Promise<Example[]>

Selects examples from the example selector given the input variables.

method
toJSON→ Serialized
method
toJSONNotImplemented→ SerializedNotImplemented
method
fromExamples→ Promise<LengthBasedExampleSelector>

Creates a new instance of LengthBasedExampleSelector and adds a list of examples to it.

method
lc_name→ string

The name of the serializable. Override to provide an alias or to preserve the serialized module name in minified environments.

Implemented as a static method to support loading logic.

Inherited fromBaseExampleSelector

Properties

Plc_kwargs: SerializedFieldsPlc_namespace: ["langchain_core", "callbacks", string]
—

A path to the module that contains the class, eg. ["langchain", "llms"]

Plc_serializable: booleanPlc_aliases: __type | undefinedPlc_attributes: __type | undefinedPlc_id: string[]Plc_secrets: __type | undefinedPlc_serializable_keys: string[] | undefined

Methods

MaddExample→ Promise<string | void>
—

Adds an example to the example selector.

MselectExamples→ Promise<Example[]>
—

Selects examples from the example selector given the input variables.

MtoJSON→ SerializedMtoJSONNotImplemented→ SerializedNotImplementedMlc_name→ string
—

The name of the serializable. Override to provide an alias or

Inherited fromSerializable

Properties

Plc_kwargs: SerializedFieldsPlc_namespace: ["langchain_core", "callbacks", string]
—

A path to the module that contains the class, eg. ["langchain", "llms"]

Plc_serializable: booleanPlc_aliases: __type | undefinedPlc_attributes: __type | undefinedPlc_id: string[]Plc_secrets: __type | undefinedPlc_serializable_keys: string[] | undefined

Methods

MtoJSON→ SerializedMtoJSONNotImplemented→ SerializedNotImplementedMlc_name→ string
—

The name of the serializable. Override to provide an alias or

View source on GitHub