Skip to content

Commit ef6817f

Browse files
adambenali64bit
andauthored
[Completions API] Add web search options (#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 <himanshun.iitkgp@gmail.com>
1 parent 0e7a629 commit ef6817f

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed

async-openai/src/types/chat.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,57 @@ pub enum ChatCompletionToolChoiceOption {
554554
Named(ChatCompletionNamedToolChoice),
555555
}
556556

557+
#[derive(Clone, Serialize, Debug, Deserialize, PartialEq, Default)]
558+
#[serde(rename_all = "lowercase")]
559+
/// The amount of context window space to use for the search.
560+
pub enum WebSearchContextSize {
561+
Low,
562+
#[default]
563+
Medium,
564+
High,
565+
}
566+
567+
568+
#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
569+
#[serde(rename_all = "lowercase")]
570+
pub enum WebSearchUserLocationType {
571+
572+
Approximate,
573+
}
574+
575+
/// Approximate location parameters for the search.
576+
#[derive(Clone, Serialize, Debug, Default, Deserialize, PartialEq)]
577+
pub struct WebSearchLocation {
578+
/// The two-letter [ISO country code](https://en.wikipedia.org/wiki/ISO_3166-1) of the user, e.g. `US`.
579+
pub country: Option<String>,
580+
/// Free text input for the region of the user, e.g. `California`.
581+
pub region: Option<String>,
582+
/// Free text input for the city of the user, e.g. `San Francisco`.
583+
pub city: Option<String>,
584+
/// The [IANA timezone](https://timeapi.io/documentation/iana-timezones) of the user, e.g. `America/Los_Angeles`.
585+
pub timezone: Option<String>,
586+
}
587+
588+
589+
#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
590+
pub struct WebSearchUserLocation {
591+
// The type of location approximation. Always `approximate`.
592+
pub r#type: WebSearchUserLocationType,
593+
594+
pub approximate: WebSearchLocation,
595+
}
596+
597+
/// Options for the web search tool.
598+
#[derive(Clone, Serialize, Debug, Default, Deserialize, PartialEq)]
599+
pub struct WebSearchOptions {
600+
/// 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.
601+
602+
pub search_context_size: Option<WebSearchContextSize>,
603+
604+
/// Approximate location parameters for the search.
605+
pub user_location: Option<WebSearchUserLocation>,
606+
}
607+
557608
#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
558609
#[serde(rename_all = "lowercase")]
559610
pub enum ServiceTier {
@@ -798,6 +849,11 @@ pub struct CreateChatCompletionRequest {
798849
#[serde(skip_serializing_if = "Option::is_none")]
799850
pub user: Option<String>,
800851

852+
/// This tool searches the web for relevant results to use in a response.
853+
/// Learn more about the [web search tool](https://platform.openai.com/docs/guides/tools-web-search?api-mode=chat).
854+
855+
pub web_search_options: Option<WebSearchOptions>,
856+
801857
/// Deprecated in favor of `tool_choice`.
802858
///
803859
/// 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)