Skip to content

Commit

Permalink
community[patch]: Change recommended entrypoint, bump versions (langc…
Browse files Browse the repository at this point in the history
…hain-ai#4069)

* Change recommended entrypoint

* Update docs

* Fix lint

* Bump versions

* Update lock
  • Loading branch information
jacoblee93 authored Jan 18, 2024
1 parent 0e3c944 commit c311576
Show file tree
Hide file tree
Showing 18 changed files with 26 additions and 159 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { RunnableSequence } from "langchain/runnables";
import { ChatPromptTemplate, MessagesPlaceholder } from "langchain/prompts";
import { ChatOpenAI } from "@langchain/openai";
import { AgentExecutor } from "langchain/agents";
import { DynamicTool } from "langchain/tools";
import { DynamicTool } from "@langchain/core/tools";
import type { FunctionsAgentAction } from "langchain/agents/openai/output_parser";
import { convertToOpenAIFunction } from "@langchain/core/utils/function_calling";

Expand Down
2 changes: 1 addition & 1 deletion docs/core_docs/docs/modules/agents/how_to/custom_agent.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Next, let's define some tools to use.
Let's write a really simple JavaScript function to calculate the length of a word that is passed in.

```ts
import { DynamicTool } from "langchain/tools";
import { DynamicTool } from "@langchain/core/tools";

const customTool = new DynamicTool({
name: "get_word_length",
Expand Down
2 changes: 1 addition & 1 deletion examples/src/agents/agent_structured.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
ChatPromptTemplate,
MessagesPlaceholder,
} from "@langchain/core/prompts";
import { DynamicTool } from "@langchain/community/tools/dynamic";
import { DynamicTool } from "@langchain/core/tools";

const llm = new ChatOpenAI({
modelName: "gpt-4-1106-preview",
Expand Down
2 changes: 1 addition & 1 deletion examples/src/agents/custom_agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import { convertToOpenAIFunction } from "@langchain/core/utils/function_calling";
import { OpenAIFunctionsAgentOutputParser } from "langchain/agents/openai/output_parser";
import { AIMessage, BaseMessage, HumanMessage } from "@langchain/core/messages";
import { DynamicTool } from "@langchain/community/tools/dynamic";
import { DynamicTool } from "@langchain/core/tools";

/**
* Define your chat model to use.
Expand Down
5 changes: 1 addition & 4 deletions examples/src/agents/custom_tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ import { createOpenAIFunctionsAgent, AgentExecutor } from "langchain/agents";
import { pull } from "langchain/hub";

import { z } from "zod";
import {
DynamicTool,
DynamicStructuredTool,
} from "@langchain/community/tools/dynamic";
import { DynamicTool, DynamicStructuredTool } from "@langchain/core/tools";

const llm = new ChatOpenAI({
modelName: "gpt-3.5-turbo",
Expand Down
2 changes: 1 addition & 1 deletion examples/src/agents/handle_parsing_error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { ChatPromptTemplate } from "@langchain/core/prompts";
import { ChatOpenAI } from "@langchain/openai";
import { AgentExecutor, createOpenAIFunctionsAgent } from "langchain/agents";
import { pull } from "langchain/hub";
import { DynamicStructuredTool } from "@langchain/community/tools/dynamic";
import { DynamicStructuredTool } from "@langchain/core/tools";

const model = new ChatOpenAI({ temperature: 0.1 });
const tools = [
Expand Down
2 changes: 1 addition & 1 deletion examples/src/agents/openai_tools_runnable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
MessagesPlaceholder,
} from "@langchain/core/prompts";
import { RunnableSequence } from "@langchain/core/runnables";
import { DynamicStructuredTool } from "@langchain/community/tools/dynamic";
import { DynamicStructuredTool } from "@langchain/core/tools";

const model = new ChatOpenAI({
modelName: "gpt-3.5-turbo-1106",
Expand Down
2 changes: 1 addition & 1 deletion examples/src/agents/structured_chat_runnable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
} from "@langchain/core/prompts";
import { RunnableSequence } from "@langchain/core/runnables";
import { AgentStep } from "@langchain/core/agents";
import { DynamicStructuredTool } from "@langchain/community/tools/dynamic";
import { DynamicStructuredTool } from "@langchain/core/tools";

/**
* Need:
Expand Down
2 changes: 1 addition & 1 deletion langchain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1203,7 +1203,7 @@
"dependencies": {
"@anthropic-ai/sdk": "^0.9.1",
"@langchain/community": "~0.0.17",
"@langchain/core": "~0.1.13",
"@langchain/core": "~0.1.16",
"@langchain/openai": "~0.0.12",
"binary-extensions": "^2.2.0",
"expr-eval": "^2.0.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { CallbackManagerForToolRun } from "@langchain/core/callbacks/manager";
import {
DynamicStructuredTool,
DynamicStructuredToolInput,
} from "@langchain/community/tools/dynamic";
} from "@langchain/core/tools";
import { formatDocumentsAsString } from "../../../util/document.js";

/** @deprecated Use "langchain/tools/retriever" instead. */
Expand Down
2 changes: 1 addition & 1 deletion langchain/src/agents/toolkits/openapi/openapi.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { BaseLanguageModelInterface } from "@langchain/core/language_models/base";
import type { ToolInterface } from "@langchain/core/tools";
import { DynamicTool } from "@langchain/community/tools/dynamic";
import { DynamicTool } from "@langchain/core/tools";
import { Toolkit } from "@langchain/community/agents/toolkits/base";
import { JsonSpec } from "../../../tools/json.js";
import { AgentExecutor } from "../../executor.js";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import type { BaseLanguageModelInterface } from "@langchain/core/language_models/base";
import { ChainValues } from "@langchain/core/utils/types";
import { Tool } from "@langchain/core/tools";
import { Tool, DynamicStructuredTool } from "@langchain/core/tools";
import { CallbackManagerForChainRun } from "@langchain/core/callbacks/manager";
import { DynamicStructuredTool } from "@langchain/community/tools/dynamic";
import { BaseChain, ChainInputs } from "../../chains/base.js";
import {
BasePlanner,
Expand Down
3 changes: 1 addition & 2 deletions langchain/src/experimental/plan_and_execute/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import {
SystemMessagePromptTemplate,
} from "@langchain/core/prompts";

import { Tool } from "@langchain/core/tools";
import { DynamicStructuredTool } from "@langchain/community/tools/dynamic";
import { Tool, DynamicStructuredTool } from "@langchain/core/tools";

export const PLANNER_SYSTEM_PROMPT_MESSAGE_TEMPLATE = [
`Let's first understand the problem and devise a plan to solve the problem.`,
Expand Down
5 changes: 1 addition & 4 deletions langchain/src/tools/chain.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import {
DynamicTool,
DynamicToolInput,
} from "@langchain/community/tools/dynamic";
import { DynamicTool, DynamicToolInput } from "@langchain/core/tools";
import { BaseChain } from "../chains/base.js";

/**
Expand Down
2 changes: 1 addition & 1 deletion langchain/src/tools/retriever.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { CallbackManagerForToolRun } from "@langchain/core/callbacks/manager";
import {
DynamicStructuredTool,
type DynamicStructuredToolInput,
} from "@langchain/community/tools/dynamic";
} from "@langchain/core/tools";
import { formatDocumentsAsString } from "../util/document.js";

export function createRetrieverTool(
Expand Down
2 changes: 1 addition & 1 deletion libs/langchain-community/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"author": "LangChain",
"license": "MIT",
"dependencies": {
"@langchain/core": "~0.1.9",
"@langchain/core": "~0.1.16",
"@langchain/openai": "~0.0.10",
"flat": "^5.0.2",
"langsmith": "~0.0.48",
Expand Down
139 changes: 7 additions & 132 deletions libs/langchain-community/src/tools/dynamic.ts
Original file line number Diff line number Diff line change
@@ -1,132 +1,7 @@
import { z } from "zod";
import {
CallbackManagerForToolRun,
Callbacks,
parseCallbackConfigArg,
} from "@langchain/core/callbacks/manager";
import { StructuredTool, Tool, type ToolParams } from "@langchain/core/tools";
import { RunnableConfig } from "@langchain/core/runnables";

export interface BaseDynamicToolInput extends ToolParams {
name: string;
description: string;
returnDirect?: boolean;
}

/**
* Interface for the input parameters of the DynamicTool class.
*/
export interface DynamicToolInput extends BaseDynamicToolInput {
func: (
input: string,
runManager?: CallbackManagerForToolRun
) => Promise<string>;
}

/**
* Interface for the input parameters of the DynamicStructuredTool class.
*/
export interface DynamicStructuredToolInput<
// eslint-disable-next-line @typescript-eslint/no-explicit-any
T extends z.ZodObject<any, any, any, any> = z.ZodObject<any, any, any, any>
> extends BaseDynamicToolInput {
func: (
input: z.infer<T>,
runManager?: CallbackManagerForToolRun
) => Promise<string>;
schema: T;
}

/**
* A tool that can be created dynamically from a function, name, and description.
*/
export class DynamicTool extends Tool {
static lc_name() {
return "DynamicTool";
}

name: string;

description: string;

func: DynamicToolInput["func"];

constructor(fields: DynamicToolInput) {
super(fields);
this.name = fields.name;
this.description = fields.description;
this.func = fields.func;
this.returnDirect = fields.returnDirect ?? this.returnDirect;
}

async call(
arg: string | undefined | z.input<this["schema"]>,
configArg?: RunnableConfig | Callbacks
): Promise<string> {
const config = parseCallbackConfigArg(configArg);
if (config.runName === undefined) {
config.runName = this.name;
}
return super.call(arg, config);
}

/** @ignore */
async _call(
input: string,
runManager?: CallbackManagerForToolRun
): Promise<string> {
return this.func(input, runManager);
}
}

/**
* A tool that can be created dynamically from a function, name, and
* description, designed to work with structured data. It extends the
* StructuredTool class and overrides the _call method to execute the
* provided function when the tool is called.
*/
export class DynamicStructuredTool<
// eslint-disable-next-line @typescript-eslint/no-explicit-any
T extends z.ZodObject<any, any, any, any> = z.ZodObject<any, any, any, any>
> extends StructuredTool {
static lc_name() {
return "DynamicStructuredTool";
}

name: string;

description: string;

func: DynamicStructuredToolInput["func"];

schema: T;

constructor(fields: DynamicStructuredToolInput<T>) {
super(fields);
this.name = fields.name;
this.description = fields.description;
this.func = fields.func;
this.returnDirect = fields.returnDirect ?? this.returnDirect;
this.schema = fields.schema;
}

async call(
arg: z.output<T>,
configArg?: RunnableConfig | Callbacks,
/** @deprecated */
tags?: string[]
): Promise<string> {
const config = parseCallbackConfigArg(configArg);
if (config.runName === undefined) {
config.runName = this.name;
}
return super.call(arg, config, tags);
}

protected _call(
arg: z.output<T>,
runManager?: CallbackManagerForToolRun
): Promise<string> {
return this.func(arg, runManager);
}
}
export {
type BaseDynamicToolInput,
type DynamicToolInput,
type DynamicStructuredToolInput,
DynamicTool,
DynamicStructuredTool,
} from "@langchain/core/tools";
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8219,7 +8219,7 @@ __metadata:
"@gradientai/nodejs-sdk": ^1.2.0
"@huggingface/inference": ^2.6.4
"@jest/globals": ^29.5.0
"@langchain/core": ~0.1.9
"@langchain/core": ~0.1.16
"@langchain/openai": ~0.0.10
"@mozilla/readability": ^0.4.4
"@opensearch-project/opensearch": ^2.2.0
Expand Down Expand Up @@ -8558,7 +8558,7 @@ __metadata:
languageName: unknown
linkType: soft

"@langchain/core@workspace:*, @langchain/core@workspace:langchain-core, @langchain/core@~0.1, @langchain/core@~0.1.13, @langchain/core@~0.1.5, @langchain/core@~0.1.9":
"@langchain/core@workspace:*, @langchain/core@workspace:langchain-core, @langchain/core@~0.1, @langchain/core@~0.1.13, @langchain/core@~0.1.16, @langchain/core@~0.1.5":
version: 0.0.0-use.local
resolution: "@langchain/core@workspace:langchain-core"
dependencies:
Expand Down Expand Up @@ -23715,7 +23715,7 @@ __metadata:
"@google-cloud/storage": ^6.10.1
"@jest/globals": ^29.5.0
"@langchain/community": ~0.0.17
"@langchain/core": ~0.1.13
"@langchain/core": ~0.1.16
"@langchain/openai": ~0.0.12
"@notionhq/client": ^2.2.10
"@pinecone-database/pinecone": ^1.1.0
Expand Down

0 comments on commit c311576

Please sign in to comment.