(model: str, batch: list, temperature: float, n:int)
| 50 | |
| 51 | |
| 52 | def ask_llm(model: str, batch: list, temperature: float, n:int): |
| 53 | n_repeat = 0 |
| 54 | while True: |
| 55 | try: |
| 56 | if model in LLM.TASK_COMPLETIONS: |
| 57 | # TODO: self-consistency in this mode |
| 58 | assert n == 1 |
| 59 | response = ask_completion(model, batch, temperature) |
| 60 | elif model in LLM.TASK_CHAT: |
| 61 | # batch size must be 1 |
| 62 | assert len(batch) == 1, "batch must be 1 in this mode" |
| 63 | messages = [{"role": "user", "content": batch[0]}] |
| 64 | response = ask_chat(model, messages, temperature, n) |
| 65 | response['response'] = [response['response']] |
| 66 | break |
| 67 | except openai.error.RateLimitError: |
| 68 | n_repeat += 1 |
| 69 | print(f"Repeat for the {n_repeat} times for RateLimitError", end="\n") |
| 70 | time.sleep(1) |
| 71 | continue |
| 72 | except json.decoder.JSONDecodeError: |
| 73 | n_repeat += 1 |
| 74 | print(f"Repeat for the {n_repeat} times for JSONDecodeError", end="\n") |
| 75 | time.sleep(1) |
| 76 | continue |
| 77 | except Exception as e: |
| 78 | n_repeat += 1 |
| 79 | print(f"Repeat for the {n_repeat} times for exception: {e}", end="\n") |
| 80 | time.sleep(1) |
| 81 | continue |
| 82 | |
| 83 | return response |
| 84 |
nothing calls this directly
no test coverage detected