pip install openai
url: POST https://api.openai.com/v1/completions
参数:
model
: (string) text-davinci-003, text-davinci-002, text-curie-001, text-babbage-001, text-ada-001prompt
: (string/array) (<|endoftext|>)suffix
:(string) (null) 文本完成后缀max_tokens
:(int) (16) 生成令牌的最大数量,len(prompt+max_tokens) < old_max:2048(new_max:4096)temperature
: (float) (1) 值范围:0.0~2.0,越大生成内容越随机,越小生成内容越固定top_p
仅修改一个 top_p
: (float) (1) 核采样,模型考虑具有top_p
概率质量的标记结果-->0.1: 只考虑构成前 10% 概率质量的标记(mass)n
: (int) (1) 为每个prompt生成多少completionsstream
: (bool) (false)是否将部分进度流式返回。如果设置,则令牌将作为仅数据的服务器推送事件在其可用时发送,流将以data:[DONE]
消息终止。logprobs
: (int) (null) 在返回的logprobs最有可能的令牌中包含对数概率和所选择的令牌。果logprobs是5,API将返回一个最有可能的令牌列表。API将始终返回所采样令牌的对数概率,因此响应中可能有logprobs + 1个元素。logprobs的最大值为5echo
: (bool) (false)回显promptstop
: (string/array) (null) 最多4个序列,其中API将停止生成进一步的令牌。返回的文本将不包含停止序列。presence_penalty
: (int) (0) -2.0~2.0 正值会根据到目前为止文本中它们的现有频率对新令牌进行惩罚,降低模型按原样重复相同行的可能性。best_of
: (int) (1) 在服务器端生成best_of完成,并返回“best”(每个令牌的对数概率最高的完成)。结果无法流式传输。当与n一起使用时,best_of控制候选完成的数量,并且n指定要返回的数量 - best_of必须大于n。logit_bias
: (map) (null) 用于修改特定令牌出现在完成中的概率。它接受一个json对象,将令牌(由GPT令牌化器中的其令牌ID指定)映射到从-100到100的相关偏差值user
: (string) 表示您的最终用户的唯一标识符,可帮助OpenAI监视和检测滥用url: POST https://api.openai.com/v1/chat/completions
参数
model
: (string) gpt-4, gpt-4-0314, gpt-4-32k, gpt-4-32k-0314, gpt-3.5-turbo, gpt-3.5-turbo-0301messages
: (array)
- role
: (string) One of system, user, or assistant
- content
: (string) 角色消息内容
- name
: (string) max_len=64 a-z,A-Z,0-9和下划线temperature
: (float) (1) 0~2top_p
: (float) (1) n
: (int) (1)stream
: (bool) (false)stop
: (bool) (flase)max_tokens
: (int) (inf)presence_penalty
: (float) (0)frequency_penalty
: (float) (0)logit_bias
: (map) (null)user
: (string)```python
import osimport openai
openai.organization = "YOUR_ORG_ID"
openai.api_key = "OPENAI_API_KEY"
def get_completion(prompt, model="gpt-3.5-turbo"):
messages = [{"role": "user", "content": prompt}]
response = openai.ChatCompletion.create(
model=model,
messages=messages,
temperature=0, # this is the degree of randomness of the model's output
)
return response.choices[0].message["content"]
text_1 = f"""
Making a cup of tea is easy! First, you need to get some \
water boiling. While that's happening, \
grab a cup and put a tea bag in it. Once the water is \
hot enough, just pour it over the tea bag. \
Let it sit for a bit so the tea can steep. After a \
few minutes, take out the tea bag. If you \
like, you can add some sugar or milk to taste. \
And that's it! You've got yourself a delicious \
cup of tea to enjoy.
"""
prompt = f"""
You will be provided with text delimited by triple quotes.
If it contains a sequence of instructions, \
re-write those instructions in the following format:
Step 1 - ...
Step 2 - …
…
Step N - …
If the text does not contain a sequence of instructions, \
then simply write \"No steps provided.\"
\"\"\"{text_1}\"\"\"
"""
response = get_completion(prompt)
print("Completion for Text 1:")
print(response)
```
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。