artifacts: rewrite the entire artifact querying layer to create modular 'tools' and 'datasource's
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
use async_openai::types::chat::{ChatCompletionTool, ChatCompletionTools, FunctionObjectArgs};
|
||||
use schemars::{JsonSchema, Schema, schema_for};
|
||||
use serde::de::DeserializeOwned;
|
||||
|
||||
use crate::artifacts::Artifact;
|
||||
|
||||
pub trait DataSource: ToolDescription {
|
||||
type Args: JsonSchema + DeserializeOwned;
|
||||
type Error;
|
||||
fn synchronize(&mut self, artifact: &mut Artifact) -> impl Future<Output = Result<Vec<Artifact>, Self::Error>>;
|
||||
fn query(&mut self, args: &Self::Args) -> impl Future<Output = Result<Vec<Artifact>, Self::Error>>;
|
||||
}
|
||||
|
||||
pub trait ToolDescription {
|
||||
fn description(&self) -> &str;
|
||||
fn name(&self) -> &str;
|
||||
}
|
||||
|
||||
pub struct Tool {
|
||||
pub name: String,
|
||||
pub description: String,
|
||||
pub schema: Schema
|
||||
}
|
||||
|
||||
impl Tool {
|
||||
pub fn from_datasource<T: DataSource>(src: &T) -> Self {
|
||||
Self {
|
||||
name: src.name().to_string(),
|
||||
description: src.description().to_string(),
|
||||
schema: schema_for!(T::Args)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<ChatCompletionTool> for Tool {
|
||||
fn into(self) -> ChatCompletionTool {
|
||||
ChatCompletionTool {
|
||||
function: FunctionObjectArgs::default()
|
||||
.name(self.name)
|
||||
.description(self.description)
|
||||
.parameters(self.schema).build().unwrap()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<ChatCompletionTools> for Tool {
|
||||
fn into(self) -> ChatCompletionTools {
|
||||
ChatCompletionTools::Function(self.into())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user