Skip to content

Commit

Permalink
moved embedding out into its own file, fixed ui running
Browse files Browse the repository at this point in the history
  • Loading branch information
kvey committed Apr 19, 2024
1 parent 9250fdb commit f015918
Show file tree
Hide file tree
Showing 36 changed files with 343 additions and 198 deletions.
4 changes: 3 additions & 1 deletion toolchain/chidori-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,6 @@ uuid = { version = "1.6.1", features = ["v4"] }
tonic-build = "0.9.2"
target-lexicon = "0.12"
dirs = "3.0"
pyo3-build-config = "0.19.1"
pyo3-build-config = "0.19.1"
rye = {path = "/Users/coltonpierson/work/rye/rye"}
anyhow = "1.0.82"
19 changes: 19 additions & 0 deletions toolchain/chidori-core/build.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
use dirs::home_dir;
use std::env;
use std::io::{self, Write};
use std::path::PathBuf;
use std::str::FromStr;
use rye::lock::LockOptions;
use target_lexicon::{OperatingSystem, Triple};
use rye::sync::{sync, SyncMode, SyncOptions};
use rye::utils::CommandOutput;

fn install_python() -> Result<(), anyhow::Error> {
rye::platform::init();
sync(SyncOptions {
output: CommandOutput::Quiet,
dev: true,
mode: SyncMode::PythonOnly,
force: false,
no_lock: false,
lock_options: LockOptions::default(),
pyproject: Some(PathBuf::from("./pyproject.toml")),
})
}

fn add_extension_module_link_args(triple: &Triple) -> io::Result<()> {
let mut writer = io::stdout();
Expand Down Expand Up @@ -52,6 +69,8 @@ fn add_extension_module_link_args(triple: &Triple) -> io::Result<()> {
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
// TODO: use rye to install a static version of python
// install_python().unwrap();
let target_triple = env::var("TARGET").expect("TARGET was not set");
let triple = Triple::from_str(&target_triple).expect("Invalid target triple");
add_extension_module_link_args(&triple);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ def add_two_numbers(a, b):
return a + b
```


```prompt (add_population)
---
fn: add_population
model: gpt-3.5-turbo
import:
- add_two_numbers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
Memory cells are an example of persistent stateful cells, once initially invoked they expose methods
to the rest of the environment that manipulate their internal state. Memory specifically exposes "insert" and
"search" methods which are used to store and retrieve values respectively.

We must assign an embedding_fn to the memory cell, in this case we're using an embedding cell to simplify the process.
```memory (stateful_memory)
---
embedding_fn: rag
Expand Down Expand Up @@ -35,11 +37,5 @@ def read_file_and_load_to_memory(file_path):

To demonstrate this functionality we're going to search for lines that match the embedding "test".
```python (entry)
import unittest

class TestMarshalledValues(unittest.IsolatedAsyncioTestCase):
async def test_run_prompt(self):
self.assertEqual(await read_file_and_load_to_memory("./"), 4)

unittest.TextTestRunner().run(unittest.TestLoader().loadTestsFromTestCase(TestMarshalledValues))
out = await read_file_and_load_to_memory("./")
```
7 changes: 0 additions & 7 deletions toolchain/chidori-core/package_node/types/CellTypes.ts

This file was deleted.

This file was deleted.

4 changes: 0 additions & 4 deletions toolchain/chidori-core/package_node/types/CodeCell.ts

This file was deleted.

3 changes: 0 additions & 3 deletions toolchain/chidori-core/package_node/types/FunctionCall.ts

This file was deleted.

4 changes: 0 additions & 4 deletions toolchain/chidori-core/package_node/types/LLMPromptCell.ts

This file was deleted.

4 changes: 0 additions & 4 deletions toolchain/chidori-core/package_node/types/MemoryCell.ts

This file was deleted.

3 changes: 0 additions & 3 deletions toolchain/chidori-core/package_node/types/MessageRole.ts

This file was deleted.

3 changes: 0 additions & 3 deletions toolchain/chidori-core/package_node/types/ScheduleCell.ts

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

3 changes: 0 additions & 3 deletions toolchain/chidori-core/package_node/types/TemplateCell.ts

This file was deleted.

5 changes: 0 additions & 5 deletions toolchain/chidori-core/package_node/types/TemplateMessage.ts

This file was deleted.

3 changes: 0 additions & 3 deletions toolchain/chidori-core/package_node/types/WebserviceCell.ts

This file was deleted.

This file was deleted.

13 changes: 13 additions & 0 deletions toolchain/chidori-core/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[project]
name = "chidori-core"
version = "1.0.0"
description = ""
authors = [{ name = "Colton Pierson", email = "[email protected]" }]
dependencies = [
]
readme = "README.md"
requires-python = "== 3.11"

[tool.rye]
managed = true
virtual = true
61 changes: 61 additions & 0 deletions toolchain/chidori-core/src/cells/embedding_cell.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use futures_util::FutureExt;
use crate::cells::{LLMEmbeddingCell, LLMPromptCell};
use crate::execution::primitives::operation::{InputItemConfiguration, InputSignature, InputType, OperationFnOutput, OperationNode, OutputItemConfiguration, OutputSignature};
use crate::library::std::ai::llm::ai_llm_run_embedding_model;

#[tracing::instrument]
pub fn llm_embedding_cell(cell: &LLMEmbeddingCell) -> OperationNode {
let LLMEmbeddingCell {
function_invocation,
configuration,
req,
name,
..
} = cell;
let schema =
chidori_prompt_format::templating::templates::analyze_referenced_partials(&&req);
let mut role_blocks =
chidori_prompt_format::templating::templates::extract_roles_from_template(&&req);

let mut output_signature = OutputSignature::new();
if let Some(name) = name {
output_signature.functions.insert(
name.clone(),
OutputItemConfiguration::Value,
);
}

let mut input_signature = InputSignature::new();

// We only require the globals to be passed in if the user has not specified this prompt as a function
if configuration.get("fn").is_none() {
for (key, value) in &schema.items {
input_signature.globals.insert(
key.clone(),
InputItemConfiguration {
ty: Some(InputType::String),
default: None,
},
);
}
}

let (_, template) = role_blocks.drain(..).next().unwrap();
let template = template.expect("Must include template");
let name = name.clone();
let is_function_invocation = function_invocation.clone();
OperationNode::new(
name.clone(),
input_signature,
output_signature,
Box::new(move |s, x, _, _| {
let template = template.clone();
let name = name.clone();
let s = s.clone();
async move {
OperationFnOutput::with_value(ai_llm_run_embedding_model(&s, x, template, name.clone(), is_function_invocation).await)
}.boxed()
}),
)

}
74 changes: 9 additions & 65 deletions toolchain/chidori-core/src/cells/llm_prompt_cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub fn llm_prompt_cell(cell: &LLMPromptCell) -> OperationNode {
let role_blocks =
chidori_prompt_format::templating::templates::extract_roles_from_template(&&req);


let mut output_signature = OutputSignature::new();
if let Some(fn_name) = &configuration.function_name {
output_signature.functions.insert(
Expand Down Expand Up @@ -94,15 +95,20 @@ pub fn llm_prompt_cell(cell: &LLMPromptCell) -> OperationNode {
let s = s.clone();
let configuration = configuration.clone();
async move {
OperationFnOutput::with_value(
crate::library::std::ai::llm::ai_llm_run_chat_model(
let (value, state) = crate::library::std::ai::llm::ai_llm_run_chat_model(
&s,
payload,
role_blocks,
name,
is_function_invocation,
configuration.clone()
).await)
).await;
OperationFnOutput {
execution_state: state,
output: value,
stdout: vec![],
stderr: vec![],
}
}.boxed()
}),
),
Expand All @@ -114,67 +120,5 @@ pub fn llm_prompt_cell(cell: &LLMPromptCell) -> OperationNode {
OutputSignature::new(),
Box::new(|_, x, _, _| async move { OperationFnOutput::with_value(x) }.boxed()),
),
LLMPromptCell::Embedding {
function_invocation,
configuration,
req,
name,
..
} => {
let schema =
chidori_prompt_format::templating::templates::analyze_referenced_partials(&&req);
let mut role_blocks =
chidori_prompt_format::templating::templates::extract_roles_from_template(&&req);

let mut output_signature = OutputSignature::new();
if let Some(fn_name) = configuration.get("fn") {
output_signature.functions.insert(
fn_name.clone(),
OutputItemConfiguration::Value,
);
}
if let Some(name) = name {
// The result of executing the prompt is available as the name of the cell
// when the cell is named.
output_signature.globals.insert(
name.clone(),
OutputItemConfiguration::Value,
);
}

let mut input_signature = InputSignature::new();
// We only require the globals to be passed in if the user has not specified this prompt as a function
if configuration.get("fn").is_none() {
for (key, value) in &schema.items {
input_signature.globals.insert(
key.clone(),
InputItemConfiguration {
ty: Some(InputType::String),
default: None,
},
);
}
}
let (_, template) = role_blocks.drain(..).next().unwrap();
let template = template.expect("Must include template");
let name = name.clone();
let is_function_invocation = function_invocation.clone();
if configuration.get("fn").is_some() && !is_function_invocation {
return panic!("Cell is called as a function invocation without a declared fn name");
}
OperationNode::new(
name.clone(),
input_signature,
output_signature,
Box::new(move |s, x, _, _| {
let template = template.clone();
let name = name.clone();
let s = s.clone();
async move {
OperationFnOutput::with_value(ai_llm_run_embedding_model(&s, x, template, name.clone(), is_function_invocation).await)
}.boxed()
}),
)
},
}
}
Loading

0 comments on commit f015918

Please sign in to comment.