Editing
AI for Code Generation
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== <span style="color: #FFFFFF;">Applying</span> == '''Code generation with the OpenAI API:''' <syntaxhighlight lang="python"> from openai import OpenAI client = OpenAI() def generate_and_test(spec: str, test_cases: list[dict]) -> dict: """Generate code from spec, test it, and return results.""" # Step 1: Generate code response = client.chat.completions.create( model="gpt-4o", messages=[ {"role": "system", "content": "You are an expert Python programmer. " "Write clean, well-documented, production-quality code. " "Include only the function implementation, no explanation."}, {"role": "user", "content": f"Implement the following:\n\n{spec}"} ], temperature=0.2 # Low temperature for deterministic code ) code = response.choices[0].message.content # Step 2: Execute and test (in a sandbox!) results = [] exec_globals = {} exec(code, exec_globals) # In production: use subprocess or sandbox for test in test_cases: func_name = test['function'] args = test['args'] expected = test['expected'] actual = exec_globals[func_name](*args) results.append({ 'passed': actual == expected, 'expected': expected, 'actual': actual }) return {'code': code, 'results': results, 'pass_rate': sum(r['passed'] for r in results) / len(results)} # Example usage spec = """ Function: merge_intervals(intervals) Given a list of intervals [start, end], merge all overlapping intervals and return the list of merged intervals sorted by start. Example: [[1,3],[2,6],[8,10],[15,18]] β [[1,6],[8,10],[15,18]] """ test_cases = [ {'function': 'merge_intervals', 'args': [[[1,3],[2,6],[8,10],[15,18]]], 'expected': [[1,6],[8,10],[15,18]]}, {'function': 'merge_intervals', 'args': [[[1,4],[4,5]]], 'expected': [[1,5]]}, ] result = generate_and_test(spec, test_cases) print(f"Pass rate: {result['pass_rate']*100:.0f}%") </syntaxhighlight> ; Code generation use case map : '''IDE integration''' β GitHub Copilot, Cursor, Continue.dev (VS Code extension) : '''Chat-based coding''' β Claude, GPT-4o, Gemini in chat with code interpreter : '''Autonomous agents''' β Devin, SWE-agent, OpenHands (multi-step task completion) : '''Code review''' β AI-powered PR review bots (GitHub Copilot PR review, CodeRabbit) : '''Security scanning''' β Semgrep + AI, Snyk Code, GitHub Advanced Security : '''Documentation''' β AI docstring generation, README writing, API doc generation </div> <div style="background-color: #8B4500; color: #FFFFFF; padding: 20px; border-radius: 8px; margin-bottom: 15px;">
Summary:
Please note that all contributions to BloomWiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
BloomWiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information