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

fix: Made the apiKey parameter optional in the TavilyAnswerTool and TavilySearchResultsTool classes #646

Merged
merged 1 commit into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
108 changes: 108 additions & 0 deletions docs/modules/agents/tools/tavily_answer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# 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 a phenomenon where two or more particles...'
}
```

## 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);
// -> The latest findings from NASA's Perseverance Mars rover...
}
```

## 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);
// -> "The GDP growth rate of France in 2023 was 0.70%, as reported by EUROSTAT.
}
```

## Error Handling

```dart
try {
return await tool.invoke(query);
} on TavilyClientException catch (e) {
print('Error ${e.message}: ${e.code}');
return 'Failed to retrieve data';
}
```
124 changes: 124 additions & 0 deletions docs/modules/agents/tools/tavily_search_results.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
# 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);
// Here are some recent research papers and resources on quantum computing:
//
// 1. **Evidence for the utility of quantum computing before fault tolerance**
// - *Published in Nature (2023)*
...
}
```

## Error Handling

```dart
try {
return await tool.invoke(query);
} on TavilyClientException catch (e) {
print('Error ${e.message}: ${e.code}');
return 'Failed to retrieve data';
}
```
108 changes: 108 additions & 0 deletions docs_v2/docs/05-integrations/tools/tavily_answer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# 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 a phenomenon where two or more particles...'
}
```

## 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);
// -> The latest findings from NASA's Perseverance Mars rover...
}
```

## 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);
// -> "The GDP growth rate of France in 2023 was 0.70%, as reported by EUROSTAT.
}
```

## Error Handling

```dart
try {
return await tool.invoke(query);
} on TavilyClientException catch (e) {
print('Error ${e.message}: ${e.code}');
return 'Failed to retrieve data';
}
```
13 changes: 0 additions & 13 deletions docs_v2/docs/05-integrations/tools/tavily_search.md

This file was deleted.

Loading
Loading