Cohere 教程:根据给定数据回答问题

Cohere 教程:根据给定数据回答问题

为什么要用 Cohere 来回答基于数据的问题?

根据数据回答问题,基本上就是从文本中提取信息,这是语言处理中的常见任务。由于 Coheres 大型语言模型是一种基于人工智能的技术,这可以提高对数据的理解和进一步处理。

让我们开始吧

如果您还没有注册到 Cohere,请注册 https://dashboard.cohere.ai/register

在我创建 nextjs 项目之前,我在 Cohere 操场上尝试了我的概念。它是测试您的想法和感受 API 的好工具。你可以在这里找到它:https://os.cohere.ai/playground

创建一个新项目

我正在使用我们在 GitHub 上公开的存储库。你可以在这里找到它:https://github.com/lablab-ai/nextjs-cohere-boilerplate

您可以克隆存储库并使用 npm 或 yarn 安装所有依赖项。

API密钥

在 Cohere 仪表板上为您的项目创建一个新的 API 密钥。您可以在这里找到它:https://dashboard.cohere.ai/api-keys您需要将创建的 API 放入项目的 env 文件中。(切勿与任何人共享您的 API 密钥,不要将其公开存储库!)

更新 index.js

现在我们将更新 index.js 文件以能够处理多于一个的输入。我还重命名了各州以使其更加清晰。

const [question, setQuestion] = useState("");  const [companyData, setCompanyData] = useState("");

更新获取请求,因为我们将更改端点名称并发送更多信息。

const res = await fetch("/api/answer", {    method: "POST",    headers: {      "Content-Type": "application/json",    },    body: JSON.stringify({ question, companyData }),  });  const data = await res.json();  setOutput(data.summary);

我们还需要更新前端,因为我们将有两个输入:

<div className="flex flex-col justify-center items-center m-2 ">      <div className="flex flex-col w-full lg:w-1/2">        <h1 className="font-medium leading-tight text-4xl mt-0 mb-2 text-blue-700">          Company info answer bot        </h1>        <h2>Company Data</h2>        <textarea          className="w-full h-64 mb-10 text-sm p-4  text-gray-900 bg-gray-50 rounded-lg border border-gray-300 focus:ring-blue-500 focus:border-blue-500 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500"          placeholder="Paste article here"          value={companyData}          onChange={(v) => setCompanyData(v.target.value)}        />        <h2>Question</h2>        <textarea          className="w-full h-64 text-sm p-4  text-gray-900 bg-gray-50 rounded-lg border border-gray-300 focus:ring-blue-500 focus:border-blue-500 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500"          placeholder="Paste article here"          value={question}          onChange={(v) => setQuestion(v.target.value)}        />        <button          onClick={handleClick}          className="m-4 bg-blue-500 hover:bg-blue-400 text-white font-bold py-2 px-4 border-b-4 border-blue-700 hover:border-blue-500 rounded"        >          {loading ? "loading..." : "Send question"}        </button>        {output ? `Answer: ${output}` : null}      </div>    </div>

更新 API

让我们将 api 文件重命名为 answer.js 并更新代码。

我们需要请求正文中的问题和 companyDate 字段。

const { question, companyData } = req.body;

我们仍将使用生成模型。,但我们当然会更改提示。

为工作创造完美的提示

首先,我们将展示一个好的提示和一个坏提示的例子。我正在使用来自维基百科的简短公司数据文本。

我们第一次询问公司,我们将正确答案放在答案字段中。第二次我提出了一个糟糕的问题,)与公司无关)。我用了“你好吗?”这个问题 并放置答案“这个问题与公司无关”。这样模型就会知道如何对不相关且未包含在公司数据文本中的问题做出反应。我的提示如下:

prompt: `This program will answer questions based on the provided data:\\n\\nData: Siemens AG is a German multinational conglomerate corporation and the largest industrial manufacturing company in Europeheadquartered in Munich with branch offices abroad. Siemens & Halske was founded by Werner von Siemens and Johann Georg Halske on 1 October 1847. The principal divisions of the corporation are Industry, Energy, Healthcare (Siemens Healthineers), and Infrastructure & Cities, which represent the main activities of the corporation.The corporation is a prominent maker of medical diagnostics equipment and its medical health-care division, which generates about 12 percent of the corporation\\'s total sales, is its second-most profitable unit, after the industrial automation division. In this area, it is regarded as a pioneer and the company with the highest revenue in the world.The corporation is a component of the Euro Stoxx 50 stock market index. Siemens and its subsidiaries employ approximately 303,000 people worldwide and reported global revenue of around €62 billion in 2021 according to its earnings release.\\n\\nQuestion: When the company was founded?\\nAnswer: on 1 October 1847.\\n--\\nQuestion: How are you?\\nAnswer: This question is not related to the company.\\n--\\nQuestion: Who was/were the founder?\\nAnswer: Werner von Siemens and Johann Georg Halske\\n--\\nData:${companyData}\\nQuestion: ${question}?\\nAnswer:`

最后,我将响应的最后一部分切片以获得正确答案。

res.status(200).json({ summary: response.body.generations[0].text.slice(0,-3) });

我们可以开始我们的应用程序

让我们使用 yarn 或 npm run dev script 启动我们的应用程序。

测试应用程序

为了进行测试,我从维基百科中对 Robert Bosch GmbH 进行了简短描述:

`Robert Bosch GmbH , commonly known as Bosch and stylized as BOSCH, is a German multinational engineering and technology company headquartered in Gerlingen, Germany. The company was founded by Robert Bosch in Stuttgart in 1886. Bosch is 92% owned by Robert Bosch Stiftung, a charitable institution. Although the charity is funded by owning the vast majority of shares, it has no voting rights and is involved in health and social causes unrelated to Bosch’s business.Bosch's core operating areas are spread across four business sectors: mobility (hardware and software), consumer goods (including household appliances and power tools), industrial technology (including drive and control) and energy and building technology.`

我问了有关文本包含的公司的问题。“有哪些主要行业?”答案是:移动、消费品、工业技术以及能源和建筑技术。

这是对的!该模型能够理解问题并正确回答。当然,可以随时对提示进行微调以获得更好的结果。因为模型会尝试回答所有问题。

结论

Cohere 是创建聊天机器人的绝佳工具。它易于使用,并且对有限数量的请求是免费的。

您可以在这里找到该项目的代码:https://github.com/lablab-ai/cohere-nextjs-answerbot

加入我们的 AI 黑客马拉松来测试您的知识,并在我们的导师的帮助下构建基于 AI 的工具来改变世界!检查链接下的即将到来的[此处粘贴指向即将到来的 Hackathons 页面的链接]

谢谢你!如果您喜欢本教程,您可以在我们的教程页面上找到更多信息并继续阅读 – AI未来百科 ; 探索AI的边界与未来! 懂您的AI未来站

声明:本站部分文章来源于网络,如有侵犯您的合法权益,请您即时与我们联系,我们将在第一时间处理。如需转载本站文章,请在转载时标明出处并保留原文链接,否则我们将保留追究法律责任的权利。

给TA打赏
共{{data.count}}人
人已打赏
AI教程

AI 黑客马拉松教程:如何将 Github 用于您的黑客马拉松项目

2023-5-5 20:45:24

AI教程

Cohere 教程:使用 Cohere 进行聊天/对话 Summarizer

2023-5-5 20:49:25

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧