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

refactor: cleanup unused code #82

Merged
merged 1 commit into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
27 changes: 26 additions & 1 deletion crates/llm-ls/src/backend.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{APIError, APIResponse, Generation, NAME, VERSION};
use super::{Generation, NAME, VERSION};
use custom_types::llm_ls::{Backend, Ide};
use reqwest::header::{HeaderMap, HeaderValue, AUTHORIZATION, USER_AGENT};
use serde::{Deserialize, Serialize};
Expand All @@ -7,6 +7,31 @@ use std::fmt::Display;

use crate::error::{Error, Result};

#[derive(Debug, Deserialize)]
pub struct APIError {
error: String,
}

impl std::error::Error for APIError {
fn description(&self) -> &str {
&self.error
}
}

impl Display for APIError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.error)
}
}

#[derive(Debug, Deserialize)]
#[serde(untagged)]
pub enum APIResponse {
Generation(Generation),
Generations(Vec<Generation>),
Error(APIError),
}

fn build_tgi_headers(api_token: Option<&String>, ide: Ide) -> Result<HeaderMap> {
let mut headers = HeaderMap::new();
let user_agent = format!("{NAME}/{VERSION}; rust/unknown; ide/{ide:?}");
Expand Down
6 changes: 3 additions & 3 deletions crates/llm-ls/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub enum Error {
#[error("io error: {0}")]
Io(#[from] std::io::Error),
#[error("inference api error: {0}")]
InferenceApi(crate::APIError),
InferenceApi(crate::backend::APIError),
#[error("You are attempting to parse a result in the API inference format when using the `tgi` backend")]
InvalidBackend,
#[error("invalid header value: {0}")]
Expand All @@ -30,7 +30,7 @@ pub enum Error {
#[error("invalid tokenizer path")]
InvalidTokenizerPath,
#[error("ollama error: {0}")]
Ollama(crate::APIError),
Ollama(crate::backend::APIError),
#[error("openai error: {0}")]
OpenAI(crate::backend::OpenAIError),
#[error("index out of bounds: {0}")]
Expand All @@ -44,7 +44,7 @@ pub enum Error {
#[error("serde json error: {0}")]
SerdeJson(#[from] serde_json::Error),
#[error("tgi error: {0}")]
Tgi(crate::APIError),
Tgi(crate::backend::APIError),
#[error("tree-sitter language error: {0}")]
TreeSitterLanguage(#[from] tree_sitter::LanguageError),
#[error("tokenizer error: {0}")]
Expand Down
66 changes: 0 additions & 66 deletions crates/llm-ls/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,77 +121,11 @@ fn should_complete(document: &Document, position: Position) -> Result<Completion
}
}

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct RequestParams {
max_new_tokens: u32,
temperature: f32,
do_sample: bool,
top_p: f32,
stop_tokens: Option<Vec<String>>,
}

#[derive(Debug, Serialize)]
struct APIParams {
max_new_tokens: u32,
temperature: f32,
do_sample: bool,
top_p: f32,
#[allow(dead_code)]
#[serde(skip_serializing)]
stop: Option<Vec<String>>,
return_full_text: bool,
}

impl From<RequestParams> for APIParams {
fn from(params: RequestParams) -> Self {
Self {
max_new_tokens: params.max_new_tokens,
temperature: params.temperature,
do_sample: params.do_sample,
top_p: params.top_p,
stop: params.stop_tokens,
return_full_text: false,
}
}
}

#[derive(Serialize)]
struct APIRequest {
inputs: String,
parameters: APIParams,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Generation {
generated_text: String,
}

#[derive(Debug, Deserialize)]
pub struct APIError {
error: String,
}

impl std::error::Error for APIError {
fn description(&self) -> &str {
&self.error
}
}

impl Display for APIError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.error)
}
}

#[derive(Debug, Deserialize)]
#[serde(untagged)]
pub enum APIResponse {
Generation(Generation),
Generations(Vec<Generation>),
Error(APIError),
}

struct LlmService {
cache_dir: PathBuf,
client: Client,
Expand Down
Loading