64bit / async-openai

Rust library for OpenAI
https://docs.rs/async-openai
MIT License
1.11k stars 165 forks source link

impls.rs: AssistantTools #166

Closed louis030195 closed 8 months ago

louis030195 commented 9 months ago

hey

i'm saving AssistantTools to DB and have to do some hacks atm because serialization is not implemented i believe

something like this would solve the problem:

use serde::{Deserialize, Deserializer};

impl<'de> Deserialize<'de> for AssistantTools {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let json: Value = Value::deserialize(deserializer)?;
        let type_field = json.get("type").and_then(Value::as_str);

        match type_field {
            Some("function") => {
                let function_tool = serde_json::from_value(json).map(AssistantTools::Function);
                function_tool.map_err(serde::de::Error::custom)
            }
            Some("retrieval") => {
                let retrieval_tool = serde_json::from_value(json).map(AssistantTools::Retrieval);
                retrieval_tool.map_err(serde::de::Error::custom)
            }
           Some("code_interpreter") => {
                let code_tool = serde_json::from_value(json).map(AssistantTools::Code);
                retrieval_tool.map_err(serde::de::Error::custom)
            }
            _ => Err(serde::de::Error::custom("Invalid type")),
        }
    }
}
louis030195 commented 9 months ago

Actually CreateAssistantRequest does not serialize function tools properly for some reason (ignore function prop)

louis030195 commented 9 months ago

i think it serialize all tools as code:

[Code(AssistantToolsCode { type: "function" }), Code(AssistantToolsCode { type: "retrieval" })]

64bit commented 8 months ago

Hello, thank you for the issue.

Actually AssitantTools does implment Serialize and Deserialize traits - so you should be able to read write from DB.

https://github.com/64bit/async-openai/blob/main/async-openai/src/types/assistants/assistant.rs#L52

Not sure why custom deserialization needs to be implemented as in description of this issue?