Skip to content

Commit 9b6814b

Browse files
CogniSwitch Connector (run-llama#604)
* CogniSwitch Connector - Tools - Tests - Notebook & Readme - Added to Library JSON * Update test_cogniswitchtoolspec.py importing directly from init * Update README.md * updated the urls and also conditions * used make format to format the changes accordingly --------- Co-authored-by: saiCogniswitch <[email protected]>
1 parent cfb4b70 commit 9b6814b

File tree

8 files changed

+2568
-1
lines changed

8 files changed

+2568
-1
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
## Cogniswitch ToolSpec
2+
3+
**Use cogniswitch to have chat with your knowledge in just three steps**
4+
5+
visit https://www.cogniswitch.ai/developer.<br>
6+
7+
**Registration:**
8+
- Signup with your email and verify your registration
9+
- You will get a mail with a platform token and oauth token for using the services.
10+
11+
12+
**step 1: Instantiate the Cogniswitch ToolSpec:**<br>
13+
- Use your cogniswitch token, openAI API key, oauth token to instantiate the toolspec. <br>
14+
15+
**step 2: Cogniswitch Store data:**<br>
16+
- use store_data function in the toolspec and input your file or url. <br>
17+
- it will be processed and stored in your knowledge store. <br>
18+
- you can check the status of document processing in cogniswitch console. <br>
19+
20+
**step 3: Cogniswitch Answer:**<br>
21+
- Use query_knowledge function in the toolspec and input your query. <br>
22+
- You will get the answer from your knowledge as the response. <br>
23+
24+
25+
### Import Required Libraries
26+
27+
28+
```python
29+
import warnings
30+
warnings.filterwarnings("ignore")
31+
from llama_hub.tools import CogniswitchToolspec
32+
```
33+
34+
### Cogniswitch Credentials and OpenAI token
35+
36+
37+
```python
38+
# cs_token = <your cogniswitch platform token>
39+
# OAI_token = <your openai token>
40+
# oauth_token = <your cogniswitch apikey>
41+
```
42+
43+
### Instantiate the Tool Spec
44+
45+
46+
```python
47+
toolspec = CogniswitchToolSpec(cs_token=cs_token, OAI_token=OAI_token, apiKey=oauth_token)
48+
```
49+
50+
### Use the Tool Spec for storing data in cogniswitch with a single call
51+
52+
53+
```python
54+
store_response = toolspec.store_data(url = "https://cogniswitch.ai/dev",
55+
document_name="Cogniswitch dev",
56+
document_description="This is a cogniswitch website for developers.")
57+
print(store_response)
58+
```
59+
60+
{'data': {'knowledgeSourceId': 42, 'sourceType': 'https://cogniswitch.ai/dev', 'sourceURL': None, 'sourceFileName': None, 'sourceName': 'Cogniswitch dev', 'sourceDescription': 'This is a cogniswitch website for developers.', 'status': 'UPLOADED'}, 'list': None, 'message': "We're processing your content & will send you an email on completion, hang tight!", 'statusCode': 1000}
61+
62+
63+
### Use Tool Spec for answering using the query knowledge with a single call
64+
65+
66+
```python
67+
answer_response = toolspec.query_knowledge("tell me about cogniswitch")
68+
print(answer_response)
69+
```
70+
71+
{'data': {'answer': 'CogniSwitch is a technology platform that enhances the reliability of Generative AI applications for enterprises. It does this by gathering and organizing knowledge from documented sources, eliminating hallucinations and bias in AI responses. The platform uses AI to automatically gather and organize knowledge, which can then be reviewed and curated by experts before being published. The CogniSwitch API enables Gen AI applications to access this knowledge as needed, ensuring reliability. It is specifically designed to complement Generative AI and offers customized solutions for different business functions within an enterprise.'}, 'list': None, 'message': None, 'statusCode': 1000}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from llama_hub.tools.cogniswitch.base import CogniswitchToolSpec
2+
3+
__all__ = [
4+
"CogniswitchToolSpec",
5+
]
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
import requests
2+
from typing import Optional, Type
3+
from llama_index.bridge.pydantic import BaseModel
4+
from llama_index.tools.tool_spec.base import BaseToolSpec
5+
6+
7+
class CogniswitchToolSpec(BaseToolSpec):
8+
"""Cogniswitch Tool Spec.
9+
A tool used to store data using the Cogniswitch service.
10+
"""
11+
12+
spec_functions = ["store_data", "query_knowledge"]
13+
14+
def __init__(self, cs_token: str, OAI_token: str, apiKey: str) -> None:
15+
"""
16+
Args:
17+
cs_token (str): Cogniswitch token.
18+
OAI_token (str): OpenAI token.
19+
apiKey (str): Oauth token
20+
"""
21+
self.cs_token = cs_token
22+
self.OAI_token = OAI_token
23+
self.apiKey = apiKey
24+
self.source_URL_endpoint = (
25+
"https://api.cogniswitch.ai:8243/cs-api/0.0.1/cs/knowledgeSource/url"
26+
)
27+
self.source_file_endpoint = (
28+
"https://api.cogniswitch.ai:8243/cs-api/0.0.1/cs/knowledgeSource/file"
29+
)
30+
self.knowledge_request_endpoint = (
31+
"https://api.cogniswitch.ai:8243/cs-api/0.0.1/cs/knowledgeRequest"
32+
)
33+
self.headers = {
34+
"apiKey": self.apiKey,
35+
"platformToken": self.cs_token,
36+
"openAIToken": self.OAI_token,
37+
}
38+
39+
def store_data(
40+
self,
41+
url: Optional[str] = None,
42+
file: Optional[str] = None,
43+
document_name: Optional[str] = None,
44+
document_description: Optional[str] = None,
45+
) -> dict:
46+
"""
47+
Store data using the Cogniswitch service.
48+
49+
Args:
50+
url (Optional[str]): URL link.
51+
file (Optional[str]): file path of your file.
52+
the current files supported by the files are
53+
.txt, .pdf, .docx, .doc, .html
54+
document_name (Optional[str]): Name of the document you are uploading.
55+
document_description (Optional[str]): Description of the document.
56+
57+
58+
59+
Returns:
60+
dict: Response JSON from the Cogniswitch service.
61+
"""
62+
if not file and not url:
63+
return {
64+
"message": "No input provided",
65+
}
66+
elif file and url:
67+
return {
68+
"message": "Too many inputs, please provide either file or url",
69+
}
70+
elif url:
71+
api_url = self.source_URL_endpoint
72+
headers = self.headers
73+
files = None
74+
data = {
75+
"url": url,
76+
"documentName": document_name,
77+
"documentDescription": document_description,
78+
}
79+
response = requests.post(
80+
api_url, headers=headers, verify=False, data=data, files=files
81+
)
82+
83+
elif file:
84+
api_url = self.source_file_endpoint
85+
86+
headers = self.headers
87+
if file is not None:
88+
files = {"file": open(file, "rb")}
89+
else:
90+
files = None
91+
data = {
92+
"url": url,
93+
"documentName": document_name,
94+
"documentDescription": document_description,
95+
}
96+
response = requests.post(
97+
api_url, headers=headers, verify=False, data=data, files=files
98+
)
99+
if response.status_code == 200:
100+
return response.json()
101+
else:
102+
# error_message = response.json()["message"]
103+
return {
104+
"message": "Bad Request",
105+
}
106+
107+
def query_knowledge(self, query: str) -> dict:
108+
"""
109+
Send a query to the Cogniswitch service and retrieve the response.
110+
111+
Args:
112+
query (str): Query to be answered.
113+
114+
Returns:
115+
dict: Response JSON from the Cogniswitch service.
116+
"""
117+
api_url = self.knowledge_request_endpoint
118+
119+
headers = self.headers
120+
121+
data = {"query": query}
122+
response = requests.post(api_url, headers=headers, verify=False, data=data)
123+
if response.status_code == 200:
124+
return response.json()
125+
else:
126+
# error_message = response.json()["message"]
127+
return {
128+
"message": "Bad Request",
129+
}
130+
131+
def get_fn_schema_from_fn_name(self, fn_name: str) -> Optional[Type[BaseModel]]:
132+
pass

llama_hub/tools/library.json

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,5 +139,16 @@
139139
"neo4j",
140140
"cypher"
141141
]
142-
}
142+
},
143+
"CogniswitchToolSpec": {
144+
"id": "tools/cogniswitch",
145+
"author": "cogniswitch",
146+
"keywords": [
147+
"graph",
148+
"knowledge graph",
149+
"neural",
150+
"symbolic",
151+
"embedding"
152+
]
153+
}
143154
}

0 commit comments

Comments
 (0)