Skip to content

Commit

Permalink
Merge pull request #21 from erhant/main
Browse files Browse the repository at this point in the history
fix: handle error instead of `expect`
  • Loading branch information
andthattoo authored Nov 4, 2024
2 parents 72b090f + 32d70eb commit 831a236
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 12 deletions.
15 changes: 7 additions & 8 deletions src/api_interface/openai_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,20 @@ impl OpenAIExecutor {
let messages = vec![ChatMessageBuilder::default()
.content(ChatMessageContent::Text(prompt.to_string()))
.build()
.expect("OpenAI function call message build error")];
.map_err(|e| OllamaError::from(format!("Could not build chat message: {:?}", e)))?];

let parameters = ChatCompletionParametersBuilder::default()
.model(self.model.clone())
.messages(messages)
.tools(openai_tools)
.build()
.expect("Error while building tools.");
.map_err(|e| {
OllamaError::from(format!("Could not build message parameters: {:?}", e))
})?;

let result = self
.client
.chat()
.create(parameters)
.await
.expect("OpenAI Function call failed");
let result = self.client.chat().create(parameters).await.map_err(|e| {
OllamaError::from(format!("Failed to parse OpenAI API response: {:?}", e))
})?;
let message = result.choices[0].message.clone();

if raw_mode {
Expand Down
2 changes: 1 addition & 1 deletion src/memory/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl Embedder for OpenAIEmbedder {
.input(EmbeddingInput::String(_prompt.to_string()))
.encoding_format(EmbeddingEncodingFormat::Float)
.build()
.expect("Error building OpenAI embedder");
.map_err(EmbeddingError::BuilderError)?;

let result = client.embeddings().create(parameters).await;

Expand Down
4 changes: 4 additions & 0 deletions src/program/errors.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::error::Error;
use std::fmt;

use openai_dive::v1::resources::embedding::EmbeddingParametersBuilderError;

#[allow(clippy::enum_variant_names)]
#[derive(Debug)]
pub enum CustomError {
Expand All @@ -25,6 +27,7 @@ pub enum EmbeddingError {
DocumentEmbedding(String),
QueryEmbedding(String),
ModelDoesNotExist,
BuilderError(EmbeddingParametersBuilderError),
}

#[derive(Debug)]
Expand Down Expand Up @@ -78,6 +81,7 @@ impl fmt::Display for EmbeddingError {
EmbeddingError::DocumentEmbedding(doc) => write!(f, "Error while generating embeddings for doc: {}", doc),
EmbeddingError::QueryEmbedding(query) => write!(f, "Error while generating embeddings for query: {}", query),
EmbeddingError::ModelDoesNotExist => write!(f, "Model does not exist. run ollama run hellord/mxbai-embed-large-v1:f16 to create it."),
EmbeddingError::BuilderError(err) => write!(f, "Embedding builder error: {}", err),
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/tools/browserless.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ impl Tool for Browserless {

async fn run(&self, input: Value) -> Result<String, Box<dyn Error>> {
let website = input["website"].as_str().ok_or("Website URL is required")?;
let browserless_token =
env::var("BROWSERLESS_TOKEN").expect("BROWSERLESS_TOKEN must be set");
let browserless_token = env::var("BROWSERLESS_TOKEN")?;
let url = format!("http://0.0.0.0:3000/content?token={}", browserless_token);
let payload = json!({
"url": website
Expand Down
2 changes: 1 addition & 1 deletion src/tools/serper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ impl Tool for SearchTool {
}

let client = Client::new();
let api_key = env::var("SERPER_API_KEY").expect("SERPER_API_KEY must be set");
let api_key = env::var("SERPER_API_KEY")?;
let response = client
.post(&url)
.header("X-API-KEY", api_key)
Expand Down

0 comments on commit 831a236

Please sign in to comment.