-
Notifications
You must be signed in to change notification settings - Fork 48
/
c04-chat-options.rs
44 lines (34 loc) · 1.67 KB
/
c04-chat-options.rs
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
//! This example shows how to use a custom AdapterKindResolver to have some custom
//! mapping from a model name to an AdapterKind.
//! This allows mapping missing models to their Adapter implementations.
use genai::chat::printer::print_chat_stream;
use genai::chat::{ChatMessage, ChatOptions, ChatRequest};
use genai::{Client, ClientConfig};
// const MODEL: &str = "gpt-4o-mini";
// const MODEL: &str = "command-light";
// const MODEL: &str = "claude-3-haiku-20240307";
// const MODEL: &str = "gemini-1.5-flash-latest";
// const MODEL: &str = "llama3-8b-8192";
const MODEL: &str = "gemma:2b";
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let question = "Why is the sky red?";
// -- Global ChatOptions
// Note: The properties of ChatOptions set at the client config level will be
// the fallback values if not provided at the chat execution level.
let client_config =
ClientConfig::default().with_chat_options(ChatOptions::default().with_temperature(0.0).with_top_p(0.99));
// -- Build the new client with this client_config
let client = Client::builder().with_config(client_config).build();
// -- Build the chat request
let chat_req = ChatRequest::new(vec![ChatMessage::user(question)]);
// -- Build the chat request options (used per execution chat)
let options = ChatOptions::default().with_max_tokens(1000);
// -- Execute and print
println!("\n--- Question:\n{question}");
let chat_res = client.exec_chat_stream(MODEL, chat_req.clone(), Some(&options)).await?;
let adapter_kind = client.resolve_service_target(MODEL)?.model.adapter_kind;
println!("\n--- Answer: ({MODEL} - {adapter_kind})");
print_chat_stream(chat_res, None).await?;
Ok(())
}