A call experience of the OpenApi interface (with code)

Go get an api_key: https://platform.openai.com/account/api-keys 

 First look at all available models:


import openai

openai.api_key = 'sk-3MXseSpzjlPaPg0jKMPuT3BlbkFJ70WHA5twkr'

# 列出所有GPT-3模型
models = openai.Model.list()
for model in models["data"]:
    print(model["id"])

return:

babbage davinci text-davinci-edit-001 babbage-code-search-code text-similarity-babbage-001 code-davinci-edit-001 text-davinci-001 ada curie-instruct-beta babbage-code-search-text babbage-similarity whisper-1 code-search-babbage-text-001 text-curie-001 code-search-babbage-code-001 text-ada-001 text-embedding-ada-002 text-similarity-ada-001 ada-code-search-code ada-similarity text-davinci-003 code-search-ada-text-001 text-search-ada-query-001 davinci-search-document ada-code-search-text text-search-ada-doc-001 davinci-instruct-beta text-similarity-curie-001 code-search-ada-code-001 ada-search-query text-search-davinci-query-001 curie-search-query gpt-3.5-turbo-0301 davinci-search-query babbage-search-document ada-search-document text-search-curie-query-001 text-search-babbage-doc-001 gpt-3.5-turbo curie-search-document text-search-curie-doc-001 babbage-search-query text-babbage-001 text-search-davinci-doc-001 text-search-babbage-query-001 curie-similarity curie text-similarity-davinci-001 text-davinci-002 davinci-similarity cushman:2020-05-03 ada:2020-05-03 babbage:2020-05-03 curie:2020-05-03 davinci:2020-05-03 if-davinci-v2 if-curie-v2 if-davinci:3.0.0 davinci-if:3.0.0 davinci-instruct-beta:2.0.0 text-ada:001 text-davinci:001 text-curie:001 text-babbage:001

You can use most of these models. However, some models may require different API keys or use different API endpoints. For example, some models can only be used for text generation, while others can only be used for encoding or search. You will need to review the OpenAI documentation carefully to understand the purpose and limitations of each model, and to ensure that your code calls the chosen model correctly.

It's worth noting that some models may require a higher OpenAI subscription level for better performance and additional features. For example, some models may need to use a larger maximum generation length, while other models may need to use more API calls for better text generation quality. If you want to use these features, you will need to upgrade your OpenAI subscription.

Does the gpt-3.5-turbo model require a subscription?

Yes, the gpt-3.5-turbo model requires a paid subscription and requires a higher OpenAI subscription level. If you want to use the gpt-3.5-turbo model, you need to have OpenAI's Developer Pro or Team Pro subscription to use it. At the same time, please note that using this model will incur relatively high fees. So, before using the model, make sure you understand OpenAI's pricing plans and budget accordingly.

Then choose this model: davinci

import openai
import time

openai.api_key = 'sk-3MXseSpzjlPaPg0jKMPuT3BlbkFJ70WHA5twkr'

def ask_openai(question, model, max_tokens=50):
    """Asks a question to OpenAI's API and returns the response."""
    prompt = f"Q: {question}\nA:"
    response = openai.Completion.create(
        engine=model,
        prompt=prompt,
        max_tokens=max_tokens,
        n=1,
        stop=None,
        temperature=0.7,
    )
    answer = response.choices[0].text.strip()
    return answer

model = "davinci"
while True:
    question = input("You: ")
    if question.lower() == "exit":
        break
    start_time = time.time()
    answer = ask_openai(question, model)
    end_time = time.time()
    print(f"Bot: {answer} ({end_time - start_time:.2f} seconds)")

 Forget it, no need. . .

Guess you like

Origin blog.csdn.net/liuruiaaa/article/details/129816565