Skip to content

Commit

Permalink
add text_to_speech & change field name
Browse files Browse the repository at this point in the history
  • Loading branch information
sunjiayu committed Jul 26, 2024
1 parent 6a59110 commit 456c58e
Show file tree
Hide file tree
Showing 18 changed files with 135 additions and 69 deletions.
8 changes: 6 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@ jobs:
- name: checkout repository
uses: actions/checkout@v4

- name: Build
- name: Replace secrets for auth.yaml
run: |
sed -i "s/OPENAI_API_KEY/${{ secrets.OPENAI_API_KEY }}/g" ./conf/auth.yaml
sed -i "s/QWEN_API_KEY/${{ secrets.QWEN_API_KEY }}/g" ./conf/auth.yaml
sed -i "s/OSS_ACCESS_ENDPOINT/${{ secrets.OSS_ACCESS_ENDPOINT }}/g" ./conf/auth.yaml
sed -i "s/OSS_ACCESS_KEY_ID/${{ secrets.OSS_ACCESS_KEY_ID }}/g" ./conf/auth.yaml
sed -i "s/OSS_ACCESS_KEY_SECRET/${{ secrets.OSS_ACCESS_KEY_SECRET }}/g" ./conf/auth.yaml
sed -i "s/OPENAI_API_KEY/${{ secrets.OPENAI_API_KEY }}/g" ./conf/auth.yaml
- name: Build
run: |
bazel build --action_env=CC=clang-14 //...
mkdir -p output/bin output/conf
mv ./bazel-bin/src/ielts_ai output/bin/
Expand Down
12 changes: 6 additions & 6 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ http_archive(
http_archive(
name = "com_github_d7ead_liboai",
build_file = "//bazel:liboai.BUILD",
sha256 = "71d1c7fe55fdec465e3f1ec376104c1bac92e014f5984a9024749a73e6a312ff",
strip_prefix = "liboai-1.0.7/liboai",
urls = ["https://github.com/pkusunjy/liboai/archive/v1.0.7.tar.gz"],
sha256 = "83108eb34c71b6bf0d18d8b7071a76a6c1609fea60f32afedaf7f69c95f0b827",
strip_prefix = "liboai-1.0.11/liboai",
urls = ["https://github.com/pkusunjy/liboai/archive/v1.0.11.tar.gz"],
)

http_archive(
name = "com_github_pkusunjy_openai_server_proto",
sha256 = "5c6cf3341f68cc9ac54534363fe2e0c923bfa22c1583a476934cd98d2e2f2540",
strip_prefix = "openai-server-proto-1.1.6",
urls = ["https://github.com/pkusunjy/openai-server-proto/archive/v1.1.6.tar.gz"],
sha256 = "d4d1e58c1f72e4ac93068e9f4bbc381d9a5ea1ddc37407635d175138b06ee92c",
strip_prefix = "openai-server-proto-1.1.13",
urls = ["https://github.com/pkusunjy/openai-server-proto/archive/v1.1.13.tar.gz"],
)

http_archive(
Expand Down
6 changes: 6 additions & 0 deletions conf/auth.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
# ai api keys
openai_api_key: OPENAI_API_KEY
qwen_api_key: QWEN_API_KEY
# ai url roots
openai_url_root: https://mikiai-tokyo-onctmshaci.ap-northeast-1.fcapp.run/v1
qwen_url_root: https://dashscope.aliyuncs.com/compatible-mode/v1
# aliyun object storage service
oss_endpoint: OSS_ACCESS_ENDPOINT
oss_access_key_id: OSS_ACCESS_KEY_ID
oss_access_key_secret: OSS_ACCESS_KEY_SECRET
8 changes: 0 additions & 8 deletions src/ielts_ai.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,6 @@ int32_t main(int32_t argc, char* argv[]) {
return 0;
}

std::string openai_api_key = token_instance.get_token_by_name("openai_api_key");
LOG(INFO) << "openai_api_key: " << openai_api_key;

if (!liboai::Authorization::Authorizer().SetKey(openai_api_key)) {
LOG(WARNING) << "OPENAI_API_KEY not found, server quit";
return 0;
}

grpc::EnableDefaultHealthCheckService(true);
grpc::reflection::InitProtoReflectionServerBuilderPlugin();

Expand Down
1 change: 1 addition & 0 deletions src/service/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ cc_library(
"//src/plugin:log_sink",
"//src/plugin:oss",
"//src/plugin:prompt",
"//src/plugin:token",
"@boost.scope_exit",
"@com_github_d7ead_liboai//:oai",
"@com_github_grpc_grpc//:grpc++",
Expand Down
60 changes: 59 additions & 1 deletion src/service/ielts_ai.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,26 @@
namespace chat_completion {

int32_t IeltsAI::initialize() {
auto& token_instance = plugin::TokenFactory::instance();
// audio auth
_audio_auth.SetKey(token_instance.get_token_by_name("openai_api_key"));
_chat_auth.SetKey(token_instance.get_token_by_name("qwen_api_key"));
// audio
_audio = std::make_unique<liboai::Audio>();
if (_audio == nullptr) {
LOG(WARNING) << "liboai audio ctor failed";
return -1;
}
_audio->SetAuth(_audio_auth);
_audio->UpdateOpenAIRoot(token_instance.get_token_by_name("openai_url_root"));
// chat
_chat_completion = std::make_unique<liboai::ChatCompletion>();
if (_chat_completion == nullptr) {
LOG(WARNING) << "liboai completion ctor failed";
return -1;
}
_chat_completion->SetAuth(_chat_auth);
_chat_completion->UpdateOpenAIRoot(token_instance.get_token_by_name("qwen_url_root"));
// aliyun oss
_oss = std::make_unique<plugin::OssClient>();
if (_oss == nullptr || _oss->initialize() != 0) {
Expand Down Expand Up @@ -65,12 +73,62 @@ grpc::Status IeltsAI::transcribe_judge(grpc::ServerContext* ctx, const ChatMessa
resp->set_content(transcribe_res);
// 4. TODO: delete aliyun oos
absl::Time step4 = absl::Now();
LOG(INFO) << "logid " << req->logid() << " uid " << req->uid()
LOG(INFO) << "logid " << req->logid() << " uid " << req->userid()
<< " get from oss cost: " << absl::ToDoubleMilliseconds(step2 - step1)
<< " transcribe cost: " << absl::ToDoubleMilliseconds(step3 - step2)
<< " unlink cost: " << absl::ToDoubleMilliseconds(step4 - step3)
<< ", total cost: " << absl::ToDoubleMilliseconds(step4 - step1);
return grpc::Status::OK;
}

grpc::Status IeltsAI::text_to_speech(grpc::ServerContext* ctx, const ChatMessage* req, ChatMessage* resp) {
if (_audio == nullptr) {
LOG(WARNING) << "text_to_speech not ready";
return grpc::Status(grpc::StatusCode::FAILED_PRECONDITION, "text_to_speech not ready");
}
// 1. call api
absl::Time step1 = absl::Now();
LOG(INFO) << "received stage:" << req->stage() << " text: " << req->content();
std::string voice = req->stage() == 1 ? "alloy" : "nova";
std::string input = req->content();

auto res = _audio->speech("tts-1", voice, input);

std::string local_filename = absl::StrFormat("text_to_speech_%s_%llu.mp3", req->userid(), req->logid());
{
std::ofstream ocout(local_filename, std::ios::binary);
ocout << res.content;
ocout.close();
}

BOOST_SCOPE_EXIT(&req, &local_filename) {
// delete audio file on disk
if (unlink(local_filename.c_str()) < 0) {
char buf[256];
strerror_r(errno, buf, 256);
LOG(WARNING) << "logid " << req->logid() << "unlink failed file: " << local_filename << ", errno: " << errno
<< ", errmsg: " << buf;
}
}
BOOST_SCOPE_EXIT_END
// 2. upload to oss
absl::Time step2 = absl::Now();
if (_oss->put_object(local_filename, local_filename) != 0) {
LOG(WARNING) << "OssClient put_object failed";
return grpc::Status(grpc::StatusCode::FAILED_PRECONDITION, "oss error");
}
// 3. response
absl::Time step3 = absl::Now();
LOG(INFO) << "logid " << req->logid() << " text_to_speech input: " << input << " result: " << local_filename;

resp->set_content(local_filename);
// 4. TODO: delete aliyun oos
absl::Time step4 = absl::Now();
LOG(INFO) << "logid " << req->logid() << " uid " << req->userid()
<< " text_to_speech cost: " << absl::ToDoubleMilliseconds(step2 - step1)
<< " upload_oss cost: " << absl::ToDoubleMilliseconds(step3 - step2)
<< ", total cost: " << absl::ToDoubleMilliseconds(step3 - step1);
return grpc::Status::OK;
}

} // namespace chat_completion
7 changes: 6 additions & 1 deletion src/service/ielts_ai.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
#include "chat_completion/chat_completion.grpc.pb.h"
#include "components/audio.h"
#include "components/chat.h"
#include "core/authorization.h"
#include "src/plugin/oss.h"
#include "src/plugin/prompt.h"
#include "src/plugin/token.h"

namespace chat_completion {

Expand All @@ -26,8 +28,9 @@ class IeltsAI final : public ChatService::Service {
IeltsAI() = default;
virtual ~IeltsAI() = default;
int32_t initialize();
// 音频转文字
// 音频文字互转
grpc::Status transcribe_judge(grpc::ServerContext*, const ChatMessage*, ChatMessage*) override;
grpc::Status text_to_speech(grpc::ServerContext*, const ChatMessage*, ChatMessage*) override;
// 雅思口语P1
grpc::Status ielts_speaking_p1_generate(grpc::ServerContext*, const ChatMessage*,
grpc::ServerWriter<ChatMessage>*) override;
Expand Down Expand Up @@ -100,6 +103,8 @@ class IeltsAI final : public ChatService::Service {
void stream_handler() {}

private:
liboai::Authorization _audio_auth{};
liboai::Authorization _chat_auth{};
std::unique_ptr<liboai::Audio> _audio{nullptr};
std::unique_ptr<liboai::ChatCompletion> _chat_completion{nullptr};
std::string _model{"qwen-plus"};
Expand Down
8 changes: 4 additions & 4 deletions src/service/ielts_speaking_p1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ grpc::Status IeltsAI::ielts_speaking_p1_generate(grpc::ServerContext* ctx, const
openai_resp.wait();

absl::Time step2 = absl::Now();
LOG(INFO) << "logid " << req->logid() << " uid " << req->uid() << " content " << req->content() << " total cost time "
<< absl::ToDoubleMilliseconds(step2 - step1);
LOG(INFO) << "logid " << req->logid() << " uid " << req->userid() << " content " << req->content()
<< " total cost time " << absl::ToDoubleMilliseconds(step2 - step1);
return grpc::Status::OK;
}

Expand Down Expand Up @@ -85,8 +85,8 @@ grpc::Status IeltsAI::ielts_speaking_p1_enrich(grpc::ServerContext* ctx, const C
openai_resp.wait();

absl::Time step2 = absl::Now();
LOG(INFO) << "logid " << req->logid() << " uid " << req->uid() << " content " << req->content() << " total cost time "
<< absl::ToDoubleMilliseconds(step2 - step1);
LOG(INFO) << "logid " << req->logid() << " uid " << req->userid() << " content " << req->content()
<< " total cost time " << absl::ToDoubleMilliseconds(step2 - step1);
return grpc::Status::OK;
}

Expand Down
12 changes: 6 additions & 6 deletions src/service/ielts_speaking_p2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ grpc::Status IeltsAI::ielts_speaking_p2_generate(grpc::ServerContext* ctx, const
openai_resp.wait();

absl::Time step2 = absl::Now();
LOG(INFO) << "logid " << req->logid() << " uid " << req->uid() << " content " << req->content() << " total cost time "
<< absl::ToDoubleMilliseconds(step2 - step1);
LOG(INFO) << "logid " << req->logid() << " uid " << req->userid() << " content " << req->content()
<< " total cost time " << absl::ToDoubleMilliseconds(step2 - step1);
return grpc::Status::OK;
}

Expand Down Expand Up @@ -85,8 +85,8 @@ grpc::Status IeltsAI::ielts_speaking_p2_enrich(grpc::ServerContext* ctx, const C
openai_resp.wait();

absl::Time step2 = absl::Now();
LOG(INFO) << "logid " << req->logid() << " uid " << req->uid() << " content " << req->content() << " total cost time "
<< absl::ToDoubleMilliseconds(step2 - step1);
LOG(INFO) << "logid " << req->logid() << " uid " << req->userid() << " content " << req->content()
<< " total cost time " << absl::ToDoubleMilliseconds(step2 - step1);
return grpc::Status::OK;
}

Expand Down Expand Up @@ -129,8 +129,8 @@ grpc::Status IeltsAI::ielts_speaking_p2_score(grpc::ServerContext* ctx, const Ch
openai_resp.wait();

absl::Time step2 = absl::Now();
LOG(INFO) << "logid " << req->logid() << " uid " << req->uid() << " content " << req->content() << " total cost time "
<< absl::ToDoubleMilliseconds(step2 - step1);
LOG(INFO) << "logid " << req->logid() << " uid " << req->userid() << " content " << req->content()
<< " total cost time " << absl::ToDoubleMilliseconds(step2 - step1);
return grpc::Status::OK;
}

Expand Down
8 changes: 4 additions & 4 deletions src/service/ielts_speaking_p3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ grpc::Status IeltsAI::ielts_speaking_p3_generate(grpc::ServerContext* ctx, const
openai_resp.wait();

absl::Time step2 = absl::Now();
LOG(INFO) << "logid " << req->logid() << " uid " << req->uid() << " content " << req->content() << " total cost time "
<< absl::ToDoubleMilliseconds(step2 - step1);
LOG(INFO) << "logid " << req->logid() << " uid " << req->userid() << " content " << req->content()
<< " total cost time " << absl::ToDoubleMilliseconds(step2 - step1);
return grpc::Status::OK;
}

Expand Down Expand Up @@ -85,8 +85,8 @@ grpc::Status IeltsAI::ielts_speaking_p3_enrich(grpc::ServerContext* ctx, const C
openai_resp.wait();

absl::Time step2 = absl::Now();
LOG(INFO) << "logid " << req->logid() << " uid " << req->uid() << " content " << req->content() << " total cost time "
<< absl::ToDoubleMilliseconds(step2 - step1);
LOG(INFO) << "logid " << req->logid() << " uid " << req->userid() << " content " << req->content()
<< " total cost time " << absl::ToDoubleMilliseconds(step2 - step1);
return grpc::Status::OK;
}

Expand Down
20 changes: 10 additions & 10 deletions src/service/ielts_words.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ grpc::Status IeltsAI::ielts_speaking_words_synonyms(grpc::ServerContext* ctx, co
openai_resp.wait();

absl::Time step2 = absl::Now();
LOG(INFO) << "logid " << req->logid() << " uid " << req->uid() << " content " << req->content() << " total cost time "
<< absl::ToDoubleMilliseconds(step2 - step1);
LOG(INFO) << "logid " << req->logid() << " uid " << req->userid() << " content " << req->content()
<< " total cost time " << absl::ToDoubleMilliseconds(step2 - step1);
return grpc::Status::OK;
}

Expand Down Expand Up @@ -83,8 +83,8 @@ grpc::Status IeltsAI::ielts_speaking_words_usage(grpc::ServerContext* ctx, const
openai_resp.wait();

absl::Time step2 = absl::Now();
LOG(INFO) << "logid " << req->logid() << " uid " << req->uid() << " content " << req->content() << " total cost time "
<< absl::ToDoubleMilliseconds(step2 - step1);
LOG(INFO) << "logid " << req->logid() << " uid " << req->userid() << " content " << req->content()
<< " total cost time " << absl::ToDoubleMilliseconds(step2 - step1);
return grpc::Status::OK;
}

Expand Down Expand Up @@ -127,8 +127,8 @@ grpc::Status IeltsAI::ielts_writing_words_theme(grpc::ServerContext* ctx, const
openai_resp.wait();

absl::Time step2 = absl::Now();
LOG(INFO) << "logid " << req->logid() << " uid " << req->uid() << " content " << req->content() << " total cost time "
<< absl::ToDoubleMilliseconds(step2 - step1);
LOG(INFO) << "logid " << req->logid() << " uid " << req->userid() << " content " << req->content()
<< " total cost time " << absl::ToDoubleMilliseconds(step2 - step1);
return grpc::Status::OK;
}

Expand Down Expand Up @@ -170,8 +170,8 @@ grpc::Status IeltsAI::ielts_writing_words_synonyms(grpc::ServerContext* ctx, con
openai_resp.wait();

absl::Time step2 = absl::Now();
LOG(INFO) << "logid " << req->logid() << " uid " << req->uid() << " content " << req->content() << " total cost time "
<< absl::ToDoubleMilliseconds(step2 - step1);
LOG(INFO) << "logid " << req->logid() << " uid " << req->userid() << " content " << req->content()
<< " total cost time " << absl::ToDoubleMilliseconds(step2 - step1);
return grpc::Status::OK;
}

Expand Down Expand Up @@ -213,8 +213,8 @@ grpc::Status IeltsAI::ielts_writing_words_combination(grpc::ServerContext* ctx,
openai_resp.wait();

absl::Time step2 = absl::Now();
LOG(INFO) << "logid " << req->logid() << " uid " << req->uid() << " content " << req->content() << " total cost time "
<< absl::ToDoubleMilliseconds(step2 - step1);
LOG(INFO) << "logid " << req->logid() << " uid " << req->userid() << " content " << req->content()
<< " total cost time " << absl::ToDoubleMilliseconds(step2 - step1);
return grpc::Status::OK;
}

Expand Down
4 changes: 2 additions & 2 deletions src/service/ielts_writing_t1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ grpc::Status IeltsAI::ielts_writing_t1_enrich(grpc::ServerContext* ctx, const Ch
openai_resp.wait();

absl::Time step2 = absl::Now();
LOG(INFO) << "logid " << req->logid() << " uid " << req->uid() << " content " << req->content()
LOG(INFO) << "logid " << req->logid() << " uid " << req->userid() << " content " << req->content()
<< ", enrich total cost time " << absl::ToDoubleMilliseconds(step2 - step1);
return grpc::Status::OK;
}
Expand Down Expand Up @@ -83,7 +83,7 @@ grpc::Status IeltsAI::ielts_writing_t1_score(grpc::ServerContext* ctx, const Cha
openai_resp.wait();

absl::Time step2 = absl::Now();
LOG(INFO) << "logid " << req->logid() << " uid " << req->uid() << " content " << req->content()
LOG(INFO) << "logid " << req->logid() << " uid " << req->userid() << " content " << req->content()
<< ", score total cost time " << absl::ToDoubleMilliseconds(step2 - step1);
return grpc::Status::OK;
}
Expand Down
6 changes: 3 additions & 3 deletions src/service/ielts_writing_t2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ grpc::Status IeltsAI::ielts_writing_t2_generate(grpc::ServerContext* ctx, const
openai_resp.wait();

absl::Time step2 = absl::Now();
LOG(INFO) << "logid " << req->logid() << " uid " << req->uid() << "generate cost time "
LOG(INFO) << "logid " << req->logid() << " uid " << req->userid() << "generate cost time "
<< absl::ToDoubleMilliseconds(step2 - step1);
return grpc::Status::OK;
}
Expand Down Expand Up @@ -84,7 +84,7 @@ grpc::Status IeltsAI::ielts_writing_t2_enrich(grpc::ServerContext* ctx, const Ch
openai_resp.wait();

absl::Time step2 = absl::Now();
LOG(INFO) << "logid " << req->logid() << " uid " << req->uid() << " content " << req->content()
LOG(INFO) << "logid " << req->logid() << " uid " << req->userid() << " content " << req->content()
<< ", enrich total cost time " << absl::ToDoubleMilliseconds(step2 - step1);
return grpc::Status::OK;
}
Expand Down Expand Up @@ -127,7 +127,7 @@ grpc::Status IeltsAI::ielts_writing_t2_score(grpc::ServerContext* ctx, const Cha
openai_resp.wait();

absl::Time step2 = absl::Now();
LOG(INFO) << "logid " << req->logid() << " uid " << req->uid() << " content " << req->content()
LOG(INFO) << "logid " << req->logid() << " uid " << req->userid() << " content " << req->content()
<< ", score total cost time " << absl::ToDoubleMilliseconds(step2 - step1);
return grpc::Status::OK;
}
Expand Down
Loading

0 comments on commit 456c58e

Please sign in to comment.