mirror of
https://github.com/hwchase17/langchain.git
synced 2025-09-25 13:07:58 +00:00
community[minor]: CogniSwitch Agent Toolkit for LangChain (#17312)
**Description**: CogniSwitch focusses on making GenAI usage more reliable. It abstracts out the complexity & decision making required for tuning processing, storage & retrieval. Using simple APIs documents / URLs can be processed into a Knowledge Graph that can then be used to answer questions. **Dependencies**: No dependencies. Just network calls & API key required **Tag maintainer**: @hwchase17 **Twitter handle**: https://github.com/CogniSwitch **Documentation**: Please check `docs/docs/integrations/toolkits/cogniswitch.ipynb` **Tests**: The usual tool & toolkits tests using `test_imports.py` PR has passed linting and testing before this submission. --------- Co-authored-by: Saicharan Sridhara <145636106+saiCogniswitch@users.noreply.github.com>
This commit is contained in:
@@ -18,6 +18,7 @@ from langchain_community.agent_toolkits.amadeus.toolkit import AmadeusToolkit
|
||||
from langchain_community.agent_toolkits.azure_cognitive_services import (
|
||||
AzureCognitiveServicesToolkit,
|
||||
)
|
||||
from langchain_community.agent_toolkits.cogniswitch.toolkit import CogniswitchToolkit
|
||||
from langchain_community.agent_toolkits.connery import ConneryToolkit
|
||||
from langchain_community.agent_toolkits.file_management.toolkit import (
|
||||
FileManagementToolkit,
|
||||
@@ -51,6 +52,7 @@ __all__ = [
|
||||
"AINetworkToolkit",
|
||||
"AmadeusToolkit",
|
||||
"AzureCognitiveServicesToolkit",
|
||||
"CogniswitchToolkit",
|
||||
"ConneryToolkit",
|
||||
"FileManagementToolkit",
|
||||
"GmailToolkit",
|
||||
|
@@ -0,0 +1 @@
|
||||
"""CogniSwitch Toolkit"""
|
@@ -0,0 +1,40 @@
|
||||
from typing import List
|
||||
|
||||
from langchain_community.agent_toolkits.base import BaseToolkit
|
||||
from langchain_community.tools import BaseTool
|
||||
from langchain_community.tools.cogniswitch.tool import (
|
||||
CogniswitchKnowledgeRequest,
|
||||
CogniswitchKnowledgeSourceFile,
|
||||
CogniswitchKnowledgeSourceURL,
|
||||
CogniswitchKnowledgeStatus,
|
||||
)
|
||||
|
||||
|
||||
class CogniswitchToolkit(BaseToolkit):
|
||||
"""
|
||||
Toolkit for CogniSwitch.
|
||||
|
||||
Use the toolkit to get all the tools present in the cogniswitch and
|
||||
use them to interact with your knowledge
|
||||
"""
|
||||
|
||||
cs_token: str # cogniswitch token
|
||||
OAI_token: str # OpenAI API token
|
||||
apiKey: str # Cogniswitch OAuth token
|
||||
|
||||
def get_tools(self) -> List[BaseTool]:
|
||||
"""Get the tools in the toolkit."""
|
||||
return [
|
||||
CogniswitchKnowledgeStatus(
|
||||
cs_token=self.cs_token, OAI_token=self.OAI_token, apiKey=self.apiKey
|
||||
),
|
||||
CogniswitchKnowledgeRequest(
|
||||
cs_token=self.cs_token, OAI_token=self.OAI_token, apiKey=self.apiKey
|
||||
),
|
||||
CogniswitchKnowledgeSourceFile(
|
||||
cs_token=self.cs_token, OAI_token=self.OAI_token, apiKey=self.apiKey
|
||||
),
|
||||
CogniswitchKnowledgeSourceURL(
|
||||
cs_token=self.cs_token, OAI_token=self.OAI_token, apiKey=self.apiKey
|
||||
),
|
||||
]
|
@@ -118,6 +118,32 @@ def _import_brave_search_tool() -> Any:
|
||||
return BraveSearch
|
||||
|
||||
|
||||
def _import_cogniswitch_store_file_tool() -> Any:
|
||||
from langchain_community.tools.cogniswitch.tool import (
|
||||
CogniswitchKnowledgeSourceFile,
|
||||
)
|
||||
|
||||
return CogniswitchKnowledgeSourceFile
|
||||
|
||||
|
||||
def _import_cogniswitch_store_url_tool() -> Any:
|
||||
from langchain_community.tools.cogniswitch.tool import CogniswitchKnowledgeSourceURL
|
||||
|
||||
return CogniswitchKnowledgeSourceURL
|
||||
|
||||
|
||||
def _import_cogniswitch_answer_tool() -> Any:
|
||||
from langchain_community.tools.cogniswitch.tool import CogniswitchKnowledgeRequest
|
||||
|
||||
return CogniswitchKnowledgeRequest
|
||||
|
||||
|
||||
def _import_cogniswitch_knowledge_status_tool() -> Any:
|
||||
from langchain_community.tools.cogniswitch.tool import CogniswitchKnowledgeStatus
|
||||
|
||||
return CogniswitchKnowledgeStatus
|
||||
|
||||
|
||||
def _import_connery_tool() -> Any:
|
||||
from langchain_community.tools.connery import ConneryAction
|
||||
|
||||
@@ -803,6 +829,14 @@ def __getattr__(name: str) -> Any:
|
||||
return _import_bing_search_tool_BingSearchRun()
|
||||
elif name == "BraveSearch":
|
||||
return _import_brave_search_tool()
|
||||
elif name == "CogniswitchKnowledgeSourceFile":
|
||||
return _import_cogniswitch_store_file_tool()
|
||||
elif name == "CogniswitchKnowledgeSourceURL":
|
||||
return _import_cogniswitch_store_url_tool()
|
||||
elif name == "CogniswitchKnowledgeRequest":
|
||||
return _import_cogniswitch_answer_tool()
|
||||
elif name == "CogniswitchKnowledgeStatus":
|
||||
return _import_cogniswitch_knowledge_status_tool()
|
||||
elif name == "ConneryAction":
|
||||
return _import_connery_tool()
|
||||
elif name == "DuckDuckGoSearchResults":
|
||||
@@ -1043,6 +1077,10 @@ __all__ = [
|
||||
"BingSearchRun",
|
||||
"BraveSearch",
|
||||
"ClickTool",
|
||||
"CogniswitchKnowledgeSourceFile",
|
||||
"CogniswitchKnowledgeSourceURL",
|
||||
"CogniswitchKnowledgeRequest",
|
||||
"CogniswitchKnowledgeStatus",
|
||||
"ConneryAction",
|
||||
"CopyFileTool",
|
||||
"CurrentWebPageTool",
|
||||
|
@@ -0,0 +1 @@
|
||||
"Cogniswitch Tools"
|
399
libs/community/langchain_community/tools/cogniswitch/tool.py
Normal file
399
libs/community/langchain_community/tools/cogniswitch/tool.py
Normal file
@@ -0,0 +1,399 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Dict, Optional
|
||||
|
||||
import requests
|
||||
from langchain_core.callbacks import CallbackManagerForToolRun
|
||||
from langchain_core.tools import BaseTool
|
||||
|
||||
|
||||
class CogniswitchKnowledgeRequest(BaseTool):
|
||||
"""
|
||||
A tool for interacting with the Cogniswitch service to answer questions.
|
||||
name: str = "cogniswitch_knowledge_request"
|
||||
description: str = (
|
||||
"A wrapper around cogniswitch service to answer the question
|
||||
from the knowledge base."
|
||||
"Input should be a search query."
|
||||
)
|
||||
"""
|
||||
|
||||
name: str = "cogniswitch_knowledge_request"
|
||||
description: str = """A wrapper around cogniswitch service to
|
||||
answer the question from the knowledge base."""
|
||||
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]:
|
||||
"""
|
||||
Use the tool 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 CogniswitchKnowledgeStatus(BaseTool):
|
||||
"""
|
||||
A cogniswitch tool for interacting with the Cogniswitch services to know the
|
||||
status of the document or url uploaded.
|
||||
name: str = "cogniswitch_knowledge_status"
|
||||
description: str = (
|
||||
"A wrapper around cogniswitch services to know the status of
|
||||
the document uploaded from a url or a file. "
|
||||
"Input should be a file name or the url link"
|
||||
)
|
||||
"""
|
||||
|
||||
name: str = "cogniswitch_knowledge_status"
|
||||
description: str = """A wrapper around cogniswitch services to know
|
||||
the status of the document uploaded from a url or a file."""
|
||||
cs_token: str
|
||||
OAI_token: str
|
||||
apiKey: str
|
||||
knowledge_status_url = (
|
||||
"https://api.cogniswitch.ai:8243/cs-api/0.0.1/cs/knowledgeSource/status"
|
||||
)
|
||||
|
||||
def _run(
|
||||
self,
|
||||
document_name: str,
|
||||
run_manager: Optional[CallbackManagerForToolRun] = None,
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
Use the tool to know the status of the document uploaded.
|
||||
|
||||
Args:
|
||||
document_name (str): name of the document or
|
||||
the url uploaded
|
||||
run_manager (Optional[CallbackManagerForChainRun]):
|
||||
Manager for chain run callbacks.
|
||||
|
||||
Returns:
|
||||
Dict[str, Any]: Output dictionary containing
|
||||
the 'response' from the service.
|
||||
"""
|
||||
response = self.knowledge_status(document_name)
|
||||
return response
|
||||
|
||||
def knowledge_status(self, document_name: str) -> dict:
|
||||
"""
|
||||
Use this function to know the status of the document or the URL uploaded
|
||||
Args:
|
||||
document_name (str): The document name or the url that is uploaded.
|
||||
|
||||
Returns:
|
||||
dict: Response JSON from the Cogniswitch service.
|
||||
"""
|
||||
|
||||
params = {"docName": document_name, "platformToken": self.cs_token}
|
||||
headers = {
|
||||
"apiKey": self.apiKey,
|
||||
"openAIToken": self.OAI_token,
|
||||
"platformToken": self.cs_token,
|
||||
}
|
||||
response = requests.get(
|
||||
self.knowledge_status_url,
|
||||
headers=headers,
|
||||
params=params,
|
||||
verify=False,
|
||||
)
|
||||
if response.status_code == 200:
|
||||
source_info = response.json()
|
||||
source_data = dict(source_info[-1])
|
||||
status = source_data.get("status")
|
||||
if status == 0:
|
||||
source_data["status"] = "SUCCESS"
|
||||
elif status == 1:
|
||||
source_data["status"] = "PROCESSING"
|
||||
elif status == 2:
|
||||
source_data["status"] = "UPLOADED"
|
||||
elif status == 3:
|
||||
source_data["status"] = "FAILURE"
|
||||
elif status == 4:
|
||||
source_data["status"] = "UPLOAD_FAILURE"
|
||||
elif status == 5:
|
||||
source_data["status"] = "REJECTED"
|
||||
|
||||
if "filePath" in source_data.keys():
|
||||
source_data.pop("filePath")
|
||||
if "savedFileName" in source_data.keys():
|
||||
source_data.pop("savedFileName")
|
||||
if "integrationConfigId" in source_data.keys():
|
||||
source_data.pop("integrationConfigId")
|
||||
if "metaData" in source_data.keys():
|
||||
source_data.pop("metaData")
|
||||
if "docEntryId" in source_data.keys():
|
||||
source_data.pop("docEntryId")
|
||||
return source_data
|
||||
else:
|
||||
# error_message = response.json()["message"]
|
||||
return {
|
||||
"message": response.status_code,
|
||||
}
|
||||
|
||||
|
||||
class CogniswitchKnowledgeSourceFile(BaseTool):
|
||||
"""
|
||||
A cogniswitch tool for interacting with the Cogniswitch services to store data.
|
||||
name: str = "cogniswitch_knowledge_source_file"
|
||||
description: str = (
|
||||
"This calls the CogniSwitch services to analyze & store data from a file.
|
||||
If the input looks like a file path, assign that string value to file key.
|
||||
Assign document name & description only if provided in input."
|
||||
)
|
||||
"""
|
||||
|
||||
name: str = "cogniswitch_knowledge_source_file"
|
||||
description: str = """
|
||||
This calls the CogniSwitch services to analyze & store data from a file.
|
||||
If the input looks like a file path, assign that string value to file key.
|
||||
Assign document name & description only if provided in input.
|
||||
"""
|
||||
cs_token: str
|
||||
OAI_token: str
|
||||
apiKey: str
|
||||
knowledgesource_file = (
|
||||
"https://api.cogniswitch.ai:8243/cs-api/0.0.1/cs/knowledgeSource/file"
|
||||
)
|
||||
|
||||
def _run(
|
||||
self,
|
||||
file: Optional[str] = None,
|
||||
document_name: Optional[str] = None,
|
||||
document_description: Optional[str] = None,
|
||||
run_manager: Optional[CallbackManagerForToolRun] = None,
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
Execute the tool to store the data given from a file.
|
||||
This calls the CogniSwitch services to analyze & store data from a file.
|
||||
If the input looks like a file path, assign that string value to file key.
|
||||
Assign document name & description only if provided in input.
|
||||
|
||||
Args:
|
||||
file Optional[str]: The file path 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:
|
||||
return {
|
||||
"message": "No input provided",
|
||||
}
|
||||
else:
|
||||
response = self.store_data(
|
||||
file=file,
|
||||
document_name=document_name,
|
||||
document_description=document_description,
|
||||
)
|
||||
return response
|
||||
|
||||
def store_data(
|
||||
self,
|
||||
file: Optional[str],
|
||||
document_name: Optional[str],
|
||||
document_description: Optional[str],
|
||||
) -> dict:
|
||||
"""
|
||||
Store data using the Cogniswitch service.
|
||||
This calls the CogniSwitch services to analyze & store data from a file.
|
||||
If the input looks like a file path, assign that string value to file key.
|
||||
Assign document name & description only if provided in input.
|
||||
|
||||
Args:
|
||||
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 file is not None:
|
||||
files = {"file": open(file, "rb")}
|
||||
|
||||
data = {
|
||||
"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"}
|
||||
|
||||
|
||||
class CogniswitchKnowledgeSourceURL(BaseTool):
|
||||
"""
|
||||
A cogniswitch tool for interacting with the Cogniswitch services to store data.
|
||||
name: str = "cogniswitch_knowledge_source_url"
|
||||
description: str = (
|
||||
"This calls the CogniSwitch services to analyze & store data from a url.
|
||||
the URL is provided in input, assign that value to the url key.
|
||||
Assign document name & description only if provided in input"
|
||||
)
|
||||
"""
|
||||
|
||||
name: str = "cogniswitch_knowledge_source_url"
|
||||
description: str = """
|
||||
This calls the CogniSwitch services to analyze & store data from a url.
|
||||
the URL is provided in input, assign that value to the url key.
|
||||
Assign document name & description only if provided in input"""
|
||||
cs_token: str
|
||||
OAI_token: str
|
||||
apiKey: str
|
||||
knowledgesource_url = (
|
||||
"https://api.cogniswitch.ai:8243/cs-api/0.0.1/cs/knowledgeSource/url"
|
||||
)
|
||||
|
||||
def _run(
|
||||
self,
|
||||
url: Optional[str] = None,
|
||||
document_name: Optional[str] = None,
|
||||
document_description: Optional[str] = None,
|
||||
run_manager: Optional[CallbackManagerForToolRun] = None,
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
Execute the tool to store the data given from a url.
|
||||
This calls the CogniSwitch services to analyze & store data from a url.
|
||||
the URL is provided in input, assign that value to the url key.
|
||||
Assign document name & description only if provided in input.
|
||||
|
||||
Args:
|
||||
url Optional[str]: The website/url link 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 url:
|
||||
return {
|
||||
"message": "No input provided",
|
||||
}
|
||||
response = self.store_data(
|
||||
url=url,
|
||||
document_name=document_name,
|
||||
document_description=document_description,
|
||||
)
|
||||
return response
|
||||
|
||||
def store_data(
|
||||
self,
|
||||
url: Optional[str],
|
||||
document_name: Optional[str],
|
||||
document_description: Optional[str],
|
||||
) -> dict:
|
||||
"""
|
||||
Store data using the Cogniswitch service.
|
||||
This calls the CogniSwitch services to analyze & store data from a url.
|
||||
the URL is provided in input, assign that value to the url key.
|
||||
Assign document name & description only if provided in input.
|
||||
|
||||
Args:
|
||||
url (Optional[str]): URL link.
|
||||
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 url:
|
||||
return {
|
||||
"message": "No input provided",
|
||||
}
|
||||
else:
|
||||
data = {"url": url}
|
||||
response = requests.post(
|
||||
self.knowledgesource_url,
|
||||
headers=headers,
|
||||
verify=False,
|
||||
data=data,
|
||||
)
|
||||
if response.status_code == 200:
|
||||
return response.json()
|
||||
else:
|
||||
return {"message": "Bad Request"}
|
@@ -28,6 +28,7 @@ EXPECTED_ALL = [
|
||||
"create_pbi_chat_agent",
|
||||
"create_spark_sql_agent",
|
||||
"create_sql_agent",
|
||||
"CogniswitchToolkit",
|
||||
]
|
||||
|
||||
|
||||
|
@@ -24,6 +24,10 @@ EXPECTED_ALL = [
|
||||
"BingSearchRun",
|
||||
"BraveSearch",
|
||||
"ClickTool",
|
||||
"CogniswitchKnowledgeSourceFile",
|
||||
"CogniswitchKnowledgeSourceURL",
|
||||
"CogniswitchKnowledgeRequest",
|
||||
"CogniswitchKnowledgeStatus",
|
||||
"ConneryAction",
|
||||
"CopyFileTool",
|
||||
"CurrentWebPageTool",
|
||||
|
@@ -25,6 +25,10 @@ _EXPECTED = [
|
||||
"BingSearchRun",
|
||||
"BraveSearch",
|
||||
"ClickTool",
|
||||
"CogniswitchKnowledgeSourceFile",
|
||||
"CogniswitchKnowledgeStatus",
|
||||
"CogniswitchKnowledgeSourceURL",
|
||||
"CogniswitchKnowledgeRequest",
|
||||
"ConneryAction",
|
||||
"CopyFileTool",
|
||||
"CurrentWebPageTool",
|
||||
|
Reference in New Issue
Block a user