简体   繁体   English

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

[英]How to get token or code embedding using Codex API?

For a given code snippet, how to get embedding using the Codex API?对于给定的代码片段,如何使用 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)

But I want to figure out given a python code block like the following, can I get the embedding from codex?但我想弄清楚给定一个 python 代码块,如下所示,我可以从 codex 获得嵌入吗?

Input:输入:

 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 ")

Output: Output:

  • Embedding of the input code嵌入输入代码

Yes, OpenAI can create embedding for any input text -- even if it's code.是的,OpenAI 可以为任何输入文本创建嵌入——即使它是代码。 Try out this code:试试这个代码:

# Standard library imports
from typing import List

# Third-party imports
import openai


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")
print(len(embedding))

# Output:
# 12288


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.")
""")
print(len(embedding))

# Output:
# 12288

NOTE: You can replace the model or engine using engine parameter for get_embedding() .注意:您可以使用get_embedding()engine参数替换模型或引擎。

References:参考:

The function get_embedding will give us an embedding for an input text. function get_embedding将为我们提供输入文本的嵌入。

Canonical code from OpenAI here: https://github.com/openai/openai-python/blob/main/examples/embeddings/Get_embeddings.ipynb此处来自 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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM