1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
pub mod chunking;
pub mod document_index;
pub mod inference;
pub mod language;
use chunking::ChunkRequest;
use document_index::{Document, SearchResult};
use inference::{ChatRequest, ChatResponse, Completion, CompletionRequest};
use language::SelectLanguageRequest;
use serde::{Deserialize, Serialize};
use crate::{DocumentPath, LanguageCode, SearchRequest};
/// Cognitive System Interface
pub trait Csi {
/// Chunk the given text into smaller pieces that fit within the
/// maximum token amount for a given model.
fn chunk(&self, request: ChunkRequest) -> Vec<String> {
self.chunk_concurrently(vec![request]).remove(0)
}
/// Process multiple chunking requests at once
fn chunk_concurrently(&self, requests: Vec<ChunkRequest>) -> Vec<Vec<String>>;
/// Search for documents in a given index.
fn search(&self, request: SearchRequest) -> Vec<SearchResult> {
self.search_concurrently(vec![request]).remove(0)
}
/// Process multiple search requests at once
fn search_concurrently(&self, requests: Vec<SearchRequest>) -> Vec<Vec<SearchResult>>;
/// Retrieve a document from the Document Index by its path.
///
/// # Errors
/// Will return an error if document metadata cannot be deserialized.
fn document<Metadata>(&self, path: DocumentPath) -> anyhow::Result<Document<Metadata>>
where
Metadata: for<'a> Deserialize<'a> + Serialize,
{
Ok(self.documents(vec![path])?.remove(0))
}
/// Retrieve multiple documents from the Document Index by their paths.
///
/// # Errors
/// Will return an error if document metadata cannot be deserialized.
fn documents<'m, Metadata>(
&self,
paths: Vec<DocumentPath>,
) -> anyhow::Result<Vec<Document<Metadata>>>
where
Metadata: for<'a> Deserialize<'a> + Serialize;
/// Retrieve a document's metadata from the Document Index by its path.
///
/// # Errors
/// Will return an error if metadata cannot be deserialized.
fn document_metadata<Metadata>(&self, path: DocumentPath) -> anyhow::Result<Option<Metadata>>
where
Metadata: for<'a> Deserialize<'a> + Serialize,
{
Ok(self.documents_metadata(vec![path])?.remove(0))
}
/// Retrieve multiple documents' metadata from the Document Index by their paths.
///
/// # Errors
/// Will return an error if metadata cannot be deserialized.
fn documents_metadata<Metadata>(
&self,
paths: Vec<DocumentPath>,
) -> anyhow::Result<Vec<Option<Metadata>>>
where
Metadata: for<'a> Deserialize<'a> + Serialize;
/// Send messages with a particular role to a model and receive a response.
/// Provides a higher level interface than completion for chat scenarios.
fn chat(&self, request: ChatRequest) -> ChatResponse {
self.chat_concurrently(vec![request]).remove(0)
}
/// Process multiple chat requests at once
fn chat_concurrently(&self, requests: Vec<ChatRequest>) -> Vec<ChatResponse>;
/// Generate a completion for a given prompt using a specific model.
fn complete(&self, request: CompletionRequest) -> Completion {
self.complete_concurrently(vec![request]).remove(0)
}
/// Process multiple completion requests at once
fn complete_concurrently(&self, requests: Vec<CompletionRequest>) -> Vec<Completion>;
/// Select the detected language for the provided input based on the list of possible languages.
/// If no language matches, None is returned.
///
/// text: Text input
/// languages: All languages that should be considered during detection.
fn select_language(&self, request: SelectLanguageRequest) -> Option<LanguageCode> {
self.select_language_concurrently(vec![request]).remove(0)
}
/// Process multiple select language requests at once
fn select_language_concurrently(
&self,
requests: Vec<SelectLanguageRequest>,
) -> Vec<Option<LanguageCode>>;
}