forked from graniet/llm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenai_stt_example.rs
More file actions
27 lines (23 loc) · 1 KB
/
openai_stt_example.rs
File metadata and controls
27 lines (23 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Import required modules from the LLM library for OpenAI integration
use llm::{
builder::{LLMBackend, LLMBuilder}, // Builder pattern components
};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Get OpenAI API key from environment variable or use test key as fallback
let api_key = std::env::var("OPENAI_API_KEY").unwrap_or("sk-TESTKEY".into());
// Initialize and configure the LLM client
let llm = LLMBuilder::new()
.backend(LLMBackend::OpenAI) // Use OpenAI as the LLM provider
.api_key(api_key) // Set the API key
.model("whisper-1") // Use gpt-4o-transcribe model
.max_tokens(512) // Limit response length
.temperature(0.7) // Control response randomness (0.0-1.0)
.build()
.expect("Failed to build LLM (OpenAI)");
match llm.transcribe_file("audio2.m4a").await {
Ok(text) => println!("Audio transcription:\n{text}"),
Err(e) => eprintln!("Audio transcription error: {e}"),
}
Ok(())
}