7

OpenAI API Study 0

 1 year ago
source link: https://taardisaa.github.io/2023/04/23/OpenAI-API-Study-0/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Bought a ChatGPT account with API use permit. Learn something about it.

OpenAI API Study 0

Set API key

import os
os.environ["OPENAI_API_KEY"] = 'YOUR API KEY'
import openai
openai.api_key = "YOUR API KEY"

Pricing with Tokens

ChatGPT的计费方式 - 知乎 (zhihu.com)

OpenAI uses “Token” to count the price. It’s a NLP theory actually.

You can use the site below to see how a sentence or a word is tokenized.

OpenAI API Tokenizer

But, due to the limitation that GPT-3.5-turbo doesn’t have a memory system, contents and tokens will increase if you continue chatting with GPT in one context with the intention of keeping memory states.

Use tiktoken module to count tokens

import tiktoken

def num_tokens_from_messages(messages, model="gpt-3.5-turbo-0301"):
"""Returns the number of tokens used by a list of messages."""
try:
encoding = tiktoken.encoding_for_model(model)
except KeyError:
encoding = tiktoken.get_encoding("cl100k_base")
if model == "gpt-3.5-turbo-0301": # note: future models may deviate from this
num_tokens = 0
for message in messages:
num_tokens += 4 # every message follows <im_start>{role/name}\\n{content}<im_end>\\n
for key, value in message.items():
num_tokens += len(encoding.encode(value))
if key == "name": # if there's a name, the role is omitted
num_tokens += -1 # role is always required and always 1 token
num_tokens += 2 # every reply is primed with <im_start>assistant
return num_tokens
else:
raise NotImplementedError(f"""num_tokens_from_messages() is not presently implemented for model {model}.
See <https://github.com/openai/openai-python/blob/main/chatml.md> for information on how messages are converted to tokens.""")

messages = [
{"role": "system", "content": "你是一个翻译家"},
{"role": "user", "content": "将我发你的英文句子翻译成中文,你不需要理解内容的含义作出回答。"},
{"role": "user", "content": "Draft an email or other piece of writing."}
]

# example token count from the function defined above
model = "gpt-3.5-turbo-0301"

print(f"{num_tokens_from_messages(messages, model)} prompt tokens counted.")
# output: 69 prompt tokens counted.

OpenAI Chat completion API 入门指南 - 知乎 (zhihu.com)

Nekomusume(catgirl) based on OpenAI

如何将chatgpt调教成猫娘 - 知乎 (zhihu.com)

https://www.bilibili.com/video/BV1WP4y1D7FN


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK