| 75 | return num_tokens |
| 76 | |
| 77 | def llm_gpt(prompt: List[Dict[str, str]], model: str) -> str: |
| 78 | if not 'OPENAI_API_KEY' in os.environ: |
| 79 | raise ValueError("OPENAI_API_KEY must be set to eval GPT models.") |
| 80 | |
| 81 | for _ in range(3): |
| 82 | try: |
| 83 | openai_api_key = os.environ['OPENAI_API_KEY'] |
| 84 | openai_api_base = os.environ.get('OPENAI_API_BASE', 'https://api.openai.com/v1') |
| 85 | response = requests.post( |
| 86 | openai_api_base + "/chat/completions", |
| 87 | headers={ |
| 88 | 'Authorization': f'Bearer {openai_api_key}' |
| 89 | }, |
| 90 | json={ |
| 91 | 'model': model, |
| 92 | 'messages': prompt, |
| 93 | 'temperature': 0.0, |
| 94 | 'max_tokens': 256, |
| 95 | }, |
| 96 | timeout=120, |
| 97 | ) |
| 98 | text = response.json()['choices'][0]['message']['content'] |
| 99 | print(text) |
| 100 | return text.strip() |
| 101 | # if timeout or connection error, retry |
| 102 | except Timeout: |
| 103 | print("Timeout, retrying...") |
| 104 | except ConnectionError: |
| 105 | print("Connection error, retrying...") |
| 106 | except Exception: |
| 107 | traceback.print_exc() |
| 108 | try: |
| 109 | print(response) |
| 110 | print(response.text) |
| 111 | except: |
| 112 | pass |
| 113 | time.sleep(5) |
| 114 | else: |
| 115 | raise Exception("Timeout after 3 retries.") |
| 116 | |
| 117 | def llm_tgi(prompt: str) -> str: |
| 118 | data = { |