mirror of
				https://github.com/meilisearch/meilisearch.git
				synced 2025-10-31 07:56:28 +00:00 
			
		
		
		
	Generate a new default chat API key
This commit is contained in:
		| @@ -351,6 +351,7 @@ pub struct IndexSearchRules { | ||||
| fn generate_default_keys(store: &HeedAuthStore) -> Result<()> { | ||||
|     store.put_api_key(Key::default_admin())?; | ||||
|     store.put_api_key(Key::default_search())?; | ||||
|     store.put_api_key(Key::default_chat())?; | ||||
|  | ||||
|     Ok(()) | ||||
| } | ||||
|   | ||||
| @@ -158,6 +158,21 @@ impl Key { | ||||
|             updated_at: now, | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     pub fn default_chat() -> Self { | ||||
|         let now = OffsetDateTime::now_utc(); | ||||
|         let uid = Uuid::new_v4(); | ||||
|         Self { | ||||
|             name: Some("Default Chat API Key".to_string()), | ||||
|             description: Some("Use it to chat and search from the frontend".to_string()), | ||||
|             uid, | ||||
|             actions: vec![Action::Chat, Action::Search], | ||||
|             indexes: vec![IndexUidPattern::all()], | ||||
|             expires_at: None, | ||||
|             created_at: now, | ||||
|             updated_at: now, | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | ||||
| fn parse_expiration_date( | ||||
| @@ -310,7 +325,7 @@ pub enum Action { | ||||
|     NetworkUpdate, | ||||
|     #[serde(rename = "chat.get")] | ||||
|     #[deserr(rename = "chat.get")] | ||||
|     ChatGet, | ||||
|     Chat, | ||||
|     #[serde(rename = "chatSettings.get")] | ||||
|     #[deserr(rename = "chatSettings.get")] | ||||
|     ChatSettingsGet, | ||||
| @@ -358,7 +373,7 @@ impl Action { | ||||
|             EXPERIMENTAL_FEATURES_UPDATE => Some(Self::ExperimentalFeaturesUpdate), | ||||
|             NETWORK_GET => Some(Self::NetworkGet), | ||||
|             NETWORK_UPDATE => Some(Self::NetworkUpdate), | ||||
|             CHAT_GET => Some(Self::ChatGet), | ||||
|             CHAT => Some(Self::Chat), | ||||
|             _otherwise => None, | ||||
|         } | ||||
|     } | ||||
| @@ -408,7 +423,7 @@ pub mod actions { | ||||
|     pub const NETWORK_GET: u8 = NetworkGet.repr(); | ||||
|     pub const NETWORK_UPDATE: u8 = NetworkUpdate.repr(); | ||||
|  | ||||
|     pub const CHAT_GET: u8 = ChatGet.repr(); | ||||
|     pub const CHAT: u8 = Chat.repr(); | ||||
|     pub const CHAT_SETTINGS_GET: u8 = ChatSettingsGet.repr(); | ||||
|     pub const CHAT_SETTINGS_UPDATE: u8 = ChatSettingsUpdate.repr(); | ||||
| } | ||||
|   | ||||
| @@ -299,8 +299,8 @@ pub mod policies { | ||||
|             auth: &AuthController, | ||||
|             token: &str, | ||||
|         ) -> Result<TenantTokenOutcome, AuthError> { | ||||
|             // Only search action can be accessed by a tenant token. | ||||
|             if A != actions::SEARCH { | ||||
|             // Only search and chat actions can be accessed by a tenant token. | ||||
|             if A != actions::SEARCH && A != actions::CHAT { | ||||
|                 return Ok(TenantTokenOutcome::NotATenantToken); | ||||
|             } | ||||
|  | ||||
|   | ||||
| @@ -47,7 +47,7 @@ pub fn configure(cfg: &mut web::ServiceConfig) { | ||||
|  | ||||
| /// Get a chat completion | ||||
| async fn chat( | ||||
|     index_scheduler: GuardedData<ActionPolicy<{ actions::CHAT_GET }>, Data<IndexScheduler>>, | ||||
|     index_scheduler: GuardedData<ActionPolicy<{ actions::CHAT }>, Data<IndexScheduler>>, | ||||
|     search_queue: web::Data<SearchQueue>, | ||||
|     web::Json(chat_completion): web::Json<CreateChatCompletionRequest>, | ||||
| ) -> impl Responder { | ||||
| @@ -114,7 +114,7 @@ fn setup_search_tool(chat_completion: &mut CreateChatCompletionRequest, prompts: | ||||
|  | ||||
| /// Process search request and return formatted results | ||||
| async fn process_search_request( | ||||
|     index_scheduler: &GuardedData<ActionPolicy<{ actions::CHAT_GET }>, Data<IndexScheduler>>, | ||||
|     index_scheduler: &GuardedData<ActionPolicy<{ actions::CHAT }>, Data<IndexScheduler>>, | ||||
|     search_queue: &web::Data<SearchQueue>, | ||||
|     index_uid: String, | ||||
|     q: Option<String>, | ||||
| @@ -175,7 +175,7 @@ async fn process_search_request( | ||||
| } | ||||
|  | ||||
| async fn non_streamed_chat( | ||||
|     index_scheduler: GuardedData<ActionPolicy<{ actions::CHAT_GET }>, Data<IndexScheduler>>, | ||||
|     index_scheduler: GuardedData<ActionPolicy<{ actions::CHAT }>, Data<IndexScheduler>>, | ||||
|     search_queue: web::Data<SearchQueue>, | ||||
|     mut chat_completion: CreateChatCompletionRequest, | ||||
| ) -> Result<HttpResponse, ResponseError> { | ||||
| @@ -245,7 +245,7 @@ async fn non_streamed_chat( | ||||
| } | ||||
|  | ||||
| async fn streamed_chat( | ||||
|     index_scheduler: GuardedData<ActionPolicy<{ actions::CHAT_GET }>, Data<IndexScheduler>>, | ||||
|     index_scheduler: GuardedData<ActionPolicy<{ actions::CHAT }>, Data<IndexScheduler>>, | ||||
|     search_queue: web::Data<SearchQueue>, | ||||
|     mut chat_completion: CreateChatCompletionRequest, | ||||
| ) -> impl Responder { | ||||
|   | ||||
| @@ -820,6 +820,22 @@ async fn list_api_keys() { | ||||
|           "createdAt": "[ignored]", | ||||
|           "updatedAt": "[ignored]" | ||||
|         }, | ||||
|         { | ||||
|           "name": "Default Chat API Key", | ||||
|           "description": "Use it to chat and search from the frontend", | ||||
|           "key": "[ignored]", | ||||
|           "uid": "[ignored]", | ||||
|           "actions": [ | ||||
|             "search", | ||||
|             "chat.get" | ||||
|           ], | ||||
|           "indexes": [ | ||||
|             "*" | ||||
|           ], | ||||
|           "expiresAt": null, | ||||
|           "createdAt": "[ignored]", | ||||
|           "updatedAt": "[ignored]" | ||||
|         }, | ||||
|         { | ||||
|           "name": "Default Search API Key", | ||||
|           "description": "Use it to search from the frontend", | ||||
|   | ||||
		Reference in New Issue
	
	Block a user