import os import requests class ToothFairyClient: def __init__(self): self.api_key = os.getenv('TOOTHFAIRYAI_API_KEY') self.workspace_id = os.getenv('TOOTHFAIRYAI_WORKSPACE_ID') self.base_url = os.getenv('TOOTHFAIRYAI_API_URL', 'https://api.toothfairyai.com') def _get_headers(self): return { "x-api-key": self.api_key, "Content-Type": "application/json" } def create_search_tool(self, ngrok_url): """Creates the API Tool that tells the agent how to search your DB.""" # Corrected endpoint based on the documentation endpoint = f"{self.base_url}/function/create" payload = { "workspaceid": self.workspace_id, "name": "Search_SharePoint_Database", "url": f"{ngrok_url.rstrip('/')}/api/search/chunks", "requestType": "POST", "authorisationType": "none", "description": "Searches the company's internal SharePoint database for document excerpts. Use this whenever the user asks about internal files, reports, or policies.", "parameters": [ { "name": "query", "type": "string", "required": True, "description": "The exact search query to find relevant information." } ] } response = requests.post(endpoint, json=payload, headers=self._get_headers()) response.raise_for_status() # Adjust parsing based on typical wrapper responses if needed, usually data.id or just id response_json = response.json() return response_json.get("data", {}).get("id") or response_json.get("id") def create_agent(self, tool_id): """Creates an Operator agent and attaches your new search tool.""" endpoint = f"{self.base_url}/agent/create" payload = { "workspaceid": self.workspace_id, "label": "SharePoint Assistant", "mode": "retriever", "interpolationString": "You are a helpful corporate assistant. Use your Search_SharePoint_Database tool to find answers to user questions. Always cite the 'source_file' in your responses.", "goals": "Answer questions accurately using internal documentation.", "temperature": 0.3, "maxTokens": 2000, "maxHistory": 10, # <-- REQUIRED FIELD "topK": 10, # <-- REQUIRED FIELD "docTopK": 5, # <-- REQUIRED FIELD "hasFunctions": True, "agentFunctions": [tool_id], "agenticRAG": True } response = requests.post(endpoint, json=payload, headers=self._get_headers()) if not response.ok: print(f"❌ Server Error Output: {response.text}") response.raise_for_status() response_json = response.json() return response_json.get("data", {}).get("id") or response_json.get("id")