如何使用 OpenAI GPT-3实现语义搜索

本文介绍了如何利用OpenAI的GPT-3 API进行语义搜索。首先,展示了如何创建JSONL文件上传文档,然后通过Python调用API上传文件。接着,提供了一个示例查询,演示了如何执行语义搜索并获取相关性得分。实验表明,GPT-3能有效地进行文本匹配和排序,适用于多种语义搜索任务。
摘要由CSDN通过智能技术生成

生成预训练 Transformer 3(GPT-3)是一种用于 OpenAI 生成文本的自回归语言模型。GPT-3展示了一个真正智能的语言模型生成文本的惊人潜力,并且有能力完成诸如问题回答、摘要、语义搜索、聊天机器人以及写诗或写论文等令人惊叹的任务。其中,我们已经在 GPT-3、广告生成、句子释义、意图分类等方面进行了问答实验。现在,让我们使用 OpenAI 提供的 GPT-3 API 端点为语义搜索任务做一些实验。

OpenAI 的 API for search 允许你在一组文档中进行语义搜索。基于语义相关的查询文本,它为每个文档提供分数并给出它们的等级。

因为它是基于 api 的访问,所以很容易使用。我们只需要以文档的形式提供文本,然后查询文本。API 将返回根据相关性得分排序的与查询匹配的多个结果。

下面是使用 OpenAI API 进行语义搜索的步骤。这里我们使用 Python 调用 API,但是,您也可以发出 cURL 请求。

# 安装必要模块
pip install openai

要执行语义搜索,首先需要以 JSONL 文件格式上传文档。下面是一个 JSONL 文件格式示例。

{"text": "Hello OpenAI", "metadata": "sample data"}

接下来,我们将创建一个 JSONL 文件用于语义搜索,将其命名为 sample _ search. JSONL,并将以下代码复制到其中:

{“text”: “The rebuilding of economies after the COVID-19 crisis offers a unique opportunity to transform the global food system and make it resilient to future shocks, ensuring environmentally sustainable and healthy nutrition for all. To make this happen, United Nations agencies like the Food and Agriculture Organization, the United Nations Environment Program, the Intergovernmental Panel on Climate Change, the International Fund for Agricultural Development, and the World Food Program, collectively, suggest four broad shifts in the food system.”, “metadata”: “Economic reset”} 
{“text”: “In the past few weeks healthcare professionals have been fully focussed caring for enormous numbers of people infected with COVID-19. They did an amazing job. Not in the least because healthcare professionals and leaders have been using continues improvement as part of their accreditation program for many years. It has become part of their DNA. This has enabled them to change many processes as needed during COVID-19, using a cross-functional problem solving approach in (very) rapid improvement cycles.”, “metadata”: “Supporting adaptive healthcare”}

现在是时候使用 API 键上传这个 JSONL 文件了,方法是将用途设置为搜索语义搜索。创建一个名为 upload _ file.py 的文件,然后复制下面的代码并提供 OpenAI API 密钥。

import openai
openai.api_key = "YOUR-API-KEY" response = openai.File.create(file=open("sample_doc.jsonl"), purpose="search")
print(response)

当你运行 upload _ file.py 文件时,你会得到下面的响应:
在这里插入图片描述
从上面步骤中的响应中复制 id。
现在让我们来测试一下。要测试 GPT-3语义搜索的能力,请在查询文本参数中提供查询。

import openai
openai.api_key = "YOUR-API-KEY"
 
search_response = openai.Engine("davinci").search(
    search_model="davinci",
    query="healthcare",
    max_rerank=5,
    file="file-8ejPA5eM13J4J0dWy3bBbvTf",
    return_metadata=True
 )
 print(search_response)

回应如下图所示:在这里插入图片描述
使用 GPT-3对给定查询执行语义搜索非常简单。在 JSON 响应中,我们得到与查询匹配的文档文本,得分显示结果的相关性。在我们的测试中,我们只提供了一个文档。如果我们提供多个文档,那么我们会得到多个不同分数的结果。我们可以看到,

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值