ChatGPT插件例子 Python 到自然语言
用人类可以理解的语言解释一段 Python 代码。
提问
# Python 3
def remove_common_prefix(x, prefix, ws_prefix):
x["completion"] = x["completion"].str[len(prefix) :]
if ws_prefix:
# keep the single whitespace as prefix
x["completion"] = " " + x["completion"]
return x
# Explanation of what the code does
#
回答
此代码从包含字符串的数据框列中删除一个公共前缀。它接收数据帧、要删除的前缀和一个布尔值,该值指示是否保留单个空格作为前缀。然后它从数据框列中删除前缀,如果布尔值为真,则添加一个空格作为前缀。最后,它返回修改后的数据框。
API请求
import os
import openai
openai.api_key = os.getenv("OPENAI_API_KEY")
response = openai.Completion.create(
model="text-davinci-003",
prompt="# Python 3 \ndef remove_common_prefix(x, prefix, ws_prefix): \n x[\"completion\"] = x[\"completion\"].str[len(prefix) :] \n if ws_prefix: \n # keep the single whitespace as prefix \n x[\"completion\"] = \" \" + x[\"completion\"] \nreturn x \n\n# Explanation of what the code does\n\n#",
temperature=0,
max_tokens=150,
top_p=1.0,
frequency_penalty=0.0,
presence_penalty=0.0
)