cortex_sdk/client/
query.rs1use super::transport::decode_value;
2use super::CortexDbClient;
3use crate::http::path;
4use crate::{
5 AqlResponse, ContextPackResponse, GroundedAnswerRequest, GroundedAnswerResponse,
6 RememberResponse, SdkResult, VerificationReportResponse, VerifyRequest,
7};
8
9impl CortexDbClient {
10 pub fn aql(&self, scope: &str, statement: &str) -> SdkResult<serde_json::Value> {
11 self.post(&path("/v1/aql", &[("scope", scope)]), statement)
12 }
13
14 pub fn aql_response(&self, scope: &str, statement: &str) -> SdkResult<AqlResponse> {
15 decode_value(self.aql(scope, statement)?)
16 }
17
18 pub fn context(&self, scope: &str, statement: &str) -> SdkResult<serde_json::Value> {
19 self.post(&path("/v1/context", &[("scope", scope)]), statement)
20 }
21
22 pub fn context_response(&self, scope: &str, statement: &str) -> SdkResult<ContextPackResponse> {
23 decode_value(self.context(scope, statement)?)
24 }
25
26 pub fn context_prompt(&self, scope: &str, statement: &str) -> SdkResult<String> {
27 self.post_text(
28 &path("/v1/context", &[("scope", scope), ("format", "prompt")]),
29 statement,
30 )
31 }
32
33 pub fn context_markdown(&self, scope: &str, statement: &str) -> SdkResult<String> {
34 self.post_text(
35 &path("/v1/context", &[("scope", scope), ("format", "markdown")]),
36 statement,
37 )
38 }
39
40 pub fn verify(&self, scope: &str, statement: &str) -> SdkResult<serde_json::Value> {
41 self.post(&path("/v1/verify", &[("scope", scope)]), statement)
42 }
43
44 pub fn verify_response(
45 &self,
46 scope: &str,
47 statement: &str,
48 ) -> SdkResult<VerificationReportResponse> {
49 decode_value(self.verify(scope, statement)?)
50 }
51
52 pub fn verify_request_response(
53 &self,
54 request: &VerifyRequest,
55 ) -> SdkResult<VerificationReportResponse> {
56 let request = request.clone().json();
57 decode_value(self.post(&request.path(), request.statement())?)
58 }
59
60 pub fn verify_request_export(&self, request: &VerifyRequest) -> SdkResult<String> {
61 self.post_text(&request.path(), request.statement())
62 }
63
64 pub fn answer_with_grounded_context(
65 &self,
66 request: GroundedAnswerRequest,
67 answerer: impl FnOnce(&ContextPackResponse) -> SdkResult<String>,
68 ) -> SdkResult<GroundedAnswerResponse> {
69 let retrieve_statement = request.retrieve_statement()?;
70 let context = self.context_response(request.scope(), &retrieve_statement)?;
71 let answer = answerer(&context)?;
72 let verify_statement = request.verify_statement(&answer)?;
73 let verification = match verify_statement.as_deref() {
74 Some(statement) => Some(self.verify_response(request.scope(), statement)?),
75 None => None,
76 };
77 Ok(GroundedAnswerResponse::from_context_answer(
78 &request,
79 retrieve_statement,
80 verify_statement,
81 context,
82 answer,
83 verification,
84 ))
85 }
86
87 pub fn remember(&self, scope: &str, statement: &str) -> SdkResult<serde_json::Value> {
88 self.post(&path("/v1/remember", &[("scope", scope)]), statement)
89 }
90
91 pub fn remember_response(&self, scope: &str, statement: &str) -> SdkResult<RememberResponse> {
92 decode_value(self.remember(scope, statement)?)
93 }
94}