Team Ai
Apppublic

mnbrshd/CodeGenerator

sourceHugging Faceccupdated 3y agoView on Hugging Face
0likes
solutions.py54 linesDownload Raw Back to oalib
1"""Library with OpenAI API solutions as functions2 3References:4 5For building code:  https://beta.openai.com/docs/guides/code/introduction6 7"""8 9import openai10import os11 12 13def submit_question(text):14    """This submits a question to the OpenAI API"""15 16    openai.api_key = os.getenv("OPENAI_API_KEY")17    prompt = text18 19    result = openai.completions.create(20        prompt=prompt,21        temperature=0,22        max_tokens=300,23        top_p=1,24        frequency_penalty=0,25        presence_penalty=0,26        model="davinci-002",27    )28    return result.choices[0].text.strip(" \n")29 30 31# build a function that converts a comment into code in any language32def create_code(text, language):33    """This submits a comment to the OpenAI API to create code in any languag34 35    Example:36        language = '# Python3'37        text = f"Calculate the mean distance between an array of points"38        create_code(text, language)39 40    """41    openai.api_key = os.getenv("OPENAI_API_KEY")42    prompt = f"## {language}\n\n{text}"43 44    result = openai.completions.create(45        prompt=prompt,46        temperature=0,47        max_tokens=300,48        top_p=1,49        frequency_penalty=0,50        presence_penalty=0,51        model="gpt-3.5-turbo-instruct",52    )53    return result.choices[0].text.strip(" \n")54