diff --git a/README.md b/README.md index d0a9db7..eecc0d5 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,16 @@ pip install agixtsdk ``` ## Usage + +```python +from agixtsdk import AGiXTSDK + +base_uri = "http://localhost:7437" +api_key = "your_agixt_api_key" + +ApiClient = AGiXTSDK(base_uri=base_uri, api_key=api_key) +``` + Check out the AGiXT [Examples and Tests Notebook](https://github.com/Josh-XT/AGiXT/blob/main/tests/tests.ipynb) for examples of how to use the AGiXT SDK for Python. ## More Documentation diff --git a/agixtsdk/__init__.py b/agixtsdk/__init__.py index ab73c40..32cae02 100644 --- a/agixtsdk/__init__.py +++ b/agixtsdk/__init__.py @@ -3,48 +3,67 @@ class AGiXTSDK: - def __init__(self, base_uri: str = None): + def __init__(self, base_uri: str = None, api_key: str = None): if not base_uri: base_uri = "http://localhost:7437" self.base_uri = base_uri + if api_key: + self.headers = {"Authorization": f"Bearer {api_key}"} + else: + self.headers = {} if self.base_uri[-1] == "/": self.base_uri = self.base_uri[:-1] def handle_error(self, response: requests.Response) -> str: return "Unable to retrieve data." - def get_providers(self) -> List[str]: - try: - response = requests.get(f"{self.base_uri}/api/provider") - return response.json()["providers"] + def agixt_request(self, endpoint: str, method: str, data: Dict[str, Any] = {}): + try: + if method == "GET": + response = requests.get( + f"{self.base_uri}/api/{endpoint}", headers=self.headers + ) + elif method == "POST": + response = requests.post( + f"{self.base_uri}/api/{endpoint}", json=data, headers=self.headers + ) + elif method == "PUT": + response = requests.put( + f"{self.base_uri}/api/{endpoint}", json=data, headers=self.headers + ) + elif method == "PATCH": + response = requests.patch( + f"{self.base_uri}/api/{endpoint}", json=data, headers=self.headers + ) + elif method == "DELETE": + response = requests.delete( + f"{self.base_uri}/api/{endpoint}", headers=self.headers + ) + return response.json() except requests.RequestException: return self.handle_error(response) + def get_providers(self) -> List[str]: + return self.agixt_request(endpoint="provider", method="GET")["providers"] + def get_provider_settings(self, provider_name: str) -> Dict[str, Any]: - try: - response = requests.get(f"{self.base_uri}/api/provider/{provider_name}") - return response.json()["settings"] - except requests.RequestException: - return self.handle_error(response) + return self.agixt_request(endpoint=f"provider/{provider_name}", method="GET")[ + "settings" + ] def get_embed_providers(self) -> List[str]: - try: - response = requests.get(f"{self.base_uri}/api/embedding_providers") - return response.json()["providers"] - except requests.RequestException: - return self.handle_error(response) + return self.agixt_request(endpoint="embedding_providers", method="GET")[ + "providers" + ] def add_agent( self, agent_name: str, settings: Dict[str, Any] = {} ) -> Dict[str, Any]: - try: - response = requests.post( - f"{self.base_uri}/api/agent", - json={"agent_name": agent_name, "settings": settings}, - ) - return response.json() - except requests.RequestException: - return self.handle_error(response) + return self.agixt_request( + endpoint="agent", + method="POST", + data={"agent_name": agent_name, "settings": settings}, + ) def import_agent( self, @@ -52,122 +71,81 @@ def import_agent( settings: Dict[str, Any] = {}, commands: Dict[str, Any] = {}, ) -> Dict[str, Any]: - try: - response = requests.post( - f"{self.base_uri}/api/agent/import", - json={ - "agent_name": agent_name, - "settings": settings, - "commands": commands, - }, - ) - return response.json() - except requests.RequestException: - return self.handle_error(response) + return self.agixt_request( + endpoint="agent/import", + method="POST", + data={ + "agent_name": agent_name, + "settings": settings, + "commands": commands, + }, + ) def rename_agent(self, agent_name: str, new_name: str) -> str: - try: - response = requests.patch( - f"{self.base_uri}/api/agent/{agent_name}", - json={"new_name": new_name}, - ) - return response.json() - except requests.RequestException: - return self.handle_error(response) + return self.agixt_request( + endpoint=f"agent/{agent_name}", + method="PATCH", + data={"new_name": new_name}, + ) def update_agent_settings(self, agent_name: str, settings: Dict[str, Any]) -> str: - try: - response = requests.put( - f"{self.base_uri}/api/agent/{agent_name}", - json={"settings": settings, "agent_name": agent_name}, - ) - return response.json()["message"] - except requests.RequestException: - return self.handle_error(response) + return self.agixt_request( + endpoint=f"agent/{agent_name}", + method="PUT", + data={"settings": settings, "agent_name": agent_name}, + )["message"] def update_agent_commands(self, agent_name: str, commands: Dict[str, Any]) -> str: - try: - response = requests.put( - f"{self.base_uri}/api/agent/{agent_name}/commands", - json={"commands": commands, "agent_name": agent_name}, - ) - return response.json()["message"] - except requests.RequestException: - return self.handle_error(response) + return self.agixt_request( + endpoint=f"agent/{agent_name}/commands", + method="PUT", + data={"commands": commands, "agent_name": agent_name}, + )["message"] def delete_agent(self, agent_name: str) -> str: - try: - response = requests.delete(f"{self.base_uri}/api/agent/{agent_name}") - return response.json()["message"] - except requests.RequestException: - return self.handle_error(response) + return self.agixt_request(endpoint=f"agent/{agent_name}", method="DELETE")[ + "message" + ] - def get_agents( - self, - ) -> List[Dict[str, Any]]: - try: - response = requests.get(f"{self.base_uri}/api/agent") - return response.json()["agents"] - except requests.RequestException: - return self.handle_error(response) + def get_agents(self) -> List[Dict[str, Any]]: + return self.agixt_request(endpoint="agent", method="GET")["agents"] def get_agentconfig(self, agent_name: str) -> Dict[str, Any]: - try: - response = requests.get(f"{self.base_uri}/api/agent/{agent_name}") - return response.json()["agent"] - except requests.RequestException: - return self.handle_error(response) + return self.agixt_request(endpoint=f"agent/{agent_name}", method="GET")["agent"] def get_chat_history(self, agent_name: str) -> List[Dict[str, Any]]: - try: - response = requests.get(f"{self.base_uri}/api/{agent_name}/chat") - return response.json()["chat_history"] - except requests.RequestException: - return self.handle_error(response) + return self.agixt_request(endpoint=f"{agent_name}/chat", method="GET")[ + "chat_history" + ] def delete_agent_history(self, agent_name: str) -> str: - try: - response = requests.delete( - f"{self.base_uri}/api/agent/{agent_name}/history" - ) - return response.json()["message"] - except requests.RequestException: - return self.handle_error(response) + return self.agixt_request( + endpoint=f"agent/{agent_name}/history", + method="DELETE", + )["message"] def delete_history_message(self, agent_name: str, message: str) -> str: - try: - response = requests.delete( - f"{self.base_uri}/api/agent/{agent_name}/history/message", - json={"message": message}, - ) - return response.json()["message"] - except requests.RequestException: - return self.handle_error(response) + return self.agixt_request( + endpoint=f"agent/{agent_name}/history/message", + method="DELETE", + data={"message": message}, + )["message"] def wipe_agent_memories(self, agent_name: str) -> str: - try: - response = requests.delete(f"{self.base_uri}/api/agent/{agent_name}/memory") - return response.json()["message"] - except requests.RequestException: - return self.handle_error(response) - - def prompt_agent( - self, - agent_name: str, - prompt_name: str, - prompt_args: dict, - ) -> str: - try: - response = requests.post( - f"{self.base_uri}/api/agent/{agent_name}/prompt", - json={ - "prompt_name": prompt_name, - "prompt_args": prompt_args, - }, - ) - return response.json()["response"] - except requests.RequestException: - return self.handle_error(response) + return self.agixt_request( + endpoint=f"agent/{agent_name}/memory", + method="DELETE", + )["message"] + + def prompt_agent(self, agent_name: str, prompt_name: str, prompt_args: dict) -> str: + return self.agixt_request( + endpoint=f"agent/{agent_name}/prompt", + method="POST", + data={ + "prompt_name": prompt_name, + "prompt_args": prompt_args, + }, + )["response"] def instruct(self, agent_name: str, prompt: str) -> str: return self.prompt_agent( @@ -202,42 +180,29 @@ def smartchat(self, agent_name: str, prompt: str) -> str: ) def get_commands(self, agent_name: str) -> Dict[str, Dict[str, bool]]: - try: - response = requests.get(f"{self.base_uri}/api/agent/{agent_name}/command") - return response.json()["commands"] - except requests.RequestException: - return self.handle_error(response) + return self.agixt_request( + endpoint=f"agent/{agent_name}/command", + method="GET", + )["commands"] def toggle_command(self, agent_name: str, command_name: str, enable: bool) -> str: - try: - response = requests.patch( - f"{self.base_uri}/api/agent/{agent_name}/command", - json={"command_name": command_name, "enable": enable}, - ) - return response.json()["message"] - except requests.RequestException: - return self.handle_error(response) + return self.agixt_request( + endpoint=f"agent/{agent_name}/command", + method="PATCH", + data={"command_name": command_name, "enable": enable}, + )["message"] def get_chains(self) -> List[str]: - try: - response = requests.get(f"{self.base_uri}/api/chain") - return response.json() - except requests.RequestException: - return self.handle_error(response) + return self.agixt_request(endpoint="chain", method="GET") def get_chain(self, chain_name: str) -> Dict[str, Any]: - try: - response = requests.get(f"{self.base_uri}/api/chain/{chain_name}") - return response.json()["chain"] - except requests.RequestException: - return self.handle_error(response) + return self.agixt_request(endpoint=f"chain/{chain_name}", method="GET")["chain"] def get_chain_responses(self, chain_name: str) -> Dict[str, Any]: - try: - response = requests.get(f"{self.base_uri}/api/chain/{chain_name}/responses") - return response.json()["chain"] - except requests.RequestException: - return self.handle_error(response) + return self.agixt_request( + endpoint=f"chain/{chain_name}/responses", + method="GET", + )["chain"] def run_chain( self, @@ -247,56 +212,42 @@ def run_chain( all_responses: bool = False, from_step: int = 1, ) -> str: - try: - response = requests.post( - f"{self.base_uri}/api/chain/{chain_name}/run", - json={ - "prompt": user_input, - "agent_override": agent_name, - "all_responses": all_responses, - "from_step": int(from_step), - }, - ) - return response.json() - except requests.RequestException: - return self.handle_error(response) + return self.agixt_request( + endpoint=f"chain/{chain_name}/run", + method="POST", + data={ + "prompt": user_input, + "agent_override": agent_name, + "all_responses": all_responses, + "from_step": int(from_step), + }, + ) def add_chain(self, chain_name: str) -> str: - try: - response = requests.post( - f"{self.base_uri}/api/chain", - json={"chain_name": chain_name}, - ) - return response.json()["message"] - except requests.RequestException: - return self.handle_error(response) + return self.agixt_request( + endpoint="chain", + method="POST", + data={"chain_name": chain_name}, + )["message"] def import_chain(self, chain_name: str, steps: dict) -> str: - try: - response = requests.post( - f"{self.base_uri}/api/chain/import", - json={"chain_name": chain_name, "steps": steps}, - ) - return response.json()["message"] - except requests.RequestException: - return self.handle_error(response) + return self.agixt_request( + endpoint="chain/import", + method="POST", + data={"chain_name": chain_name, "steps": steps}, + )["message"] def rename_chain(self, chain_name: str, new_name: str) -> str: - try: - response = requests.put( - f"{self.base_uri}/api/chain/{chain_name}", - json={"new_name": new_name}, - ) - return response.json()["message"] - except requests.RequestException: - return self.handle_error(response) + return self.agixt_request( + endpoint=f"chain/{chain_name}", + method="PUT", + data={"new_name": new_name}, + )["message"] def delete_chain(self, chain_name: str) -> str: - try: - response = requests.delete(f"{self.base_uri}/api/chain/{chain_name}") - return response.json()["message"] - except requests.RequestException: - return self.handle_error(response) + return self.agixt_request(endpoint=f"chain/{chain_name}", method="DELETE")[ + "message" + ] def add_step( self, @@ -306,19 +257,16 @@ def add_step( prompt_type: str, prompt: dict, ) -> str: - try: - response = requests.post( - f"{self.base_uri}/api/chain/{chain_name}/step", - json={ - "step_number": step_number, - "agent_name": agent_name, - "prompt_type": prompt_type, - "prompt": prompt, - }, - ) - return response.json()["message"] - except requests.RequestException: - return self.handle_error(response) + return self.agixt_request( + endpoint=f"chain/{chain_name}/step", + method="POST", + data={ + "step_number": step_number, + "agent_name": agent_name, + "prompt_type": prompt_type, + "prompt": prompt, + }, + )["message"] def update_step( self, @@ -328,19 +276,16 @@ def update_step( prompt_type: str, prompt: dict, ) -> str: - try: - response = requests.put( - f"{self.base_uri}/api/chain/{chain_name}/step/{step_number}", - json={ - "step_number": step_number, - "agent_name": agent_name, - "prompt_type": prompt_type, - "prompt": prompt, - }, - ) - return response.json()["message"] - except requests.RequestException: - return self.handle_error(response) + return self.agixt_request( + endpoint=f"chain/{chain_name}/step/{step_number}", + method="PUT", + data={ + "step_number": step_number, + "agent_name": agent_name, + "prompt_type": prompt_type, + "prompt": prompt, + }, + )["message"] def move_step( self, @@ -348,114 +293,77 @@ def move_step( old_step_number: int, new_step_number: int, ) -> str: - try: - response = requests.patch( - f"{self.base_uri}/api/chain/{chain_name}/step/move", - json={ - "old_step_number": old_step_number, - "new_step_number": new_step_number, - }, - ) - return response.json()["message"] - except requests.RequestException: - return self.handle_error(response) + return self.agixt_request( + endpoint=f"chain/{chain_name}/step/move", + method="PATCH", + data={ + "old_step_number": old_step_number, + "new_step_number": new_step_number, + }, + )["message"] def delete_step(self, chain_name: str, step_number: int) -> str: - try: - response = requests.delete( - f"{self.base_uri}/api/chain/{chain_name}/step/{step_number}" - ) - return response.json()["message"] - except requests.RequestException: - return self.handle_error(response) + return self.agixt_request( + endpoint=f"chain/{chain_name}/step/{step_number}", + method="DELETE", + )["message"] def add_prompt(self, prompt_name: str, prompt: str) -> str: - try: - response = requests.post( - f"{self.base_uri}/api/prompt", - json={"prompt_name": prompt_name, "prompt": prompt}, - ) - return response.json()["message"] - except requests.RequestException: - return self.handle_error(response) + return self.agixt_request( + endpoint="prompt", + method="POST", + data={"prompt_name": prompt_name, "prompt": prompt}, + )["message"] def get_prompt(self, prompt_name: str) -> Dict[str, Any]: - try: - response = requests.get(f"{self.base_uri}/api/prompt/{prompt_name}") - return response.json()["prompt"] - except requests.RequestException: - return self.handle_error(response) + return self.agixt_request(endpoint=f"prompt/{prompt_name}", method="GET")[ + "prompt" + ] def get_prompts(self) -> List[str]: - try: - response = requests.get(f"{self.base_uri}/api/prompt") - return response.json()["prompts"] - except requests.RequestException: - return self.handle_error(response) + return self.agixt_request(endpoint="prompt", method="GET")["prompts"] def get_prompt_args(self, prompt_name: str) -> Dict[str, Any]: - try: - response = requests.get(f"{self.base_uri}/api/prompt/{prompt_name}/args") - return response.json()["prompt_args"] - except requests.RequestException: - return self.handle_error(response) + return self.agixt_request( + endpoint=f"prompt/{prompt_name}/args", + method="GET", + )["prompt_args"] def delete_prompt(self, prompt_name: str) -> str: - try: - response = requests.delete(f"{self.base_uri}/api/prompt/{prompt_name}") - return response.json()["message"] - except requests.RequestException: - return self.handle_error(response) + return self.agixt_request(endpoint=f"prompt/{prompt_name}", method="DELETE")[ + "message" + ] def update_prompt(self, prompt_name: str, prompt: str) -> str: - try: - response = requests.put( - f"{self.base_uri}/api/prompt/{prompt_name}", - json={"prompt": prompt, "prompt_name": prompt_name}, - ) - return response.json()["message"] - except requests.RequestException: - return self.handle_error(response) + return self.agixt_request( + endpoint=f"prompt/{prompt_name}", + method="PUT", + data={"prompt": prompt, "prompt_name": prompt_name}, + )["message"] def get_extension_settings(self) -> Dict[str, Any]: - try: - response = requests.get(f"{self.base_uri}/api/extensions/settings") - return response.json()["extension_settings"] - except requests.RequestException: - return self.handle_error(response) + return self.agixt_request(endpoint="extensions/settings", method="GET")[ + "extension_settings" + ] def get_extensions(self): - try: - response = requests.get(f"{self.base_uri}/api/extensions") - return response.json()["extensions"] - except requests.RequestException: - return self.handle_error(response) + return self.agixt_request(endpoint="extensions", method="GET")["extensions"] def get_command_args(self, command_name: str) -> Dict[str, Any]: - try: - response = requests.get( - f"{self.base_uri}/api/extensions/{command_name}/args" - ) - return response.json()["command_args"] - except requests.RequestException: - return self.handle_error(response) + return self.agixt_request( + endpoint=f"extensions/{command_name}/args", method="GET" + )["command_args"] def learn_url(self, agent_name: str, url: str) -> str: - try: - response = requests.post( - f"{self.base_uri}/api/agent/{agent_name}/learn/url", - json={"url": url}, - ) - return response.json()["message"] - except requests.RequestException: - return self.handle_error(response) + return self.agixt_request( + endpoint=f"agent/{agent_name}/learn/url", + method="POST", + data={"url": url}, + )["message"] def learn_file(self, agent_name: str, file_name: str, file_content: str) -> str: - try: - response = requests.post( - f"{self.base_uri}/api/agent/{agent_name}/learn/file", - json={"file_name": file_name, "file_content": file_content}, - ) - return response.json()["message"] - except requests.RequestException: - return self.handle_error(response) + return self.agixt_request( + endpoint=f"agent/{agent_name}/learn/file", + method="POST", + data={"file_name": file_name, "file_content": file_content}, + )["message"] diff --git a/setup.py b/setup.py index 3a729bd..6717b4b 100644 --- a/setup.py +++ b/setup.py @@ -8,7 +8,7 @@ setup( name="agixtsdk", - version="0.0.7", + version="0.0.8", description="The AGiXT SDK for Python.", long_description=long_description, long_description_content_type="text/markdown",