upgrade langchain version 0.1.0

This commit is contained in:
Henry
2024-01-26 23:41:55 +00:00
parent 601a4d6b66
commit 0606d2c6dd
180 changed files with 905 additions and 746 deletions
@@ -1,5 +1,5 @@
import { AIPluginTool } from '@langchain/community/tools/aiplugin'
import { INode, INodeData, INodeParams } from '../../../src/Interface'
import { AIPluginTool } from 'langchain/tools'
import { getBaseClasses } from '../../../src/utils'
class AIPlugin implements INode {
@@ -1,6 +1,6 @@
import { BraveSearch } from '@langchain/community/tools/brave_search'
import { ICommonObject, INode, INodeData, INodeParams } from '../../../src/Interface'
import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils'
import { BraveSearch } from 'langchain/tools'
class BraveSearchAPI_Tools implements INode {
label: string
@@ -1,6 +1,6 @@
import { BaseChain } from 'langchain/chains'
import { INode, INodeData, INodeParams } from '../../../src/Interface'
import { getBaseClasses } from '../../../src/utils'
import { BaseChain } from 'langchain/chains'
import { ChainTool } from './core'
class ChainTool_Tools implements INode {
@@ -1,4 +1,4 @@
import { DynamicTool, DynamicToolInput } from 'langchain/tools'
import { DynamicTool, DynamicToolInput } from '@langchain/core/tools'
import { BaseChain } from 'langchain/chains'
import { handleEscapeCharacters } from '../../../src/utils'
@@ -1,9 +1,9 @@
import { z } from 'zod'
import { NodeVM } from 'vm2'
import { availableDependencies, defaultAllowBuiltInDep, prepareSandboxVars } from '../../../src/utils'
import { RunnableConfig } from '@langchain/core/runnables'
import { StructuredTool, ToolParams } from '@langchain/core/tools'
import { CallbackManagerForToolRun, Callbacks, CallbackManager, parseCallbackConfigArg } from '@langchain/core/callbacks/manager'
import { availableDependencies, defaultAllowBuiltInDep, prepareSandboxVars } from '../../../src/utils'
class ToolInputParsingException extends Error {
output?: string
@@ -1,6 +1,6 @@
import { GoogleCustomSearch } from '@langchain/community/tools/google_custom_search'
import { ICommonObject, INode, INodeData, INodeParams } from '../../../src/Interface'
import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils'
import { GoogleCustomSearch } from 'langchain/tools'
class GoogleCustomSearchAPI_Tools implements INode {
label: string
@@ -1,8 +1,8 @@
import { ICommonObject, INode, INodeData, INodeParams } from '../../../src/Interface'
import { OpenApiToolkit } from 'langchain/agents'
import { JsonSpec, JsonObject } from 'langchain/tools'
import { BaseLanguageModel } from 'langchain/base_language'
import { load } from 'js-yaml'
import { BaseLanguageModel } from '@langchain/core/language_models/base'
import { OpenApiToolkit } from 'langchain/agents'
import { JsonSpec, JsonObject } from './core'
import { ICommonObject, INode, INodeData, INodeParams } from '../../../src/Interface'
import { getCredentialData, getCredentialParam } from '../../../src'
class OpenAPIToolkit_Tools implements INode {
@@ -0,0 +1,140 @@
import jsonpointer from 'jsonpointer'
import { Serializable } from '@langchain/core/load/serializable'
import { Tool, ToolParams } from '@langchain/core/tools'
export type Json = string | number | boolean | null | { [key: string]: Json } | Json[]
export type JsonObject = { [key: string]: Json }
/**
* Represents a JSON object in the LangChain framework. Provides methods
* to get keys and values from the JSON object.
*/
export class JsonSpec extends Serializable {
lc_namespace = ['langchain', 'tools', 'json']
obj: JsonObject
maxValueLength = 4000
constructor(obj: JsonObject, max_value_length = 4000) {
super(...arguments)
this.obj = obj
this.maxValueLength = max_value_length
}
/**
* Retrieves all keys at a given path in the JSON object.
* @param input The path to the keys in the JSON object, provided as a string in JSON pointer syntax.
* @returns A string containing all keys at the given path, separated by commas.
*/
public getKeys(input: string): string {
const pointer = jsonpointer.compile(input)
const res = pointer.get(this.obj) as Json
if (typeof res === 'object' && !Array.isArray(res) && res !== null) {
return Object.keys(res)
.map((i) => i.replaceAll('~', '~0').replaceAll('/', '~1'))
.join(', ')
}
throw new Error(`Value at ${input} is not a dictionary, get the value directly instead.`)
}
/**
* Retrieves the value at a given path in the JSON object.
* @param input The path to the value in the JSON object, provided as a string in JSON pointer syntax.
* @returns The value at the given path in the JSON object, as a string. If the value is a large dictionary or exceeds the maximum length, a message is returned instead.
*/
public getValue(input: string): string {
const pointer = jsonpointer.compile(input)
const res = pointer.get(this.obj) as Json
if (res === null || res === undefined) {
throw new Error(`Value at ${input} is null or undefined.`)
}
const str = typeof res === 'object' ? JSON.stringify(res) : res.toString()
if (typeof res === 'object' && !Array.isArray(res) && str.length > this.maxValueLength) {
return `Value is a large dictionary, should explore its keys directly.`
}
if (str.length > this.maxValueLength) {
return `${str.slice(0, this.maxValueLength)}...`
}
return str
}
}
export interface JsonToolFields extends ToolParams {
jsonSpec: JsonSpec
}
/**
* A tool in the LangChain framework that lists all keys at a given path
* in a JSON object.
*/
export class JsonListKeysTool extends Tool {
static lc_name() {
return 'JsonListKeysTool'
}
name = 'json_list_keys'
jsonSpec: JsonSpec
constructor(jsonSpec: JsonSpec)
constructor(fields: JsonToolFields)
constructor(fields: JsonSpec | JsonToolFields) {
if (!('jsonSpec' in fields)) {
// eslint-disable-next-line no-param-reassign
fields = { jsonSpec: fields }
}
super(fields)
this.jsonSpec = fields.jsonSpec
}
/** @ignore */
async _call(input: string) {
try {
return this.jsonSpec.getKeys(input)
} catch (error) {
return `${error}`
}
}
description = `Can be used to list all keys at a given path.
Before calling this you should be SURE that the path to this exists.
The input is a text representation of the path to the json as json pointer syntax (e.g. /key1/0/key2).`
}
/**
* A tool in the LangChain framework that retrieves the value at a given
* path in a JSON object.
*/
export class JsonGetValueTool extends Tool {
static lc_name() {
return 'JsonGetValueTool'
}
name = 'json_get_value'
constructor(public jsonSpec: JsonSpec) {
super()
}
/** @ignore */
async _call(input: string) {
try {
return this.jsonSpec.getValue(input)
} catch (error) {
return `${error}`
}
}
description = `Can be used to see value in string format at a given path.
Before calling this you should be SURE that the path to this exists.
The input is a text representation of the path to the json as json pointer syntax (e.g. /key1/0/key2).`
}
@@ -1,7 +1,14 @@
import { z } from 'zod'
import { StructuredTool, ToolParams } from '@langchain/core/tools'
import { Serializable } from '@langchain/core/load/serializable'
import { NodeFileStore } from 'langchain/stores/file/node'
import { INode, INodeData, INodeParams } from '../../../src/Interface'
import { getBaseClasses } from '../../../src/utils'
import { ReadFileTool } from 'langchain/tools'
import { NodeFileStore } from 'langchain/stores/file/node'
abstract class BaseFileStore extends Serializable {
abstract readFile(path: string): Promise<string>
abstract writeFile(path: string, contents: string): Promise<void>
}
class ReadFile_Tools implements INode {
label: string
@@ -41,4 +48,38 @@ class ReadFile_Tools implements INode {
}
}
interface ReadFileParams extends ToolParams {
store: BaseFileStore
}
/**
* Class for reading files from the disk. Extends the StructuredTool
* class.
*/
export class ReadFileTool extends StructuredTool {
static lc_name() {
return 'ReadFileTool'
}
schema = z.object({
file_path: z.string().describe('name of file')
})
name = 'read_file'
description = 'Read file from disk'
store: BaseFileStore
constructor({ store }: ReadFileParams) {
super(...arguments)
this.store = store
}
async _call({ file_path }: z.infer<typeof this.schema>) {
return await this.store.readFile(file_path)
}
}
module.exports = { nodeClass: ReadFile_Tools }
@@ -1,5 +1,5 @@
import fetch from 'node-fetch'
import { Tool } from 'langchain/tools'
import { Tool } from '@langchain/core/tools'
export const desc = `A portal to the internet. Use this when you need to get specific content from a website.
Input should be a url (i.e. https://www.google.com). The output will be the text response of the GET request.`
@@ -1,4 +1,4 @@
import { Tool } from 'langchain/tools'
import { Tool } from '@langchain/core/tools'
import fetch from 'node-fetch'
export const desc = `Use this when you want to POST to a website.
@@ -1,8 +1,8 @@
import { DynamicTool } from '@langchain/core/tools'
import { BaseRetriever } from '@langchain/core/retrievers'
import { createRetrieverTool } from 'langchain/tools/retriever'
import { INode, INodeData, INodeParams } from '../../../src/Interface'
import { getBaseClasses } from '../../../src/utils'
import { DynamicTool } from 'langchain/tools'
import { createRetrieverTool } from 'langchain/agents/toolkits'
import { BaseRetriever } from 'langchain/schema/retriever'
class Retriever_Tools implements INode {
label: string
@@ -1,6 +1,6 @@
import { SearchApi } from '@langchain/community/tools/searchapi'
import { ICommonObject, INode, INodeData, INodeParams } from '../../../src/Interface'
import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils'
import { SearchApi } from 'langchain/tools'
class SearchAPI_Tools implements INode {
label: string
@@ -1,6 +1,6 @@
import { SerpAPI } from '@langchain/community/tools/serpapi'
import { ICommonObject, INode, INodeData, INodeParams } from '../../../src/Interface'
import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils'
import { SerpAPI } from 'langchain/tools'
class SerpAPI_Tools implements INode {
label: string
@@ -1,6 +1,6 @@
import { Serper } from '@langchain/community/tools/serper'
import { ICommonObject, INode, INodeData, INodeParams } from '../../../src/Interface'
import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils'
import { Serper } from 'langchain/tools'
class Serper_Tools implements INode {
label: string
@@ -1,8 +1,8 @@
import { BaseLanguageModel } from 'langchain/base_language'
import { BaseLanguageModel } from '@langchain/core/language_models/base'
import { Embeddings } from '@langchain/core/embeddings'
import { WebBrowser } from 'langchain/tools/webbrowser'
import { INode, INodeData, INodeParams } from '../../../src/Interface'
import { getBaseClasses } from '../../../src/utils'
import { WebBrowser } from 'langchain/tools/webbrowser'
import { Embeddings } from 'langchain/embeddings/base'
class WebBrowser_Tools implements INode {
label: string
@@ -1,7 +1,14 @@
import { z } from 'zod'
import { StructuredTool, ToolParams } from '@langchain/core/tools'
import { Serializable } from '@langchain/core/load/serializable'
import { NodeFileStore } from 'langchain/stores/file/node'
import { INode, INodeData, INodeParams } from '../../../src/Interface'
import { getBaseClasses } from '../../../src/utils'
import { WriteFileTool } from 'langchain/tools'
import { NodeFileStore } from 'langchain/stores/file/node'
abstract class BaseFileStore extends Serializable {
abstract readFile(path: string): Promise<string>
abstract writeFile(path: string, contents: string): Promise<void>
}
class WriteFile_Tools implements INode {
label: string
@@ -41,4 +48,40 @@ class WriteFile_Tools implements INode {
}
}
interface WriteFileParams extends ToolParams {
store: BaseFileStore
}
/**
* Class for writing data to files on the disk. Extends the StructuredTool
* class.
*/
export class WriteFileTool extends StructuredTool {
static lc_name() {
return 'WriteFileTool'
}
schema = z.object({
file_path: z.string().describe('name of file'),
text: z.string().describe('text to write to file')
})
name = 'write_file'
description = 'Write file from disk'
store: BaseFileStore
constructor({ store, ...rest }: WriteFileParams) {
super(rest)
this.store = store
}
async _call({ file_path, text }: z.infer<typeof this.schema>) {
await this.store.writeFile(file_path, text)
return 'File written to successfully.'
}
}
module.exports = { nodeClass: WriteFile_Tools }