|
| 1 | +from lazyops import get_logger, timer |
| 2 | +from lazyops.apis import LazyAPI |
| 3 | + |
| 4 | +## Eleuther.ai API |
| 5 | +## Source: https://github.com/kingoflolz/mesh-transformer-jax/#gpt-j-6b |
| 6 | + |
| 7 | +eai_config = { |
| 8 | + 'url': 'https://api.eleuther.ai', |
| 9 | + 'token': None, |
| 10 | + 'default_params': {'top_p': 0.9, 'temp': 1}, |
| 11 | + 'params_key': None, |
| 12 | + 'data_key': 'context', |
| 13 | + 'default_fetch': 'complete', |
| 14 | + 'default_async': 'complete_async', |
| 15 | + 'route_config': { |
| 16 | + 'complete': { |
| 17 | + 'method': 'POST', |
| 18 | + 'path': '/complete', |
| 19 | + 'params': {'top_p': 0.9, 'temp': 1}, |
| 20 | + 'params_key': None, |
| 21 | + 'data_key': 'context', |
| 22 | + 'is_async': False, |
| 23 | + 'prefix_payload': False, |
| 24 | + 'decode_json': True, |
| 25 | + }, |
| 26 | + 'complete_async': { |
| 27 | + 'method': 'POST', |
| 28 | + 'path': '/complete', |
| 29 | + 'params': {'top_p': 0.9, 'temp': 1}, |
| 30 | + 'params_key': None, |
| 31 | + 'data_key': 'context', |
| 32 | + 'is_async': True, |
| 33 | + 'prefix_payload': False, |
| 34 | + 'decode_json': True, |
| 35 | + }, |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +# TextSynth API |
| 40 | +# Source: https://bellard.org/textsynth/ |
| 41 | + |
| 42 | + |
| 43 | +textsynth_config = { |
| 44 | + 'url': 'https://bellard.org/textsynth/api/v1/engines/gptj_6B', |
| 45 | + 'token': None, |
| 46 | + 'default_params': {'top_p': 0.9, 'temperature': 1, 'seed': 0, 'stream': False, 'top_k': 40}, |
| 47 | + 'params_key': None, |
| 48 | + 'data_key': 'prompt', |
| 49 | + 'default_fetch': 'complete', |
| 50 | + 'default_async': 'complete_async', |
| 51 | + 'route_config': { |
| 52 | + 'complete': { |
| 53 | + 'method': 'POST', |
| 54 | + 'path': '/completions', |
| 55 | + 'params': {'top_p': 0.9, 'temperature': 1, 'seed': 0, 'stream': False, 'top_k': 40}, |
| 56 | + 'params_key': None, |
| 57 | + 'data_key': 'prompt', |
| 58 | + 'is_async': False, |
| 59 | + 'prefix_payload': False, |
| 60 | + 'decode_json': True, |
| 61 | + }, |
| 62 | + 'complete_async': { |
| 63 | + 'method': 'POST', |
| 64 | + 'path': '/completions', |
| 65 | + 'params': {'top_p': 0.9, 'temperature': 1, 'seed': 0, 'stream': False, 'top_k': 40}, |
| 66 | + 'params_key': None, |
| 67 | + 'data_key': 'prompt', |
| 68 | + 'is_async': True, |
| 69 | + 'prefix_payload': False, |
| 70 | + 'decode_json': True, |
| 71 | + }, |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +logger = get_logger('LazyEXP', 'GPTJ') |
| 76 | + |
| 77 | +class GPTJAPI: |
| 78 | + def __init__(self, fallback='eai'): |
| 79 | + self.eai = LazyAPI.build(eai_config) |
| 80 | + self._eai_config = {'top_p': 0.9, 'temp': 1} |
| 81 | + self.ts = LazyAPI.build(textsynth_config) |
| 82 | + self._ts_config = {'top_p': 0.9, 'temperature': 1, 'seed': 0, 'stream': False, 'top_k': 40} |
| 83 | + self._fallback = fallback |
| 84 | + |
| 85 | + @property |
| 86 | + def primary(self): |
| 87 | + if self._fallback == 'eai': |
| 88 | + return self.ts |
| 89 | + return self.eai |
| 90 | + |
| 91 | + @property |
| 92 | + def fallback(self): |
| 93 | + if self._fallback == 'eai': |
| 94 | + return self.eai |
| 95 | + return self.ts |
| 96 | + |
| 97 | + def get_base_params(self, is_fallback=False): |
| 98 | + if is_fallback: |
| 99 | + if self._fallback == 'eai': |
| 100 | + return self._eai_config.copy() |
| 101 | + return self._ts_config.copy() |
| 102 | + if self._fallback == 'eai': |
| 103 | + return self._ts_config.copy() |
| 104 | + return self._eai_config.copy() |
| 105 | + |
| 106 | + def build_params(self, text, is_fallback=False, **config): |
| 107 | + p = self.get_base_params(is_fallback) |
| 108 | + if config: |
| 109 | + for k,v in config.items(): |
| 110 | + if k in p: |
| 111 | + config[k] = v |
| 112 | + return p |
| 113 | + |
| 114 | + def predict(self, text, **config): |
| 115 | + p = self.build_params(text, **config) |
| 116 | + try: |
| 117 | + res = self.primary.complete(data=text, **p) |
| 118 | + #print(res) |
| 119 | + return res.get('completion', res.get('text','')), res |
| 120 | + |
| 121 | + except Exception as e: |
| 122 | + logger.error(f'Error Calling API: {str(e)}') |
| 123 | + p = self.build_params(text, is_fallback=True, **config) |
| 124 | + try: |
| 125 | + res = self.fallback.complete(data=text, **p) |
| 126 | + return res.get('text', res.get('completion','')), res |
| 127 | + except Exception as e: |
| 128 | + logger.error(f'Error Calling API: {str(e)}') |
| 129 | + return None, None |
| 130 | + |
| 131 | + async def as_predict(self, text, **config): |
| 132 | + p = self.build_params(text, **config) |
| 133 | + try: |
| 134 | + res = await self.primary.complete_async(**p) |
| 135 | + return res.get('completion', res.get('text','')), res |
| 136 | + |
| 137 | + except Exception as e: |
| 138 | + logger.error(f'Error Calling API: {str(e)}') |
| 139 | + p = self.build_params(text, is_fallback=True, **config) |
| 140 | + try: |
| 141 | + res = await self.fallback.complete_async(**p) |
| 142 | + return res.get('text', res.get('completion','')), res |
| 143 | + except Exception as e: |
| 144 | + logger.error(f'Error Calling API: {str(e)}') |
| 145 | + return None, None |
| 146 | + |
| 147 | + |
| 148 | + |
| 149 | + |
| 150 | +if __name__ == '__main__': |
| 151 | + test_texts = [ |
| 152 | + 'In a shocking finding, scientist discovered a herd of unicorns living in a remote, previously unexplored valley, in the Andes Mountains. Even more surprising to the researchers was the fact that the unicorns spoke perfect English.', |
| 153 | + 'Game of Thrones is', |
| 154 | + 'For todays homework assignment, please describe the reasons for the US Civil War.' |
| 155 | + ] |
| 156 | + api = GPTJAPI() |
| 157 | + for txt in test_texts: |
| 158 | + res, _ = api.predict(text=txt) |
| 159 | + logger.info(res) |
| 160 | + |
| 161 | + |
| 162 | + |
| 163 | + |
| 164 | + |
| 165 | + |
| 166 | + |
0 commit comments