python 如何使用Codex API获取令牌或代码嵌入?

t2a7ltrp  于 2023-01-19  发布在  Python
关注(0)|答案(2)|浏览(306)

对于给定的代码片段,如何使用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.")

输出:

  • 输入代码的嵌入
pqwbnv8z

pqwbnv8z1#

是的,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强大,我在回答这个问题时使用了code-search-babbage-code-001。如果你也想做代码搜索,请阅读下面的参考文献。

1qczuiv0

1qczuiv02#

函数get_embedding将为我们提供一个输入文本的嵌入。
OpenAI的规范代码如下:https://github.com/openai/openai-python/blob/main/examples/embeddings/Get_embeddings.ipynb

import openai
from tenacity import retry, wait_random_exponential, stop_after_attempt

@retry(wait=wait_random_exponential(min=1, max=20), stop=stop_after_attempt(6))
def get_embedding(text: str, engine="text-similarity-davinci-001") -> List[float]:

    # replace newlines, which can negatively affect performance.
    text = text.replace("\n", " ")

    return openai.Embedding.create(input=[text], engine=engine)["data"][0]["embedding"]

embedding = get_embedding("Sample query text goes here", engine="text-search-ada-query-001")
print(len(embedding))

相关问题