From 0ce8580d52ed4217488745058827d715d9d610d5 Mon Sep 17 00:00:00 2001 From: Nana Kwame Date: Sun, 2 Feb 2025 01:32:48 +0000 Subject: [PATCH] fix: made apiKey optional for `TavilyAnswerTool` and `TavilySearchResultsTool` --- docs/_sidebar.md | 2 + docs/modules/agents/tools/tavily_answer.md | 106 ++++++++++++++++ .../agents/tools/tavily_search_results.md | 119 ++++++++++++++++++ .../05-integrations/tools/tavily_answer.md | 106 ++++++++++++++++ .../05-integrations/tools/tavily_search.md | 13 -- .../tools/tavily_search_results.md | 119 ++++++++++++++++++ docs_v2/sidebars.js | 3 +- packages/langchain/README.md | 12 +- packages/langchain_community/README.md | 1 + .../lib/src/tools/tavily/tavily_answer.dart | 4 +- .../tools/tavily/tavily_search_results.dart | 4 +- .../test/tools/tavily_test.dart | 4 +- .../src/generated/schema/schema.freezed.dart | 32 ++--- .../lib/src/generated/schema/schema.g.dart | 87 +++++-------- .../src/generated/schema/search_request.dart | 2 +- packages/tavily_dart/oas/tavily_openapi.yaml | 1 - packages/tavily_dart/test/tavily_test.dart | 2 +- 17 files changed, 516 insertions(+), 101 deletions(-) create mode 100644 docs/modules/agents/tools/tavily_answer.md create mode 100644 docs/modules/agents/tools/tavily_search_results.md create mode 100644 docs_v2/docs/05-integrations/tools/tavily_answer.md delete mode 100644 docs_v2/docs/05-integrations/tools/tavily_search.md create mode 100644 docs_v2/docs/05-integrations/tools/tavily_search_results.md diff --git a/docs/_sidebar.md b/docs/_sidebar.md index 27aaf9be..9060dbc7 100644 --- a/docs/_sidebar.md +++ b/docs/_sidebar.md @@ -128,4 +128,6 @@ - [Tools](/modules/agents/tools/tools.md) - [Calculator](/modules/agents/tools/calculator.md) - [DALL-E Image Generator](/modules/agents/tools/openai_dall_e.md) + - [Tavily Answer](/modules/agents/tools/tavily_answer.md) + - [Tavily Search Results](/modules/agents/tools/tavily_search_results.md) - [Toolkits](/modules/agents/toolkits/toolkits.md) diff --git a/docs/modules/agents/tools/tavily_answer.md b/docs/modules/agents/tools/tavily_answer.md new file mode 100644 index 00000000..ca05c78d --- /dev/null +++ b/docs/modules/agents/tools/tavily_answer.md @@ -0,0 +1,106 @@ +# Tavily Answer + +## Overview +The `TavilyAnswerTool` is part of the [Tavily Search API](https://tavily.com) integration, specifically designed to provide direct answers to queries. This tool is optimized for scenarios where you need concise, accurate responses rather than raw search results. + +## Installation + +Add these dependencies to your project: +```yaml +dependencies: + langchain: { version } + langchain_community: { version } +``` + +Install via terminal: + +#### Dart +```bash +dart pub add langchain langchain_community +``` + +#### Flutter +```bash +flutter pub add langchain langchain_community +``` + +## Configuration + +### Authentication Options + +#### 1. API Key (Recommended) +```dart +TavilyAnswerTool(apiKey: 'your_tavily_key') +``` + +#### 2. Base URL Override (For proxies/custom endpoints) +```dart +TavilyAnswerTool(baseUrl: 'https://your-proxy.com/') +``` + +## Basic Usage + +```dart +import 'package:langchain_community/langchain_community.dart'; + +final tool = TavilyAnswerTool( + apiKey: Platform.environment['TAVILY_API_KEY'], +); + +void main() async { + final answer = await tool.invoke('Explain quantum entanglement simply'); + print(answer); + // -> 'Quantum entanglement is when particles become interlinked...' +} +``` + +## Advanced Usage + +### Custom Configuration with Domain Filtering +```dart +final expertTool = TavilyAnswerTool( + baseUrl: 'https://science-proxy.com/', + defaultOptions: const TavilyAnswerToolOptions( + searchDepth: TavilySearchDepth.advanced, + includeDomains: ['nasa.gov', 'nature.com'], + ), +); + +void main() async { + final expertAnswer = await expertTool.invoke('Latest Mars rover findings'); + print(expertAnswer); +} +``` + +## Agent Integration Example + +```dart +void main() async { + final aiAgent = ToolsAgent.fromLLMAndTools( + llm: ChatOpenAI(apiKey: openAiKey), + tools: [ + TavilyAnswerTool(apiKey: 'tavily_key'), + CalculatorTool(), + ], + ); + + final executor = AgentExecutor(agent: aiAgent); + + final res = await executor.run( + 'Calculate the GDP growth rate of France in 2023 using official sources.', + ); + + print(res); +} +``` + +## Error Handling + +```dart +try { + return await tool.invoke(query); +} on TavilyClientException catch (e) { + print('Error ${e.message}: ${e.code}'); + return 'Failed to retrieve data'; +} +``` \ No newline at end of file diff --git a/docs/modules/agents/tools/tavily_search_results.md b/docs/modules/agents/tools/tavily_search_results.md new file mode 100644 index 00000000..86bce949 --- /dev/null +++ b/docs/modules/agents/tools/tavily_search_results.md @@ -0,0 +1,119 @@ +# Tavily Search Results + +## Overview +The `TavilySearchResultsTool` is a component of the [Tavily Search API](https://tavily.com) integration that returns structured search results. This tool is ideal when you need detailed search data including sources, URLs, and confidence scores. + +## Installation + +Add these dependencies to your project: +```yaml +dependencies: + langchain: { version } + langchain_community: { version } +``` + +Install via terminal: + +#### Dart +```bash +dart pub add langchain langchain_community +``` + +#### Flutter +```bash +flutter pub add langchain langchain_community +``` + +## Configuration + +### Authentication Options + +#### 1. API Key (Recommended) +```dart +TavilySearchResultsTool(apiKey: 'your_tavily_key') +``` + +#### 2. Base URL Override (For proxies/custom endpoints) +```dart +TavilySearchResultsTool(baseUrl: 'https://your-proxy.com/') +``` + +## Basic Usage + +```dart +final searchTool = TavilySearchResultsTool(apiKey: 'your_key'); + +void main() async { + final response = await searchTool.invoke('Latest renewable energy innovations'); + final results = response.results; + + print(results.first.title); // -> '5 smart renewable energy innovations...' + print(results.first.url); // -> 'https://www.weforum.org/stories/...' + print(results.first.score); // -> 0.98855 +} +``` + +## Advanced Features + +### Result Processing +```dart +void processResults(TavilySearchResults response) { + // Filter high-confidence results + final highConfidence = results.where((r) => r.score > 0.9).toList(); + + // Extract URLs for verification + final urls = results.map((r) => r.url).toList(); + + // Get content from specific domains + final scientificSources = results.where( + (r) => r.url.contains('nature.com') || r.url.contains('science.org'), + ).toList(); +} +``` + +### Custom Configuration +```dart +final customSearchTool = TavilySearchResultsTool( + apiKey: 'your_key', + defaultOptions: const TavilySearchResultsToolOptions( + searchDepth: TavilySearchDepth.advanced, + maxResults: 10, + includeRawContent: true, + includeDomains: ['trusted-source.com'], + excludeDomains: ['untrusted-source.com'], + ), +); +``` + +## Agent Integration Example + +```dart +void main() async { + final aiAgent = ToolsAgent.fromLLMAndTools( + llm: ChatOpenAI(apiKey: openAiKey), + tools: [ + TavilySearchResultsTool(apiKey: 'tavily_key'), + CalculatorTool(), + ], + ); + + final executor = AgentExecutor(agent: aiAgent); + + final res = await executor.run( + 'Find recent research papers on quantum computing.', + ); + + print(res); +} +``` + +## Error Handling + +```dart +try { +return await tool.invoke(query); +} on TavilyClientException catch (e) { +print('Error ${e.message}: ${e.code}'); +return 'Failed to retrieve data'; +} +``` \ No newline at end of file diff --git a/docs_v2/docs/05-integrations/tools/tavily_answer.md b/docs_v2/docs/05-integrations/tools/tavily_answer.md new file mode 100644 index 00000000..ca05c78d --- /dev/null +++ b/docs_v2/docs/05-integrations/tools/tavily_answer.md @@ -0,0 +1,106 @@ +# Tavily Answer + +## Overview +The `TavilyAnswerTool` is part of the [Tavily Search API](https://tavily.com) integration, specifically designed to provide direct answers to queries. This tool is optimized for scenarios where you need concise, accurate responses rather than raw search results. + +## Installation + +Add these dependencies to your project: +```yaml +dependencies: + langchain: { version } + langchain_community: { version } +``` + +Install via terminal: + +#### Dart +```bash +dart pub add langchain langchain_community +``` + +#### Flutter +```bash +flutter pub add langchain langchain_community +``` + +## Configuration + +### Authentication Options + +#### 1. API Key (Recommended) +```dart +TavilyAnswerTool(apiKey: 'your_tavily_key') +``` + +#### 2. Base URL Override (For proxies/custom endpoints) +```dart +TavilyAnswerTool(baseUrl: 'https://your-proxy.com/') +``` + +## Basic Usage + +```dart +import 'package:langchain_community/langchain_community.dart'; + +final tool = TavilyAnswerTool( + apiKey: Platform.environment['TAVILY_API_KEY'], +); + +void main() async { + final answer = await tool.invoke('Explain quantum entanglement simply'); + print(answer); + // -> 'Quantum entanglement is when particles become interlinked...' +} +``` + +## Advanced Usage + +### Custom Configuration with Domain Filtering +```dart +final expertTool = TavilyAnswerTool( + baseUrl: 'https://science-proxy.com/', + defaultOptions: const TavilyAnswerToolOptions( + searchDepth: TavilySearchDepth.advanced, + includeDomains: ['nasa.gov', 'nature.com'], + ), +); + +void main() async { + final expertAnswer = await expertTool.invoke('Latest Mars rover findings'); + print(expertAnswer); +} +``` + +## Agent Integration Example + +```dart +void main() async { + final aiAgent = ToolsAgent.fromLLMAndTools( + llm: ChatOpenAI(apiKey: openAiKey), + tools: [ + TavilyAnswerTool(apiKey: 'tavily_key'), + CalculatorTool(), + ], + ); + + final executor = AgentExecutor(agent: aiAgent); + + final res = await executor.run( + 'Calculate the GDP growth rate of France in 2023 using official sources.', + ); + + print(res); +} +``` + +## Error Handling + +```dart +try { + return await tool.invoke(query); +} on TavilyClientException catch (e) { + print('Error ${e.message}: ${e.code}'); + return 'Failed to retrieve data'; +} +``` \ No newline at end of file diff --git a/docs_v2/docs/05-integrations/tools/tavily_search.md b/docs_v2/docs/05-integrations/tools/tavily_search.md deleted file mode 100644 index 2f3d461d..00000000 --- a/docs_v2/docs/05-integrations/tools/tavily_search.md +++ /dev/null @@ -1,13 +0,0 @@ -# Tavily Search - -[Tavily's Search API](https://tavily.com) is a search engine built specifically for AI agents (LLMs), delivering real-time, accurate, and factual results at speed. - -## Setup - -The integration lives in the `langchain-community` package. We also need to install the `tavily-python` package itself. - -```bash -dart pub add langchain langchain_community -``` - -We also need to set our Tavily API key. \ No newline at end of file diff --git a/docs_v2/docs/05-integrations/tools/tavily_search_results.md b/docs_v2/docs/05-integrations/tools/tavily_search_results.md new file mode 100644 index 00000000..86bce949 --- /dev/null +++ b/docs_v2/docs/05-integrations/tools/tavily_search_results.md @@ -0,0 +1,119 @@ +# Tavily Search Results + +## Overview +The `TavilySearchResultsTool` is a component of the [Tavily Search API](https://tavily.com) integration that returns structured search results. This tool is ideal when you need detailed search data including sources, URLs, and confidence scores. + +## Installation + +Add these dependencies to your project: +```yaml +dependencies: + langchain: { version } + langchain_community: { version } +``` + +Install via terminal: + +#### Dart +```bash +dart pub add langchain langchain_community +``` + +#### Flutter +```bash +flutter pub add langchain langchain_community +``` + +## Configuration + +### Authentication Options + +#### 1. API Key (Recommended) +```dart +TavilySearchResultsTool(apiKey: 'your_tavily_key') +``` + +#### 2. Base URL Override (For proxies/custom endpoints) +```dart +TavilySearchResultsTool(baseUrl: 'https://your-proxy.com/') +``` + +## Basic Usage + +```dart +final searchTool = TavilySearchResultsTool(apiKey: 'your_key'); + +void main() async { + final response = await searchTool.invoke('Latest renewable energy innovations'); + final results = response.results; + + print(results.first.title); // -> '5 smart renewable energy innovations...' + print(results.first.url); // -> 'https://www.weforum.org/stories/...' + print(results.first.score); // -> 0.98855 +} +``` + +## Advanced Features + +### Result Processing +```dart +void processResults(TavilySearchResults response) { + // Filter high-confidence results + final highConfidence = results.where((r) => r.score > 0.9).toList(); + + // Extract URLs for verification + final urls = results.map((r) => r.url).toList(); + + // Get content from specific domains + final scientificSources = results.where( + (r) => r.url.contains('nature.com') || r.url.contains('science.org'), + ).toList(); +} +``` + +### Custom Configuration +```dart +final customSearchTool = TavilySearchResultsTool( + apiKey: 'your_key', + defaultOptions: const TavilySearchResultsToolOptions( + searchDepth: TavilySearchDepth.advanced, + maxResults: 10, + includeRawContent: true, + includeDomains: ['trusted-source.com'], + excludeDomains: ['untrusted-source.com'], + ), +); +``` + +## Agent Integration Example + +```dart +void main() async { + final aiAgent = ToolsAgent.fromLLMAndTools( + llm: ChatOpenAI(apiKey: openAiKey), + tools: [ + TavilySearchResultsTool(apiKey: 'tavily_key'), + CalculatorTool(), + ], + ); + + final executor = AgentExecutor(agent: aiAgent); + + final res = await executor.run( + 'Find recent research papers on quantum computing.', + ); + + print(res); +} +``` + +## Error Handling + +```dart +try { +return await tool.invoke(query); +} on TavilyClientException catch (e) { +print('Error ${e.message}: ${e.code}'); +return 'Failed to retrieve data'; +} +``` \ No newline at end of file diff --git a/docs_v2/sidebars.js b/docs_v2/sidebars.js index 19c41a76..63013667 100644 --- a/docs_v2/sidebars.js +++ b/docs_v2/sidebars.js @@ -149,7 +149,8 @@ const sidebars = { id: 'integrations/tools/index', }, items: [ - 'integrations/tools/tavily_search', + 'integrations/tools/tavily_answer', + 'integrations/tools/tavily_search_results', ], }, ], diff --git a/packages/langchain/README.md b/packages/langchain/README.md index bb169643..c70cac93 100644 --- a/packages/langchain/README.md +++ b/packages/langchain/README.md @@ -148,12 +148,12 @@ _Note: Prefer using Chat Models over LLMs as many providers have deprecated them ### Tools -| Tool | Package | Description | -|-----------------------------------------------------------------------------------|---------------------------------------------------------------------|--------------------------------------------------------------------------------------------| -| [CalculatorTool](https://langchaindart.dev/#/modules/agents/tools/calculator) | [langchain_community](https://pub.dev/packages/langchain_community) | To calculate math expressions | -| [OpenAIDallETool](https://langchaindart.dev/#/modules/agents/tools/openai_dall_e) | [langchain_openai](https://pub.dev/packages/langchain_openai) | [OpenAI's DALL-E Image Generator](https://platform.openai.com/docs/api-reference/images) | -| TavilyAnswerTool | [langchain_community](https://pub.dev/packages/langchain_community) | Returns an answer for a query using the [Tavily](https://tavily.com) search engine | -| TavilySearchResultsTool | [langchain_community](https://pub.dev/packages/langchain_community) | Returns a list of results for a query using the [Tavily](https://tavily.com) search engine | +| Tool | Package | Description | +|---------------------------------------------------------------------------------------------------|---------------------------------------------------------------------|--------------------------------------------------------------------------------------------| +| [CalculatorTool](https://langchaindart.dev/#/modules/agents/tools/calculator) | [langchain_community](https://pub.dev/packages/langchain_community) | To calculate math expressions | +| [OpenAIDallETool](https://langchaindart.dev/#/modules/agents/tools/openai_dall_e) | [langchain_openai](https://pub.dev/packages/langchain_openai) | [OpenAI's DALL-E Image Generator](https://platform.openai.com/docs/api-reference/images) | +| [TavilyAnswerTool](https://langchaindart.dev/#/modules/agents/tools/tavily_answer) | [langchain_community](https://pub.dev/packages/langchain_community) | Returns an answer for a query using the [Tavily](https://tavily.com) search engine | +| [TavilySearchResultsTool](https://langchaindart.dev/#/modules/agents/tools/tavily_search_results) | [langchain_community](https://pub.dev/packages/langchain_community) | Returns a list of results for a query using the [Tavily](https://tavily.com) search engine | ## Getting started diff --git a/packages/langchain_community/README.md b/packages/langchain_community/README.md index 1dcb80e3..0a28c5e6 100644 --- a/packages/langchain_community/README.md +++ b/packages/langchain_community/README.md @@ -25,6 +25,7 @@ The most popular third-party integrations have their own packages (e.g. [langcha * `CsvLoader`: for CSV or TSV files. * `JsonLoader` for JSON files. * `WebBaseLoader`: for web pages. + * `DirectoryLoader`: for directories of files. - Tools: * `CalculatorTool`: to calculate math expressions. * `TavilySearchResultsTool`: returns a list of results for a query using the [Tavily](https://tavily.com) search engine. diff --git a/packages/langchain_community/lib/src/tools/tavily/tavily_answer.dart b/packages/langchain_community/lib/src/tools/tavily/tavily_answer.dart index a5ad637f..366adf68 100644 --- a/packages/langchain_community/lib/src/tools/tavily/tavily_answer.dart +++ b/packages/langchain_community/lib/src/tools/tavily/tavily_answer.dart @@ -45,7 +45,7 @@ final class TavilyAnswerTool extends StringTool { /// - `client`: the HTTP client to use. You can set your own HTTP client if /// you need further customization (e.g. to use a Socks5 proxy). TavilyAnswerTool({ - required this.apiKey, + this.apiKey, final String? baseUrl, final Map headers = const {}, final Map queryParams = const {}, @@ -71,7 +71,7 @@ final class TavilyAnswerTool extends StringTool { final TavilyClient _client; /// Your Tavily API key. - String apiKey; + String? apiKey; @override Future invokeInternal( diff --git a/packages/langchain_community/lib/src/tools/tavily/tavily_search_results.dart b/packages/langchain_community/lib/src/tools/tavily/tavily_search_results.dart index 7e5693c7..2b131c5c 100644 --- a/packages/langchain_community/lib/src/tools/tavily/tavily_search_results.dart +++ b/packages/langchain_community/lib/src/tools/tavily/tavily_search_results.dart @@ -54,7 +54,7 @@ final class TavilySearchResultsTool /// - `client`: the HTTP client to use. You can set your own HTTP client if /// you need further customization (e.g. to use a Socks5 proxy). TavilySearchResultsTool({ - required this.apiKey, + this.apiKey, final String? baseUrl, final Map headers = const {}, final Map queryParams = const {}, @@ -89,7 +89,7 @@ final class TavilySearchResultsTool final TavilyClient _client; /// Your Tavily API key. - String apiKey; + String? apiKey; @override Future invokeInternal( diff --git a/packages/langchain_community/test/tools/tavily_test.dart b/packages/langchain_community/test/tools/tavily_test.dart index 85214c6c..0bd2f206 100644 --- a/packages/langchain_community/test/tools/tavily_test.dart +++ b/packages/langchain_community/test/tools/tavily_test.dart @@ -8,7 +8,7 @@ void main() { group('TavilySearchResultsTool tests', () { test('Calculate expressions', () async { final tool = TavilySearchResultsTool( - apiKey: Platform.environment['TAVILY_API_KEY']!, + apiKey: Platform.environment['TAVILY_API_KEY'], ); final res = await tool.invoke('What is the weather like in New York?'); expect(res.results, isNotEmpty); @@ -21,7 +21,7 @@ void main() { group('TavilyAnswerTool tests', () { test('Invoke TavilyAnswerTool', () async { final tool = TavilyAnswerTool( - apiKey: Platform.environment['TAVILY_API_KEY']!, + apiKey: Platform.environment['TAVILY_API_KEY'], ); final res = await tool.invoke('What is the weather like in New York?'); expect(res, isNotEmpty); diff --git a/packages/tavily_dart/lib/src/generated/schema/schema.freezed.dart b/packages/tavily_dart/lib/src/generated/schema/schema.freezed.dart index 70075a8b..bad7f634 100644 --- a/packages/tavily_dart/lib/src/generated/schema/schema.freezed.dart +++ b/packages/tavily_dart/lib/src/generated/schema/schema.freezed.dart @@ -21,8 +21,8 @@ SearchRequest _$SearchRequestFromJson(Map json) { /// @nodoc mixin _$SearchRequest { /// Your unique API key. - @JsonKey(name: 'api_key') - String get apiKey => throw _privateConstructorUsedError; + @JsonKey(name: 'api_key', includeIfNull: false) + String? get apiKey => throw _privateConstructorUsedError; /// The search query string. String get query => throw _privateConstructorUsedError; @@ -73,7 +73,7 @@ abstract class $SearchRequestCopyWith<$Res> { _$SearchRequestCopyWithImpl<$Res, SearchRequest>; @useResult $Res call( - {@JsonKey(name: 'api_key') String apiKey, + {@JsonKey(name: 'api_key', includeIfNull: false) String? apiKey, String query, @JsonKey(name: 'search_depth') SearchRequestSearchDepth searchDepth, @JsonKey(name: 'include_images') bool includeImages, @@ -101,7 +101,7 @@ class _$SearchRequestCopyWithImpl<$Res, $Val extends SearchRequest> @pragma('vm:prefer-inline') @override $Res call({ - Object? apiKey = null, + Object? apiKey = freezed, Object? query = null, Object? searchDepth = null, Object? includeImages = null, @@ -112,10 +112,10 @@ class _$SearchRequestCopyWithImpl<$Res, $Val extends SearchRequest> Object? excludeDomains = freezed, }) { return _then(_value.copyWith( - apiKey: null == apiKey + apiKey: freezed == apiKey ? _value.apiKey : apiKey // ignore: cast_nullable_to_non_nullable - as String, + as String?, query: null == query ? _value.query : query // ignore: cast_nullable_to_non_nullable @@ -161,7 +161,7 @@ abstract class _$$SearchRequestImplCopyWith<$Res> @override @useResult $Res call( - {@JsonKey(name: 'api_key') String apiKey, + {@JsonKey(name: 'api_key', includeIfNull: false) String? apiKey, String query, @JsonKey(name: 'search_depth') SearchRequestSearchDepth searchDepth, @JsonKey(name: 'include_images') bool includeImages, @@ -187,7 +187,7 @@ class __$$SearchRequestImplCopyWithImpl<$Res> @pragma('vm:prefer-inline') @override $Res call({ - Object? apiKey = null, + Object? apiKey = freezed, Object? query = null, Object? searchDepth = null, Object? includeImages = null, @@ -198,10 +198,10 @@ class __$$SearchRequestImplCopyWithImpl<$Res> Object? excludeDomains = freezed, }) { return _then(_$SearchRequestImpl( - apiKey: null == apiKey + apiKey: freezed == apiKey ? _value.apiKey : apiKey // ignore: cast_nullable_to_non_nullable - as String, + as String?, query: null == query ? _value.query : query // ignore: cast_nullable_to_non_nullable @@ -242,7 +242,7 @@ class __$$SearchRequestImplCopyWithImpl<$Res> @JsonSerializable() class _$SearchRequestImpl extends _SearchRequest { const _$SearchRequestImpl( - {@JsonKey(name: 'api_key') required this.apiKey, + {@JsonKey(name: 'api_key', includeIfNull: false) this.apiKey, required this.query, @JsonKey(name: 'search_depth') this.searchDepth = SearchRequestSearchDepth.basic, @@ -263,8 +263,8 @@ class _$SearchRequestImpl extends _SearchRequest { /// Your unique API key. @override - @JsonKey(name: 'api_key') - final String apiKey; + @JsonKey(name: 'api_key', includeIfNull: false) + final String? apiKey; /// The search query string. @override @@ -383,7 +383,7 @@ class _$SearchRequestImpl extends _SearchRequest { abstract class _SearchRequest extends SearchRequest { const factory _SearchRequest( - {@JsonKey(name: 'api_key') required final String apiKey, + {@JsonKey(name: 'api_key', includeIfNull: false) final String? apiKey, required final String query, @JsonKey(name: 'search_depth') final SearchRequestSearchDepth searchDepth, @JsonKey(name: 'include_images') final bool includeImages, @@ -401,8 +401,8 @@ abstract class _SearchRequest extends SearchRequest { /// Your unique API key. @override - @JsonKey(name: 'api_key') - String get apiKey; + @JsonKey(name: 'api_key', includeIfNull: false) + String? get apiKey; /// The search query string. @override diff --git a/packages/tavily_dart/lib/src/generated/schema/schema.g.dart b/packages/tavily_dart/lib/src/generated/schema/schema.g.dart index f9214d02..8f25c264 100644 --- a/packages/tavily_dart/lib/src/generated/schema/schema.g.dart +++ b/packages/tavily_dart/lib/src/generated/schema/schema.g.dart @@ -10,7 +10,7 @@ part of 'schema.dart'; _$SearchRequestImpl _$$SearchRequestImplFromJson(Map json) => _$SearchRequestImpl( - apiKey: json['api_key'] as String, + apiKey: json['api_key'] as String?, query: json['query'] as String, searchDepth: $enumDecodeNullable( _$SearchRequestSearchDepthEnumMap, json['search_depth']) ?? @@ -27,27 +27,18 @@ _$SearchRequestImpl _$$SearchRequestImplFromJson(Map json) => .toList(), ); -Map _$$SearchRequestImplToJson(_$SearchRequestImpl instance) { - final val = { - 'api_key': instance.apiKey, - 'query': instance.query, - 'search_depth': _$SearchRequestSearchDepthEnumMap[instance.searchDepth]!, - 'include_images': instance.includeImages, - 'include_answer': instance.includeAnswer, - 'include_raw_content': instance.includeRawContent, - 'max_results': instance.maxResults, - }; - - void writeNotNull(String key, dynamic value) { - if (value != null) { - val[key] = value; - } - } - - writeNotNull('include_domains', instance.includeDomains); - writeNotNull('exclude_domains', instance.excludeDomains); - return val; -} +Map _$$SearchRequestImplToJson(_$SearchRequestImpl instance) => + { + if (instance.apiKey case final value?) 'api_key': value, + 'query': instance.query, + 'search_depth': _$SearchRequestSearchDepthEnumMap[instance.searchDepth]!, + 'include_images': instance.includeImages, + 'include_answer': instance.includeAnswer, + 'include_raw_content': instance.includeRawContent, + 'max_results': instance.maxResults, + if (instance.includeDomains case final value?) 'include_domains': value, + if (instance.excludeDomains case final value?) 'exclude_domains': value, + }; const _$SearchRequestSearchDepthEnumMap = { SearchRequestSearchDepth.basic: 'basic', @@ -70,23 +61,16 @@ _$SearchResponseImpl _$$SearchResponseImplFromJson(Map json) => ); Map _$$SearchResponseImplToJson( - _$SearchResponseImpl instance) { - final val = {}; - - void writeNotNull(String key, dynamic value) { - if (value != null) { - val[key] = value; - } - } - - writeNotNull('answer', instance.answer); - val['query'] = instance.query; - val['response_time'] = instance.responseTime; - writeNotNull('images', instance.images); - writeNotNull('follow_up_questions', instance.followUpQuestions); - val['results'] = instance.results.map((e) => e.toJson()).toList(); - return val; -} + _$SearchResponseImpl instance) => + { + if (instance.answer case final value?) 'answer': value, + 'query': instance.query, + 'response_time': instance.responseTime, + if (instance.images case final value?) 'images': value, + if (instance.followUpQuestions case final value?) + 'follow_up_questions': value, + 'results': instance.results.map((e) => e.toJson()).toList(), + }; _$SearchResultImpl _$$SearchResultImplFromJson(Map json) => _$SearchResultImpl( @@ -97,20 +81,11 @@ _$SearchResultImpl _$$SearchResultImplFromJson(Map json) => score: (json['score'] as num).toDouble(), ); -Map _$$SearchResultImplToJson(_$SearchResultImpl instance) { - final val = { - 'title': instance.title, - 'url': instance.url, - 'content': instance.content, - }; - - void writeNotNull(String key, dynamic value) { - if (value != null) { - val[key] = value; - } - } - - writeNotNull('raw_content', instance.rawContent); - val['score'] = instance.score; - return val; -} +Map _$$SearchResultImplToJson(_$SearchResultImpl instance) => + { + 'title': instance.title, + 'url': instance.url, + 'content': instance.content, + if (instance.rawContent case final value?) 'raw_content': value, + 'score': instance.score, + }; diff --git a/packages/tavily_dart/lib/src/generated/schema/search_request.dart b/packages/tavily_dart/lib/src/generated/schema/search_request.dart index c0d16e7a..163d54f0 100644 --- a/packages/tavily_dart/lib/src/generated/schema/search_request.dart +++ b/packages/tavily_dart/lib/src/generated/schema/search_request.dart @@ -16,7 +16,7 @@ class SearchRequest with _$SearchRequest { /// Factory constructor for SearchRequest const factory SearchRequest({ /// Your unique API key. - @JsonKey(name: 'api_key') required String apiKey, + @JsonKey(name: 'api_key', includeIfNull: false) String? apiKey, /// The search query string. required String query, diff --git a/packages/tavily_dart/oas/tavily_openapi.yaml b/packages/tavily_dart/oas/tavily_openapi.yaml index 250fa447..b74fcc1f 100644 --- a/packages/tavily_dart/oas/tavily_openapi.yaml +++ b/packages/tavily_dart/oas/tavily_openapi.yaml @@ -96,7 +96,6 @@ components: type: string description: A list of domains to specifically exclude from the search results. Default is None. required: - - api_key - query SearchResponse: type: object diff --git a/packages/tavily_dart/test/tavily_test.dart b/packages/tavily_dart/test/tavily_test.dart index 0df02cb8..c3d7074f 100644 --- a/packages/tavily_dart/test/tavily_test.dart +++ b/packages/tavily_dart/test/tavily_test.dart @@ -21,7 +21,7 @@ void main() { test('Test call search API', () async { final res = await client.search( request: SearchRequest( - apiKey: Platform.environment['TAVILY_API_KEY']!, + apiKey: Platform.environment['TAVILY_API_KEY'], query: 'Should I invest in Apple right now?', includeAnswer: true, includeImages: true,