(
self,
path: str,
token: str,
url: str,
meta_template: Optional[Dict] = None,
query_per_second: int = 1,
retry: int = 3,
generation_kwargs: Dict = {},
max_seq_len=4096,
)
| 53 | """ |
| 54 | |
| 55 | def __init__( |
| 56 | self, |
| 57 | path: str, |
| 58 | token: str, |
| 59 | url: str, |
| 60 | meta_template: Optional[Dict] = None, |
| 61 | query_per_second: int = 1, |
| 62 | retry: int = 3, |
| 63 | generation_kwargs: Dict = {}, |
| 64 | max_seq_len=4096, |
| 65 | ): |
| 66 | super().__init__( |
| 67 | path=path, |
| 68 | max_seq_len=max_seq_len, |
| 69 | query_per_second=query_per_second, |
| 70 | meta_template=meta_template, |
| 71 | retry=retry, |
| 72 | generation_kwargs=generation_kwargs, |
| 73 | ) |
| 74 | |
| 75 | self.logger.info(f'Bailing API Model Init path: {path} url={url}') |
| 76 | if not token: |
| 77 | token = os.environ.get('BAILING_API_KEY') |
| 78 | if token: |
| 79 | self._headers = {'Authorization': f'Bearer {token}'} |
| 80 | else: |
| 81 | raise RuntimeError('There is not valid token.') |
| 82 | else: |
| 83 | self._headers = {'Authorization': f'Bearer {token}'} |
| 84 | |
| 85 | self._headers['Content-Type'] = 'application/json' |
| 86 | self._url = (url if url else |
| 87 | 'https://bailingchat.alipay.com/chat/completions') |
| 88 | self._model = path |
| 89 | self._sessions = [] |
| 90 | self._num = (int(os.environ.get('BAILING_API_PARALLEL_NUM')) |
| 91 | if os.environ.get('BAILING_API_PARALLEL_NUM') else 1) |
| 92 | try: |
| 93 | for _ in range(self._num): |
| 94 | adapter = HTTPAdapterWithSocketOptions() |
| 95 | sess = requests.Session() |
| 96 | sess.mount('http://', adapter) |
| 97 | sess.mount('https://', adapter) |
| 98 | self._sessions.append(sess) |
| 99 | except Exception as e: |
| 100 | self.logger.error(f'Fail to setup the session. {e}') |
| 101 | raise e |
| 102 | |
| 103 | def generate( |
| 104 | self, |
no test coverage detected