Skip to content

Commit

Permalink
Renamed new to with_key and provided context on request method
Browse files Browse the repository at this point in the history
  • Loading branch information
jpalvarezl committed Sep 12, 2024
1 parent 7ca609b commit 65c41d2
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 13 deletions.
2 changes: 1 addition & 1 deletion sdk/openai/inference/examples/azure_chat_completions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub async fn main() -> Result<()> {
std::env::var("AZURE_OPENAI_ENDPOINT").expect("Set AZURE_OPENAI_ENDPOINT env variable");
let secret = std::env::var("AZURE_OPENAI_KEY").expect("Set AZURE_OPENAI_KEY env variable");

let azure_openai_client = AzureOpenAIClient::new(
let azure_openai_client = AzureOpenAIClient::with_key(
endpoint,
secret,
Some(
Expand Down
19 changes: 8 additions & 11 deletions sdk/openai/inference/src/clients/azure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,20 @@ use crate::response::CreateChatCompletionsResponse;
use azure_core::{self, Method, Policy, Result};
use azure_core::{Context, Url};

pub struct AzureOpenAIClient<'a> {
pub struct AzureOpenAIClient {
endpoint: Url,
context: Context<'a>,
pipeline: azure_core::Pipeline,
options: AzureOpenAIClientOptions,
}

impl AzureOpenAIClient<'_> {
// TODO: not sure if this should be named `with_key_credential` instead
pub fn new(
impl AzureOpenAIClient {
pub fn with_key(
endpoint: impl AsRef<str>,
secret: String,
secret: impl Into<String>,
client_options: Option<AzureOpenAIClientOptions>,
) -> Result<Self> {
let endpoint = Url::parse(endpoint.as_ref())?;
let key_credential = AzureKeyCredential::new(secret);

let context = Context::new();
let key_credential = AzureKeyCredential::new(secret.into());

let options = client_options.unwrap_or_default();
let per_call_policies: Vec<Arc<dyn Policy>> = key_credential.clone().into();
Expand All @@ -34,7 +30,6 @@ impl AzureOpenAIClient<'_> {

Ok(AzureOpenAIClient {
endpoint,
context,
pipeline,
options,
})
Expand Down Expand Up @@ -76,6 +71,8 @@ impl AzureOpenAIClient<'_> {
&self.options.api_service_version.to_string(),
))?;

let context = Context::new();

let mut request = azure_core::Request::new(url, Method::Post);
// adding the mandatory header shouldn't be necessary if the pipeline was setup correctly (?)
// request.add_mandatory_header(&self.key_credential);
Expand All @@ -84,7 +81,7 @@ impl AzureOpenAIClient<'_> {

let response = self
.pipeline
.send::<CreateChatCompletionsResponse>(&self.context, &mut request)
.send::<CreateChatCompletionsResponse>(&context, &mut request)
.await?;
response.into_body().json().await
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions sdk/openai/inference/src/clients/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// use serde::Serialize;

pub mod azure;
pub mod chat_completions_client;
pub mod non_azure;

// pub(crate) fn build_request<T>(
Expand Down
2 changes: 1 addition & 1 deletion sdk/openai/inference/src/options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use azure_core::ClientOptions;

pub use service_version::AzureServiceVersion;

// TODO: I was not able to find ClientOptions as a derive macros
// TODO: I was not able to find ClientOptions as a derive macros
#[derive(Clone, Debug, Default)]
pub struct AzureOpenAIClientOptions {
pub(crate) client_options: ClientOptions,
Expand Down

0 comments on commit 65c41d2

Please sign in to comment.