in ultravox/tools/ds_tool/caching.py [0:0]
def chat_completion(self, **kwargs) -> str:
text_hash = hashlib.sha256(json.dumps(kwargs).encode()).hexdigest()
# Try to read from cache
cache_path = self._get_prefixed_path(text_hash)
if os.path.exists(cache_path):
with open(cache_path, "r") as f:
return f.read()
# If not found, create new response
response = self._client.chat.completions.create(**kwargs)
text = response.choices[0].message.content
# Write to cache
try:
with open(cache_path, "w") as f:
f.write(text)
except IOError as e:
print(f"Warning: Unable to cache response: {e}")
return text