mirror of
https://github.com/meilisearch/meilisearch.git
synced 2025-06-07 04:35:37 +00:00
Introduce the first version of the /chat route that mimics the OpenAI API
This commit is contained in:
parent
bed442528f
commit
0efb72fe66
4
Cargo.lock
generated
4
Cargo.lock
generated
@ -6067,9 +6067,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tokio"
|
name = "tokio"
|
||||||
version = "1.45.0"
|
version = "1.45.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "2513ca694ef9ede0fb23fe71a4ee4107cb102b9dc1930f6d0fd77aae068ae165"
|
checksum = "75ef51a33ef1da925cea3e4eb122833cb377c61439ca401b770f54902b806779"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"backtrace",
|
"backtrace",
|
||||||
"bytes",
|
"bytes",
|
||||||
|
@ -389,6 +389,7 @@ impl Action {
|
|||||||
EXPERIMENTAL_FEATURES_UPDATE => Some(Self::ExperimentalFeaturesUpdate),
|
EXPERIMENTAL_FEATURES_UPDATE => Some(Self::ExperimentalFeaturesUpdate),
|
||||||
NETWORK_GET => Some(Self::NetworkGet),
|
NETWORK_GET => Some(Self::NetworkGet),
|
||||||
NETWORK_UPDATE => Some(Self::NetworkUpdate),
|
NETWORK_UPDATE => Some(Self::NetworkUpdate),
|
||||||
|
CHAT_GET => Some(Self::ChatGet),
|
||||||
_otherwise => None,
|
_otherwise => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
32
crates/meilisearch/src/routes/chat.rs
Normal file
32
crates/meilisearch/src/routes/chat.rs
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
use actix_web::web::{self, Data};
|
||||||
|
use actix_web::HttpResponse;
|
||||||
|
use async_openai::config::OpenAIConfig;
|
||||||
|
use async_openai::types::CreateChatCompletionRequest;
|
||||||
|
use async_openai::Client;
|
||||||
|
use index_scheduler::IndexScheduler;
|
||||||
|
use meilisearch_types::error::ResponseError;
|
||||||
|
use meilisearch_types::keys::actions;
|
||||||
|
|
||||||
|
use crate::extractors::authentication::policies::ActionPolicy;
|
||||||
|
use crate::extractors::authentication::GuardedData;
|
||||||
|
|
||||||
|
pub fn configure(cfg: &mut web::ServiceConfig) {
|
||||||
|
cfg.service(web::resource("").route(web::post().to(chat)));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get a chat completion
|
||||||
|
async fn chat(
|
||||||
|
_index_scheduler: GuardedData<ActionPolicy<{ actions::CHAT_GET }>, Data<IndexScheduler>>,
|
||||||
|
web::Json(chat_completion): web::Json<CreateChatCompletionRequest>,
|
||||||
|
) -> Result<HttpResponse, ResponseError> {
|
||||||
|
// To enable later on, when the feature will be experimental
|
||||||
|
// index_scheduler.features().check_chat("Using the /chat route")?;
|
||||||
|
|
||||||
|
let api_key = std::env::var("MEILI_OPENAI_API_KEY")
|
||||||
|
.expect("cannot find OpenAI API Key (MEILI_OPENAI_API_KEY)");
|
||||||
|
let config = OpenAIConfig::default().with_api_key(&api_key); // we can also change the API base
|
||||||
|
let client = Client::with_config(config);
|
||||||
|
let response = client.chat().create(chat_completion).await.unwrap();
|
||||||
|
|
||||||
|
Ok(HttpResponse::Ok().json(response))
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user