(prompt, log_id=None, max_tokens=10000, max_retries=3)
| 70 | |
| 71 | |
| 72 | def request_claude_token(prompt, log_id=None, max_tokens=10000, max_retries=3): |
| 73 | base_url = cfg("claude", "base_url") |
| 74 | api_key = cfg("claude", "api_key") |
| 75 | client = OpenAI(base_url=base_url, api_key=api_key) |
| 76 | |
| 77 | if log_id is None: |
| 78 | log_id = generate_log_id() |
| 79 | |
| 80 | extra_headers = {"X-TT-LOGID": log_id} |
| 81 | usage_info = {"prompt_tokens": 0, "completion_tokens": 0, "total_tokens": 0} |
| 82 | |
| 83 | retry_count = 0 |
| 84 | while retry_count < max_retries: |
| 85 | try: |
| 86 | completion = client.chat.completions.create( |
| 87 | model="claude-4-opus", |
| 88 | messages=[ |
| 89 | { |
| 90 | "role": "user", |
| 91 | "content": [ |
| 92 | { |
| 93 | "type": "text", |
| 94 | "text": prompt, |
| 95 | }, |
| 96 | ], |
| 97 | } |
| 98 | ], |
| 99 | max_tokens=max_tokens, |
| 100 | extra_headers=extra_headers, |
| 101 | ) |
| 102 | # --- MODIFIED: token usage --- |
| 103 | if completion.usage: |
| 104 | usage_info["prompt_tokens"] = completion.usage.prompt_tokens |
| 105 | usage_info["completion_tokens"] = completion.usage.completion_tokens |
| 106 | usage_info["total_tokens"] = completion.usage.total_tokens |
| 107 | return completion, usage_info |
| 108 | |
| 109 | except Exception as e: |
| 110 | retry_count += 1 |
| 111 | if retry_count >= max_retries: |
| 112 | raise Exception(f"Failed after {max_retries} attempts. Last error: {str(e)}") |
| 113 | |
| 114 | # Exponential backoff with jitter |
| 115 | delay = (2**retry_count) * 0.1 + (random.random() * 0.1) |
| 116 | print( |
| 117 | f"Request failed with error: {str(e)}. Retrying in {delay:.2f} seconds... (Attempt {retry_count}/{max_retries})" |
| 118 | ) |
| 119 | time.sleep(delay) |
| 120 | |
| 121 | return None, usage_info |
| 122 | |
| 123 | |
| 124 | def request_gemini_with_video(prompt: str, video_path: str, log_id=None, max_tokens: int = 10000, max_retries: int = 3): |
nothing calls this directly
no test coverage detected