forked from graniet/llm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchain_audio_text_example.rs
More file actions
59 lines (53 loc) · 2.35 KB
/
chain_audio_text_example.rs
File metadata and controls
59 lines (53 loc) · 2.35 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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//! Example demonstrating a multi-step chain combining speech-to-text and text processing
//!
//! This example shows how to:
//! 1. Initialize multiple LLM backends (OpenAI and ElevenLabs)
//! 2. Create a registry to manage the backends
//! 3. Build a chain that transcribes audio and processes the text
//! 4. Execute the chain and display results
use llm::{
builder::{LLMBackend, LLMBuilder},
chain::{LLMRegistryBuilder, MultiChainStepBuilder, MultiChainStepMode, MultiPromptChain},
};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Initialize OpenAI backend with API key and model settings
let openai_llm = LLMBuilder::new()
.backend(LLMBackend::OpenAI)
.api_key(std::env::var("OPENAI_API_KEY").unwrap_or("sk-OPENAI".into()))
.model("gpt-4o")
.build()?;
// Initialize ElevenLabs backend for speech-to-text
let elevenlabs_llm = LLMBuilder::new()
.backend(LLMBackend::ElevenLabs)
.api_key(std::env::var("ELEVENLABS_API_KEY").unwrap_or("elevenlabs-key".into()))
.model("scribe_v1")
.build()?;
// Create registry to manage multiple backends
let registry = LLMRegistryBuilder::new()
.register("openai", openai_llm)
.register("elevenlabs", elevenlabs_llm)
.build();
// Build multi-step chain using different backends
let chain_res = MultiPromptChain::new(®istry)
// Step 1: Transcribe audio file using ElevenLabs
.step(
MultiChainStepBuilder::new(MultiChainStepMode::SpeechToText)
.provider_id("elevenlabs")
.id("transcription")
.template("test-stt.m4a")
.build()?
)
// Step 2: Process transcription into JSON format using OpenAI
.step(
MultiChainStepBuilder::new(MultiChainStepMode::Chat)
.provider_id("openai")
.id("jsonify")
.template("Here is the transcription: {{transcription}}\n\nPlease convert the transcription text into a JSON object with the following fields: 'text', 'words' (array of objects with 'text', 'start', 'end'), 'language_code', 'language_probability'. The JSON should be formatted as a string.")
.build()?
)
.run().await?;
// Display results from all steps
println!("Results: {chain_res:?}");
Ok(())
}