| 36 | CONTROLLER_ADDR = os.environ[f'CONTROLLER_ADDR'].split(',') |
| 37 | |
| 38 | def llm_llama(prompt: str, config: lm_config.LMConfig) -> str: |
| 39 | data = { |
| 40 | "inputs": prompt, |
| 41 | "parameters": { |
| 42 | "max_new_tokens": config.gen_config['max_tokens'], |
| 43 | "do_sample": True, |
| 44 | 'temperature': config.gen_config['temperature'], |
| 45 | 'top_p': config.gen_config['top_p'], |
| 46 | 'truncate': 4000, |
| 47 | } |
| 48 | } |
| 49 | for _ in range(5): |
| 50 | try: |
| 51 | response = requests.post( |
| 52 | random.choice(CONTROLLER_ADDR) + "/generate", |
| 53 | json=data, |
| 54 | timeout=120, |
| 55 | proxies={'http': '', 'https': ''}, |
| 56 | ) |
| 57 | text = response.json()["generated_text"] |
| 58 | return text |
| 59 | # if timeout or connection error, retry |
| 60 | except Timeout: |
| 61 | print("Timeout, retrying...") |
| 62 | except ConnectionError: |
| 63 | print("Connection error, retrying...") |
| 64 | except Exception as e: |
| 65 | traceback.print_exc() |
| 66 | try: |
| 67 | print(response) |
| 68 | print(response.text) |
| 69 | except: |
| 70 | pass |
| 71 | time.sleep(5) |
| 72 | else: |
| 73 | raise Exception("Timeout after 5 retries.") |
| 74 | |
| 75 | class Agent: |
| 76 | """Base class for the agent""" |