Skip to content

Commit 3db6c87

Browse files
adambenaliifsheldon
authored andcommitted
[Completions API] Add web search options (64bit#370)
* [Completons API] Add web search options * Update async-openai/src/types/chat.rs * Update async-openai/src/types/chat.rs * Update async-openai/src/types/chat.rs * Update async-openai/src/types/chat.rs * Update async-openai/src/types/chat.rs * Update async-openai/src/types/chat.rs * Update async-openai/src/types/chat.rs * Update async-openai/src/types/chat.rs * Update async-openai/src/types/chat.rs * Update examples/completions-web-search/src/main.rs * Update examples/completions-web-search/src/main.rs --------- Co-authored-by: Himanshu Neema <[email protected]> (cherry picked from commit ef6817f)
1 parent 452824d commit 3db6c87

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed

async-openai-wasm/src/types/chat.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,57 @@ pub enum ChatCompletionToolChoiceOption {
559559
Named(ChatCompletionNamedToolChoice),
560560
}
561561

562+
#[derive(Clone, Serialize, Debug, Deserialize, PartialEq, Default)]
563+
#[serde(rename_all = "lowercase")]
564+
/// The amount of context window space to use for the search.
565+
pub enum WebSearchContextSize {
566+
Low,
567+
#[default]
568+
Medium,
569+
High,
570+
}
571+
572+
573+
#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
574+
#[serde(rename_all = "lowercase")]
575+
pub enum WebSearchUserLocationType {
576+
577+
Approximate,
578+
}
579+
580+
/// Approximate location parameters for the search.
581+
#[derive(Clone, Serialize, Debug, Default, Deserialize, PartialEq)]
582+
pub struct WebSearchLocation {
583+
/// The two-letter [ISO country code](https://en.wikipedia.org/wiki/ISO_3166-1) of the user, e.g. `US`.
584+
pub country: Option<String>,
585+
/// Free text input for the region of the user, e.g. `California`.
586+
pub region: Option<String>,
587+
/// Free text input for the city of the user, e.g. `San Francisco`.
588+
pub city: Option<String>,
589+
/// The [IANA timezone](https://timeapi.io/documentation/iana-timezones) of the user, e.g. `America/Los_Angeles`.
590+
pub timezone: Option<String>,
591+
}
592+
593+
594+
#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
595+
pub struct WebSearchUserLocation {
596+
// The type of location approximation. Always `approximate`.
597+
pub r#type: WebSearchUserLocationType,
598+
599+
pub approximate: WebSearchLocation,
600+
}
601+
602+
/// Options for the web search tool.
603+
#[derive(Clone, Serialize, Debug, Default, Deserialize, PartialEq)]
604+
pub struct WebSearchOptions {
605+
/// High level guidance for the amount of context window space to use for the search. One of `low`, `medium`, or `high`. `medium` is the default.
606+
607+
pub search_context_size: Option<WebSearchContextSize>,
608+
609+
/// Approximate location parameters for the search.
610+
pub user_location: Option<WebSearchUserLocation>,
611+
}
612+
562613
#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
563614
#[serde(rename_all = "lowercase")]
564615
pub enum ServiceTier {
@@ -803,6 +854,11 @@ pub struct CreateChatCompletionRequest {
803854
#[serde(skip_serializing_if = "Option::is_none")]
804855
pub user: Option<String>,
805856

857+
/// This tool searches the web for relevant results to use in a response.
858+
/// Learn more about the [web search tool](https://platform.openai.com/docs/guides/tools-web-search?api-mode=chat).
859+
860+
pub web_search_options: Option<WebSearchOptions>,
861+
806862
/// Deprecated in favor of `tool_choice`.
807863
///
808864
/// Controls which (if any) function is called by the model.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "completions-web-search"
3+
version = "0.1.0"
4+
edition = "2021"
5+
publish = false
6+
7+
8+
[dependencies]
9+
async-openai = {path = "../../async-openai"}
10+
tokio = { version = "1.43.0", features = ["full"] }
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
use async_openai::types::{
2+
ChatCompletionRequestUserMessageArgs, WebSearchContextSize, WebSearchOptions,
3+
WebSearchUserLocation, WebSearchLocation,
4+
WebSearchUserLocationType,
5+
};
6+
use async_openai::{types::CreateChatCompletionRequestArgs, Client};
7+
8+
#[tokio::main]
9+
async fn main() -> Result<(), Box<dyn std::error::Error>> {
10+
let client = Client::new();
11+
let user_prompt = "What is the weather like today? Be concise.";
12+
13+
let request = CreateChatCompletionRequestArgs::default()
14+
.max_tokens(256u32)
15+
.model("gpt-4o-mini-search-preview")
16+
.messages([ChatCompletionRequestUserMessageArgs::default()
17+
.content(user_prompt)
18+
.build()?
19+
.into()])
20+
.web_search_options(WebSearchOptions {
21+
search_context_size: Some(WebSearchContextSize::Low),
22+
user_location: Some(WebSearchUserLocation {
23+
r#type: WebSearchUserLocationType::Approximate,
24+
approximate: WebSearchLocation {
25+
city: Some("Paris".to_string()),
26+
..Default::default()
27+
},
28+
}),
29+
})
30+
.build()?;
31+
32+
let response_message = client
33+
.chat()
34+
.create(request)
35+
.await?
36+
.choices
37+
.first()
38+
.unwrap()
39+
.message
40+
.clone();
41+
42+
if let Some(content) = response_message.content {
43+
println!("Response: {}", content);
44+
}
45+
46+
Ok(())
47+
}

0 commit comments

Comments
 (0)