Skip to main content

cortex_sdk/async_client/
query.rs

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