Skip to content

Commit

Permalink
Fix merge conflicts with main when adding LLM functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
ettolrach committed Mar 2, 2024
2 parents 064cd34 + 204d525 commit 8a4d472
Show file tree
Hide file tree
Showing 13 changed files with 140 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,5 @@ Cargo.lock

backend/target/


.env
1 change: 1 addition & 0 deletions backend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.env
2 changes: 1 addition & 1 deletion backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ edition = "2021"
openai_api_rust = "0.1.8"
diesel = { version = "2.1.0", features = ["postgres"] }
dotenvy = "0.15"
uuid = { version = "1.7.0", features = ["v5"] }
uuid = { version = "1.7.0", features = ["v5", "fast-rng", "macro-diagnostics"] }
tokio = { version = "1", features = ["full"] }
async-trait = "0.1.77"
6 changes: 6 additions & 0 deletions backend/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Setup
1. Install Rust
2. cargo build
3. echo DATABASE_URL=postgres://username:password@localhost/diesel_demo > .env
4. diesel setup
5. cargo run
22 changes: 22 additions & 0 deletions backend/src/cache.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use crate::kube::{KubeId, Kube};
use std::vec::Vec;

pub struct Recipe {
items: Vec<KubeId>,
}

impl Recipe {
pub fn new(items: Vec<KubeId>) -> Self {
let mut items = items;
items.sort();
Recipe { items }
}
pub fn hash(&self) -> u64 {
let big_key = self.items.iter().fold(0, |acc, x| acc ^ x.as_u128());
(big_key >> 64 & big_key) as u64
}
}

pub struct PsqlCache {

}
20 changes: 20 additions & 0 deletions backend/src/cache_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#[cfg(test)]
mod tests {
// Note this useful idiom: importing names from outer (for mod tests) scope.
use super::*;

#[test]

fn test_kube_cache_hash_is_not_order_sensitive() {
let mut items = vec![];
items.push(KubeId::new("a"));
items.push(KubeId::new("b"));
items.push(KubeId::new("b"));
items.push(KubeId::new("c"));
let recipe1 = Recipe::new(items.clone());

items.reverse();
let recipe2 = Recipe::new(items.clone());
assert_eq!(recipe1.hash(), recipe2.hash());
}
}
33 changes: 31 additions & 2 deletions backend/src/kube.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,34 @@
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
use uuid::Uuid;

#[derive(PartialEq, Eq, PartialOrd, Ord, Debug)]
pub struct KubeId {
uuid: Uuid,
}

impl KubeId {
pub fn new(name: &str) -> Self {
let mut name = name.to_string();
name.push_str("kube");
KubeId {
uuid: Uuid::new_v5(&Uuid::NAMESPACE_DNS, name.as_bytes()),
}
}

pub fn as_u128(&self) -> u128 {
self.uuid.as_u128()
}
}

#[derive(PartialEq, Debug)]
pub struct Kube {
pub uuid: uuid::Uuid,
pub id: KubeId,
pub name: String,
}
impl Kube {
pub fn new(name: String) -> Kube {
Kube {
id: KubeId::new(name.as_str()),
name,
}
}
}
1 change: 1 addition & 0 deletions backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ pub mod space;
pub mod kube;
pub mod player;
pub mod llm;
pub mod cache;

type Coordinate = [u64; 2];
10 changes: 5 additions & 5 deletions backend/src/llm.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::kube::Kube;use async_trait::async_trait;
use crate::kube::Kube;
use async_trait::async_trait;

/// Trait for interacting with an LLM.
#[async_trait]
Expand Down Expand Up @@ -28,8 +29,7 @@ impl LLM for FakeLLM {
for kube in kubes {
new_string.push_str(kube.name.as_str());
}
let uuid = uuid::Uuid::new_v5(&uuid::Uuid::NAMESPACE_DNS, b"poo");
Kube { name: new_string, uuid }
Kube::new(new_string)
}
}

Expand All @@ -39,8 +39,8 @@ mod tests {
#[tokio::test]
async fn fake_combine_test() {
let kubes = vec![
Kube { name: String::from("water"), uuid: uuid::Uuid::new_v5(&uuid::Uuid::NAMESPACE_DNS, b"poo") },
Kube { name: String::from("glass"), uuid: uuid::Uuid::new_v5(&uuid::Uuid::NAMESPACE_DNS, b"poo") }
Kube::new(String::from("water")),
Kube::new(String::from("glass")),
];
let fake_llm = FakeLLM::new();
let response_kube = fake_llm.combine(&kubes).await;
Expand Down
9 changes: 9 additions & 0 deletions diesel.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# For documentation on how to configure this file,
# see https://diesel.rs/guides/configuring-diesel-cli

[print_schema]
file = "src/schema.rs"
custom_type_derives = ["diesel::query_builder::QueryId"]

[migrations_directory]
dir = "migrations"
Empty file added migrations/.keep
Empty file.
6 changes: 6 additions & 0 deletions migrations/00000000000000_diesel_initial_setup/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- This file was automatically created by Diesel to setup helper functions
-- and other internal bookkeeping. This file is safe to edit, any future
-- changes will be added to existing projects as new migrations.

DROP FUNCTION IF EXISTS diesel_manage_updated_at(_tbl regclass);
DROP FUNCTION IF EXISTS diesel_set_updated_at();
36 changes: 36 additions & 0 deletions migrations/00000000000000_diesel_initial_setup/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
-- This file was automatically created by Diesel to setup helper functions
-- and other internal bookkeeping. This file is safe to edit, any future
-- changes will be added to existing projects as new migrations.




-- Sets up a trigger for the given table to automatically set a column called
-- `updated_at` whenever the row is modified (unless `updated_at` was included
-- in the modified columns)
--
-- # Example
--
-- ```sql
-- CREATE TABLE users (id SERIAL PRIMARY KEY, updated_at TIMESTAMP NOT NULL DEFAULT NOW());
--
-- SELECT diesel_manage_updated_at('users');
-- ```
CREATE OR REPLACE FUNCTION diesel_manage_updated_at(_tbl regclass) RETURNS VOID AS $$
BEGIN
EXECUTE format('CREATE TRIGGER set_updated_at BEFORE UPDATE ON %s
FOR EACH ROW EXECUTE PROCEDURE diesel_set_updated_at()', _tbl);
END;
$$ LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION diesel_set_updated_at() RETURNS trigger AS $$
BEGIN
IF (
NEW IS DISTINCT FROM OLD AND
NEW.updated_at IS NOT DISTINCT FROM OLD.updated_at
) THEN
NEW.updated_at := current_timestamp;
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;

0 comments on commit 8a4d472

Please sign in to comment.