Socket
Socket
Sign inDemoInstall

@langchain/core

Package Overview
Dependencies
Maintainers
8
Versions
152
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@langchain/core - npm Package Compare versions

Comparing version 0.2.6 to 0.2.7

4

dist/prompts/template.js

@@ -62,3 +62,5 @@ import mustache from "mustache";

}
else if (temp[0] === "#") {
else if (["#", "&"].includes(temp[0])) {
// # represents a section, "&" represents an unescaped variable.
// These should both be considered variables.
return { type: "variable", name: temp[1] };

@@ -65,0 +67,0 @@ }

import { test, expect } from "@jest/globals";
import { PromptTemplate } from "../prompt.js";
import { parseTemplate } from "../template.js";
test("Single input variable.", async () => {

@@ -86,1 +87,20 @@ const template = "This is a {{foo}} test.";

});
test("Escaped variables", async () => {
const template = `test: {{{text}}}`;
const parsed = parseTemplate(template, "mustache");
expect(parsed[0]).toStrictEqual({
type: "literal",
text: "test: ",
});
expect(parsed[1]).toStrictEqual({
type: "variable",
name: "text",
});
const promptTemplate = PromptTemplate.fromTemplate(template, {
templateFormat: "mustache",
});
const result = await promptTemplate.invoke({
text: `hello i have a "quote`,
});
expect(result.value).toBe(`test: hello i have a "quote`);
});

@@ -10,3 +10,3 @@ /* eslint-disable no-promise-executor-return */

import { AIMessage, AIMessageChunk, HumanMessage, SystemMessage, } from "../../messages/index.js";
import { DynamicStructuredTool, DynamicTool } from "../../tools.js";
import { DynamicStructuredTool, DynamicTool, tool } from "../../tools.js";
import { Document } from "../../documents/document.js";

@@ -1752,2 +1752,66 @@ import { PromptTemplate } from "../../prompts/prompt.js";

});
test("Runnable streamEvents method with tools that return objects", async () => {
const adderFunc = (_params) => {
return JSON.stringify({ sum: 3 });
};
const parameterlessTool = tool(adderFunc, {
name: "parameterless",
});
const events = [];
const eventStream = parameterlessTool.streamEvents({}, { version: "v2" });
for await (const event of eventStream) {
events.push(event);
}
expect(events).toEqual([
{
data: { input: {} },
event: "on_tool_start",
metadata: {},
name: "parameterless",
run_id: expect.any(String),
tags: [],
},
{
data: {
output: JSON.stringify({ sum: 3 }),
},
event: "on_tool_end",
metadata: {},
name: "parameterless",
run_id: expect.any(String),
tags: [],
},
]);
const adderTool = tool(adderFunc, {
name: "with_parameters",
description: "A tool that does nothing",
schema: z.object({
x: z.number(),
y: z.number(),
}),
});
const events2 = [];
const eventStream2 = adderTool.streamEvents({ x: 1, y: 2 }, { version: "v2" });
for await (const event of eventStream2) {
events2.push(event);
}
expect(events2).toEqual([
{
data: { input: { x: 1, y: 2 } },
event: "on_tool_start",
metadata: {},
name: "with_parameters",
run_id: expect.any(String),
tags: [],
},
{
data: { output: JSON.stringify({ sum: 3 }) },
event: "on_tool_end",
metadata: {},
name: "with_parameters",
run_id: expect.any(String),
tags: [],
},
]);
});
test("Runnable streamEvents method with a retriever", async () => {

@@ -1754,0 +1818,0 @@ const retriever = new FakeRetriever({

@@ -5,3 +5,4 @@ import { z } from "zod";

import { type RunnableConfig } from "./runnables/config.js";
import type { RunnableInterface } from "./runnables/base.js";
import type { RunnableFunc, RunnableInterface } from "./runnables/base.js";
type ZodAny = z.ZodObject<any, any, any, any>;
/**

@@ -21,3 +22,3 @@ * Parameters for the Tool classes.

}
export interface StructuredToolInterface<T extends z.ZodObject<any, any, any, any> = z.ZodObject<any, any, any, any>> extends RunnableInterface<(z.output<T> extends string ? string : never) | z.input<T>, string> {
export interface StructuredToolInterface<T extends ZodAny = ZodAny> extends RunnableInterface<(z.output<T> extends string ? string : never) | z.input<T>, string> {
lc_namespace: string[];

@@ -46,3 +47,3 @@ schema: T | z.ZodEffects<T>;

*/
export declare abstract class StructuredTool<T extends z.ZodObject<any, any, any, any> = z.ZodObject<any, any, any, any>> extends BaseLangChain<(z.output<T> extends string ? string : never) | z.input<T>, string> {
export declare abstract class StructuredTool<T extends ZodAny = ZodAny> extends BaseLangChain<(z.output<T> extends string ? string : never) | z.input<T>, string> {
abstract schema: T | z.ZodEffects<T>;

@@ -128,3 +129,3 @@ get lc_namespace(): string[];

*/
export interface DynamicStructuredToolInput<T extends z.ZodObject<any, any, any, any> = z.ZodObject<any, any, any, any>> extends BaseDynamicToolInput {
export interface DynamicStructuredToolInput<T extends ZodAny = ZodAny> extends BaseDynamicToolInput {
func: (input: z.infer<T>, runManager?: CallbackManagerForToolRun, config?: RunnableConfig) => Promise<string>;

@@ -155,7 +156,7 @@ schema: T;

*/
export declare class DynamicStructuredTool<T extends z.ZodObject<any, any, any, any> = z.ZodObject<any, any, any, any>> extends StructuredTool {
export declare class DynamicStructuredTool<T extends ZodAny = ZodAny> extends StructuredTool<T> {
static lc_name(): string;
name: string;
description: string;
func: DynamicStructuredToolInput["func"];
func: DynamicStructuredToolInput<T>["func"];
schema: T;

@@ -180,1 +181,38 @@ constructor(fields: DynamicStructuredToolInput<T>);

}
/**
* Parameters for the tool function.
* @template {ZodAny} RunInput The input schema for the tool.
*/
interface ToolWrapperParams<RunInput extends ZodAny = ZodAny> extends ToolParams {
/**
* The name of the tool. If using with an LLM, this
* will be passed as the tool name.
*/
name: string;
/**
* The description of the tool.
* @default `${fields.name} tool`
*/
description?: string;
/**
* The input schema for the tool. If using an LLM, this
* will be passed as the tool schema to generate arguments
* for.
*/
schema?: RunInput;
}
/**
* Creates a new StructuredTool instance with the provided function, name, description, and schema.
* @function
* @template {ZodAny} RunInput The input schema for the tool.
*
* @param {RunnableFunc<RunInput, string>} func - The function to invoke when the tool is called.
* @param fields - An object containing the following properties:
* @param {string} fields.name The name of the tool.
* @param {string | undefined} fields.description The description of the tool. Defaults to either the description on the Zod schema, or `${fields.name} tool`.
* @param {z.ZodObject<any, any, any, any>} fields.schema The Zod schema defining the input for the tool.
*
* @returns {StructuredTool<RunInput, string>} A new StructuredTool instance.
*/
export declare function tool<RunInput extends ZodAny = ZodAny>(func: RunnableFunc<z.infer<RunInput>, string>, fields: ToolWrapperParams<RunInput>): DynamicStructuredTool<RunInput>;
export {};

@@ -227,1 +227,25 @@ import { z } from "zod";

}
/**
* Creates a new StructuredTool instance with the provided function, name, description, and schema.
* @function
* @template {ZodAny} RunInput The input schema for the tool.
*
* @param {RunnableFunc<RunInput, string>} func - The function to invoke when the tool is called.
* @param fields - An object containing the following properties:
* @param {string} fields.name The name of the tool.
* @param {string | undefined} fields.description The description of the tool. Defaults to either the description on the Zod schema, or `${fields.name} tool`.
* @param {z.ZodObject<any, any, any, any>} fields.schema The Zod schema defining the input for the tool.
*
* @returns {StructuredTool<RunInput, string>} A new StructuredTool instance.
*/
export function tool(func, fields) {
const schema = fields.schema ??
z.object({ input: z.string().optional() }).transform((obj) => obj.input);
const description = fields.description ?? schema.description ?? `${fields.name} tool`;
return new DynamicStructuredTool({
name: fields.name,
description,
schema: schema,
func: async (input, _runManager, config) => func(input, config),
});
}
{
"name": "@langchain/core",
"version": "0.2.6",
"version": "0.2.7",
"description": "Core LangChain.js abstractions and schemas",

@@ -5,0 +5,0 @@ "type": "module",

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc