Compare commits

...

21 Commits

Author SHA1 Message Date
Bagatur
9c3947025e more 2023-11-19 21:34:53 -08:00
Bagatur
3d6ee92254 test 2023-11-19 21:23:50 -08:00
Bagatur
67b5e6a261 test 2023-11-19 21:21:11 -08:00
Bagatur
af234b5adf fix 2023-11-19 21:18:41 -08:00
Saicharan Sridhara
19ff447dc7 Merge branch 'master' into cogniswitch_chains 2023-11-20 09:54:44 +05:30
CogniJT
070ae11b5f Merge branch 'master' into cogniswitch_chains 2023-11-18 13:44:15 +05:30
Saicharan Sridhara
833f89c54d Merge branch 'master' into cogniswitch_chains 2023-11-18 01:06:10 +05:30
Saicharan Sridhara
e3937e4232 Update imported tools 2023-11-18 00:59:52 +05:30
Saicharan Sridhara
78b4366364 added cogniswitch store tool and answer tool in the init file 2023-11-18 00:49:30 +05:30
Saicharan Sridhara
642c93ccbd added cogniswitch tool 2023-11-18 00:47:16 +05:30
Saicharan Sridhara
23cdab909d added unit test for the cogniswitch tools 2023-11-18 00:46:10 +05:30
Saicharan Sridhara
2beddffb36 added a cogniswitch example notebook 2023-11-18 00:44:37 +05:30
CogniJT
47b07cd196 Delete libs/langchain/tests/unit_tests/chains/test_cogniswitch_store.py 2023-11-18 00:07:27 +05:30
CogniJT
b3cf4e7894 Delete libs/langchain/tests/unit_tests/chains/test_cogniswitch_answer.py 2023-11-18 00:07:14 +05:30
CogniJT
36525fb58f Delete libs/langchain/langchain/chains/cogniswitch directory 2023-11-18 00:06:37 +05:30
CogniJT
7d0df1858d Delete docs/extras/use_cases/cogniswitch_chain_usage.ipynb 2023-11-18 00:05:57 +05:30
CogniJT
6611122082 Merge branch 'master' into cogniswitch_chains 2023-09-21 15:07:14 +05:30
CogniJT
8d1140a0d4 Merge branch 'master' into cogniswitch_chains 2023-09-21 03:57:27 +05:30
CogniJT
bdd6460794 Merge branch 'langchain-ai:master' into cogniswitch_chains 2023-09-21 02:49:59 +05:30
JT
e6946ac621 Merge branch 'cogniswitch_chains' of https://github.com/CogniSwitch/langchain into cogniswitch_chains 2023-09-21 02:40:01 +05:30
JT
979c6b6a1a Adding CogniSwitch.ai calls as a chain
- Jupyter Notebook
- Init to import
- Store Chain
- Answer Chain
- Tests for the chains
2023-09-21 02:39:20 +05:30
7 changed files with 537 additions and 0 deletions

View File

@@ -0,0 +1,224 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "19062701",
"metadata": {},
"source": [
"## Cogniswitch Tools\n",
"\n",
"**Use CogniSwitch to build production ready applications that can consume, organize and retrieve knowledge flawlessly. Using the framework of your choice, in this case Langchain CogniSwitch helps alleviate the stress of decision making when it comes to, choosing the right storage and retrieval formats. It also eradicates reliability issues and hallucinations when it comes to responses that are generated. Get started by interacting with your knowledge in just two simple steps.**\n",
"\n",
"Visit [https://www.cogniswitch.ai/developer to register](https://www.cogniswitch.ai/developer?utm_source=langchain&utm_medium=langchainbuild&utm_id=dev)\n",
"\n",
"**Registration:** \n",
"- Signup with your email and verify your registration \n",
"- You will get a mail with a platform token and oauth token for using the services.\n",
"\n",
"**Step 1: Cogniswitch Store Tool:** \n",
"- Use your cogniswitch token, openAI API key, oauth token and your file or URL to run the tool. \n",
"- It will be processed and stored in your knowledge store. \n",
"- You can check the status of document processing in cogniswitch console. \n",
"\n",
"**Step 2: Cogniswitch Answer Tool:**\n",
"- Use your cogniswitch token, openAI API key, oauth token and your question to run the tool. \n",
"- You will get the answer from your knowledge as the response. "
]
},
{
"cell_type": "markdown",
"id": "1435b193",
"metadata": {},
"source": [
"### Import necessary libraries"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "3d2dfc9f",
"metadata": {},
"outputs": [],
"source": [
"import warnings\n",
"\n",
"warnings.filterwarnings(\"ignore\")\n",
"\n",
"from langchain.tools import CogniswitchAnswerTool, CogniswitchStoreTool"
]
},
{
"cell_type": "markdown",
"id": "6e6acf0e",
"metadata": {},
"source": [
"### Cogniswitch platform token, OAuth token and OpenAI API key"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "30e3b86c",
"metadata": {},
"outputs": [],
"source": [
"cs_token = \"<Your cogniswitch platform token>\"\n",
"OAI_token = \"<Your OpenAI API key>\"\n",
"oauth_token = \"<Your OAuth token>\""
]
},
{
"cell_type": "markdown",
"id": "3476857f",
"metadata": {},
"source": [
"## Cogniswitch Store Chain"
]
},
{
"cell_type": "markdown",
"id": "320e02fc",
"metadata": {},
"source": [
"### Instantiate the Store Tool"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "89f58167",
"metadata": {},
"outputs": [],
"source": [
"cs_store = CogniswitchStoreTool(\n",
" cs_token=cs_token, OAI_token=OAI_token, apiKey=oauth_token\n",
")"
]
},
{
"cell_type": "markdown",
"id": "42c9890e",
"metadata": {},
"source": [
"### Run the tool with either url or a file path"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "5c6410e9",
"metadata": {},
"outputs": [],
"source": [
"response = cs_store._run(url=\"https://cogniswitch.ai/developer\")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "794b4fba",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'data': {'knowledgeSourceId': 67, 'sourceType': 'https://cogniswitch.ai/developer', 'sourceURL': None, 'sourceFileName': None, 'sourceName': None, 'sourceDescription': None, 'status': 'UPLOADED'}, 'list': None, 'message': \"We're processing your content & will send you an email on completion, hang tight!\", 'statusCode': 1000}\n"
]
}
],
"source": [
"print(response)"
]
},
{
"cell_type": "markdown",
"id": "b5e9ca94",
"metadata": {},
"source": [
"## Cogniswitch Answer Tool"
]
},
{
"cell_type": "markdown",
"id": "c7d32067",
"metadata": {},
"source": [
"### Instantiate the Answer Tool"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "1365953e",
"metadata": {},
"outputs": [],
"source": [
"cs_answer = CogniswitchAnswerTool(\n",
" cs_token=cs_token, OAI_token=OAI_token, apiKey=oauth_token\n",
")"
]
},
{
"cell_type": "markdown",
"id": "0ba9aca9",
"metadata": {},
"source": [
"### Run the tool with the query"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "eb059787",
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"response = cs_answer._run(query=\"tell me about cogniswitch\")"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "e73e963f",
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'data': {'answer': 'CogniSwitch is a platform that offers intelligent support tools and knowledge on-demand. It utilizes automation tools to process documents and create dynamic knowledge graphs. This allows for intuitive exploration of data and provides user-friendly answers. Additionally, CogniSwitch keeps track of any edits made to the knowledge and incorporates human expertise.'}, 'list': None, 'message': None, 'statusCode': 1000}\n"
]
}
],
"source": [
"print(response)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.1"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View File

@@ -110,6 +110,18 @@ def _import_brave_search_tool() -> Any:
return BraveSearch
def _import_cogniswitch_answer_tool() -> Any:
from langchain.tools.cogniswitch.tool import CogniswitchAnswerTool
return CogniswitchAnswerTool
def _import_cogniswitch_store_tool() -> Any:
from langchain.tools.cogniswitch.tool import CogniswitchStoreTool
return CogniswitchStoreTool
def _import_ddg_search_tool_DuckDuckGoSearchResults() -> Any:
from langchain.tools.ddg_search.tool import DuckDuckGoSearchResults
@@ -707,6 +719,10 @@ def __getattr__(name: str) -> Any:
return _import_bing_search_tool_BingSearchRun()
elif name == "BraveSearch":
return _import_brave_search_tool()
elif name == "CogniswitchAnswerTool":
return _import_cogniswitch_answer_tool()
elif name == "CogniswitchStoreTool":
return _import_cogniswitch_store_tool()
elif name == "DuckDuckGoSearchResults":
return _import_ddg_search_tool_DuckDuckGoSearchResults()
elif name == "DuckDuckGoSearchRun":
@@ -921,6 +937,8 @@ __all__ = [
"BingSearchRun",
"BraveSearch",
"ClickTool",
"CogniswitchAnswerTool",
"CogniswitchStoreTool",
"CopyFileTool",
"CurrentWebPageTool",
"DeleteFileTool",

View File

@@ -0,0 +1 @@
"Cogniswitch Toolkit"

View File

@@ -0,0 +1,207 @@
from typing import Any, Dict, Optional
import requests
from langchain.callbacks.manager import CallbackManagerForToolRun
from langchain.tools import BaseTool
class CogniswitchAnswerTool(BaseTool):
"""
A chain class for interacting with the Cogniswitch service to answer questions.
name: str = "Cogniswitch"
description: str = (
"A wrapper around cogniswitch. "
"Input should be a search query."
)
"""
name: str = "cogniswitch_answer"
description: str = "A wrapper around cogniswitch. "
cs_token: str
OAI_token: str
apiKey: str
api_url = "https://api.cogniswitch.ai:8243/cs-api/0.0.1/cs/knowledgeRequest"
def _run(
self,
query: str,
run_manager: Optional[CallbackManagerForToolRun] = None,
) -> Dict[str, Any]:
"""
Execute the chain to answer a query.
Args:
query (str): Natural language query,
that you would like to ask to your knowledge graph.
run_manager (Optional[CallbackManagerForChainRun]):
Manager for chain run callbacks.
Returns:
Dict[str, Any]: Output dictionary containing
the 'response' from the service.
"""
response = self.answer_cs(self.cs_token, self.OAI_token, query, self.apiKey)
return response
def answer_cs(self, cs_token: str, OAI_token: str, query: str, apiKey: str) -> dict:
"""
Send a query to the Cogniswitch service and retrieve the response.
Args:
cs_token (str): Cogniswitch token.
OAI_token (str): OpenAI token.
apiKey (str): OAuth token.
query (str): Query to be answered.
Returns:
dict: Response JSON from the Cogniswitch service.
"""
if not cs_token:
raise ValueError("Missing cs_token")
if not OAI_token:
raise ValueError("Missing OpenAI token")
if not apiKey:
raise ValueError("Missing cogniswitch OAuth token")
if not query:
raise ValueError("Missing input query")
headers = {
"apiKey": apiKey,
"platformToken": cs_token,
"openAIToken": OAI_token,
}
data = {"query": query}
response = requests.post(self.api_url, headers=headers, verify=False, data=data)
return response.json()
class CogniswitchStoreTool(BaseTool):
"""
A chain class for interacting with the Cogniswitch service to store data.
name: str = "Cogniswitch"
description: str = (
"A wrapper around cogniswitch. "
"Input should be a file path or url."
)
"""
name: str = "cogniswitch_store"
description: str = "A wrapper around cogniswitch. "
cs_token: str
OAI_token: str
apiKey: str
knowledgesource_file = (
"https://api.cogniswitch.ai:8243/cs-api/0.0.1/cs/knowledgeSource/file"
)
knowledgesource_url = (
"https://api.cogniswitch.ai:8243/cs-api/0.0.1/cs/knowledgeSource/url"
)
def _run(
self,
file: Optional[str] = None,
url: Optional[str] = None,
document_name: Optional[str] = None,
document_description: Optional[str] = None,
run_manager: Optional[CallbackManagerForToolRun] = None,
) -> Dict[str, Any]:
"""
Execute the chain to answer a query.
Args:
file Optional[str]: The file path of your knowledge
url Optional[str]: The url of your knowledge
document_name Optional[str]: Name of your knowledge document
document_description Optional[str]: Description of your knowledge document
run_manager (Optional[CallbackManagerForChainRun]):
Manager for chain run callbacks.
Returns:
Dict[str, Any]: Output dictionary containing
the 'response' from the service.
"""
if not file:
file = None
if not url:
url = None
response = self.store_data(
file=file,
url=url,
document_name=document_name,
document_description=document_description,
)
return response
def store_data(
self,
url: Optional[str],
file: Optional[str],
document_name: Optional[str],
document_description: Optional[str],
) -> dict:
"""
Store data using the Cogniswitch service.
Args:
url (Optional[str]): URL link.
file (Optional[str]): file path of your file.
the current files supported by the files are
.txt, .pdf, .docx, .doc, .html
document_name (Optional[str]): Name of the document you are uploading.
document_description (Optional[str]): Description of the document.
Returns:
dict: Response JSON from the Cogniswitch service.
"""
headers = {
"apiKey": self.apiKey,
"openAIToken": self.OAI_token,
"platformToken": self.cs_token,
}
data: Dict[str, Any]
if not document_name:
document_name = ""
if not document_description:
document_description = ""
if not file and not url:
return {
"message": "No input provided",
}
elif file and url:
return {
"message": "Too many inputs, please provide either file or url",
}
elif url:
files = None
data = {"url": url}
response = requests.post(
self.knowledgesource_url,
headers=headers,
verify=False,
data=data,
files=files,
)
elif file:
if file is not None:
files = {"file": open(file, "rb")}
else:
files = None
data = {
"url": url,
"documentName": document_name,
"documentDescription": document_description,
}
response = requests.post(
self.knowledgesource_file,
headers=headers,
verify=False,
data=data,
files=files,
)
if response.status_code == 200:
return response.json()
else:
return {"message": "Bad Request"}

View File

@@ -0,0 +1,83 @@
import unittest
from typing import Any
from unittest.mock import patch
from langchain.tools.cogniswitch.tool import CogniswitchAnswerTool, CogniswitchStoreTool
class TestCogniswitchAnswerTool(unittest.TestCase):
def setUp(self) -> None:
self.tool = CogniswitchAnswerTool(
cs_token="cs_token", OAI_token="OAI_token", apiKey="apiKey"
)
@patch("requests.post")
def test_answer_cs(self, mock_post: Any) -> None:
query = "test query"
expected_response = {"response": "test response"}
mock_post.return_value.json.return_value = expected_response
response = self.tool.answer_cs(
self.tool.cs_token, self.tool.OAI_token, query, self.tool.apiKey
)
mock_post.assert_called_once_with(
self.tool.api_url,
headers={
"apiKey": self.tool.apiKey,
"platformToken": self.tool.cs_token,
"openAIToken": self.tool.OAI_token,
},
verify=False,
data={"query": query},
)
self.assertEqual(response, expected_response)
def test_run(self) -> None:
query = "test query"
expected_response = {"response": "test response"}
with patch.object(
self.tool, "answer_cs", return_value=expected_response
) as mock_answer_cs:
response = self.tool._run(query)
mock_answer_cs.assert_called_once_with(
self.tool.cs_token, self.tool.OAI_token, query, self.tool.apiKey
)
self.assertEqual(response, expected_response)
class TestCogniswitchStoreTool(unittest.TestCase):
def setUp(self) -> None:
self.tool = CogniswitchStoreTool(
cs_token="cs_token", OAI_token="OAI_token", apiKey="apiKey"
)
@patch("requests.post")
def test_store_data_with_url(self, mock_post: Any) -> None:
url = "https://example.com"
expected_response = {"response": "test response"}
mock_post.return_value.json.return_value = expected_response
response = self.tool.store_data(
url=url,
file=None,
document_name="doc_name",
document_description="doc_desc",
)
mock_post.assert_called_once_with(
self.tool.knowledgesource_url,
headers={
"apiKey": self.tool.apiKey,
"platformToken": self.tool.cs_token,
"openAIToken": self.tool.OAI_token,
},
verify=False,
data={"url": url},
files=None,
)
self.assertEqual(response, expected_response)

View File

@@ -24,6 +24,8 @@ EXPECTED_ALL = [
"BingSearchRun",
"BraveSearch",
"ClickTool",
"CogniswitchAnswerTool",
"CogniswitchStoreTool",
"CopyFileTool",
"CurrentWebPageTool",
"DeleteFileTool",

View File

@@ -25,6 +25,8 @@ _EXPECTED = [
"BingSearchRun",
"BraveSearch",
"ClickTool",
"CogniswitchAnswerTool",
"CogniswitchStoreTool",
"CopyFileTool",
"CurrentWebPageTool",
"DeleteFileTool",