Implement performance improvements for memory caching, HTTP client reuse, and regex optimization

Co-authored-by: Kenearos <86194771+Kenearos@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot] 2026-01-27 17:33:17 +00:00
parent 860e4d5027
commit b72cd9db1c
5 changed files with 451 additions and 246 deletions

View file

@ -30,12 +30,27 @@ class PerplexityProvider:
self.base_url = "https://api.perplexity.ai"
self.logger = logger or logging.getLogger(__name__)
# Reusable HTTP client (created lazily)
self._client: Optional[httpx.AsyncClient] = None
# Statistics
self.total_requests = 0
self.total_tokens = 0
self.total_errors = 0
self.last_response_time = 0
async def _get_client(self) -> httpx.AsyncClient:
"""Get or create the HTTP client (lazy initialization)"""
if self._client is None or self._client.is_closed:
self._client = httpx.AsyncClient(timeout=30.0)
return self._client
async def close(self):
"""Close the HTTP client"""
if self._client is not None and not self._client.is_closed:
await self._client.aclose()
self._client = None
async def get_response(self, messages):
"""
Send messages to Perplexity API and get response
@ -66,30 +81,30 @@ class PerplexityProvider:
start_time = time.time()
try:
async with httpx.AsyncClient(timeout=30.0) as client:
response = await client.post(
f"{self.base_url}/chat/completions",
json=payload,
headers=headers
)
client = await self._get_client()
response = await client.post(
f"{self.base_url}/chat/completions",
json=payload,
headers=headers
)
self.last_response_time = time.time() - start_time
self.last_response_time = time.time() - start_time
if response.status_code == 200:
data = response.json()
content = data['choices'][0]['message']['content']
tokens_used = data.get('usage', {}).get('total_tokens', 0)
if response.status_code == 200:
data = response.json()
content = data['choices'][0]['message']['content']
tokens_used = data.get('usage', {}).get('total_tokens', 0)
# Update statistics
self.total_requests += 1
self.total_tokens += tokens_used
# Update statistics
self.total_requests += 1
self.total_tokens += tokens_used
return content
else:
self.total_errors += 1
error_msg = f"API Error {response.status_code}: {response.text}"
self.logger.error(error_msg)
return None
return content
else:
self.total_errors += 1
error_msg = f"API Error {response.status_code}: {response.text}"
self.logger.error(error_msg)
return None
except httpx.TimeoutException:
self.total_errors += 1