Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: cleanup unused code
Browse files Browse the repository at this point in the history
McPatate committed Feb 19, 2024

Verified

This commit was signed with the committer’s verified signature.
McPatate Luc Georges
1 parent fe1f6aa commit ef08eb9
Showing 3 changed files with 29 additions and 70 deletions.
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};
@@ -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:?}");
6 changes: 3 additions & 3 deletions crates/llm-ls/src/error.rs
Original file line number Diff line number Diff line change
@@ -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}")]
@@ -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}")]
@@ -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}")]
66 changes: 0 additions & 66 deletions crates/llm-ls/src/main.rs
Original file line number Diff line number Diff line change
@@ -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,

0 comments on commit ef08eb9

Please sign in to comment.