| 23 | interval=20 |
| 24 | ) |
| 25 | def connect_gpt(engine, api_key, api_base, prompt, max_tokens, temperature, stop=None, task="chat"): |
| 26 | openai.api_key = api_key |
| 27 | openai.api_type = "azure" |
| 28 | openai.api_base = api_base |
| 29 | openai.api_version = "2023-05-15" |
| 30 | n_repeat = 0 |
| 31 | while True: |
| 32 | try: |
| 33 | if task == "chat": |
| 34 | response = openai.ChatCompletion.create( |
| 35 | engine=engine, messages=[{"role": "user", "content": f"{prompt}"}], temperature=temperature) |
| 36 | result = response['choices'][0]['message']['content'] |
| 37 | |
| 38 | elif task == "completion": |
| 39 | response = openai.Completion.create(engine=engine, prompt=prompt, |
| 40 | max_tokens=max_tokens, temperature=temperature, stop=stop) |
| 41 | result = response['choices'][0]['text'] |
| 42 | break |
| 43 | except openai.error.RateLimitError: |
| 44 | n_repeat += 1 |
| 45 | print(f"Repeat for the {n_repeat} times for RateLimitError", end="\n") |
| 46 | time.sleep(1) |
| 47 | if n_repeat >= 30: |
| 48 | result = f"error, exception: RateLimitError" |
| 49 | break |
| 50 | continue |
| 51 | except json.decoder.JSONDecodeError: |
| 52 | n_repeat += 1 |
| 53 | print(f"Repeat for the {n_repeat} times for JSONDecodeError", end="\n") |
| 54 | time.sleep(1) |
| 55 | if n_repeat >= 10: |
| 56 | result = f"error, exception: JSONDecodeError" |
| 57 | break |
| 58 | continue |
| 59 | except Exception as e: |
| 60 | n_repeat += 1 |
| 61 | print(f"Repeat for the {n_repeat} times for exception: {e}", end="\n") |
| 62 | time.sleep(1) |
| 63 | if n_repeat >= 10: |
| 64 | result = f"error, exception: {e}" |
| 65 | break |
| 66 | continue |
| 67 | return result |
| 68 | |
| 69 | |
| 70 | def ask_llm(prompt, temp=None): |