Skip to main content

Remote Functions

LangDB enables users to create and integrate their own custom functions using AWS Lambda.

These remote functions can be seamlessly incorporated into LangDB, allowing you to extend the database's functionality and perform specific tasks tailored to your application's requirements.

Echo

Let's take an example of a function which returns back the input.

#[derive(Clone, Debug, Deserialize, Default, JsonSchema)]
pub struct EchoFunction {}

derive_executable!(EchoFunction);

impl FunctionDef for EchoFunction {
fn schema(
&self,
_input_query: Option<String>,
) -> crate::langdb::types::FnResult<crate::langdb::types::FunctionSchema> {
Ok(FunctionSchema::Map(IndexMap::from([(
"value".to_string(),
"String".to_string(),
)])))
}
}
#[async_trait::async_trait]
impl ExecuteFn for EchoFunction {
async fn execute(
&self,
_headers: Vec<String>,
chunk: Vec<Vec<Value>>,
tx: Sender<Vec<Value>>,
) -> FnResult<()> {
for row in chunk {
let val = row
.first()
.ok_or(FnError::CustomError("no input".to_string()))?
.to_owned();
tx.send(vec![val]).await?;
}
Ok(())
}
}

We are using Cargo-lambda here, you can check out their documentation.

Deployment

To deploy our function, it's as simple as running the following commands.

cargo lambda build --release
cargo lambda deploy

Function Provider

Once the function has been deployed to AWS. We can use our credentials to create a function provider in LangDB.

CREATE PROVIDER lambda_provider
ENGINE = AwsLambda(
access_key = 'xxxx',
access_secret = 'xxxx',
region = 'xxxx'
);

Usage

SELECT * FROM echo('Hello World')
value
Hello World