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

refactor!: Improve Tool abstractions #398

Merged
merged 1 commit into from
May 2, 2024
Merged

refactor!: Improve Tool abstractions #398

merged 1 commit into from
May 2, 2024

Conversation

davidmigloz
Copy link
Owner

@davidmigloz davidmigloz commented May 2, 2024

Preparation work for #397

Changes

  • ToolSpec: new class that contains the specification of a LangChain tool without the actual implementation.
  • BaseTool has been renamed to Tool: the base class for a tool (implements ToolSpec). It also now has 3 generic parameters: the input type, the options type and the output type.
  • Classes that extend from Tool must now implement a getInputFromJson method that converts a JSON Map into the input type of the tool (used to convert the response from the language model to the actual input type).
  • Tool has been renamed to StringTool, tools that accept a single string input and returns a string output.

Example of a String tool:

final echoTool = StringTool.fromFunction(
  name: 'echo',
  description: 'echo',
  func: (String input) => input,
);

Example of a tool with a custom input object:

class SearchInput {
  const SearchInput({
    required this.query,
    required this.n,
  });

  final String query;
  final int n;

  SearchInput.fromJson(final Map<String, dynamic> json)
      : this(
          query: json['query'] as String,
          n: json['n'] as int,
        );
}

String callYourSearchFunction(final SearchInput input) {
  return 'Results:\n${List<String>.generate(input.n, (final i) => 'Result ${i + 1}').join('\n')}';
}

final tool = Tool.fromFunction<SearchInput, String>(
  name: 'search',
  description: 'Tool for searching the web.',
  inputJsonSchema: const {
    'type': 'object',
    'properties': {
      'query': {
        'type': 'string',
        'description': 'The query to search for',
      },
      'n': {
        'type': 'number',
        'description': 'The number of results to return',
      },
    },
    'required': ['query'],
  },
  func: callYourSearchFunction,
  getInputFromJson: SearchInput.fromJson,
);

@davidmigloz davidmigloz self-assigned this May 2, 2024
@davidmigloz davidmigloz added c:tools Tools. p:langchain_core langchain_core package. t:enhancement New feature or request labels May 2, 2024
@davidmigloz davidmigloz added this to the v0.7.0 milestone May 2, 2024
@davidmigloz davidmigloz merged commit 2a50aec into main May 2, 2024
1 check passed
@davidmigloz davidmigloz deleted the tools branch May 2, 2024 21:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c:tools Tools. p:langchain_core langchain_core package. t:enhancement New feature or request
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

1 participant