Skip to content

Commit

Permalink
fix: made apiKey optional for TavilyAnswerTool and `TavilySearchRes…
Browse files Browse the repository at this point in the history
…ultsTool`
  • Loading branch information
Nana-Kwame-bot committed Feb 2, 2025
1 parent f819520 commit 0ce8580
Show file tree
Hide file tree
Showing 17 changed files with 516 additions and 101 deletions.
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)
106 changes: 106 additions & 0 deletions docs/modules/agents/tools/tavily_answer.md
Original file line number Diff line number Diff line change
@@ -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';
}
```
119 changes: 119 additions & 0 deletions docs/modules/agents/tools/tavily_search_results.md
Original file line number Diff line number Diff line change
@@ -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';
}
```
106 changes: 106 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,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';
}
```
13 changes: 0 additions & 13 deletions docs_v2/docs/05-integrations/tools/tavily_search.md

This file was deleted.

Loading

0 comments on commit 0ce8580

Please sign in to comment.