Skip to content

Commit

Permalink
Add Support for Configuration
Browse files Browse the repository at this point in the history
This commit adds support for reading configuration values from a YAML
file instead of using hard-coded values. This allows for greater
flexibility and easier deployment of the application, as the
configuration can be customized for each environment.
  • Loading branch information
PatWie committed Aug 9, 2024
1 parent eecdc0c commit b9f6be6
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 19 deletions.
5 changes: 5 additions & 0 deletions config/server_config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
model:
bedrock:
model_id: anthropic.claude-3-haiku-20240307-v1:0
region: us-east-1
aws_profile: my-aws-bedrock
58 changes: 58 additions & 0 deletions src/configuration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use serde::{Deserialize, Serialize};

use std::{
env, fs,
path::{Path, PathBuf},
};

#[derive(Debug, Deserialize, Serialize)]
pub struct PolyglotConfig {
pub model: ModelConfig,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct ModelConfig {
pub bedrock: BedrockConfig,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct BedrockConfig {
pub model_id: String,
pub region: String,
pub aws_profile: String,
}

impl Default for PolyglotConfig {
fn default() -> Self {
Self {
model: ModelConfig {
bedrock: BedrockConfig {
model_id: "anthropic.claude-3-haiku-20240307-v1:0".to_string(),
region: "us-east-1".to_string(),
aws_profile: "my-aws-bedrock".to_string(),
},
},
}
}
}

impl PolyglotConfig {
pub fn default_file_path() -> PathBuf {
let home_dir = env::var("HOME").unwrap_or_else(|_| ".".to_string());
let config_path = PathBuf::from(home_dir).join("./config/polyglot_ls.yaml");
config_path
}

pub fn try_read_from_file<P: AsRef<Path>>(config_path: P) -> anyhow::Result<Self> {
if config_path.as_ref().exists() {
match fs::read_to_string(&config_path) {
Ok(config_data) => {
let cfg: PolyglotConfig = serde_yaml::from_str(&config_data)?;
cfg
}
Err(err) => anyhow::bail!(err),
};
}
anyhow::bail!("path does not exists")
}
}
33 changes: 25 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use std::path::{Path, PathBuf};
use std::sync::{Arc, RwLock};

pub mod code_action_providers;
pub mod configuration;
pub mod nonsense;
pub mod prompt_handlers;

Expand Down Expand Up @@ -344,24 +345,42 @@ struct Args {
#[arg(long)]
bind: Option<u16>, // Option to allow it to be optional

/// LSP server will read input from stdin
/// LSP server will read input from stdin and reply in stdout
#[arg(long)]
stdio: bool, // Just a flag, no value needed

/// Path to polyglot configuration YAML file
#[arg(long)]
polyglot_config_path: Option<String>,
}

#[tokio::main]
async fn main() {
let args = Args::parse();
#[cfg(feature = "runtime-agnostic")]
use tokio_util::compat::{TokioAsyncReadCompatExt, TokioAsyncWriteCompatExt};

let home_dir = env::var("HOME").expect("Failed to get home directory");
let config_base_dir = Path::new(&home_dir).join(".config").join("polyglot_ls");

let polyglot_config_path = args.polyglot_config_path.unwrap_or(
config_base_dir
.join("server_config.yaml")
.to_string_lossy()
.to_string(),
);
let polyglot_config = configuration::PolyglotConfig::try_read_from_file(&polyglot_config_path)
.unwrap_or_default();

tracing_subscriber::fmt().init();
//log::info!("Start");
let prompt_handler = Arc::new(BedrockConverse::new().await.unwrap());
let prompt_handler = Arc::new(
BedrockConverse::new(&polyglot_config.model.bedrock)
.await
.unwrap(),
);
let mut providers: HashMap<String, Vec<Box<dyn ActionProvider>>> = Default::default();

let home_dir = env::var("HOME").expect("Failed to get home directory");
let config_base_dir = Path::new(&home_dir).join(".config").join("polyglot_ls");

//log::info!("Processing config-dir: {:?}", config_dir);
for language in SUPPORTED_LANGUAGES {
let config_dir = config_base_dir.join("code_actions").join(language);
Expand All @@ -381,7 +400,7 @@ async fn main() {
)));
}
}
Err(e) => {
Err(_e) => {
//log::warn!("Cannot read {:?} because of {}", &config_path, e);
}
};
Expand All @@ -407,8 +426,6 @@ async fn main() {
indexed_text: Arc::new(RwLock::new(nonsense::IndexedText::new("".to_owned()))),
});

let args = Args::parse();

if let Some(port) = args.socket {
// If no argument is supplied (args is just the program name), then
// we presume that the client has opened the TCP port and is waiting
Expand Down
24 changes: 13 additions & 11 deletions src/prompt_handlers/claude.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,39 @@
use aws_config::BehaviorVersion;
use aws_config::{BehaviorVersion, Region};
use aws_sdk_bedrockruntime::{
operation::converse::{ConverseError, ConverseOutput},
types::{ContentBlock, ConversationRole, Message},
Client,
};

use crate::configuration::BedrockConfig;

use super::traits::PromptHandler;

// Set the model ID, e.g., Claude 3 Haiku.
const MODEL_ID: &str = "anthropic.claude-3-haiku-20240307-v1:0";
const CLAUDE_REGION: &str = "us-east-1";
const AWS_PROFILE: &str = "my-aws-bedrock";
// const MODEL_ID: &str = "anthropic.claude-3-haiku-20240307-v1:0";
// const CLAUDE_REGION: &str = "us-east-1";
// const AWS_PROFILE: &str = "my-aws-bedrock";
// Start a conversation with the user message.

#[derive(Debug)]
pub struct BedrockConverse {
client: Client,
model_id: &'static str,
model_id: String,
}

impl BedrockConverse {
pub async fn new() -> Result<Self, BedrockConverseError> {
pub async fn new(config: &BedrockConfig) -> Result<Self, BedrockConverseError> {
let sdk_config = aws_config::defaults(BehaviorVersion::latest())
.region(CLAUDE_REGION)
.profile_name(AWS_PROFILE)
.region(Region::new(config.region.clone()))
.profile_name(config.aws_profile.clone())
.load()
.await;

let client = Client::new(&sdk_config);

Ok(BedrockConverse {
client,
model_id: MODEL_ID,
model_id: config.model_id.clone(),
})
}
}
Expand All @@ -43,7 +45,7 @@ impl PromptHandler for BedrockConverse {
let response = self
.client
.converse()
.model_id(self.model_id)
.model_id(&self.model_id)
.messages(
Message::builder()
// .role(ConversationRole::Assistant)
Expand Down Expand Up @@ -71,7 +73,7 @@ pub struct BedrockConverseError(String);

impl std::fmt::Display for BedrockConverseError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Can't invoke '{}'. Reason: {}", MODEL_ID, self.0)
write!(f, "Can't invoke model. Reason: {}", self.0)
}
}

Expand Down

0 comments on commit b9f6be6

Please sign in to comment.