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

Allow arbitrary query params. #69

Closed
wants to merge 13 commits into from
Closed
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
14 changes: 14 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@ env:
RUSTDOCFLAGS: -D warnings

jobs:
Features:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install libasound2-dev
run: |
sudo apt-get update
sudo apt-get install libasound2-dev
- name: Check no features
run: cargo check --all-targets --no-default-features
- name: Check prerecorded feature
run: cargo check --all-targets --no-default-features --features=prerecorded
- name: Check live feature
run: cargo check --all-targets --no-default-features --features=live
Build:
runs-on: ubuntu-latest
steps:
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
- Deprecate tiers and add explicit support for all currently available models.
- Expand language enum to include all currently-supported languages.
- Add (default on) feature flags for live and prerecorded transcription.
- Support arbitrary query params in transcription options.

## [0.4.0] - 2023-11-01

Expand Down
35 changes: 32 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "deepgram"
version = "0.4.0"
version = "0.5.0"
authors = ["Deepgram <[email protected]>"]
edition = "2021"
description = "Official Rust SDK for Deepgram's automated speech recognition APIs."
Expand All @@ -22,9 +22,9 @@ serde = { version = "1", features = ["derive"] }
serde_json = "1"
thiserror = "1"
tokio = { version = "1.13.0", features = ["full"] }
tokio-tungstenite = { version = "0.20.1", features = ["rustls-tls-webpki-roots"] }
tokio-tungstenite = { version = "0.20.1", features = ["rustls-tls-webpki-roots"], optional = true }
tokio-util = { version = "0.7.1", features = ["codec", "io"] }
tungstenite = "0.20.1"
tungstenite = { version = "0.20.1", optional = true }
url = "2"
uuid = { version = "1", features = ["serde"] }
# Dependencies below are specified only to satisfy minimal-versions.
Expand All @@ -34,3 +34,32 @@ proc-macro2 = "1.0.60"
pkg-config = "0.3.27"
cpal = "0.13"
crossbeam = "0.8"

[features]
default = ["prerecorded", "live"]
live = ["dep:tungstenite", "dep:tokio-tungstenite"]
prerecorded = []

[[example]]
name = "prerecorded_from_file"
required-features = ["prerecorded"]

[[example]]
name = "callback"
required-features = ["prerecorded"]

[[example]]
name = "make_prerecorded_request_builder"
required-features = ["prerecorded"]

[[example]]
name = "microphone_stream"
required-features = ["live"]

[[example]]
name = "prerecorded_from_url"
required-features = ["prerecorded"]

[[example]]
name = "simple_stream"
required-features = ["live"]
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ mod response;
/// Make transcriptions requests using [`Deepgram::transcription`].
#[derive(Debug, Clone)]
pub struct Deepgram {
#[cfg_attr(not(feature = "live"), allow(unused))]
api_key: String,
client: reqwest::Client,
}
Expand Down Expand Up @@ -65,6 +66,7 @@ pub enum DeepgramError {
#[error("Something went wrong during I/O: {0}")]
IoError(#[from] io::Error),

#[cfg(feature = "live")]
/// Something went wrong with WS.
#[error("Something went wrong with WS: {0}")]
WsError(#[from] tungstenite::Error),
Expand Down
4 changes: 3 additions & 1 deletion src/transcription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

use crate::Deepgram;

#[cfg(feature = "live")]
pub mod live;
#[cfg(feature = "prerecorded")]
pub mod prerecorded;

/// Transcribe audio using Deepgram's automated speech recognition.
Expand All @@ -17,7 +19,7 @@ pub mod prerecorded;
///
/// [api]: https://developers.deepgram.com/api-reference/#transcription
#[derive(Debug, Clone)]
pub struct Transcription<'a>(&'a Deepgram);
pub struct Transcription<'a>(#[allow(unused)] &'a Deepgram);

impl Deepgram {
/// Construct a new [`Transcription`] from a [`Deepgram`].
Expand Down
35 changes: 35 additions & 0 deletions src/transcription/prerecorded/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub struct Options {
utterances: Option<Utterances>,
tags: Vec<String>,
detect_language: Option<bool>,
query_params: Vec<(String, String)>,
}

/// Used as a parameter for [`OptionsBuilder::model`] and [`OptionsBuilder::multichannel_with_models`].
Expand Down Expand Up @@ -464,6 +465,7 @@ impl OptionsBuilder {
utterances: None,
tags: Vec::new(),
detect_language: None,
query_params: Vec::new(),
})
}

Expand Down Expand Up @@ -1229,6 +1231,34 @@ impl OptionsBuilder {

self
}

/// Append extra query parameters to the end of the transcription request.
/// Users should prefer using the other builder methods over this one. This
/// exists as an escape hatch for using features before they have been added
/// to the SDK.
///
/// Calling this twice will add both sets of parameters.
///
/// # Examples
///
/// ```
/// # use deepgram::transcription::prerecorded::options::Options;
///
/// use std::collections::HashMap;
/// let mut params = HashMap::new(); // Could also be a Vec<(String, String)>
/// params.insert("extra".to_string(), "parameter".to_string());
/// let more_params = vec![("final".to_string(), "option".to_string())];
/// let options = Options::builder()
/// .query_params(params)
/// .query_params(more_params)
/// .build();
///
/// ```
pub fn query_params(mut self, params: impl IntoIterator<Item = (String, String)>) -> Self {
self.0.query_params.extend(params);
self
}

/// Finish building the [`Options`] object.
pub fn build(self) -> Options {
self.0
Expand Down Expand Up @@ -1268,6 +1298,7 @@ impl Serialize for SerializableOptions<'_> {
utterances,
tags,
detect_language,
query_params,
} = self.0;

match multichannel {
Expand Down Expand Up @@ -1381,6 +1412,10 @@ impl Serialize for SerializableOptions<'_> {
seq.serialize_element(&("detect_language", detect_language))?;
}

for (param, value) in query_params {
seq.serialize_element(&(param, value))?;
}

seq.end()
}
}
Expand Down
Loading