Skip to content

Cogniswitch

CogniswitchQueryEngine #

Bases: BaseQueryEngine

Source code in llama_index/core/query_engine/cogniswitch_query_engine.py
 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
class CogniswitchQueryEngine(BaseQueryEngine):
    def __init__(self, cs_token: str, OAI_token: str, apiKey: str) -> None:
        """所需的字段。

Args:
    cs_token (str): Cogniswitch令牌。
    OAI_token (str): OpenAI令牌。
    apiKey (str): Oauth令牌。
"""
        self.cs_token = cs_token
        self.OAI_token = OAI_token
        self.apiKey = apiKey
        self.knowledge_request_endpoint = (
            "https://api.cogniswitch.ai:8243/cs-api/0.0.1/cs/knowledgeRequest"
        )
        self.headers = {
            "apiKey": self.apiKey,
            "platformToken": self.cs_token,
            "openAIToken": self.OAI_token,
        }

    def query_knowledge(self, query: str) -> Response:
        """向Cogniswitch服务发送查询并检索响应。

Args:
    query (str): 待回答的查询。

Returns:
    dict: 来自Cogniswitch服务的响应JSON。
"""
        data = {"query": query}
        response = requests.post(
            self.knowledge_request_endpoint,
            headers=self.headers,
            data=data,
        )
        if response.status_code == 200:
            resp = response.json()
            answer = resp["data"]["answer"]

            return Response(response=answer)
        else:
            error_message = response.json()["message"]
            return Response(response=error_message)

    def _query(self, query_bundle: QueryBundle) -> Response:
        return self.query_knowledge(query_bundle.query_str)

    async def _aquery(self, query_bundle: QueryBundle) -> Response:
        return self.query_knowledge(query_bundle.query_str)

    def _get_prompt_modules(self) -> Dict[str, Any]:
        """获取提示。"""
        return {}

query_knowledge #

query_knowledge(query: str) -> Response

向Cogniswitch服务发送查询并检索响应。

Parameters:

Name Type Description Default
query str

待回答的查询。

required

Returns:

Name Type Description
dict Response

来自Cogniswitch服务的响应JSON。

Source code in llama_index/core/query_engine/cogniswitch_query_engine.py
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
    def query_knowledge(self, query: str) -> Response:
        """向Cogniswitch服务发送查询并检索响应。

Args:
    query (str): 待回答的查询。

Returns:
    dict: 来自Cogniswitch服务的响应JSON。
"""
        data = {"query": query}
        response = requests.post(
            self.knowledge_request_endpoint,
            headers=self.headers,
            data=data,
        )
        if response.status_code == 200:
            resp = response.json()
            answer = resp["data"]["answer"]

            return Response(response=answer)
        else:
            error_message = response.json()["message"]
            return Response(response=error_message)