Skip to content

Commit

Permalink
core embedder/llm DATA_SOURCES api keys (#6496)
Browse files Browse the repository at this point in the history
  • Loading branch information
spolu authored Jul 24, 2024
1 parent ded2575 commit a0fa5b9
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 48 deletions.
48 changes: 24 additions & 24 deletions core/src/providers/mistral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -882,24 +882,20 @@ impl LLM for MistralAILLM {
self.id.clone()
}
async fn initialize(&mut self, credentials: Credentials) -> Result<()> {
match std::env::var("CORE_DATA_SOURCES_MISTRAL_API_KEY") {
Ok(key) => {
self.api_key = Some(key);
match credentials.get("MISTRAL_API_KEY") {
Some(api_key) => {
self.api_key = Some(api_key.clone());
}
Err(_) => match credentials.get("MISTRAL_API_KEY") {
Some(api_key) => {
self.api_key = Some(api_key.clone());
None => match tokio::task::spawn_blocking(|| std::env::var("MISTRAL_API_KEY")).await? {
Ok(key) => {
self.api_key = Some(key);
}
None => match std::env::var("MISTRAL_API_KEY") {
Ok(key) => {
self.api_key = Some(key);
}
Err(_) => Err(anyhow!(
"Credentials or environment variable `MISTRAL_API_KEY` is not set."
))?,
},
Err(_) => Err(anyhow!(
"Credentials or environment variable `MISTRAL_API_KEY` is not set."
))?,
},
}

Ok(())
}

Expand Down Expand Up @@ -1109,20 +1105,24 @@ impl Embedder for MistralEmbedder {
));
}

match credentials.get("MISTRAL_API_KEY") {
Some(api_key) => {
self.api_key = Some(api_key.clone());
match std::env::var("CORE_DATA_SOURCES_MISTRAL_API_KEY") {
Ok(key) => {
self.api_key = Some(key);
}
None => match tokio::task::spawn_blocking(|| std::env::var("MISTRAL_API_KEY")).await? {
Ok(key) => {
self.api_key = Some(key);
Err(_) => match credentials.get("MISTRAL_API_KEY") {
Some(api_key) => {
self.api_key = Some(api_key.clone());
}
Err(_) => Err(anyhow!(
"Credentials or environment variable `MISTRAL_API_KEY` is not set."
))?,
None => match std::env::var("MISTRAL_API_KEY") {
Ok(key) => {
self.api_key = Some(key);
}
Err(_) => Err(anyhow!(
"Credentials or environment variable `MISTRAL_API_KEY` is not set."
))?,
},
},
}

Ok(())
}

Expand Down
48 changes: 24 additions & 24 deletions core/src/providers/openai.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1721,23 +1721,17 @@ impl LLM for OpenAILLM {
}

async fn initialize(&mut self, credentials: Credentials) -> Result<()> {
// Give priority to `CORE_DATA_SOURCES_OPENAI_API_KEY` env variable
match std::env::var("CORE_DATA_SOURCES_OPENAI_API_KEY") {
Ok(key) => {
self.api_key = Some(key);
match credentials.get("OPENAI_API_KEY") {
Some(api_key) => {
self.api_key = Some(api_key.clone());
}
Err(_) => match credentials.get("OPENAI_API_KEY") {
Some(api_key) => {
self.api_key = Some(api_key.clone());
None => match tokio::task::spawn_blocking(|| std::env::var("OPENAI_API_KEY")).await? {
Ok(key) => {
self.api_key = Some(key);
}
None => match std::env::var("OPENAI_API_KEY") {
Ok(key) => {
self.api_key = Some(key);
}
Err(_) => Err(anyhow!(
"Credentials or environment variable `OPENAI_API_KEY` is not set."
))?,
},
Err(_) => Err(anyhow!(
"Credentials or environment variable `OPENAI_API_KEY` is not set."
))?,
},
}
Ok(())
Expand Down Expand Up @@ -2154,17 +2148,23 @@ impl Embedder for OpenAIEmbedder {
));
}

match credentials.get("OPENAI_API_KEY") {
Some(api_key) => {
self.api_key = Some(api_key.clone());
// Give priority to `CORE_DATA_SOURCES_OPENAI_API_KEY` env variable
match std::env::var("CORE_DATA_SOURCES_OPENAI_API_KEY") {
Ok(key) => {
self.api_key = Some(key);
}
None => match tokio::task::spawn_blocking(|| std::env::var("OPENAI_API_KEY")).await? {
Ok(key) => {
self.api_key = Some(key);
Err(_) => match credentials.get("OPENAI_API_KEY") {
Some(api_key) => {
self.api_key = Some(api_key.clone());
}
Err(_) => Err(anyhow!(
"Credentials or environment variable `OPENAI_API_KEY` is not set."
))?,
None => match std::env::var("OPENAI_API_KEY") {
Ok(key) => {
self.api_key = Some(key);
}
Err(_) => Err(anyhow!(
"Credentials or environment variable `OPENAI_API_KEY` is not set."
))?,
},
},
}
Ok(())
Expand Down

0 comments on commit a0fa5b9

Please sign in to comment.