use crate::{
processor,
processors::rag::{self, graph::DecoderOutputs},
Result,
};
use kproc_llm::prelude::*;
use kproc_pmacros::Processor;
#[derive(Processor)]
#[streams(rag::graph::DecoderInputs, rag::graph::DecoderOutputs)]
pub struct Decoder<T: LargeLanguageModel>
{
model: T,
}
impl<T: LargeLanguageModel + Sync + Send> Decoder<T>
{
pub fn new(model: T) -> Self
{
Self { model }
}
}
impl<T: LargeLanguageModel + Sync + Send> processor::ValueProcessor for Decoder<T>
{
#[allow(clippy::manual_async_fn)] fn process_one(
&self,
inputs: Self::Inputs,
) -> impl std::future::Future<Output = Result<Self::Outputs>> + Send
{
async move {
let system =
r#"You are a data scientist working for a company that is building a graph database. Your task is to take a graph from a database and turn it into a sentence that can be understood by humans. The Nodes are provided in the form [ENTITY_ID, TYPE, PROPERTIES] and the relationships are in the form [ENTITY_ID_1, RELATIONSHIP, ENTITY_ID_2, PROPERTIES].
Example:
Data:
{
"nodes": [["alice", "Person", {"age": 25, "occupation": "lawyer", "name":"Alice"}], ["bob", "Person", {"occupation": "journalist", "name": "Bob"}], ["alice.com", "Webpage", {"url": "www.alice.com"}], ["bob.com", "Webpage", {"url": "www.bob.com"}]],
"relationships": [["alice", "roommate", "bob", {"start": 2021}], ["alice", "owns", "alice.com", {}], ["bob", "owns", "bob.com", {}]]
}
Output: Alice lawyer and is 25 years old and Bob is her roommate since 2001. Bob works as a journalist. Alice owns a the webpage www.alice.com and Bob owns the webpage www.bob.com.
Generate a text for the following graph."#
.to_string();
let prompt = serde_json::to_string(&inputs.graph)?;
let prompt = GenerationPrompt::prompt(prompt).system(system.to_string());
let res = self.model.generate(prompt)?.await?;
Ok(DecoderOutputs { text: res })
}
}
}