5 releases (1 stable)

new 1.0.0 Apr 24, 2024
0.2.2 Apr 24, 2024
0.2.1 Apr 24, 2024
0.2.0 Apr 24, 2024
0.1.0 Apr 24, 2024

#377 in Web programming

Download history 282/week @ 2024-04-22

282 downloads per month

MIT license

12KB
131 lines

Tavily Rust SDK

The Tavily Rust SDK is a library for interacting with the Tavily Search API. With just a few lines of code, you can perform simple or advanced search queries, customize your search options, and get relevant search results powered by LLMs 🚀.

Note: Required an api key

Functions

The Tavily Rust SDK provides three main functions:

  • search: Perform a simple search query with a single argument, query.
let response = tavily.search("your search query").await?;
  • answer: Perform an advanced search query that includes an answer to your query. This function takes a bit more time than the simple search.
let response = tavily.answer("your search query").await?;
  • call: Perform a custom search query using the SearchRequest struct. This struct provides a range of options for customizing your search, including search depth, whether to include images or raw content, and which domains to include or exclude.
let mut request = SearchRequest::new("your api key", "your search query");
request.search_depth("advanced");
request.include_answer(true);
request.include_images(true);
request.include_raw_content(true);
request.max_results(10);
request.include_domains(vec!["example.com".to_string()]);
request.exclude_domains(vec!["example.org".to_string()]);

let response = tavily.call(&request).await?;

Getting Started

To get started with the Tavily Rust SDK, add the following to your Cargo.toml file:

[dependencies]
tavily = "^1.0.0"

Then, import the library in your Rust code:

use tavily::{Tavily, SearchRequest, SearchResponse};

Here's a simple example of how to use the library to perform a search query:

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let api_key = "your_api_key";
    let query = "your_search_query";

    let tavily = Tavily::new(api_key);
    let response = tavily.search(query).await?;

    println!("{:#?}", response);

    Ok(())
}

You can also customize the search options by using the SearchRequest struct:

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let api_key = "your_api_key";
    let query = "your_search_query";

    let mut request = SearchRequest::new(api_key, query);
    request.search_depth("advanced");
    request.include_answer(true);
    request.include_images(true);
    request.include_raw_content(true);
    request.max_results(10);
    request.include_domains(vec!["example.com".to_string()]);
    request.exclude_domains(vec!["example.org".to_string()]);

    let tavily = Tavily::new(api_key);
    let response = tavily.call(&request).await?;

    println!("{:#?}", response);

    Ok(())
}

Error Codes

The Tavily Search API may return various HTTP status codes. For a complete list and their meanings, please refer to the official documentation.

Disclaimer

This is an unofficial SDK for the Tavily Search API. For the official documentation and support, please visit Tavily Search API.

License

MIT

Dependencies

~4–18MB
~232K SLoC