Skip to content
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

More specific bounderies for the struct of models based on api #26

Open
Arend-Jan opened this issue Mar 18, 2023 · 0 comments
Open

More specific bounderies for the struct of models based on api #26

Arend-Jan opened this issue Mar 18, 2023 · 0 comments

Comments

@Arend-Jan
Copy link
Owner

Arend-Jan commented Mar 18, 2023

API Spec
Create a ChatOptions struct to store the optional parameters with their default values and enforce the specified constraints on their values. You can then use this struct when constructing API requests. Here's an example implementation:

#[derive(Debug, Clone)]
pub struct ChatOptions {
    pub temperature: f64,
    pub top_p: f64,
    pub n: u32,
    pub stream: bool,
    pub stop: Option<Vec<String>>,
    pub max_tokens: Option<u32>,
    pub presence_penalty: f64,
    pub frequency_penalty: f64,
    pub logit_bias: Option<LogitBias>,
    pub user: Option<String>,
}

impl ChatOptions {
    pub fn new() -> Self {
        ChatOptions {
            temperature: 1.0,
            top_p: 1.0,
            n: 1,
            stream: false,
            stop: None,
            max_tokens: None,
            presence_penalty: 0.0,
            frequency_penalty: 0.0,
            logit_bias: None,
            user: None,
        }
    }

    pub fn set_temperature(mut self, temperature: f64) -> Result<Self, &'static str> {
        if temperature >= 0.0 && temperature <= 2.0 {
            self.temperature = temperature;
            Ok(self)
        } else {
            Err("Temperature should be between 0 and 2")
        }
    }

    pub fn set_top_p(mut self, top_p: f64) -> Result<Self, &'static str> {
        if top_p >= 0.0 && top_p <= 1.0 {
            self.top_p = top_p;
            Ok(self)
        } else {
            Err("top_p should be between 0 and 1")
        }
    }

    // Implement similar setter methods for other optional parameters with constraints
}

In this example, the ChatOptions struct is created with default values for the optional parameters. You can add setter methods for each parameter that enforces the constraints mentioned in the API spec. For example, the set_temperature method checks if the temperature value is between 0 and 2 before setting it. If the value is not within the specified range, it returns an error. You can create similar methods for other parameters with constraints, like presence_penalty and frequency_penalty.

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

No branches or pull requests

1 participant