Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs[patch]: Adds vector store and tool docstrings #6521

Merged
merged 4 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"### Integration details\n",
"\n",
"| Class | Package | [PY support](https://python.langchain.com/docs/integrations/tools/ddg/) | Package latest |\n",
"| :--- | :--- | :---: | :---: | :---: |\n",
"| :--- | :--- | :---: | :---: |\n",
"| [DuckDuckGoSearch](https://api.js.langchain.com/classes/langchain_community_tools_duckduckgo_search.DuckDuckGoSearch.html) | [`@langchain/community`](https://www.npmjs.com/package/@langchain/community) | ✅ | ![NPM - Version](https://img.shields.io/npm/v/@langchain/community?style=flat-square&label=%20&) |\n",
"\n",
"## Setup\n",
Expand Down
2 changes: 1 addition & 1 deletion docs/core_docs/docs/integrations/tools/tavily_search.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"### Integration details\n",
"\n",
"| Class | Package | [PY support](https://python.langchain.com/v0.2/docs/integrations/tools/tavily_search/) | Package latest |\n",
"| :--- | :--- | :---: | :---: | :---: |\n",
"| :--- | :--- | :---: | :---: |\n",
"| [TavilySearchResults](https://api.js.langchain.com/classes/langchain_community_tools_tavily_search.TavilySearchResults.html) | [`@langchain/community`](https://www.npmjs.com/package/@langchain/community) | ✅ | ![NPM - Version](https://img.shields.io/npm/v/@langchain/community?style=flat-square&label=%20&) |\n",
"\n",
"## Setup\n",
Expand Down
2 changes: 1 addition & 1 deletion docs/core_docs/docs/integrations/vectorstores/memory.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"id": "ef1f0986",
"metadata": {},
"source": [
"# `MemoryVectorStore`\n",
"# MemoryVectorStore\n",
"\n",
"LangChain offers is an in-memory, ephemeral vectorstore that stores embeddings in-memory and does an exact, linear search for the most similar embeddings. The default similarity metric is cosine similarity, but can be changed to any of the similarity metrics supported by [ml-distance](https://mljs.github.io/distance/modules/similarity.html).\n",
"\n",
Expand Down
111 changes: 108 additions & 3 deletions langchain/src/vectorstores/memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,114 @@ export interface MemoryVectorStoreArgs {
}

/**
* Class that extends `VectorStore` to store vectors in memory. Provides
* methods for adding documents, performing similarity searches, and
* creating instances from texts, documents, or an existing index.
* In-memory, ephemeral vector store.
*
* Setup:
* Install `langchain`:
*
* ```bash
* npm install langchain
* ```
*
* ## [Constructor args](https://api.js.langchain.com/classes/langchain.vectorstores_memory.MemoryVectorStore.html#constructor)
*
* <details open>
* <summary><strong>Instantiate</strong></summary>
*
* ```typescript
* import { MemoryVectorStore } from 'langchain/vectorstores/memory';
* // Or other embeddings
* import { OpenAIEmbeddings } from '@langchain/openai';
*
* const embeddings = new OpenAIEmbeddings({
* model: "text-embedding-3-small",
* });
*
* const vectorStore = new MemoryVectorStore(embeddings);
* ```
* </details>
*
* <br />
*
* <details>
* <summary><strong>Add documents</strong></summary>
*
* ```typescript
* import type { Document } from '@langchain/core/documents';
*
* const document1 = { pageContent: "foo", metadata: { baz: "bar" } };
* const document2 = { pageContent: "thud", metadata: { bar: "baz" } };
* const document3 = { pageContent: "i will be deleted :(", metadata: {} };
*
* const documents: Document[] = [document1, document2, document3];
*
* await vectorStore.addDocuments(documents);
* ```
* </details>
*
* <br />
*
* <details>
* <summary><strong>Similarity search</strong></summary>
*
* ```typescript
* const results = await vectorStore.similaritySearch("thud", 1);
* for (const doc of results) {
* console.log(`* ${doc.pageContent} [${JSON.stringify(doc.metadata, null)}]`);
* }
* // Output: * thud [{"baz":"bar"}]
* ```
* </details>
*
* <br />
*
*
* <details>
* <summary><strong>Similarity search with filter</strong></summary>
*
* ```typescript
* const resultsWithFilter = await vectorStore.similaritySearch("thud", 1, { baz: "bar" });
*
* for (const doc of resultsWithFilter) {
* console.log(`* ${doc.pageContent} [${JSON.stringify(doc.metadata, null)}]`);
* }
* // Output: * foo [{"baz":"bar"}]
* ```
* </details>
*
* <br />
*
*
* <details>
* <summary><strong>Similarity search with score</strong></summary>
*
* ```typescript
* const resultsWithScore = await vectorStore.similaritySearchWithScore("qux", 1);
* for (const [doc, score] of resultsWithScore) {
* console.log(`* [SIM=${score.toFixed(6)}] ${doc.pageContent} [${JSON.stringify(doc.metadata, null)}]`);
* }
* // Output: * [SIM=0.000000] qux [{"bar":"baz","baz":"bar"}]
* ```
* </details>
*
* <br />
*
* <details>
* <summary><strong>As a retriever</strong></summary>
*
* ```typescript
* const retriever = vectorStore.asRetriever({
* searchType: "mmr", // Leave blank for standard similarity search
* k: 1,
* });
* const resultAsRetriever = await retriever.invoke("thud");
* console.log(resultAsRetriever);
*
* // Output: [Document({ metadata: { "baz":"bar" }, pageContent: "thud" })]
* ```
* </details>
*
* <br />
*/
export class MemoryVectorStore extends VectorStore {
declare FilterType: (doc: Document) => boolean;
Expand Down
65 changes: 63 additions & 2 deletions libs/langchain-community/src/tools/duckduckgo_search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,69 @@ export interface DuckDuckGoSearchParameters extends ToolParams {
const DEFAULT_MAX_RESULTS = 10;

/**
* Class for interacting with the DuckDuckGo search engine
* It extends the base Tool class to perform retrieval.
* DuckDuckGo tool integration.
*
* Setup:
* Install `@langchain/community` and `duck-duck-scrape`.
*
* ```bash
* npm install @langchain/community duck-duck-scrape
* ```
*
* ## [Constructor args](https://api.js.langchain.com/classes/_langchain_community.tools_duckduckgo_search.DuckDuckGoSearch.html#constructor)
*
* <details open>
* <summary><strong>Instantiate</strong></summary>
*
* ```typescript
* import { DuckDuckGoSearch } from "@langchain/community/tools/duckduckgo_search";
*
* const tool = new DuckDuckGoSearch({ maxResults: 1 });
* ```
* </details>
*
* <br />
*
* <details>
*
* <summary><strong>Invocation</strong></summary>
*
* ```typescript
* await tool.invoke("what is the current weather in sf?");
*
* // output: [{"title":"San Francisco, CA Current Weather | AccuWeather","link":"https://www.accuweather.com/en/us/san-francisco/94103/current-weather/347629","snippet":"<b>Current</b> <b>weather</b> <b>in</b> San Francisco, CA. Check <b>current</b> conditions in San Francisco, CA with radar, hourly, and more."}]
* ```
* </details>
*
* <br />
*
* <details>
*
* <summary><strong>Invocation with tool call</strong></summary>
*
* ```typescript
* // This is usually generated by a model, but we'll create a tool call directly for demo purposes.
* const modelGeneratedToolCall = {
* args: {
* input: "what is the current weather in sf?",
* },
* id: "tool_call_id",
* name: tool.name,
* type: "tool_call",
* };
* await tool.invoke(modelGeneratedToolCall);
* ```
*
* ```text
* ToolMessage {
* "content": "[{\"title\":\"San Francisco, CA Weather Conditions | Weather Underground\",\"link\":\"https://www.wunderground.com/weather/us/ca/san-francisco\",\"snippet\":\"San Francisco <b>Weather</b> Forecasts. <b>Weather</b> Underground provides local & long-range <b>weather</b> forecasts, weatherreports, maps & tropical <b>weather</b> conditions for the San Francisco area.\"}]",
* "name": "duckduckgo-search",
* "additional_kwargs": {},
* "response_metadata": {},
* "tool_call_id": "tool_call_id"
* }
* ```
* </details>
*/
export class DuckDuckGoSearch extends Tool {
private searchOptions?: SearchOptions;
Expand Down
65 changes: 64 additions & 1 deletion libs/langchain-community/src/tools/tavily_search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,70 @@ export type TavilySearchAPIRetrieverFields = ToolParams & {
};

/**
* Tool for the Tavily search API.
* Tavily search API tool integration.
*
* Setup:
* Install `@langchain/community`. You'll also need an API key set as `TAVILY_API_KEY`.
*
* ```bash
* npm install @langchain/community
* ```
*
* ## [Constructor args](https://api.js.langchain.com/classes/_langchain_community.tools_tavily_search.TavilySearchResults.html#constructor)
*
* <details open>
* <summary><strong>Instantiate</strong></summary>
*
* ```typescript
* import { TavilySearchResults } from "@langchain/community/tools/tavily_search";
*
* const tool = new TavilySearchResults({
* maxResults: 2,
* // ...
* });
* ```
* </details>
*
* <br />
*
* <details>
*
* <summary><strong>Invocation</strong></summary>
*
* ```typescript
* await tool.invoke("what is the current weather in sf?");
* ```
* </details>
*
* <br />
*
* <details>
*
* <summary><strong>Invocation with tool call</strong></summary>
*
* ```typescript
* // This is usually generated by a model, but we'll create a tool call directly for demo purposes.
* const modelGeneratedToolCall = {
* args: {
* input: "what is the current weather in sf?",
* },
* id: "tool_call_id",
* name: tool.name,
* type: "tool_call",
* };
* await tool.invoke(modelGeneratedToolCall);
* ```
*
* ```text
* ToolMessage {
* "content": "...",
* "name": "tavily_search_results_json",
* "additional_kwargs": {},
* "response_metadata": {},
* "tool_call_id": "tool_call_id"
* }
* ```
* </details>
*/
export class TavilySearchResults extends Tool {
static lc_name(): string {
Expand Down
64 changes: 59 additions & 5 deletions libs/langchain-community/src/tools/wikipedia_query_run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,70 @@ interface PageResult {
}

/**
* Class for interacting with and fetching data from the Wikipedia API. It
* extends the Tool class.
* @example
* Wikipedia query tool integration.
*
* Setup:
* Install `@langchain/community`. You'll also need an API key.
*
* ```bash
* npm install @langchain/community
* ```
*
* ## [Constructor args](https://api.js.langchain.com/classes/_langchain_community.tools_wikipedia_query_run.WikipediaQueryRun.html#constructor)
*
* <details open>
* <summary><strong>Instantiate</strong></summary>
*
* ```typescript
* const wikipediaQuery = new WikipediaQueryRun({
* import { WikipediaQueryRun } from "@langchain/community/tools/wikipedia_query_run";
*
* const tool = new WikipediaQueryRun({
* topKResults: 3,
* maxDocContentLength: 4000,
* });
* const result = await wikipediaQuery.call("Langchain");
* ```
* </details>
*
* <br />
*
* <details>
*
* <summary><strong>Invocation</strong></summary>
*
* ```typescript
* await tool.invoke("what is the current weather in sf?");
* ```
* </details>
*
* <br />
*
* <details>
*
* <summary><strong>Invocation with tool call</strong></summary>
*
* ```typescript
* // This is usually generated by a model, but we'll create a tool call directly for demo purposes.
* const modelGeneratedToolCall = {
* args: {
* input: "what is the current weather in sf?",
* },
* id: "tool_call_id",
* name: tool.name,
* type: "tool_call",
* };
* await tool.invoke(modelGeneratedToolCall);
* ```
*
* ```text
* ToolMessage {
* "content": "...",
* "name": "wikipedia-api",
* "additional_kwargs": {},
* "response_metadata": {},
* "tool_call_id": "tool_call_id"
* }
* ```
* </details>
*/
export class WikipediaQueryRun extends Tool {
static lc_name() {
Expand Down
Loading
Loading