Skip to content

Commit

Permalink
revert back to tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
neowu committed Jul 23, 2024
1 parent a97c426 commit ec50896
Show file tree
Hide file tree
Showing 12 changed files with 131 additions and 21 deletions.
110 changes: 109 additions & 1 deletion Cargo.lock

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

7 changes: 3 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,19 @@ name = "puppet"
version = "0.3.1"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
tracing = "0"
tracing-subscriber = "0"
clap = { version = "4", features = ["derive"] }
clap_complete = "4"
serde = { version = "1", features = ["derive", "rc"] }
serde_json = "1"
log = "0"
tokio = { version = "1", features = ["full"] }
reqwest = { version = "0", features = ["stream"] }
bytes = "1"
futures = "0"
rand = "0"
base64 = "0"
uuid = { version = "1", features = ["v4"] }
regex = "1"
glob = "0"
futures = "0"
10 changes: 5 additions & 5 deletions src/azure/chatgpt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ use bytes::Bytes;
use futures::AsyncBufReadExt;
use futures::StreamExt;
use futures::TryStreamExt;
use log::info;
use reqwest::Response;
use tokio::fs;
use tracing::info;

use super::chatgpt_api::ChatCompletionChoice;
use super::chatgpt_api::ChatResponse;
Expand Down Expand Up @@ -134,8 +134,8 @@ impl ChatGPT {
for result in results {
self.add_message(ChatRequestMessage::new_function_response(result.id, json::to_json(&result.value)?));
}
} else if let Some(content) = message.content {
self.add_message(ChatRequestMessage::new_message(Role::Assistant, content));
} else {
self.add_message(ChatRequestMessage::new_message(Role::Assistant, message.content.unwrap()));
return Ok(());
}
}
Expand Down Expand Up @@ -168,7 +168,8 @@ impl ChatGPT {
let response = request.send().await?;
let status = response.status();
if status != 200 {
info!("body={}", str::from_utf8(&body)?);
let body = str::from_utf8(&body)?;
info!("body={}", body);
let response_text = response.text().await?;
return Err(Exception::ExternalError(format!(
"failed to call azure api, status={status}, response={response_text}"
Expand Down Expand Up @@ -207,7 +208,6 @@ async fn read_sse_response(http_response: Response) -> Result<ChatResponse, Exce
while let Some(line) = lines.next().await {
let line = line?;

// chatgpt always adds space after data:
if let Some(data) = line.strip_prefix("data: ") {
if data == "[DONE]" {
break;
Expand Down
2 changes: 1 addition & 1 deletion src/command/complete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ use clap::Args;
use glob::glob;
use glob::GlobError;
use glob::PatternError;
use log::info;
use regex::Regex;
use tokio::fs;
use tokio::io::AsyncBufReadExt;
use tokio::io::AsyncWriteExt;
use tokio::io::BufReader;
use tracing::info;

use crate::llm;
use crate::llm::ChatOption;
Expand Down
2 changes: 1 addition & 1 deletion src/command/speak.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ use std::path::PathBuf;

use clap::arg;
use clap::Args;
use log::info;
use tokio::fs;
use tokio::io::stdin;
use tokio::io::AsyncReadExt;
use tokio::process::Command;
use tracing::info;
use uuid::Uuid;

use crate::tts;
Expand Down
10 changes: 6 additions & 4 deletions src/gcloud/gemini.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@ use bytes::Bytes;
use futures::AsyncBufReadExt;
use futures::StreamExt;
use futures::TryStreamExt;
use log::info;
use reqwest::Response;
use tokio::fs;
use tracing::info;

use super::gemini_api::Content;
use super::gemini_api::GenerateContentResponse;
use super::gemini_api::GenerationConfig;
use super::gemini_api::InlineData;
use super::gemini_api::StreamGenerateContent;
use super::gemini_api::Tool;
use super::token;
use crate::gcloud::gemini_api::Candidate;
use crate::gcloud::gemini_api::GenerateContentStreamResponse;
use crate::gcloud::gemini_api::Role;
Expand Down Expand Up @@ -142,7 +143,7 @@ impl Gemini {
let body = Bytes::from(body);
let response = http_client::http_client()
.post(&self.url)
// .bearer_auth(token())
.bearer_auth(token())
.header("Content-Type", "application/json")
.header("Accept", "application/json")
.body(body.clone())
Expand All @@ -151,7 +152,8 @@ impl Gemini {

let status = response.status();
if status != 200 {
info!("body={}", str::from_utf8(&body)?);
let body = str::from_utf8(&body)?;
info!("body={}", body);
let response_text = response.text().await?;
return Err(Exception::ExternalError(format!(
"failed to call gcloud api, status={status}, response={response_text}"
Expand Down Expand Up @@ -183,7 +185,7 @@ async fn read_sse_response(http_response: Response) -> Result<GenerateContentRes
let mut lines = reader.lines();
while let Some(line) = lines.next().await {
let line = line?;
if let Some(data) = line.strip_prefix("data:") {
if let Some(data) = line.strip_prefix("data: ") {
let stream_response: GenerateContentStreamResponse = json::from_json(data)?;
if let Some(value) = stream_response.usage_metadata {
response.usage_metadata = value;
Expand Down
2 changes: 1 addition & 1 deletion src/gcloud/tts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use std::borrow::Cow;
use base64::prelude::BASE64_STANDARD;
use base64::DecodeError;
use base64::Engine;
use log::info;
use serde::Deserialize;
use serde::Serialize;
use tracing::info;

use super::token;
use crate::util::exception::Exception;
Expand Down
2 changes: 1 addition & 1 deletion src/llm.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::path::Path;

use log::info;
use tokio::fs;
use tracing::info;

use crate::azure::chatgpt::ChatGPT;
use crate::gcloud::gemini::Gemini;
Expand Down
2 changes: 1 addition & 1 deletion src/llm/config.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::collections::HashMap;

use log::info;
use rand::Rng;
use serde::Deserialize;
use serde_json::json;
use tracing::info;

use super::function::FunctionImplementations;
use crate::azure::chatgpt::ChatGPT;
Expand Down
2 changes: 1 addition & 1 deletion src/llm/function.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::collections::HashMap;
use std::sync::Arc;

use log::info;
use serde::Serialize;
use tokio::task::JoinSet;
use tracing::info;

use crate::util::exception::Exception;

Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub enum Command {

#[tokio::main]
async fn main() -> Result<(), Exception> {
tracing_subscriber::fmt().with_thread_ids(true).init();
let cli = Cli::parse();
match cli.command {
Command::Chat(command) => command.execute().await,
Expand Down
2 changes: 1 addition & 1 deletion src/tts.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::collections::HashMap;
use std::path::Path;

use log::info;
use serde::Deserialize;
use tokio::fs;
use tracing::info;

use crate::azure::tts::AzureTTS;
use crate::gcloud::tts::GCloudTTS;
Expand Down

0 comments on commit ec50896

Please sign in to comment.