-
Notifications
You must be signed in to change notification settings - Fork 48
/
c06-target-resolver.rs
53 lines (42 loc) · 1.92 KB
/
c06-target-resolver.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
45
46
47
48
49
50
51
52
53
//! This example demonstrates how to use a custom ServiceTargetResolver which gives full control over the final
//! mapping for Endpoint, Model/AdapterKind, and Auth.
//!
//! IMPORTANT - Here we are using xAI as an example of a custom ServiceTarget.
//! However, there is now an XaiAdapter, which gets activated on `starts_with("grok")`.
use genai::adapter::AdapterKind;
use genai::chat::{ChatMessage, ChatRequest};
use genai::resolver::{AuthData, Endpoint, ServiceTargetResolver};
use genai::{Client, ModelIden, ServiceTarget};
const MODEL: &str = "grok-beta";
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let questions = &[
// Follow-up questions
"Why is the sky blue?",
"Why is it red sometimes?",
];
// -- Build an auth_resolver and the AdapterConfig
let target_resolver = ServiceTargetResolver::from_resolver_fn(
|service_target: ServiceTarget| -> Result<ServiceTarget, genai::resolver::Error> {
let ServiceTarget { model, .. } = service_target;
let endpoint = Endpoint::from_static("https://api.x.ai/v1/");
let auth = AuthData::from_env("XAI_API_KEY");
let model = ModelIden::new(AdapterKind::OpenAI, model.model_name);
// TODO: point to xai
Ok(ServiceTarget { endpoint, auth, model })
},
);
// -- Build the new client with this adapter_config
let client = Client::builder().with_service_target_resolver(target_resolver).build();
let mut chat_req = ChatRequest::default().with_system("Answer in one sentence");
for &question in questions {
chat_req = chat_req.append_message(ChatMessage::user(question));
println!("\n--- Question:\n{question}");
let chat_res = client.exec_chat(MODEL, chat_req.clone(), None).await?;
println!("\n--- Answer: ");
let assistant_answer = chat_res.content_text_as_str().ok_or("Should have response")?;
println!("{assistant_answer}");
chat_req = chat_req.append_message(ChatMessage::assistant(assistant_answer));
}
Ok(())
}