1. Setting up OpenAI API credentials:
import openai_secret_manager

secrets = openai_secret_manager.get_secret("openai")

openai.api_key = secrets["api_key"]
  1. Creating a new GPT-3 instance:
import openai

openai.api_key = "YOUR API KEY HERE"

model_engine = "text-davinci-002"

instance = openai.Model.create(model=model_engine)
  1. Generating text with GPT-3:
import openai

openai.api_key = "YOUR API KEY HERE"

model_engine = "text-davinci-002"
prompt = "What is the meaning of life?"

response = openai.Completion.create(
    engine=model_engine,
    prompt=prompt,
    max_tokens=50
)

print(response.choices[0].text)
  1. Analyzing text sentiment with OpenAI:
import openai

openai.api_key = "YOUR API KEY HERE"

model_engine = "text-davinci-002"
text = "I am feeling happy today."

analysis = openai.Analysis.create(
    engine=model_engine,
    prompt=text,
    model="sentiment"
)

print(analysis.sentiment)
  1. Creating a new OpenAI classification model:
import openai

openai.api_key = "YOUR API KEY HERE"

model_engine = "text-davinci-002"
model_name = "my-classification-model"

new_model = openai.Model.create(
    engine=model_engine,
    name=model_name,
    training_data=[],
    training_configuration={
        "epochs": 5,
        "batch_size": 128
    }
)

print(new_model.id)
  1. Training an OpenAI classification model:
import openai

openai.api_key = "YOUR API KEY HERE"

model_id = "YOUR MODEL ID HERE"
training_data = [
    ["This is a positive example", "positive"],
    ["This is a negative example", "negative"]
]

training = openai.Model.train(
    model=model_id,
    examples=training_data,
    epochs=5,
    batch_size=128
)

print(training.id)
  1. Generating text with an OpenAI classification model:
import openai

openai.api_key = "YOUR API KEY HERE"

model_id = "YOUR MODEL ID HERE"
prompt = "This is a positive example"

response = openai.Completion.create(
    model=model_id,
    prompt=prompt,
    max_tokens=50
)

print(response.choices[0].text)
  1. Analyzing text with OpenAI's semantic search:
import openai

openai.api_key = "YOUR API KEY HERE"

model_engine = "text-davinci-002"
corpus = ["apple", "banana", "cherry"]
query = "I want to buy a fruit"

results = openai.Completion.create(
    engine=model_engine,
    prompt=query,
    max_tokens=50,
    search_model=model_engine,
    model=model_engine,
    documents=corpus,
    return_metadata=True
)

for result in results.choices:
    print(result.text)
    print(result.metadata["document"])
  1. Generating text with OpenAI's Codex:
import openai

openai.api_key = "YOUR API KEY HERE"

model_engine = "davinci-codex-002"
code = "import numpy\n\nx = numpy.array([1, 2, 3])\nprint(x)"

response = openai.Completion.create(
    engine=model_engine,
    prompt=code,
    max_tokens=50
)

print(response.choices[0].text)
  1. Generating text with OpenAI's DALL-E:
import openai

openai.api_key = "YOUR API KEY HERE"

model_engine = "image-alpha-001"
prompt = "a chair made of pizza"

response = openai.Completion.create(
    engine=model_engine,
    prompt=prompt,
    max_tokens=50
)

print(response.choices[0].text)
Write 10 Sample codes, using openai and python

原文地址: https://www.cveoy.top/t/topic/jhq 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录