use kproc_llm::{llama_cpp, ollama, simple_api};
use kproc::prelude::*;
use processor::ValueProcessor as _;
use processors::rag;
trait Example
{
type Model: kproc_llm::LargeLanguageModel + Send + Sync;
fn create_model() -> Self::Model;
async fn run()
{
let encode = rag::graph::encoder::TextEncoder::new(Self::create_model());
let graph =
encode.process_one(
rag::graph::TextEncoderInputs {prompt:
r#"Paul is a 32 years old software engineer, Josh is the boyfriend of Paul and works as a cook for the blue dragon restaurant which is owned by Liu Wang who is 40 years old. Liu Wang is a friend of Paul."#.to_string()}).await.unwrap();
println!("graph = {:?}", graph.graph);
let decode = rag::graph::decoder::Decoder::new(Self::create_model());
let sentence = decode.process_one(rag::graph::DecoderInputs {graph: serde_json::from_str( r#"{
"nodes": [["paul", "Person", {"age": 32, "occupation": "software engineer", "name": "Paul"}], ["josh", "Person", {"occupation": "chef", "name": "Josh"}], ["liu_wang", "Person", {"age": 40, "name": "Liu Wang"}], ["blue_dragon", "Restaurant", {}], ["pauls_boyfriend", "Relationship", {"name": "boyfriend"}]],
"relationships": [["paul", "is_friend_of", "liu_wang", {}], ["josh", "works_at", "blue_dragon", {}], ["paul", "boyfriend", "josh", {}]]
}"#).unwrap()}).await.unwrap();
println!("sentence = {:?}", sentence.text);
}
}
struct OllamaExample {}
impl Example for OllamaExample
{
type Model = kproc_llm::ollama::Ollama;
fn create_model() -> Self::Model
{
ollama::Ollama::from_model("llama3:instruct")
}
}
struct LlamaCppExample {}
impl Example for LlamaCppExample
{
type Model = kproc_llm::llama_cpp::LlamaCpp;
fn create_model() -> Self::Model
{
llama_cpp::LlamaCpp::from_model(
"hf://ggml-org/Meta-Llama-3.1-8B-Instruct-Q4_0-GGUF/meta-llama-3.1-8b-instruct-q4_0.gguf",
)
.unwrap()
}
}
struct SimpleApi {}
impl Example for SimpleApi
{
type Model = kproc_llm::simple_api::SimpleApi<yaaral::tokio::Runtime>;
fn create_model() -> Self::Model
{
simple_api::SimpleApi::new(
yaaral::tokio::Runtime::current(),
"http://localhost",
8080,
None,
)
.unwrap()
}
}
#[tokio::main]
async fn main()
{
let mut has_run = false;
for argument in std::env::args()
{
if argument == "--llama-cpp"
{
LlamaCppExample::run().await;
has_run = true;
}
else if argument == "--simple-api"
{
SimpleApi::run().await;
has_run = true;
}
else if argument == "--ollama"
{
OllamaExample::run().await;
has_run = true;
}
}
if !has_run
{
println!("Specify --lama-cpp, --simple-api or --ollama.")
}
}