(ds, output_path)
| 28 | BAIDU_SECRET_KEY = os.getenv("BAIDU_SECRET_KEY") |
| 29 | |
| 30 | def o1_api(ds, output_path): |
| 31 | if OPENAI_BASE_URL is None or OPENAI_API_KEY is None: |
| 32 | print("Please set OPENAI_API_KEY and OPENAI_BASE_URL environment variables.") |
| 33 | return |
| 34 | client = AsyncOpenAI(base_url=OPENAI_BASE_URL, api_key=OPENAI_API_KEY) |
| 35 | model = "o1-preview-2024-09-12" |
| 36 | MAX_CONCURRENT = 16 |
| 37 | |
| 38 | @retry(stop=stop_after_attempt(10), wait=wait_exponential(min=4, max=60)) |
| 39 | async def get_chat_completion(message, semaphore) -> str: |
| 40 | try: |
| 41 | async with semaphore: |
| 42 | response = await client.chat.completions.create( |
| 43 | model=model, |
| 44 | messages=[{"role": "user", "content": message["question"]}], |
| 45 | timeout=80 |
| 46 | ) |
| 47 | message["pred"] = response.choices[0].message.content |
| 48 | return message |
| 49 | except Exception as e: |
| 50 | print(f"Error in get_chat_completion for message {type(e).__name__} - {str(e)}") |
| 51 | raise |
| 52 | |
| 53 | async def request_model(prompts): |
| 54 | semaphore = asyncio.Semaphore(MAX_CONCURRENT) |
| 55 | tasks = [get_chat_completion(prompt, semaphore) for prompt in prompts] |
| 56 | |
| 57 | for future in tqdm.as_completed(tasks, total=len(tasks), desc="Processing prompts"): |
| 58 | result = await future |
| 59 | with open(output_path, "a", encoding="utf-8") as f: |
| 60 | f.write(json.dumps(result, ensure_ascii=False) + "\n") |
| 61 | |
| 62 | visited = [] |
| 63 | if os.path.exists(output_path): |
| 64 | with open(output_path, "r", encoding="utf-8") as f: |
| 65 | visited = [json.loads(line)["question"] for line in f] |
| 66 | prompts = [] |
| 67 | for item in ds["question"]: |
| 68 | if item not in visited: |
| 69 | temp = {"question": item} |
| 70 | prompts.append(temp) |
| 71 | |
| 72 | asyncio.run(request_model(prompts)) |
| 73 | |
| 74 | def gemini_api(ds, output_path, search=False): |
| 75 | if GEMINI_API_KEY is None or GEMINI_BASE_URL is None: |
nothing calls this directly
no test coverage detected