Skip to content

Custom Error Support for LoomError #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Sep 30, 2024

Conversation

Wolfenheimm
Copy link
Contributor

@Wolfenheimm Wolfenheimm commented Sep 28, 2024

Overview

Improve error handling by providing more type-safe and flexible error management for users of the library.

Changes

  1. Modified LoomError enum

    • Introduced a new variant Llm that uses the associated type PromptError from the Llm trait.
    • This allows for more specific and type-safe error handling.
  2. Added PromptError associated type to Llm trait:

    • Introduced a new associated type PromptError in the Llm trait.
    • This type implements the Error trait.
  3. Updated Result type alias:

    • Modified the Result type alias to use LoomError<U> as the error type.
    • This change propagates the type-safe error handling throughout the library.

Usage Example

Users can now define their custom PromptError types and use them with the library:

use std::error::Error;

#[derive(Debug, thiserror::Error)]
enum MyPromptError {
    #[error("Bad configuration: {0}")]
    BadConfig(String),
    #[error("API error: {0}")]
    ApiError(String),
}

struct MyLlm;

impl Llm<MyConfig> for MyLlm {
    type PromptError = MyPromptError;
    
    // ... other associated types and methods ...

    async fn prompt(
        &self,
        // ... other parameters ...
    ) -> Result<Self::Response, MyConfig> {
        // If an error occurs:
        Err(LoomError::Llm(MyPromptError::BadConfig("Invalid prompt".to_string())))
    }
}

Error Handling

The new error structure allows for more precise error handling:

match result {
    Ok(response) => println!("Success: {:?}", response),
    Err(LoomError::Llm(prompt_error)) => match prompt_error {
        MyPromptError::BadConfig(msg) => println!("Configuration error: {}", msg),
        MyPromptError::ApiError(msg) => println!("API error: {}", msg),
    },
    Err(LoomError::Storage(storage_error)) => println!("Storage error: {}", storage_error),
    Err(LoomError::BadConfig(msg)) => println!("Bad configuration: {}", msg),
    Err(LoomError::MaxCompletionTokensIsZero) => println!("Exceeded max prompt tokens"),
    Err(LoomError::UnknownError(msg)) => println!("Unknown error: {}", msg),
}

Benefits

  • Type-safe error handling: Errors are now more closely tied to their sources.
  • Flexible: Users can define their own error types for different LLM implementations.
  • Consistent: The Result type alias ensures consistent error handling throughout the library.

Supplemental

  • Modified Result across the codebase to accommodate this structural change.
  • Test(s) have been updated to accommodate these changes

@Wolfenheimm Wolfenheimm changed the title Changes to LoomError Enum Support custom Error type for Llm trait Sep 28, 2024
@Wolfenheimm Wolfenheimm changed the title Support custom Error type for Llm trait Support custom Error type for Llm trait Sep 28, 2024
@Wolfenheimm Wolfenheimm changed the title Support custom Error type for Llm trait Custom Error Support for LoomError Sep 28, 2024
Copy link
Collaborator

@snowmead snowmead left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The approach you took does not allow the user to specify his own custom error type and have it propagated within the weave function in a type safe way. The Llm trait functions still return dyn Error and therefore any error can be returned from the user, so there is no guarantee that the result of the weave function would yield the error they expected.

I have updated the issue #11 to describe more deeply the requirements that should be met.

@snowmead
Copy link
Collaborator

snowmead commented Sep 30, 2024

close #12

@snowmead snowmead merged commit 8666100 into loreweaver-xyz:main Sep 30, 2024
1 of 3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants