1M上下文的Gemini2.0最新版(1月22日) :能读会看还自带搜索功能,完全免费!!

开门见山:如何获取免费API密钥

获取API密钥的网址:
https://aistudio.google.com/

具体申请步骤如下图所示(建议按图操作):
图片

如果想深入了解更多技术细节,可以查看官方文档(英文):
https://ai.google.dev/gemini-api/docs/models/gemini-v2

Gemini SDK:一站式AI开发解决方案

Google这次推出的Gemini SDK特别友好,就像一个百宝箱,把各种常用功能都集成在一起了。下面我们来看看它能做些什么:

1. 图像理解功能

这段代码展示了如何让AI分析图片。你只需要提供一张图片,AI就能帮你分析图片中的内容、构图,甚至能猜测图片背后的故事。

from google import genai
import PIL.Image

# 初始化客户端
client = genai.Client(api_key="YOUR_API_KEY")

# 加载图像
image = PIL.Image.open('example.jpg')

# 多模态分析
response = client.models.generate_content(
    model="gemini-2.0-flash-exp", 
    contents=[
        image, 
        "请详细分析这张图片的内容、构图和可能的背景故事"
    ]
)

print(response.text)

2. 智能搜索功能

这个功能直接集成了Google搜索,可以帮你实时获取最新信息。比如查询节假日、新闻热点等,都非常方便。

from google import genai
from google.genai.types import Tool, GenerateContentConfig, GoogleSearch

# 配置搜索工具
search_tool = Tool(google_search=GoogleSearch())

# 执行带搜索的查询
response = client.models.generate_content(
    model="gemini-2.0-flash-exp",
    contents="你知道openai的星际之门计划么?",
    config=GenerateContentConfig(
        tools=[search_tool],
        response_modalities=["TEXT"]
    )
)

print(response.text)

3. 智能函数调用

这个功能就像给AI配备了一个智能助手,你可以定义各种功能(比如查天气),AI就能自动调用相应的功能来完成任务。

from google import genai
from google.genai import types

# 定义函数声明
weather_function = types.FunctionDeclaration(
    name="get_weather",
    description="获取指定城市的天气信息",
    parameters={
        "type": "OBJECT", 
        "properties": {
            "city": {
                "type": "STRING", 
                "description": "城市名称"
            },
            "date": {
                "type": "STRING", 
                "description": "查询日期"
            }
        }
    }
)

# 创建工具
weather_tool = types.Tool(
    function_declarations=[weather_function]
)

# 生成内容时使用函数调用
response = client.models.generate_content(
    model="gemini-2.0-flash-exp",
    contents="帮我查询上海明天的天气",
    config=types.GenerateContentConfig(
        tools=[weather_tool],
        temperature=0.2
    )
)

4. 文件处理能力

这个功能特别实用,可以直接处理PDF等文档文件。比如你有一份长文档,AI可以帮你快速总结内容。

# 上传文件
file_upload = client.files.upload(path="research_paper.pdf")

# 分析文件
response = client.models.generate_content(
    model="gemini-2.0-flash-exp",
    contents=[
        types.Content(
            role="user",
            parts=[
                types.Part.from_uri(
                    file_uri=file_upload.uri,
                    mime_type=file_upload.mime_type
                ),
                "请提供这份文档的详细摘要"
            ]
        )
    ]
)

5. 文本向量处理

这个功能可以把文字转换成数字(向量),对于做文本相似度比较、文本分类等任务特别有用。

# 生成文本向量
response = client.models.embed_content(
    model="text-embedding-004",
    contents=[
        "机器学习是人工智能的重要分支",
        "深度学习是机器学习的子领域",
        "神经网络是深度学习的基础"
    ],
    config=types.EmbedContentConfig(
        output_dimensionality=128  # 自定义向量维度
    )
)

# 查看嵌入结果
for embedding in response.embeddings:
    print(len(embedding.values))  # 应为128

6. 实时内容生成

这个功能可以让AI一边想一边说,就像人类思考时的过程一样,内容会逐步显示出来。

# 实时分块生成内容
for chunk in client.models.generate_content_stream(
    model="gemini-2.0-flash-exp",
    contents="讲述一个跨越时空的科幻故事"
):
    print(chunk.text, end='', flush=True)

7. 结构化数据生成

如果你需要AI生成特定格式的数据(比如JSON),这个功能可以帮你精确控制输出的数据格式。

from pydantic import BaseModel
from typing import List

# 定义JSON模型
class CookRecipe(BaseModel):
    name: str
    ingredients: List[str]
    steps: List[str]

# 生成结构化JSON
response = client.models.generate_content(
    model="gemini-2.0-flash-exp",
    contents="生成一个中国家常菜的食谱",
    config=types.GenerateContentConfig(
        response_mime_type="application/json",
        response_schema=CookRecipe
    )
)

8. 多轮对话功能

这个功能让AI能够记住对话的上下文,可以进行连续的对话,就像和真人聊天一样。

# 创建对话
chat = client.chats.create(
    model="gemini-2.0-flash-exp",
    config=types.GenerateContentConfig(
        system_instruction="你是一个专业的python课程老师",
        temperature=0.7
    )
)

# 发送消息
response1 = chat.send_message("如何学习Python?")
response2 = chat.send_message("能否给我一个具体的学习方法?")

这还不赶紧试试???

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值