GitHub,截至目前还没有公开发布他们的API。
然而,有人猜测GitHub Copilot使用了OpenAI的Codex(现已弃用)。根据this所说,您可以使用OpenAI的聊天模型进行代码补全、建议等。不过根据我的经验,响应时间会有所不同。另外,并不能保证它只会输出代码。
请参考下面的示例;
import os
import openai
openai.api_key = os.getenv("OPENAI_API_KEY")
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{
"role": "system",
"content": "You are a helpful assistant. Assistant will output only and only code as a response."
},
{
"role": "user",
"content": "Write a Python function that takes as input a file path to an image, loads the image into memory as a numpy array, then crops the rows and columns around the perimeter if they are darker than a threshold value. Use the mean value of rows and columns to decide if they should be marked for deletion."
}
],
temperature=0,
max_tokens=1024
)
这将输出:
import numpy as np
from PIL import Image
def crop_dark_borders(image_path, threshold):
image = Image.open(image_path)
image_array = np.array(image)
row_means = np.mean(image_array, axis=1)
col_means = np.mean(image_array, axis=0)
...
编辑;再想一想,我不会在这种情况下使用ChatCompletion,因为任务根本不是基于聊天的。相反,我会使用
Completion,并将整个代码文件作为输入提供给它。这也有自己的局限性。例如,您将无法向模型提供光标后面的内容。