如何通过 Python 使用 Google Bard

1,299 阅读5分钟

什么是 Google Bard

Google Bard 是一种开源数据可视化和探索工具,可为 开发人员 提供支持。为了处理用户提出的查询,它使用机器学习算法。它接受了大量文本和代码数据集的训练。它仍处于实验阶段,毫无疑问它与 ChatGPT 形成了直接竞争。它可以执行以下各种任务:

  • 翻译语言
  • 撰写创意内容
  • 回答您的疑问

Google Bard 的优点

  • 它很容易使用,我们可以通过在输入消息时编写任何查询来询问任何问题,或者我们也可以通过单击麦克风来询问。
  • 它根据用户之前提出的查询历史记录来个性化用户查询的结果。
  • 它可以帮助我们找到我们需要的信息,因为我们不遵循使用谷歌搜索的过程。
  • 它非常快速地(通常在一秒钟内)提供用户查询的答案。

Google Bard 的局限性

  • 由于它仍处于实验阶段或 Beta 阶段,因此它提供的信息不太准确,例如编写代码,但一般来说,Google Bard 提供准确的信息。
  • 它有多种语言版本,但可能只能以某些特定语言提供答案。
  • 它是为了回答特定类型的查询而开发的,可能无法回答某些复杂的查询。

如何通过 Python 使用 Google Bard

如果我们想通过 Python 使用这些类型的 AI 聊天机器人,那么我们可以使用它们提供的API在我们的程序或应用程序中使用它们的功能。该过程类似于在 Python 中使用 ChatGPT API 与 ChatGPT API 相同 Google Bard API 也可供用户在其程序中使用 Google Bard 的功能。目前,Google Bard API 正处于测试阶段,因此仅可供有限数量的用户使用。

通过 Python 使用 Google Bard API 的步骤

在深入了解 Google Bard 的世界之前,您需要确保您的系统中已经安装了 Python。从 Python 官方网站下载最新版本的 Python 并按照安装过程进行操作。在这里您将学习两种通过 Python 使用 Google Bard 的方法。

安装 Google Bard 库

要开始使用 Google Bard,您需要安装该库。使用 Python 的包管理器“ pip ”安装非常简单。打开终端和命令提示符并执行给定的命令。此外,确保安装了“ google_bard ”库。

pip install GoogleBard

获取Google API密钥

转到 Google 云平台:console.cloud.google.com/apis/creden…

image.png

创建或选择项目

image.png

在“API 和服务”下,单击“凭据”。

现在您的 API 密钥已生成。请确保其安全,因为它将提供对 Google Bard API 的访问。

image.png

发出 API 请求

现在,让我们使用“google_bard”模块与 Google Bard API 进行交互:

import google_bard

# 将“YOUR_API_KEY”替换为之前获得的实际API密钥
API_KEY = "YOUR_API_KEY"

def main():
	query = "What is the meaning of life?"
	response = google_bard.generate_text(query, api_key=API_KEY)
	print("Google Bard Response (Using google_bard Module):")
	print(response)

if __name__ == "__main__":
	main()

输出:

Google Bard Response (Using google_bard Module):
The meaning of life is a subjective and personal question with no definitive answer. It is up to each individual to explore and 
discover their own purpose and what brings them a sense of fulfillment and meaning.

解释

首先导入 google_bard:此行导入 google_bard 模块,该模块提供与 Google Bard API 交互所需的功能。API_KEY = “YOUR_API_KEY”:将“YOUR_API_KEY”替换为实际的 API 密钥。def main():: 这一行定义了一个名为 main() 的函数。query =“What is the meaning of life?”:我们定义一个查询以发送到 Google Bard API。response = google_bard.generate_text(query, api_key=API_KEY):此行使用 google_bard 模块提供的generate_text() 函数向 Google Bard API 发出 API 请求。

Google Bard 唱请求/JSON 库

获取谷歌API密钥

获取 Google Brad API 需要 API 密钥。请参阅方法 1、步骤 2,获取您的 API Key。要获取您的 API 密钥,您需要执行以下步骤:

  • 转到 Google 云平台:console.cloud.google.com/apis/creden…
  • 创建或选择项目。
  • 在“API 和服务”下,单击“凭据”。
  • 现在您的 API 密钥已生成。请确保其安全,因为它将提供对 Google Bard API 的访问。

导入所需的库

让我们首先导入必要的库并设置 API 密钥:

import requests
import json

# 将“YOUR_API_KEY”替换为步骤1中获得的实际API密钥
API_KEY = "YOUR_API_KEY"
URL = "https://bard.googleapis.com/v1/generate"

发出 API 请求

在此步骤中,我们将创建一个函数来与 Google Bard API 交互并获取响应:

def get_bard_response(query):
	response = requests.post(URL, headers=
				{"Authorization": "Bearer " + API_KEY},
							json={"query": query})
	data = json.loads(response.content)
	return data["text"]

从 Google Bard 获取见解

只需在 Google Bard 和 Python 的帮助下向 API 发送查询,您就可以轻松地从数据中获取见解。例如,如果你想知道 “生命的意义是什么?” ,您可以按如下方式发出 API 请求:

def main():
	query = "Geeksforgeeks"
	response = get_bard_response(query)
	print("Google Bard Response:")
	print(response)

if __name__ == "__main__":
	main()

输出:

Google Bard Response:
Geeksforgeeks is a leading platform that provides computer science resources and coding 
challenges for programmers and technology enthusiasts, along with interview and exam preparations for 
upcoming aspirants. It was founded in 2008 by Sandeep Jain and has since grown to become one of the most 
trusted and renowned names in the programming community.

解释

首先,我们导入所需的模块。requests 库用于发出 HTTP 请求,JSON 库用于解析 JSON 数据。然后,我们将 Google Bard API 密钥存储在“API_KEY”变量中,将 Google Bard API 的 URL 存储在“URL”中,将发送到 Google Bard 的查询存储在“QUERY”中,“响应”存储从 Google Bard 收到的响应。我们使用 request.post() 函数发送 post 请求,然后将响应中的 JSON 数据存储在“data”中。之后,我们打印来自 Google Bard 的响应。

Google Bard 输出:

我们还可以检查 GoogleBard 的输出,它将与我们的 Python 代码输出相同。

image.png

总结

通过使用以下步骤获取 API Key、导入所需的库并发出 API 请求,您可以轻松地将 Google Bard 无缝集成到您的 Python 项目中。它将增强您的体验以及与应用程序的交互。它使您能够提供引人入胜且拟人化的文本。