Skip to content

Commit

Permalink
Add the zino-chatbot crate
Browse files Browse the repository at this point in the history
  • Loading branch information
photino committed Dec 14, 2024
1 parent 28bf339 commit 2c9f714
Show file tree
Hide file tree
Showing 13 changed files with 64 additions and 42 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ members = [
"zino-auth",
"zino-axum",
"zino-chart",
"zino-chatbot",
"zino-connector",
"zino-core",
"zino-derive",
Expand Down
1 change: 0 additions & 1 deletion zino-auth/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ optional = true
[dependencies.toml]
version = "0.8.19"
default-features = false
features = ["parse"]

[dependencies.zino-core]
path = "../zino-core"
Expand Down
27 changes: 27 additions & 0 deletions zino-chatbot/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[package]
name = "zino-chatbot"
description = "Unified access to chatbot services for zino."
version = "0.0.1"
rust-version = "1.80"
edition = "2021"
license = "MIT"
categories = ["asynchronous", "network-programming", "web-programming"]
keywords = ["http", "web", "framework"]
homepage = "https://github.com/zino-rs/zino"
repository = "https://github.com/zino-rs/zino"
documentation = "https://docs.rs/zino-chatbot"
readme = "README.md"

[dependencies]
async-openai = "0.26.0"
futures = "0.3.31"
tracing = "0.1.41"

[dependencies.toml]
version = "0.8.19"
default-features = false

[dependencies.zino-core]
path = "../zino-core"
version = "0.29.2"
features = ["http-client"]
11 changes: 11 additions & 0 deletions zino-chatbot/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[![github]](https://github.com/zino-rs/zino)
[![crates-io]](https://crates.io/crates/zino-chatbot)
[![docs-rs]](https://docs.rs/zino-chatbot)

[github]: https://img.shields.io/badge/github-8da0cb?labelColor=555555&logo=github
[crates-io]: https://img.shields.io/badge/crates.io-fc8d62?labelColor=555555&logo=rust
[docs-rs]: https://img.shields.io/badge/docs.rs-66c2a5?labelColor=555555&logo=docs.rs

Unified access to chatbot services for [`zino`].

[`zino`]: https://github.com/zino-rs/zino
11 changes: 2 additions & 9 deletions zino-core/src/chatbot/client.rs → zino-chatbot/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
use self::ChatbotClient::*;
use super::ChatbotService;
use crate::{bail, error::Error, extension::TomlTableExt, Map};
use super::{ChatbotService, OpenAiChatCompletion};
use toml::Table;

#[cfg(feature = "chatbot-openai")]
use super::OpenAiChatCompletion;
use zino_core::{bail, error::Error, extension::TomlTableExt, Map};

/// Client for supported chatbot services.
#[non_exhaustive]
pub(super) enum ChatbotClient {
/// OpenAI
#[cfg(feature = "chatbot-openai")]
OpenAi(OpenAiChatCompletion),
}

Expand Down Expand Up @@ -43,7 +39,6 @@ impl Chatbot {
/// returning an error if it fails.
pub fn try_new(service: &str, config: &Table) -> Result<Chatbot, Error> {
match service {
#[cfg(feature = "chatbot-openai")]
"openai" => OpenAiChatCompletion::try_new_chatbot(config),
_ => {
bail!("chatbot service `{}` is unsupported", service);
Expand Down Expand Up @@ -72,14 +67,12 @@ impl ChatbotService for Chatbot {

fn model(&self) -> &str {
match &self.client {
#[cfg(feature = "chatbot-openai")]
OpenAi(chat_completion) => chat_completion.model(),
}
}

async fn try_send(&self, message: String, options: Option<Map>) -> Result<Vec<String>, Error> {
match &self.client {
#[cfg(feature = "chatbot-openai")]
OpenAi(chat_completion) => chat_completion.try_send(message, options).await,
}
}
Expand Down
21 changes: 8 additions & 13 deletions zino-core/src/chatbot/mod.rs → zino-chatbot/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
//! Unified access to different chatbot services.
//!
//! ## Supported chatbot services
//!
//! | Chatbot service | Description | Feature flag |
//! |------------------|------------------------|------------------------|
//! | `openai` | OpenAI | `chatbot-openai` |
//!
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
#![doc = include_str!("../README.md")]
#![doc(html_favicon_url = "https://zino.cc/assets/zino-logo.png")]
#![doc(html_logo_url = "https://zino.cc/assets/zino-logo.svg")]
#![allow(async_fn_in_trait)]
#![forbid(unsafe_code)]

use crate::{
use toml::Table;
use zino_core::{
application::StaticRecord, error::Error, extension::TomlTableExt, state::State, LazyLock, Map,
};
use toml::Table;

mod client;

/// Supported chatbot services.
#[cfg(feature = "chatbot-openai")]
mod openai;

pub use client::Chatbot;

#[cfg(feature = "chatbot-openai")]
use openai::OpenAiChatCompletion;

/// Underlying trait of all chatbot services for implementors.
Expand Down
14 changes: 7 additions & 7 deletions zino-core/src/chatbot/openai.rs → zino-chatbot/src/openai.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
use super::{client::ChatbotClient::OpenAi, Chatbot, ChatbotService};
use crate::{
application::http_client,
error::Error,
extension::{JsonObjectExt, TomlTableExt},
Map,
};
use async_openai::{
config::{Config, OpenAIConfig},
types::{
Expand All @@ -15,6 +9,12 @@ use async_openai::{
};
use futures::StreamExt;
use toml::Table;
use zino_core::{
application::Agent,
error::Error,
extension::{JsonObjectExt, TomlTableExt},
Map,
};

/// OpenAI chat completion.
pub(super) struct OpenAiChatCompletion<C = OpenAIConfig>
Expand Down Expand Up @@ -60,7 +60,7 @@ impl ChatbotService for OpenAiChatCompletion<OpenAIConfig> {
}

let mut client = Client::with_config(openai_config);
if let Some(reqwest_client) = http_client::SHARED_HTTP_CLIENT.get() {
if let Some(reqwest_client) = Agent::get_http_client() {
client = client.with_http_client(reqwest_client.clone());
}

Expand Down
1 change: 1 addition & 0 deletions zino-connector/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
#![doc = include_str!("../README.md")]
#![doc(html_favicon_url = "https://zino.cc/assets/zino-logo.png")]
#![doc(html_logo_url = "https://zino.cc/assets/zino-logo.svg")]
Expand Down
8 changes: 0 additions & 8 deletions zino-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ cargo-args = ["-Zunstable-options", "-Zrustdoc-scrape-examples"]
rustdoc-args = ["--cfg", "docsrs"]

[features]
all-chatbots = ["chatbot", "chatbot-openai"]
all-locales = [
"locale",
"locale-de",
Expand All @@ -33,8 +32,6 @@ all-validators = [
"validator-phone-number",
"validator-regex",
]
chatbot = []
chatbot-openai = ["dep:async-openai", "chatbot"]
cookie = ["dep:cookie", "reqwest?/cookies"]
crypto-sm = ["dep:ctr", "dep:sm3", "dep:sm4"]
debug = [
Expand All @@ -50,7 +47,6 @@ default = []
dotenv = ["dep:dotenvy"]
env-filter = ["tracing-subscriber/env-filter", "tracing-subscriber/smallvec"]
full = [
"all-chatbots",
"all-locales",
"all-validators",
"cookie",
Expand Down Expand Up @@ -135,10 +131,6 @@ url = "2.5.4"
version = "0.5.3"
features = ["std"]

[dependencies.async-openai]
version = "0.26.0"
optional = true

[dependencies.card-validate]
version = "2.4.0"
optional = true
Expand Down
1 change: 0 additions & 1 deletion zino-core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ The following optional features are available:

| Name | Description | Default? |
|----------------------|--------------------------------------------------------|----------|
| `chatbot` | Enables the chatbot services. | No |
| `cookie` | Enables the support for cookies. | No |
| `crypto-sm` | Enables China's Standards of Encryption Algorithms. | No |
| `debug` | Enables the features for ease of debugging. | No |
Expand Down
7 changes: 7 additions & 0 deletions zino-core/src/application/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ impl Application for Agent {
}

impl Agent {
/// Gets the shared HTTP client.
#[cfg(feature = "http-client")]
#[inline]
pub fn get_http_client() -> Option<&'static reqwest::Client> {
super::http_client::SHARED_HTTP_CLIENT.get()
}

/// Constructs a request builder.
#[cfg(feature = "http-client")]
#[inline]
Expand Down
2 changes: 0 additions & 2 deletions zino-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ mod mock;
#[cfg(feature = "openapi")]
mod openapi;

#[cfg(feature = "chatbot")]
pub mod chatbot;
#[cfg(feature = "orm")]
pub mod orm;
#[cfg(feature = "view")]
Expand Down
1 change: 0 additions & 1 deletion zino-http/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ features = ["raw_value"]
[dependencies.toml]
version = "0.8.19"
default-features = false
features = ["parse"]

[dependencies.unic-langid]
version = "0.9.5"
Expand Down

0 comments on commit 2c9f714

Please sign in to comment.