duckling.py 733 B

12345678910111213141516171819202122232425
  1. import os
  2. from httpx import AsyncClient, URL
  3. from app.core.config import settings
  4. from app.services.service import api_exception
  5. class Duckling:
  6. """
  7. Duckling is a Haskell library that parses text into structured data.
  8. """
  9. def __init__(self, client: AsyncClient, server_settings=settings):
  10. super(Duckling, self).__init__()
  11. self._client = client
  12. self._host = URL(os.getenv("DUCKLING_HOST", settings.DUCKLING_HOST))
  13. @api_exception
  14. async def parse(self, text: str, locale: str = "zh_CN") -> dict:
  15. url = self._host.join("parse")
  16. data = {"locale": locale, "text": text}
  17. raw_response = await self._client.post(url, data=data)
  18. return raw_response.json()