Skip to content

Commit f992045

Browse files
authored
Merge pull request #21 from firstbatchxyz/erhant/parser-logic-update
feat: historic messages & refactors
2 parents ca1031f + 4c745ad commit f992045

File tree

14 files changed

+826
-829
lines changed

14 files changed

+826
-829
lines changed

Cargo.lock

Lines changed: 475 additions & 605 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "dkn-oracle"
33
description = "Dria Knowledge Network: Oracle Node"
4-
version = "0.1.7"
4+
version = "0.1.8"
55
edition = "2021"
66
license = "Apache-2.0"
77
readme = "README.md"
@@ -36,7 +36,8 @@ bytes = "1.7.1"
3636
rand = "0.8.5"
3737
reqwest = "0.12.5"
3838

39-
# hex, serde
39+
# b64, hex, serde
40+
base64 = "0.22.1"
4041
hex = "0.4.3"
4142
hex-literal = "0.4.1"
4243
serde = "1.0.204"
@@ -49,4 +50,3 @@ clap = { version = "4.5.13", features = ["derive", "env"] }
4950
# there are many unused stuff here, but everything breaks if you use the minimal set
5051
# because Bundlr SDK is not maintained at all
5152
bundlr-sdk = { version = "0.5.0" }
52-
base64 = "0.22.1"

src/commands/coordinator.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,20 +92,21 @@ pub async fn run_oracle(
9292
}
9393

9494
// handle new tasks with subscription
95-
let event_poller = node
96-
.subscribe_to_tasks()
97-
.await
98-
.wrap_err("could not subscribe to tasks")?;
9995
log::info!(
100-
"Subscribed to LLMOracleCoordinator ({}) as {}",
96+
"Subscribing to LLMOracleCoordinator ({}) as {}",
10197
node.addresses.coordinator,
10298
kinds
10399
.iter()
104100
.map(|kind| kind.to_string())
105101
.collect::<Vec<String>>()
106102
.join(", ")
107103
);
104+
let event_poller = node
105+
.subscribe_to_tasks()
106+
.await
107+
.wrap_err("could not subscribe to tasks")?;
108108

109+
log::info!("Listening for events...");
109110
event_poller
110111
.into_stream()
111112
.for_each(|log| async {

src/commands/token.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ pub async fn claim_rewards(node: &DriaOracle) -> Result<()> {
4343
// transfer rewards
4444
node.transfer_from(node.addresses.coordinator, node.address(), allowance.amount)
4545
.await?;
46+
log::info!("Rewards claimed: {}.", allowance);
4647
}
4748

4849
Ok(())

src/compute/handlers/generation.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::{
2-
compute::WorkflowsExt,
2+
compute::Request,
33
contracts::{bytes32_to_string, bytes_to_string},
44
data::Arweave,
55
mine_nonce, DriaOracle,
@@ -8,7 +8,7 @@ use alloy::{
88
primitives::{FixedBytes, U256},
99
rpc::types::TransactionReceipt,
1010
};
11-
use dkn_workflows::{DriaWorkflowsConfig, Executor};
11+
use dkn_workflows::DriaWorkflowsConfig;
1212
use eyre::{Context, Result};
1313

1414
/// Handles a generation request.
@@ -50,14 +50,17 @@ pub async fn handle_generation(
5050
// execute task
5151
log::debug!("Executing the workflow");
5252
let protocol_string = bytes32_to_string(&protocol)?;
53-
let executor = Executor::new(model);
54-
let (output, metadata, use_storage) = executor
55-
.execute_raw(&request.input, &protocol_string)
56-
.await?;
53+
let mut input = Request::try_parse_bytes(&request.input).await?;
54+
let output = input.execute(model, Some(node)).await?;
55+
log::debug!("Output: {}", output);
56+
57+
// post-processing
58+
log::debug!("Post-processing the output");
59+
let (output, metadata, use_storage) = Request::post_process(output, &protocol_string).await?;
5760

58-
// do the Arweave trick for large inputs
59-
log::debug!("Uploading to Arweave if required");
60-
let arweave = Arweave::new_from_env().wrap_err("could not create Arweave instance")?;
61+
// uploading to storage
62+
log::debug!("Uploading output to storage");
63+
let arweave = Arweave::new_from_env()?;
6164
let output = if use_storage {
6265
arweave.put_if_large(output).await?
6366
} else {

src/compute/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ mod nonce;
55
pub use nonce::mine_nonce;
66

77
mod workflows;
8-
pub use workflows::WorkflowsExt;
8+
pub use workflows::*;

src/compute/workflows/executor.rs

Lines changed: 0 additions & 149 deletions
This file was deleted.

src/compute/workflows/mod.rs

Lines changed: 3 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,6 @@
1-
mod executor;
2-
pub use executor::WorkflowsExt;
3-
41
mod postprocess;
52

6-
#[cfg(test)]
7-
mod tests {
8-
use super::*;
9-
use alloy::primitives::Bytes;
10-
use dkn_workflows::{Entry, Executor, Model, ProgramMemory};
11-
12-
#[tokio::test]
13-
#[ignore = "run this manually"]
14-
async fn test_ollama_generation() {
15-
dotenvy::dotenv().unwrap();
16-
let executor = Executor::new(Model::Llama3_1_8B);
17-
let (output, _, _) = executor
18-
.execute_raw(&Bytes::from_static(b"What is the result of 2 + 2?"), "")
19-
.await
20-
.unwrap();
21-
22-
// funny test but it should pass
23-
println!("Output:\n{}", output);
24-
// assert!(output.contains('4')); // FIXME: make this use bytes
25-
}
26-
27-
#[tokio::test]
28-
#[ignore = "run this manually"]
29-
async fn test_openai_generation() {
30-
dotenvy::dotenv().unwrap();
31-
let executor = Executor::new(Model::Llama3_1_8B);
32-
let (output, _, _) = executor
33-
.execute_raw(&Bytes::from_static(b"What is the result of 2 + 2?"), "")
34-
.await
35-
.unwrap();
36-
37-
// funny test but it should pass
38-
println!("Output:\n{}", output);
39-
// assert!(output.contains('4')); // FIXME: make this use bytes
40-
}
3+
mod presets;
414

42-
/// Test the generation workflow with a plain input.
43-
#[tokio::test]
44-
async fn test_workflow_plain() {
45-
dotenvy::dotenv().unwrap();
46-
let executor = Executor::new(Model::GPT4o);
47-
let mut memory = ProgramMemory::new();
48-
let workflow = executor.get_generation_workflow().unwrap();
49-
let input = Entry::try_value_or_str("What is 2 + 2?");
50-
let output = executor
51-
.execute(Some(&input), workflow, &mut memory)
52-
.await
53-
.unwrap();
54-
println!("Output:\n{}", output);
55-
}
56-
}
5+
mod requests;
6+
pub use requests::Request;
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
"name": "LLM generation",
3+
"description": "Directly generate text with input",
4+
"config": {
5+
"max_steps": 10,
6+
"max_time": 50,
7+
"tools": [""]
8+
},
9+
"external_memory": {
10+
"context": [""],
11+
"question": [""],
12+
"answer": [""]
13+
},
14+
"tasks": [
15+
{
16+
"id": "A",
17+
"name": "Generate with history",
18+
"description": "Expects an array of messages for generation",
19+
"messages": [],
20+
"inputs": [],
21+
"operator": "generation",
22+
"outputs": [
23+
{
24+
"type": "write",
25+
"key": "result",
26+
"value": "__result"
27+
}
28+
]
29+
},
30+
{
31+
"id": "__end",
32+
"name": "end",
33+
"description": "End of the task",
34+
"messages": [{ "role": "user", "content": "End of the task" }],
35+
"inputs": [],
36+
"operator": "end",
37+
"outputs": []
38+
}
39+
],
40+
"steps": [
41+
{
42+
"source": "A",
43+
"target": "__end"
44+
}
45+
],
46+
"return_value": {
47+
"input": {
48+
"type": "read",
49+
"key": "result"
50+
}
51+
}
52+
}

src/compute/workflows/presets/generation.json

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,13 @@
1515
{
1616
"id": "A",
1717
"name": "Generate",
18-
"description": "",
19-
"prompt": "{text}",
18+
"description": "Executes a simple generation request",
19+
"messages": [
20+
{
21+
"role": "user",
22+
"content": "{{text}}"
23+
}
24+
],
2025
"inputs": [
2126
{
2227
"name": "text",
@@ -40,7 +45,7 @@
4045
"id": "__end",
4146
"name": "end",
4247
"description": "End of the task",
43-
"prompt": "End of the task",
48+
"messages": [{ "role": "user", "content": "End of the task" }],
4449
"inputs": [],
4550
"operator": "end",
4651
"outputs": []

0 commit comments

Comments
 (0)