base_online_platform.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import os
  2. from foundation.base.config import config_handler
  3. from foundation.logger.loggering import server_logger
  4. from openai import OpenAI
  5. class BaseApiPlatform:
  6. def __init__(self):
  7. self.api_key = os.getenv("API_KEY")
  8. self.headers = {
  9. "Authorization": f"Bearer {self.api_key}",
  10. "Content-Type": "application/json"
  11. }
  12. def get_openai_client(self , model_server_url , api_key):
  13. """
  14. 获取openai模型 client
  15. """
  16. server_logger.info(f"get_openai_client -> model_server_url:{model_server_url},api_key:{api_key}")
  17. # 若没有配置环境变量,请用百炼API Key将下行替换为:api_key="sk-xxx",
  18. client = OpenAI(
  19. api_key=api_key,
  20. base_url=model_server_url,
  21. )
  22. return client
  23. def get_chat_model(self):
  24. """
  25. 获取chat模型
  26. :return:
  27. """
  28. raise NotImplementedError
  29. def get_embeddings(self, texts: list[str]):
  30. """
  31. 向量化文本
  32. :param texts: 文本列表
  33. :return: 向量列表
  34. """
  35. raise NotImplementedError
  36. def rerank(self, input_query: str, documents: list, top_n: int = 5, return_documents: bool = True):
  37. """
  38. 使用 BGE 重排序模型进行相关性打分
  39. 使用重排序模型对候选文档进行排序
  40. :param query: 用户查询语句
  41. :param documents: 候选文本列表
  42. :param top_n: 返回前 N 个结果
  43. :return: 排序后的结果列表,包含文本和相似度分数
  44. """
  45. raise NotImplementedError