Skip to content

Instantly share code, notes, and snippets.

@itrobotics
Last active November 21, 2023 01:59

Revisions

  1. itrobotics revised this gist Nov 21, 2023. 1 changed file with 21 additions and 11 deletions.
    32 changes: 21 additions & 11 deletions openai-test.py
    Original file line number Diff line number Diff line change
    @@ -1,13 +1,23 @@
    os.environ['OPENAI_API_KEY'] = "your_openai_api_key"
    from openai import OpenAI
    client = OpenAI()

    completion = client.chat.completions.create(
    model="gpt-3.5-turbo",
    messages=[
    {"role": "system", "content": "You are a poetic assistant, skilled in explaining complex programming concepts with creative flair."},
    {"role": "user", "content": "Compose a poem that explains the concept of recursion in programming."}
    ]
    import openai

    # Replace 'YOUR_API_KEY' with your actual API key from OpenAI
    api_key = 'YOUR_API_KEY'

    # Initialize the OpenAI API client
    openai.api_key = api_key

    # Specify the prompt for text generation
    prompt = "Once upon a time,"

    # Generate text using the GPT-3 model
    response = openai.Completion.create(
    engine="text-davinci-002", # You can choose different engines based on your use case
    prompt=prompt,
    max_tokens=50, # Adjust the number of tokens in the generated text
    )

    print(completion.choices[0].message)
    # Extract the generated text from the response
    generated_text = response.choices[0].text

    # Print the generated text
    print(generated_text)
  2. itrobotics revised this gist Nov 21, 2023. No changes.
  3. itrobotics created this gist Nov 21, 2023.
    13 changes: 13 additions & 0 deletions openai-test.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    os.environ['OPENAI_API_KEY'] = "your_openai_api_key"
    from openai import OpenAI
    client = OpenAI()

    completion = client.chat.completions.create(
    model="gpt-3.5-turbo",
    messages=[
    {"role": "system", "content": "You are a poetic assistant, skilled in explaining complex programming concepts with creative flair."},
    {"role": "user", "content": "Compose a poem that explains the concept of recursion in programming."}
    ]
    )

    print(completion.choices[0].message)