Skip to content

Commit

Permalink
feat: support Perplexity chat completion
Browse files Browse the repository at this point in the history
  • Loading branch information
roushou committed Sep 16, 2024
1 parent a2a92da commit aaf5093
Show file tree
Hide file tree
Showing 11 changed files with 645 additions and 1 deletion.
23 changes: 23 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace]
members = ["anthropic", "mesh", "openai", "replicate", "examples/*"]
members = ["anthropic", "mesh", "openai", "perplexity", "replicate", "examples/*"]
resolver = "2"

[workspace.package]
Expand Down
11 changes: 11 additions & 0 deletions examples/perplexity-chat-completion/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "perplexity-chat-completion"
version = "0.1.0"
edition = "2021"
publish = false

[dependencies]
perplexity = { path = "../../perplexity" }
futures-util = "0.3.30"
tokio = { version = "1.39.2", features = ["full"] }
serde_json = "1.0.128"
1 change: 1 addition & 0 deletions examples/perplexity-chat-completion/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Perplexity chat completion Example
17 changes: 17 additions & 0 deletions examples/perplexity-chat-completion/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use perplexity::{
client::{Client, CreateChatCompletion, Model},
config::Config,
};

#[tokio::main]
async fn main() {
let api_key = std::env::var("PERPLEXITY_API_KEY")
.expect("environment variable PERPLEXITY_API_KEY should be defined");

let config = Config::new(api_key);
let client = Client::new(config).unwrap();

let message = CreateChatCompletion::new(Model::Llama31SonarLargeOnline);
let result = client.create_completion(message).await.unwrap();
println!("{:?}", result);
}
23 changes: 23 additions & 0 deletions perplexity/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[package]
name = "perplexity"
version = "0.1.0"
edition.workspace = true
authors.workspace = true
description = "Perplexity Rust SDK"
homepage.workspace = true
documentation.workspace = true
repository = "https://github.com/roushou/mesh"
readme = "README.md"
license.workspace = true
keywords = ["ai", "chat", "perplexity"]

[dependencies]
futures-util = "0.3.30"
reqwest = { version = "0.12.5", features = ["json", "stream"] }
serde = { version = "1.0.206", features = ["derive"] }
serde_json = "1.0.124"
thiserror = "1.0.63"
tokio = { version = "1.39.2", features = ["full"] }

[dev-dependencies]
pretty_assertions = "1.4.0"
55 changes: 55 additions & 0 deletions perplexity/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Perplexity Rust SDK

[![Crates.io][crates-badge]][crates-url]
[![MIT licensed][mit-badge]][mit-url]
[![APACHE-2.0 licensed][apache-badge]][apache-url]
[![Build Status][actions-badge]][actions-url]

[crates-badge]: https://img.shields.io/crates/v/perplexity.svg
[crates-url]: https://crates.io/crates/perplexity
[mit-badge]: https://img.shields.io/badge/license-MIT-blue.svg
[mit-url]: https://github.com/roushou/mesh/blob/master/LICENSE-MIT
[apache-badge]: https://img.shields.io/badge/license-apache-blue.svg
[apache-url]: https://github.com/roushou/mesh/blob/master/LICENSE-APACHE
[actions-badge]: https://github.com/roushou/mesh/workflows/CI/badge.svg
[actions-url]: https://github.com/roushou/mesh/actions?query=workflow%3ACI+branch%3Amaster

This is an unofficial Rust SDK for the Perplexity API.

More information about this crate can be found in the [crate documentation](https://crates.io/crates/perplexity).

## Installation

Add `perplexity` as a dependency to your `Cargo.toml`

```sh
$ cargo add perplexity
```

## Usage

An example to create a completion.

```rust,ignore
use perplexity::{
client::{Client, CreateChatCompletion, Model},
config::Config,
};
#[tokio::main]
async fn main() {
let api_key = std::env::var("PERPLEXITY_API_KEY")
.expect("environment variable PERPLEXITY_API_KEY should be defined");
let config = Config::new(api_key);
let client = Client::new(config).unwrap();
let message = CreateChatCompletion::new(Model::Llama31SonarLargeOnline);
let result = client.create_completion(message.clone()).await.unwrap();
println!("{:?}", result);
}
```

## License

This project is licensed under the [MIT license](../LICENSE-MIT) and [Apache-2.0](../LICENSE-APACHE) license.
Loading

0 comments on commit aaf5093

Please sign in to comment.