1、首先申请一个api key,并安装openai
pip install openai
申请地址:https://beta.openai.com/
2、新建一个json文本文件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”}3、建立搜索库
{“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”}
import openai
openai.api_key = "YOUR-API-KEY"
response = openai.File.create(file=open("/data/wwwroot/default/asset/sample-search.jsonl", 'r', encoding='utf-8'), purpose="search")
print(response)

从上面步骤中的响应中复制 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 响应中,我们得到与查询匹配的文档文本,得分显示结果的相关性。在我们的测试中,我们只提供了一个文档。如果我们提供多个文档,那么我们会得到多个不同分数的结果。
如果要使用中文gpt3,可以清源CPM (Chinese Pretrained Models),github地址:https://github.com/TsinghuaAI/CPM-1-Generate
网友回复