basic.py 885 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. from applications.config import MODEL_CONFIG, LOCAL_MODEL_CONFIG
  2. from applications.utils import AsyncHttpClient
  3. async def get_basic_embedding(text: str, model: str):
  4. """
  5. embedding text into vectors
  6. :param text:
  7. :param model:
  8. :return:tong
  9. """
  10. cfg = MODEL_CONFIG[model]
  11. async with AsyncHttpClient(timeout=20) as client:
  12. response = await client.post(
  13. url=cfg["url"],
  14. json={"input": text, "model": model},
  15. headers={"Content-Type": "application/json"},
  16. )
  17. return response['data'][0]["embedding"]
  18. async def get_local_embedding(text, model):
  19. """
  20. embedding text into vectors
  21. :param text:
  22. :param model:
  23. :return:
  24. """
  25. outputs = model.get_embedding([text])
  26. embedding = outputs[0]
  27. return embedding
  28. __all__ = [
  29. "get_basic_embedding",
  30. "get_local_embedding"
  31. ]