MCPcopy Index your code
hub / github.com/showlab/Code2Video / request_gpt4o

Function request_gpt4o

src/gpt_request.py:482–542  ·  view source on GitHub ↗

Makes a request to the gpt-4o-2024-11-20 model with retry functionality. Args: prompt (str): The text prompt to send to the model log_id (str, optional): The log ID for tracking requests, defaults to tkb+timestamp max_tokens (int, optional): Maximum tokens for respo

(prompt, log_id=None, max_tokens=8000, max_retries=3)

Source from the content-addressed store, hash-verified

480
481
482def request_gpt4o(prompt, log_id=None, max_tokens=8000, max_retries=3):
483 """
484 Makes a request to the gpt-4o-2024-11-20 model with retry functionality.
485
486 Args:
487 prompt (str): The text prompt to send to the model
488 log_id (str, optional): The log ID for tracking requests, defaults to tkb+timestamp
489 max_tokens (int, optional): Maximum tokens for response, default 8000
490 max_retries (int, optional): Maximum number of retry attempts, default 3
491
492 Returns:
493 dict: The model's response
494 """
495
496 base_url = cfg("gpt4o", "base_url")
497 api_version = cfg("gpt4o", "api_version")
498 ak = cfg("gpt4o", "api_key")
499 model_name = cfg("gpt4o", "model")
500
501 client = openai.AzureOpenAI(
502 azure_endpoint=base_url,
503 api_version=api_version,
504 api_key=ak,
505 )
506
507 if log_id is None:
508 log_id = generate_log_id()
509
510 extra_headers = {"X-TT-LOGID": log_id}
511
512 retry_count = 0
513 while retry_count < max_retries:
514 try:
515 completion = client.chat.completions.create(
516 model=model_name,
517 messages=[
518 {
519 "role": "user",
520 "content": [
521 {
522 "type": "text",
523 "text": prompt,
524 },
525 ],
526 }
527 ],
528 max_tokens=max_tokens,
529 extra_headers=extra_headers,
530 )
531 return completion.choices[0].message.content
532 except Exception as e:
533 retry_count += 1
534 if retry_count >= max_retries:
535 raise Exception(f"Failed after {max_retries} attempts. Last error: {str(e)}")
536
537 # Exponential backoff with jitter
538 delay = (2**retry_count) * 0.1 + (random.random() * 0.1)
539 print(

Callers

nothing calls this directly

Calls 2

cfgFunction · 0.85
generate_log_idFunction · 0.85

Tested by

no test coverage detected