Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: dynamic pipeline routing #4

Merged
merged 3 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ axum-extra = "0.9.4"
tracing = "0.1"
tracing-subscriber = "0.3"
serde_yaml = "0.9"
tower = "0.5.1"
tower = { version = "0.5.1", features = ["full"]}
anyhow = "1.0.92"
tower-http = { version = "0.6.1", features = ["full"]}
uuid = "1.11.0"
Expand Down
1 change: 0 additions & 1 deletion config-example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ pipelines:
- tracing:
enabled: true
endpoint: "https://api.traceloop.com/v1/traces"
api_key: "<your-traceloop-api-key>"
- model-router:
models:
- gpt-4o-azure
Expand Down
14 changes: 3 additions & 11 deletions src/ai_models/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use crate::models::chat::{ChatCompletionRequest, ChatCompletionResponse};
use crate::models::completion::{CompletionRequest, CompletionResponse};
use crate::models::embeddings::{EmbeddingsRequest, EmbeddingsResponse};
use crate::providers::provider::Provider;
use crate::state::AppState;
use axum::http::StatusCode;
use std::sync::Arc;

Expand All @@ -17,33 +16,26 @@ pub struct ModelInstance {
impl ModelInstance {
pub async fn chat_completions(
&self,
state: Arc<AppState>,
mut payload: ChatCompletionRequest,
) -> Result<ChatCompletionResponse, StatusCode> {
payload.model = self.model_type.clone();
self.provider
.chat_completions(state, payload, &self.config)
.await
self.provider.chat_completions(payload, &self.config).await
}

pub async fn completions(
&self,
state: Arc<AppState>,
mut payload: CompletionRequest,
) -> Result<CompletionResponse, StatusCode> {
payload.model = self.model_type.clone();

self.provider
.completions(state, payload, &self.config)
.await
self.provider.completions(payload, &self.config).await
}

pub async fn embeddings(
&self,
state: Arc<AppState>,
mut payload: EmbeddingsRequest,
) -> Result<EmbeddingsResponse, StatusCode> {
payload.model = self.model_type.clone();
self.provider.embeddings(state, payload, &self.config).await
self.provider.embeddings(payload, &self.config).await
}
}
1 change: 1 addition & 0 deletions src/ai_models/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use super::instance::ModelInstance;
use crate::config::models::ModelConfig;
use crate::providers::registry::ProviderRegistry;

#[derive(Clone)]
pub struct ModelRegistry {
models: HashMap<String, Arc<ModelInstance>>,
}
Expand Down
30 changes: 9 additions & 21 deletions src/config/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,38 +39,26 @@ pub enum PipelineType {
pub struct Pipeline {
pub name: String,
pub r#type: PipelineType,
#[serde(with = "serde_yaml::with::singleton_map_recursive")]
pub plugins: Vec<PluginConfig>,
}

#[derive(Debug, Deserialize, Serialize, Clone)]
#[serde(untagged)]
#[serde(rename_all = "kebab-case")]
pub enum PluginConfig {
Logging {
logging: LoggingConfig,
#[serde(default = "default_log_level")]
level: String,
},
Tracing {
tracing: TracingConfig,
endpoint: String,
api_key: String,
},
ModelRouter {
#[serde(rename = "model-router")]
model_router: ModelRouterConfig,
models: Vec<String>,
},
}

#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct LoggingConfig {
pub enabled: bool,
pub level: String,
}

#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct TracingConfig {
pub enabled: bool,
pub endpoint: String,
pub api_key: String,
}

#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct ModelRouterConfig {
pub models: Vec<String>,
fn default_log_level() -> String {
"warning".to_string()
}
22 changes: 0 additions & 22 deletions src/handlers/chat.rs

This file was deleted.

20 changes: 0 additions & 20 deletions src/handlers/completion.rs

This file was deleted.

21 changes: 0 additions & 21 deletions src/handlers/embeddings.rs

This file was deleted.

3 changes: 0 additions & 3 deletions src/handlers/mod.rs

This file was deleted.

1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
pub mod ai_models;
pub mod config;
pub mod handlers;
pub mod models;
pub mod pipelines;
pub mod providers;
Expand Down
4 changes: 1 addition & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use gateway::{config::lib::load_config, routes, state::AppState};
use std::sync::Arc;
use tracing::info;
use tracing_subscriber::fmt::Subscriber;

#[tokio::main]
async fn main() -> Result<(), anyhow::Error> {
Subscriber::builder().init();
tracing_subscriber::fmt::init();

info!("Starting the application...");

Expand All @@ -21,7 +20,6 @@ async fn main() -> Result<(), anyhow::Error> {
.unwrap();

info!("Server is running on port {}", port);

axum::serve(listener, app).await.unwrap();

Ok(())
Expand Down
116 changes: 87 additions & 29 deletions src/pipelines/pipeline.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,93 @@
use crate::config::models::{Pipeline, PipelineType};
use axum::http::HeaderMap;

pub fn select_pipeline<'a>(
pipelines: &'a [Pipeline],
pipeline_type: PipelineType,
headers: &HeaderMap,
) -> Option<&'a Pipeline> {
// Filter pipelines by type
let matching_pipelines: Vec<&Pipeline> = pipelines
.iter()
.filter(|p| p.r#type == pipeline_type)
.collect();

if matching_pipelines.is_empty() {
return None;
use std::sync::Arc;

use crate::config::models::PipelineType;
use crate::models::completion::{CompletionRequest, CompletionResponse};
use crate::models::embeddings::{EmbeddingsRequest, EmbeddingsResponse};
use crate::{
ai_models::registry::ModelRegistry,
config::models::{Pipeline, PluginConfig},
models::chat::{ChatCompletionRequest, ChatCompletionResponse},
};
use axum::{extract::State, http::StatusCode, routing::post, Json, Router};
use tower_http::trace::TraceLayer;

pub fn create_pipeline(pipeline: &Pipeline, model_registry: &ModelRegistry) -> Router {
let mut router = Router::new();

for plugin in pipeline.plugins.clone() {
router = match plugin {
PluginConfig::Tracing { .. } => router.layer(TraceLayer::new_for_http()),
PluginConfig::ModelRouter { models } => match pipeline.r#type {
PipelineType::Chat => router.route(
"/chat/completions",
post(move |state, payload| chat_completions(state, payload, models)),
),
PipelineType::Completion => router.route(
"/completions",
post(move |state, payload| completions(state, payload, models)),
),
PipelineType::Embeddings => router.route(
"/embeddings",
post(move |state, payload| embeddings(state, payload, models)),
),
},
_ => router,
};
}

return router.with_state(Arc::new(model_registry.clone()));
}

pub async fn chat_completions(
State(model_registry): State<Arc<ModelRegistry>>,
Json(payload): Json<ChatCompletionRequest>,
model_keys: Vec<String>,
) -> Result<Json<ChatCompletionResponse>, StatusCode> {
for model_key in model_keys {
let model = model_registry.get(&model_key).unwrap();

if payload.model == model.model_type {
let response = model.chat_completions(payload.clone()).await.unwrap();

return Ok(Json(response));
}
}

if matching_pipelines.len() == 1 {
return Some(matching_pipelines[0]);
Err(StatusCode::SERVICE_UNAVAILABLE)
}

pub async fn completions(
State(model_registry): State<Arc<ModelRegistry>>,
Json(payload): Json<CompletionRequest>,
model_keys: Vec<String>,
) -> Result<Json<CompletionResponse>, StatusCode> {
for model_key in model_keys {
let model = model_registry.get(&model_key).unwrap();

if payload.model == model.model_type {
let response = model.completions(payload.clone()).await.unwrap();

return Ok(Json(response));
}
}

// Check for pipeline specification in headers
if let Some(pipeline_name) = headers
.get("x-traceloop-pipeline")
.and_then(|h| h.to_str().ok())
{
matching_pipelines
.into_iter()
.find(|p| p.name == pipeline_name)
} else {
// Default to pipeline named "default"
matching_pipelines.into_iter().find(|p| p.name == "default")
Err(StatusCode::SERVICE_UNAVAILABLE)
}

pub async fn embeddings(
State(model_registry): State<Arc<ModelRegistry>>,
Json(payload): Json<EmbeddingsRequest>,
model_keys: Vec<String>,
) -> Result<Json<EmbeddingsResponse>, StatusCode> {
for model_key in model_keys {
let model = model_registry.get(&model_key).unwrap();

if payload.model == model.model_type {
let response = model.embeddings(payload.clone()).await.unwrap();

return Ok(Json(response));
}
}

Err(StatusCode::SERVICE_UNAVAILABLE)
}
2 changes: 1 addition & 1 deletion src/pipelines/services/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pub mod model_router;

Loading