什么是 AI21 Studio?
AI21 Studio 是一个功能强大的多功能平台,旨在帮助开发人员利用尖端语言模型(特别是 Jurassic-2)的功能来构建范围广泛的 AI 驱动应用程序。凭借其用户友好的 API 和专用端点,AI21 Studio 使开发人员能够轻松处理各种自然语言处理任务,例如文本生成、摘要、释义等。该平台提供可定制的解决方案,允许用户微调他们自己的模型以满足他们的特定要求。在本教程中,我们将探索 AI21 Studio 的主要功能,学习如何开始使用其 API,并查看其应用程序的真实示例。
从哪儿开始?
首先,您需要创建一个帐户并注册免费试用。在此处查找更多信息。然后,如果您之前没有任何经验,则应该熟悉 AI21 Studio。您会发现模型、工具、API 及其 AI 文档和 API 参考非常有用。在阅读和试用模型后,获取您的 API 密钥。您将需要它来从您的代码访问 API。
上下文API
Contextual Answers API 是一个功能强大的系统,旨在根据特定的文档上下文提供准确可靠的问题回答。此 API 可确保答案完全来自提供的上下文,避免使用传统语言模型时可能出现的任何事实问题。如果文档中没有问题的答案,模型将指出这一点,而不是提供可能不准确的答案。
作为特定于任务的 API,它针对效率进行了优化,并且可以轻松集成到现有系统中,而无需任何及时的工程设计。此外,它是用户友好的,使其成为准确和基于上下文的问题回答的有效解决方案。
Contextual Answers API 需要两个参数才能成功请求:
context:一个字符串,包含要回答问题的文档上下文。question:一个字符串,包含要根据提供的上下文回答的问题。
API 在其响应中返回以下参数:
answer:包含基于提供的上下文的问题答案的字符串。如果模型无法在提供的上下文中找到答案,则答案将为空。id:API 分配的唯一标识符,用于标识生成响应的特定请求。此参数可用于跟踪和记录目的,尤其是在向 API 发出多个请求时。
Pythonpayload = { "context": "In 2020 and 2021, enormous QE — approximately $4.4 trillion, or 18%, of 2021 gross domestic product (GDP) — and enormous fiscal stimulus (which has been and always will be inflationary) — approximately $5 trillion, or 21%, of 2021 GDP — stabilized markets and allowed companies to raise enormous amounts of capital. In addition, this infusion of capital saved many small businesses and put more than $2.5 trillion in the hands of consumers and almost $1 trillion into state and local coffers. These actions led to a rapid decline in unemployment, dropping from 15% to under 4% in 20 months — the magnitude and speed of which were both unprecedented. Additionally, the economy grew 7% in 2021 despite the arrival of the Delta and Omicron variants and the global supply chain shortages, which were largely fueled by the dramatic upswing in consumer spending and the shift in that spend from services to goods. Fortunately, during these two years, vaccines for COVID-19 were also rapidly developed and distributed.In today's economy, the consumer is in excellent financial shape (on average), with leverage among the lowest on record, excellent mortgage underwriting (even though we've had home price appreciation), plentiful jobs with wage increases and more than $2 trillion in excess savings, mostly due to government stimulus. Most consumers and companies (and states) are still flush with the money generated in 2020 and 2021, with consumer spending over the last several months 12% above pre-COVID-19 levels. (But we must recognize that the account balances in lower-income households, smaller to begin with, are going down faster and that income for those households is not keeping pace with rising inflation.) Today's economic landscape is completely different from the 2008 financial crisis when the consumer was extraordinarily overleveraged, as was the financial system as a whole — from banks and investment banks to shadow banks, hedge funds, private equity, Fannie Mae and many other entities. In addition, home price appreciation, fed by bad underwriting and leverage in the mortgage system, led to excessive speculation, which was missed by virtually everyone — eventually leading to nearly $1 trillion in actual losses.", "question": "Did the economy shrink after the Omicron variant arrived?"}JSON{ "id": "36d2ec63-ef15-2296-af51-ef34206fc655", "answer": "The Delta and Omicron variants and the global supply chain shortages did not slow down economic growth."}
设置项目
首先安装Flask,一个借助 Pythons PIP 的轻量级后端框架。你可以使用这个命令来完成
pip install flask
您还可以安装 Postman。Postman 是一个帮助开发人员构建、测试和管理 API(应用程序编程接口)的应用程序。它为设计 API 请求、设置参数、标头和身份验证提供了一个用户友好的界面。
创建一个 python 文件并将其命名为 app.py。用这个最小的样板代码填充它以开始。
from flask import Flask, requestimport requests as reqapp = Flask(__name__)@app.route("/")def def get_answer(methods=['GET']): return "<p>Hello, World!</p>"app.run()
实施 API
现在我们已经设置了项目的基本框架,让我们修改它以使用 AI21 API。
API 需要两个参数 – 上下文和问题。按照 API 指南修改 get_answer() 方法。将您的 API 密钥保存到单独的文件(例如 .env)到 API_key 方法范围之外的变量 API_KEY。
# load form env API_KEYAPI_KEY = os.environ.get('API_KEY')
在get_answer方法中将上下文和问题保存到变量
context = request.args.getlist('context')[0]question = request.args.getlist('question')[0]
另外提供 url、payload 和 headers
url = "https://api.ai21.com/studio/v1/experimental/answer"payload = { "context": context, "question": question }headers = { "accept": "application/json", "content-type": "application/json", "Authorization": f'Bearer {API_KEY}' }
最后返回处理后的数据
response = req.post(url, json=payload, headers=headers)return response.text
整个 Flask 应用应该看起来像这样
from flask import Flask, requestimport requests as reqimport osapp = Flask(__name__)# load form env API_KEYAPI_key = os.environ.get('API_KEY')@app.route("/")def get_answer(): context = request.args.getlist('context')[0] question = request.args.getlist('question')[0] url = "https://api.ai21.com/studio/v1/experimental/answer" payload = { "context": context, "question": question } headers = { "accept": "application/json", "content-type": "application/json", "Authorization": f'Bearer {API_KEY}' } response = req.post(url, json=payload, headers=headers) return response.textapp.run()
在 Postman 中测试
打开邮递员并运行您的应用程序。您可以使用以下命令运行您的应用程序
flask --app app run
插入上下文和问题参数。
这些是结果
{"id":"cfde030b-ffcd-4867-bf00-98af82f19932","answer":"No, the economy grew 7% in 2021 despite the arrival of the Delta and Omicron variants and the global supply chain shortages."}
概括
本教程介绍了创建基本的 Flask Web API 以及基于 AI21 Contextual Answers API 提供上下文答案。这个例子可以通过创建一个前端来进一步改进。为此,请查看我们的其他 AI21 教程。
如果您想在现实世界中测试您使用 AI21 工作室的技能,您可以参加我们即将举行的 AI21 实验室 AI 黑客马拉松,并在这 7 天的活动中构建一个惊人的工具。
你在等什么?
加入人工智能中心,与aihubpro.cn圈子共创美好未来!