Open AI API experience

open AI API

introduction

OpenAI provides an API interface that allows third-party developers to integrate models such as GPT-3 into their applications and services, which is more interactive and flexible. Compared with the chat interface provided by Chat GPT, OpenAI API provides a variety of options and settings that developers can use to customize the behavior of the model, such as the type of model, model parameters and task definition, etc. On March 1, 2023, OpenAI opened the API for the GPT-3.5 Turbo model, which is said to be the model being used in ChatGPT. The fee for using the API is $0.002 per 1,000 tokens, which is about 2 cents and 700 characters in Chinese.

concept

Tokens

Traditionally, the GPT model uses unstructured text, which is represented as a series of "token" identifiers in the model. The open AI model decomposes the text into tokens for understanding and processing, which is also the unit of model billing. Tokens can be words or blocks of characters. For example, a short and common word like "pear" is a single token, while the word "hamburger" is broken down into 3 tokens "ham", "bur" and "ger". The number of tokens processed in a given API request is the sum of inputs and outputs.

The total number of tokens in an API call affects:

  1. API call fee (paid per token)
  2. How long does the API call take, because writing more tokens takes more time
  3. Whether the API call is valid, because the total token must be lower than the maximum limit of the model (gpt-3.5-turbo-0301 processing limit is 4096 tokens)

prompts

Prompts, in simple terms, means that you want the model to output what you want, first you need to describe what you want to the model. A prompt can be a short description of the task or contain one or more output examples.

Prompt may contain the following elements

  • Instruction : Tell the model the task you want it to perform, for example: "Please translate the above text into English:" is an instruction.
  • Context Contenxt : The context of the current conversation is the background and context. For example, let ChatGPT perform role-playing, and guide AI to give more accurate output content by assigning a role to AI.
  • Input Data Input Data : The object you want to process for chatgpt, such as a piece of text, a question, such as the text content translated above.
  • Output indicator Output Indicator : Tell the model the output type or format you want. For example, let the model give you a list, or let the model give you a result separated by semicolons, all belong to the Output Indicator.
你是一个多标签文本分类系统,请帮我完成中文多标签文本分类任务。
任务要求如下:对输入的句子进行多标签文本分类并按指定格式输出。
支持的分类类别仅限{
   
   { labels|length }}类:{
   
   { labels|join('、') }}。
解释及示例:{
   
   { hint }}
输出格式要求:分类标签列表。

以下是输入句子:{
   
   { text }} 
输出:

Models

The following table shows the available models in each mode of the Open AI API:

ENDPOINT MODEL NAME describe
/v1/chat/completions gpt-4, gpt-4-0314, gpt-4-32k, gpt-4-32k-0314, gpt-3.5-turbo, gpt-3.5-turbo-0301 ChatCompletion is a specific terminal point, which is mainly used to simulate human dialogue, such as chatbot, customer service dialogue and other tasks. Different from Completion, ChatCompletion needs to deal with the dialogue context, that is, for multiple questions or answers raised by the same user, it needs to take into account the previous dialogue history in order to provide a more coherent and accurate reply.
/v1/completions text-davinci-003, text-davinci-002, text-curie-001, text-babbage-001, text-ada-001 Completion is a general-purpose endpoint, mainly used to generate text, such as writing, translating, summarizing and other tasks. It takes a text prompt as input and returns a complete text.
/v1/edits text-davinci-edit-001, code-davinci-edit-001 Users can submit raw text to the API for suggested revisions and improvements. The API will return a set of editing suggestions that include replacements, deletions, insertions, and rearrangements of the original text,
/v1/audio/transcriptions whisper-1 Provides an audio file, returns a transcript of the speech at $0.006 per minute
/v1/audio/translations whisper-1 Provides an audio file, returns a transcript in another language
/v1/fine-tunes davinci, curie, babbage, ada Provide training data, set parameters such as learning rate, number of fine-tuning rounds, batch size, and fine-tune the existing GPT model
/v1/embeddings text-embedding-ada-002, text-search-ada-doc-001 Provide sentence/paragraph-level embedding. You can take one or more text inputs as a request and get a vector embedding corresponding to each text as a response. Such as text classification, semantic search, text similarity calculation, etc. Vector embedding can be considered as a representation of text in a high-dimensional space, and the relationship between texts can be judged by calculating the distance or similarity between vectors
/v1/moderations text-moderation-stable, text-moderation-latest You can input one or more texts as a request and get the audit result corresponding to each text as a response. The review result includes two parts: whether to pass the review and the reason. If the text passes the review, the "toxicity" value in the result is 0, otherwise the value is 1, and some relevant information will be returned, such as illegal vocabulary, sentence structure, sentiment analysis, etc.
#查看模型权限
import os
import openai
openai.organization = "YOUR_ORG_ID" #使用自己的ID
openai.api_key = os.getenv("OPENAI_API_KEY") #使用账号下创建的key
openai.Model.list()

manual

1. Log in to the open AI account to obtain API keys

Generate your own api_key: https://platform.openai.com/account/api-keys

There is an initial gift of 5 US dollars to use, and you need to purchase it yourself after the quota is used up. The specific charging price is related to the model.

insert image description here

2. Access environment

Install the official Python library. When calling the API, you need to prepare a scientific Internet environment or configure a proxy

pip install openai

3. API use cases

(1)Completion

import os
import openai
openai.api_key = os.getenv("OPENAI_API_KEY")
# or openai.api_key = "YOUR OPENAI_API_KEY" 

prompt = """
Decide whether a comment on CSDN sentiment is positive, neutral, or negative.
comment: I want to learn the openai more!
Sentiment:
"""

response = openai.Completion.create(
              model="text-davinci-003",
              prompt=prompt,	
              max_tokens=1024,   
              temperature=0,
              top_p=0.1
            )

print(response)

temperature: between 0 and 2. Higher values ​​(like 0.8) will make the output more random, while lower values ​​(like 0.2) will make it more focused and deterministic.

top_p: The effect is similar to temperature, where the model considers tokens with top_p probability. So 0.1 means only consider tokens containing the top 10% probability

max_tokens: The maximum number of tokens generated, plus the tokens of the prompt, cannot exceed the maximum limit of the model.

Output result:

{
  "choices": [
    {
      "finish_reason": "stop",
      "index": 0,
      "logprobs": null,
      "text": "Positive"
    }
  ],
  "created": 1681656672,
  "id": "cmpl-75y0ePNfI8aT41A03W3WpYYwfvcuD",
  "model": "text-davinci-003",
  "object": "text_completion",
  "usage": {
    "completion_tokens": 2,
    "prompt_tokens": 36,
    "total_tokens": 38
  }
}

Process finished with exit code 0


(2)ChatCompletion

content = '''
you are my assistant, Could we talk a little bit about 
the schedule today, what will we do in  the afternoon ?
'''
response = openai.ChatCompletion.create(
  model="gpt-3.5-turbo",
  messages=[{"role": "user", "content": content}],
  temperature=0.3,
  max_tokens=1048,
  top_p=1.0,
)

text = response.choices[0].message["content"]
print(text)

Output result:

Of course! Can you please provide me with some more information about your schedule today? What activities do you have planned for the morning? This will help me better understand what we can do in the afternoon.

(3)Images

The OpenAI API creates extended or generated images given an original image or hints.

response = openai.Image.create(
  prompt="A super cute girl sitting in a basket of flowers, pop mart style, chibi",
  n=1,
  size="1024x1024"
)
image_url = response['data'][0]['url']
print(image_url)

Generated image:

insert image description here

(4)Edit

edit mode if given a hint and instruction, the model will return an edited version

mycode = '''
while(!dead) { 
    code(); 
} 
'''

response = openai.Edit.create(
  model="code-davinci-edit-001",
  input=mycode,
  instruction="Fix the code mistakes and optimize it"
)

print(response)

Output result:

{
  "choices": [
    {
      "index": 0,
      "text": "while(!dead) \n    code();\n"
    }
  ],
  "created": 1681659196,
  "object": "edit",
  "usage": {
    "completion_tokens": 41,
    "prompt_tokens": 35,
    "total_tokens": 76
  }
}

Process finished with exit code 0

Commonly used model description:

LATEST MODEL DESCRIPTION MAX TOKENS TRAINING DATA
gpt-3.5-turbo Most capable GPT-3.5 model and optimized for chat at 1/10th the cost of text-davinci-003. Will be updated with our latest model iteration. 4,096 tokens Up to Sep 2021
gpt-3.5-turbo-0301 Snapshot of gpt-3.5-turbo from March 1st 2023. Unlike gpt-3.5-turbo, this model will not receive updates, and will only be supported for a three month period ending on June 1st 2023. 4,096 tokens Up to Sep 2021
text-davinci-003 Can do any language task with better quality, longer output, and consistent instruction-following than the curie, babbage, or ada models. Also supports [inserting] completions within text. 4,097 tokens Up to Jun 2021
text-davinci-002 Similar capabilities to text-davinci-003 but trained with supervised fine-tuning instead of reinforcement learning 4,097 tokens Up to Jun 2021
code-davinci-002 Optimized for code-completion tasks 8,001 tokens Up to Jun 2021

reference

https://platform.openai.com/docs

https://www.dataapplab.com/a-simple-guide-to-openai-api-with-python/

Guess you like

Origin blog.csdn.net/weixin_52020016/article/details/130430360