整个 AI助手 Google Gemini

随着GPT系列的对话生成语言模型的推出和发展,我们可以比较轻松的借助这些工具搭建出AI助手服务。可以说,GPT系列的语言模型就是搭建个人助手服务的一个要素

要素 : 某个项目工程的研发需要依赖一个或多个基础资源,这些资源可以被称作该项目工程的研发要素 ,简称为要素

注册申请API Key

通过科学的上网方式,我们可以有效地从ai.google.dev获取所需的Gemini-Pro API Key。

安装依赖

使用Python来做尝试,官方文档推荐了对应的SDK,我们可以安装一下:

bash 复制代码
pip install -q -U google-generativeai

通过SDK启用模型

我们先引入SDK:

python 复制代码
import google.generativeai as genai

接下来配置API Key:

python 复制代码
genai.configure(api_key='你的API Key')

可以看一下有哪些内容生成类型的模型:

python 复制代码
for m in genai.list_models():
    if 'generateContent' in m.supported_generation_methods:
        print(m.name)

目前来说有两个生成类型的模型:

  • 'gemini-pro': 文本对话模型
  • 'gemini-pro-vision': 具有视觉理解的文本对话模型

选择并启用文本对话模型:

python 复制代码
model = genai.GenerativeModel('gemini-pro')

尝试对话

现在,我们可以发送一段文本指令,让其返回一段内容:

python 复制代码
response = model.generate_content('写一首七言律诗,描述冬日萧瑟的田间景色。')

generate_content方法会返回response对象,我们可以通过它的text属性查看模型生成的内容:

python 复制代码
print(response.text)

上述代码的打印输出「一般不会相同」:

bash 复制代码
寒风萧萧冷气侵,
枯草败叶满目陈。
农田寂静无人迹,
唯有寒鸦绕树鸣。

沟渠冰封水尽涸,
枝头衰柳苦伶仃。
落日西沉归山去,
田间萧瑟倍凄清。

我们再让它回答一个编程问题:

python 复制代码
response = model.generate_content('使用golang编写一个简单的http文件服务器。')
print(response.text)

上述代码的打印输出「一般不会相同」:

go 复制代码
package main

import (
	"fmt"
	"log"
	"net/http"
	"os"
)

func main() {
	// Specify the directory that you want to serve files from
	dir := "static"

	// Create a new HTTP file server
	fs := http.FileServer(http.Dir(dir))

	// Create a new HTTP server
	server := &http.Server{
		Addr:    ":8080",
		Handler: fs,
	}

	// Start the HTTP server
	log.Printf("Listening on port :8080")
	if err := server.ListenAndServe(); err != nil {
		// Handle the error
		fmt.Println(err)
		os.Exit(1)
	}
}
  1. package main: This line indicates that this is the main Go source file for the program.

  2. import (...):: This line includes the necessary standard libraries for the program to function.

  3. func main(): This is the entry point for the program, where execution begins.

  4. dir := "static": This line specifies the directory from which the program will serve files.

  5. fs := http.FileServer(http.Dir(dir)): This line creates a new HTTP file server that serves files from the specified directory.

  6. server := &http.Server{...}: This line creates a new HTTP server object. The Addr field specifies the port on which the server will listen for requests, and the Handler field specifies the HTTP file server that will handle the requests.

  7. server.ListenAndServe(): This line starts the HTTP server. If an error occurs while starting the server, it is printed to the console and the program exits with an exit code of 1.

To use this program, you can follow these steps:

  1. Create a directory named "static" in the same directory as the program file.

  2. Copy the files that you want to serve to the "static" directory.

  3. Run the program using the command go run main.go.

  4. Open a web browser and navigate to http://localhost:8080 to access the files in the "static" directory.

多轮对话模式

除了这种单次内容生成,SDK还支持多轮对话:

python 复制代码
model = genai.GenerativeModel('gemini-pro')
chat = model.start_chat(history=[])

response = chat.send_message(
    "Pretend you\'re a snowman and stay in character for each response.")
print(response.text)

response = chat.send_message(
    "What\'s your favorite season of the year?")
print(response.text)

今天我们简单的了解了一下Gemini AI的开发使用,如果你对更多Gemini相关的内容感兴趣,可以翻看一下它的官方文档做进一步的学习。

相关推荐
牛大了20239 天前
【LLM学习】2-简短学习BERT、GPT主流大模型
gpt·学习·bert
1213410 天前
LLM:重构数字世界的“智能操作系统”
gpt·aigc·ai编程·llama·gpu算力
叠叠乐15 天前
ROS2编译的理解,与GPT对话
gpt
蚂蚁数据AntData17 天前
DB-GPT V0.7.2 版本更新:图表组件可视化增强、支持混合搜索 、支持DeepSeek-R1-0528模型等
大数据·数据库·gpt·架构·数据库架构
PacosonSWJTU20 天前
加载GPT-2模型参数报错:TensorFlow不存在
人工智能·gpt·tensorflow
zm-v-1593043398622 天前
GPT-ArcGIS 在生态评价中的综合应用:多因子权重分析与适宜性制图
gpt·arcgis
激进小猪100223 天前
LLM基础5_从零开始实现 GPT 模型
gpt
阿部多瑞 ABU1 个月前
# 从底层架构到应用实践:为何部分大模型在越狱攻击下失守?
gpt·安全·ai·自然语言处理
阿部多瑞 ABU1 个月前
大模型安全测试报告:千问、GPT 全系列、豆包、Claude 表现优异,DeepSeek、Grok-3 与 Kimi 存在安全隐患
gpt·安全·ai
武子康1 个月前
AI炼丹日志-28 - Audiblez 将你的电子书epub转换为音频mp3 做有声书
人工智能·爬虫·gpt·算法·机器学习·ai·音视频