gpt4 book ai didi

python - 如何使用 Codex API 获取 token 或代码嵌入?

转载 作者:行者123 更新时间:2023-12-02 22:46:33 25 4
gpt4 key购买 nike

对于给定的代码片段,如何使用 Codex API 进行嵌入?

import os
import openai
import config


openai.api_key = config.OPENAI_API_KEY

def runSomeCode():
response = openai.Completion.create(
engine="code-davinci-001",
prompt="\"\"\"\n1. Get a reputable free news api\n2. Make a request to the api for the latest news stories\n\"\"\"",
temperature=0,
max_tokens=1500,
top_p=1,
frequency_penalty=0,
presence_penalty=0)

if 'choices' in response:
x = response['choices']
if len(x) > 0:
return x[0]['text']
else:
return ''
else:
return ''



answer = runSomeCode()
print(answer)

但是我想弄清楚给定一个如下所示的 python 代码块,我可以从 codex 获取嵌入吗?

输入:

import Random
a = random.randint(1,12)
b = random.randint(1,12)
for i in range(10):
question = "What is "+a+" x "+b+"? "
answer = input(question)
if answer = a*b
print (Well done!)
else:
print("No.")

输出:

  • 嵌入输入代码

最佳答案

是的,OpenAI 可以为任何输入文本创建嵌入——即使它是代码。您只需要在其 get_embedding() 函数调用中传递正确的引擎或模型。我测试了这段代码:

# Third-party imports
import openai

from openai.embeddings_utils import get_embedding


openai.api_key = OPENAI_SEC_KEY


embedding = get_embedding("""
def sample_code():
print("Hello from IamAshKS !!!")
""", engine="code-search-babbage-code-001")

print()
print(f"{embedding=}")
print(f"{len(embedding)=}")

# OUTPUT:
# embedding=[-0.007094269152730703, 0.006055716425180435, -0.005044757854193449, ...]
# len(embedding)=2048


embedding = get_embedding("""
import Random
a = random.randint(1,12)
b = random.randint(1,12)
for i in range(10):
question = "What is "+a+" x "+b+"? "
answer = input(question)
if answer = a*b
print (Well done!)
else:
print("No.")
""", engine="code-search-babbage-code-001")

print()
print(f"{embedding=}")
print(f"{len(embedding)=}")

# OUTPUT:
# embedding=[-0.011341490782797337, -0.005919027142226696, 0.0011923711281269789, ...]
# len(embedding)=2048

注意:您可以使用 get_embedding()engine 参数替换模型或引擎。

上面给出的代码为您提供了任何代码的嵌入。还有另一个名为 code-search-ada-code-001 的代码搜索引擎/模型,但它不如 code-search-babbage-code-001 强大,我用于此答案。如果您还想进行代码搜索,请阅读下面的引用资料。

引用资料:

关于python - 如何使用 Codex API 获取 token 或代码嵌入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72986749/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com