Skip to content

Commit

Permalink
Merge pull request #39 from B-urb/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
B-urb authored May 7, 2024
2 parents a2f5309 + d31681b commit 9d6c8a6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,15 @@ async fn process_documents(client: &Client, ollama: &Ollama, model: &str, base_u
match get_data_from_paperless(&client, &base_url, filter).await {
Ok(data) => {
for document in data {
slog_scope::info!("Generate Response with LLM {}", "model");
let res = generate_response(ollama, &model.to_string(), &prompt_base.to_string(), &document).await?;
if let Some(json_str) = extract_json_object(&res.response) {
match serde_json::from_str(&json_str) {
Ok(json) => update_document_fields(client, document.id, &fields, &json, base_url).await?,
Err(e) => slog_scope::error!("Error parsing JSON: {}", e.to_string()),
Err(e) => {
slog_scope::error!("Error parsing llm response json {}", e.to_string());
slog_scope::debug!("JSON String was: {}",&json_str);
},
}
} else {
slog_scope::error!("No JSON object found in the response{}", "!");
Expand Down
10 changes: 8 additions & 2 deletions src/paperless.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub async fn get_data_from_paperless(
// Read token from environment
//Define filter string
let filter = filter;

slog_scope::info!("Retrieve Documents from paperless at: {}, with query: {}",url, filter);
let response = client.get(format!("{}/api/documents/?query={}", url, filter)).send().await?;


Expand All @@ -34,7 +34,10 @@ pub async fn get_data_from_paperless(
// Parse the JSON string into the Response struct
let data: std::result::Result<Response<Document>, _> = serde_json::from_str(json);
match data {
Ok(data) => Ok(data.results),
Ok(data) => {
slog_scope::info!("Successfully retrieved {} Documents", data.results.len());
Ok(data.results)
},
Err(e) => {
let column = e.column();
let start = (column as isize - 30).max(0) as usize;
Expand All @@ -56,6 +59,7 @@ pub async fn get_data_from_paperless(
client: &Client,
base_url: &str,
) -> std::result::Result<Vec<Field>, Box<dyn std::error::Error>> {
slog_scope::info!("Fetching custom fields from paperless at {}", base_url);
let res = client
.get(format!("{}/api/custom_fields/", base_url))
.send()
Expand Down Expand Up @@ -127,12 +131,14 @@ pub async fn get_data_from_paperless(
payload.insert("title".to_string(), serde_json::json!(value));
}
let url = format!("{}/api/documents/{}/", base_url, document_id);
slog_scope::info!("Updating document with ID: {}", document_id);
let res = client.patch(&url).json(&payload).send().await?;
let response_result = res.error_for_status();
match response_result {
Ok(data) => {
let body = data.text().await?;
slog_scope::debug!("{}", body);
slog_scope::info!("Document with ID: {} successfully updated", document_id);
Ok(())
}
Err(e) => {
Expand Down

0 comments on commit 9d6c8a6

Please sign in to comment.