diff --git a/demos/prompt_guards/bolt_config.yaml b/demos/prompt_guards/bolt_config.yaml new file mode 100644 index 00000000..d9de7632 --- /dev/null +++ b/demos/prompt_guards/bolt_config.yaml @@ -0,0 +1,48 @@ +default_prompt_endpoint: "127.0.0.1" +load_balancing: "round_robin" +timeout_ms: 5000 + + +# should not be here +embedding_provider: + name: "bge-large-en-v1.5" + model: "BAAI/bge-large-en-v1.5" + +llm_providers: + + - name: open-ai-gpt-4 + api_key: $OPEN_AI_API_KEY + model: gpt-4 + default: true + +prompt_guards: + input_guard: + - name: jailbreak + on_exception_message: Looks like you are curious about my abilities… + - name: toxic + on_exception_message: Looks like you are curious about my toxic detection abilities… + +prompt_targets: + + - type: function_resolver + name: weather_forecast + description: This function resolver provides weather forecast information for a given city. + few_shot_examples: + - what is the weather in New York? + - how is the weather in San Francisco? + - what is the forecast in Chicago? + parameters: + - name: city + required: true + description: The city for which the weather forecast is requested. + - name: days + description: The number of days for which the weather forecast is requested. + - name: units + description: The units in which the weather forecast is requested. + endpoint: + cluster: weatherhost + path: /weather + system_prompt: | + You are a helpful weather forecaster. Use weater data that is provided to you. Please following following guidelines when responding to user queries: + - Use farenheight for temperature + - Use miles per hour for wind speed diff --git a/demos/prompt_guards/docker-compose.yaml b/demos/prompt_guards/docker-compose.yaml new file mode 100644 index 00000000..be92f250 --- /dev/null +++ b/demos/prompt_guards/docker-compose.yaml @@ -0,0 +1,82 @@ + +services: + + config_generator: + build: + context: ../../ + dockerfile: config_generator/Dockerfile + volumes: + - ../../envoyfilter/envoy.template.yaml:/usr/src/app/envoy.template.yaml + - ./bolt_config.yaml:/usr/src/app/bolt_config.yaml + - ./generated:/usr/src/app/out + + bolt: + build: + context: ../../ + dockerfile: envoyfilter/Dockerfile + hostname: bolt + ports: + - "10000:10000" + - "19901:9901" + volumes: + - ./generated/envoy.yaml:/etc/envoy/envoy.yaml + - /etc/ssl/cert.pem:/etc/ssl/cert.pem + depends_on: + config_generator: + condition: service_completed_successfully + model_server: + condition: service_healthy + environment: + - LOG_LEVEL=debug + + model_server: + build: + context: ../../model_server + dockerfile: Dockerfile + ports: + - "18081:80" + healthcheck: + test: ["CMD", "curl" ,"http://localhost:80/healthz"] + interval: 5s + retries: 20 + volumes: + - ~/.cache/huggingface:/root/.cache/huggingface + + function_resolver: + build: + context: ../../function_resolver + dockerfile: Dockerfile + ports: + - "18082:80" + healthcheck: + test: ["CMD", "curl" ,"http://localhost:80/healthz"] + interval: 5s + retries: 20 + volumes: + - ~/.cache/huggingface:/root/.cache/huggingface + environment: + # use ollama endpoint that is hosted by host machine (no virtualization) + - OLLAMA_ENDPOINT=host.docker.internal + # uncomment following line to use ollama endpoint that is hosted by docker + # - OLLAMA_ENDPOINT=ollama + + ollama: + image: ollama/ollama + container_name: ollama + volumes: + - ./ollama:/root/.ollama + restart: unless-stopped + ports: + - '11434:11434' + profiles: + - manual + + chatbot_ui: + build: + context: ../../chatbot_ui + dockerfile: Dockerfile + ports: + - "18080:8080" + environment: + - OPENAI_API_KEY=${OPENAI_API_KEY} + - CHAT_COMPLETION_ENDPOINT=http://bolt:10000/v1/chat/completions diff --git a/envoyfilter/Cargo.lock b/envoyfilter/Cargo.lock index 32925b37..61c13799 100644 --- a/envoyfilter/Cargo.lock +++ b/envoyfilter/Cargo.lock @@ -1458,6 +1458,7 @@ version = "0.1.0" dependencies = [ "open-message-format-embeddings", "serde", + "serde_yaml", ] [[package]] diff --git a/public_types/Cargo.lock b/public_types/Cargo.lock index 71075198..bf5d1ac5 100644 --- a/public_types/Cargo.lock +++ b/public_types/Cargo.lock @@ -4,18 +4,18 @@ version = 3 [[package]] name = "addr2line" -version = "0.22.0" +version = "0.24.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6e4503c46a5c0c7844e948c9a4d6acd9f50cccb4de1c48eb9e291ea17470c678" +checksum = "f5fb1d8e4442bd405fdfd1dacb42792696b0cf9cb15882e5d097b742a676d375" dependencies = [ "gimli", ] [[package]] -name = "adler" -version = "1.0.2" +name = "adler2" +version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" +checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627" [[package]] name = "atomic-waker" @@ -31,17 +31,17 @@ checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" [[package]] name = "backtrace" -version = "0.3.73" +version = "0.3.74" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5cc23269a4f8976d0a4d2e7109211a419fe30e8d88d677cd60b6bc79c5732e0a" +checksum = "8d82cb332cdfaed17ae235a638438ac4d4839913cc2af585c3c6746e8f8bee1a" dependencies = [ "addr2line", - "cc", "cfg-if", "libc", "miniz_oxide", "object", "rustc-demangle", + "windows-targets", ] [[package]] @@ -70,9 +70,9 @@ checksum = "8318a53db07bb3f8dca91a600466bdb3f2eaadeedfdbcf02e1accbad9271ba50" [[package]] name = "cc" -version = "1.1.15" +version = "1.1.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "57b6a275aa2903740dc87da01c62040406b8812552e97129a63ea8850a17c6e6" +checksum = "45bcde016d64c21da4be18b655631e5ab6d3107607e71a73a9f53eb48aae23fb" dependencies = [ "shlex", ] @@ -212,9 +212,9 @@ dependencies = [ [[package]] name = "gimli" -version = "0.29.0" +version = "0.31.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "40ecd4077b5ae9fd2e9e169b102c6c330d0605168eb0e8bf79952b256dbefffd" +checksum = "32085ea23f3234fc7846555e85283ba4de91e21016dc0455a16286d87a292d64" [[package]] name = "h2" @@ -309,9 +309,9 @@ dependencies = [ [[package]] name = "hyper-rustls" -version = "0.27.2" +version = "0.27.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5ee4be2c948921a1a5320b629c4193916ed787a7f7f293fd3f7f5a6c9de74155" +checksum = "08afdbb5c31130e3034af566421053ab03787c640246a446327f550d11bcb333" dependencies = [ "futures-util", "http", @@ -342,9 +342,9 @@ dependencies = [ [[package]] name = "hyper-util" -version = "0.1.7" +version = "0.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cde7055719c54e36e95e8719f95883f22072a48ede39db7fc17a4e1d5281e9b9" +checksum = "da62f120a8a37763efb0cf8fdf264b884c7b8b9ac8660b900c8661030c00e6ba" dependencies = [ "bytes", "futures-channel", @@ -382,9 +382,9 @@ dependencies = [ [[package]] name = "ipnet" -version = "2.9.0" +version = "2.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f518f335dce6725a761382244631d86cf0ccb2863413590b31338feb467f9c3" +checksum = "187674a687eed5fe42285b40c6291f9a01517d415fad1c3cbc6a9f778af7fcd4" [[package]] name = "itoa" @@ -443,11 +443,11 @@ dependencies = [ [[package]] name = "miniz_oxide" -version = "0.7.4" +version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b8a240ddb74feaf34a79a7add65a741f3167852fba007066dcac1ca548d89c08" +checksum = "e2d80299ef12ff69b16a84bb182e3b9df68b5a91574d3d4fa6e41b65deec4df1" dependencies = [ - "adler", + "adler2", ] [[package]] @@ -608,6 +608,7 @@ version = "0.1.0" dependencies = [ "open-message-format-embeddings", "serde", + "serde_yaml", ] [[package]] @@ -686,9 +687,9 @@ checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f" [[package]] name = "rustix" -version = "0.38.35" +version = "0.38.37" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a85d50532239da68e9addb745ba38ff4612a242c1c7ceea689c4bc7c2f43c36f" +checksum = "8acb788b847c24f28525660c4d7758620a7210875711f79e7f663cc152726811" dependencies = [ "bitflags", "errno", @@ -699,9 +700,9 @@ dependencies = [ [[package]] name = "rustls" -version = "0.23.12" +version = "0.23.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c58f8c84392efc0a126acce10fa59ff7b3d2ac06ab451a33f2741989b806b044" +checksum = "f2dabaac7466917e566adb06783a81ca48944c6898a1b08b9374106dd671f4c8" dependencies = [ "once_cell", "rustls-pki-types", @@ -728,9 +729,9 @@ checksum = "fc0a2ce646f8655401bb81e7927b812614bd5d91dbc968696be50603510fcaf0" [[package]] name = "rustls-webpki" -version = "0.102.7" +version = "0.102.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "84678086bd54edf2b415183ed7a94d0efb049f1b646a33e22a36f3794be6ae56" +checksum = "64ca1bc8749bd4cf37b5ce386cc146580777b4e8572c7b97baf22c83f444bee9" dependencies = [ "ring", "rustls-pki-types", @@ -745,11 +746,11 @@ checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" [[package]] name = "schannel" -version = "0.1.23" +version = "0.1.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fbc91545643bcf3a0bbb6569265615222618bdf33ce4ffbbd13c4bbd4c093534" +checksum = "e9aaafd5a2b6e3d657ff009d82fbd630b6bd54dd4eb06f21693925cdf80f9b8b" dependencies = [ - "windows-sys 0.52.0", + "windows-sys 0.59.0", ] [[package]] @@ -777,18 +778,18 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.209" +version = "1.0.210" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "99fce0ffe7310761ca6bf9faf5115afbc19688edd00171d81b1bb1b116c63e09" +checksum = "c8e3592472072e6e22e0a54d5904d9febf8508f65fb8552499a1abc7d1078c3a" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.209" +version = "1.0.210" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a5831b979fd7b5439637af1752d535ff49f4860c0f341d1baeb6faf0f4242170" +checksum = "243902eda00fad750862fc144cea25caca5e20d615af0a81bee94ca738f1df1f" dependencies = [ "proc-macro2", "quote", @@ -797,9 +798,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.127" +version = "1.0.128" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8043c06d9f82bd7271361ed64f415fe5e12a77fdb52e573e7f06a516dea329ad" +checksum = "6ff5456707a1de34e7e37f2a6fd3d3f808c318259cbd01ab6377795054b483d8" dependencies = [ "itoa", "memchr", @@ -819,6 +820,19 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_yaml" +version = "0.9.34+deprecated" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a8b1a1a2ebf674015cc02edccce75287f1a0130d394307b36743c2f5d504b47" +dependencies = [ + "indexmap", + "itoa", + "ryu", + "serde", + "unsafe-libyaml", +] + [[package]] name = "shlex" version = "1.3.0" @@ -969,9 +983,9 @@ dependencies = [ [[package]] name = "tokio-util" -version = "0.7.11" +version = "0.7.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9cf6b47b3771c49ac75ad09a6162f53ad4b8088b76ac60e8ec1455b31a189fe1" +checksum = "61e7c3654c13bcd040d4a03abee2c75b1d14a37b423cf5a813ceae1cc903ec6a" dependencies = [ "bytes", "futures-core", @@ -1049,19 +1063,25 @@ checksum = "08f95100a766bf4f8f28f90d77e0a5461bbdb219042e7679bebe79004fed8d75" [[package]] name = "unicode-ident" -version = "1.0.12" +version = "1.0.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" +checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe" [[package]] name = "unicode-normalization" -version = "0.1.23" +version = "0.1.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a56d1686db2308d901306f92a263857ef59ea39678a5458e7cb17f01415101f5" +checksum = "5033c97c4262335cded6d6fc3e5c18ab755e1a3dc96376350f3d8e9f009ad956" dependencies = [ "tinyvec", ] +[[package]] +name = "unsafe-libyaml" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "673aac59facbab8a9007c7f6108d11f63b603f7cabff99fabf650fea5c32b861" + [[package]] name = "untrusted" version = "0.9.0" diff --git a/public_types/Cargo.toml b/public_types/Cargo.toml index ccbd220d..9e334a78 100644 --- a/public_types/Cargo.toml +++ b/public_types/Cargo.toml @@ -6,3 +6,4 @@ edition = "2021" [dependencies] serde = { version = "1.0", features = ["derive"] } open-message-format-embeddings = { path = "../open-message-format/clients/omf-embeddings-rust" } +serde_yaml = "0.9.34" diff --git a/public_types/src/configuration.rs b/public_types/src/configuration.rs index eb71f3ae..663455e3 100644 --- a/public_types/src/configuration.rs +++ b/public_types/src/configuration.rs @@ -7,11 +7,23 @@ pub struct Configuration { pub timeout_ms: u64, pub embedding_provider: EmbeddingProviver, pub llm_providers: Vec, + pub prompt_guards: Option, pub system_prompt: Option, pub prompt_targets: Vec, pub ratelimits: Option>, } +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct PromptGuard { + pub input_guard: Vec, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct InputGuard { + pub name: String, + pub on_exception_message: String, +} + #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Ratelimit { pub provider: String, @@ -126,12 +138,18 @@ system_prompt: | - Use farenheight for temperature - Use miles per hour for wind speed +prompt_guards: + input_guard: + - name: jailbreak + on_exception_message: Looks like you are curious about my abilities… + - name: toxic + on_exception_message: Looks like you are curious about my abilities… + prompt_targets: - type: function_resolver name: weather_forecast - few_shot_examples: - - what is the weather in New York? + description: Get the weather forecast for a location endpoint: cluster: weatherhost path: /weather @@ -142,6 +160,7 @@ prompt_targets: - type: function_resolver name: weather_forecast_2 + description: Get the weather forecast for a location few_shot_examples: - what is the weather in New York? endpoint: @@ -162,6 +181,7 @@ ratelimits: #[test] fn test_deserialize_configuration() { - let _: super::Configuration = serde_yaml::from_str(CONFIGURATION).unwrap(); + let c: super::Configuration = serde_yaml::from_str(CONFIGURATION).unwrap(); + assert_eq!(c.prompt_guards.unwrap().input_guard.len(), 2); } }