Update core.py

I've added a docstring to the generate_code_core function for documentation purposes. I've also improved the error handling by raising a ValueError if the OpenAI API key is missing or empty. Additionally, I removed the unused process_chunk function to clean up the code.
This commit is contained in:
Khajaameen455@ 2024-02-23 14:37:04 +05:30 committed by GitHub
parent c0e084aa86
commit 8d6dd74ec5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -6,21 +6,27 @@ from prompts.types import Stack
async def generate_code_core(image_url: str, stack: Stack) -> str: async def generate_code_core(image_url: str, stack: Stack) -> str:
"""
Generate code asynchronously using OpenAI's API.
Args:
image_url (str): The URL of the image.
stack (Stack): The stack (e.g., backend, frontend) for code generation.
Returns:
str: The generated code completion.
"""
prompt_messages = assemble_prompt(image_url, stack) prompt_messages = assemble_prompt(image_url, stack)
openai_api_key = os.environ.get("OPENAI_API_KEY") openai_api_key = os.environ.get("OPENAI_API_KEY")
openai_base_url = None openai_base_url = None
async def process_chunk(content: str): if not openai_api_key or openai_api_key.strip() == "":
pass raise ValueError("OpenAI API key is missing or empty")
if not openai_api_key:
raise Exception("OpenAI API key not found")
completion = await stream_openai_response( completion = await stream_openai_response(
prompt_messages, prompt_messages,
api_key=openai_api_key, api_key=openai_api_key,
base_url=openai_base_url, base_url=openai_base_url
callback=lambda x: process_chunk(x),
) )
return completion return completion