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

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

import osimport openaiimport configopenai.api_key = config.OPENAI_API_KEYdef 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 Randoma = 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.")

输出:

  • 输入代码的嵌入

回答:

函数 get_embedding 将为我们提供输入文本的嵌入。

OpenAI的标准代码在这里: https://github.com/openai/openai-python/blob/main/examples/embeddings/Get_embeddings.ipynb

import openaifrom 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]:    # 替换换行符,这可能会对性能产生负面影响。    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))

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注