首页
/ 【亲测免费】 Google Gemini 生成式 AI Python 项目教程

【亲测免费】 Google Gemini 生成式 AI Python 项目教程

2026-01-20 01:51:04作者:舒璇辛Bertina

1. 项目目录结构及介绍

generative-ai-python/
├── docs/
│   ├── README.md
│   └── ...
├── examples/
│   ├── example1.py
│   └── ...
├── tests/
│   ├── test_example1.py
│   └── ...
├── third_party/
│   ├── library1/
│   └── ...
├── .editorconfig
├── .gitignore
├── CODEOWNERS
├── CODE_OF_CONDUCT.md
├── CONTRIBUTING.md
├── LICENSE
├── README.md
├── RELEASE.md
├── pyproject.toml
└── setup.py

目录结构说明

  • docs/: 存放项目的文档文件,包括 README.md 等。
  • examples/: 存放项目的示例代码文件,如 example1.py
  • tests/: 存放项目的测试代码文件,如 test_example1.py
  • third_party/: 存放第三方库或依赖项。
  • .editorconfig: 配置文件,用于统一代码风格。
  • .gitignore: Git 忽略文件配置。
  • CODEOWNERS: 定义项目的主要维护者。
  • CODE_OF_CONDUCT.md: 项目的行为准则。
  • CONTRIBUTING.md: 贡献指南。
  • LICENSE: 项目的开源许可证。
  • README.md: 项目的介绍和使用说明。
  • RELEASE.md: 发布说明。
  • pyproject.toml: Python 项目配置文件。
  • setup.py: Python 项目安装脚本。

2. 项目启动文件介绍

项目的启动文件通常是 examples/ 目录下的示例代码文件,例如 example1.py。这些文件展示了如何使用项目的主要功能。

示例代码文件 (example1.py)

import google.generativeai as genai
import os

# 配置 API 密钥
genai.configure(api_key=os.environ["GEMINI_API_KEY"])

# 创建模型并运行提示
model = genai.GenerativeModel('gemini-1.5-flash')
response = model.generate_content("The opposite of hot is")
print(response.text)

启动步骤

  1. 设置环境变量 GEMINI_API_KEY 为你的 API 密钥。
  2. 运行 python examples/example1.py 启动示例代码。

3. 项目配置文件介绍

pyproject.toml

pyproject.toml 是 Python 项目的配置文件,用于定义项目的依赖、构建系统和工具配置。

[build-system]
requires = ["setuptools>=42", "wheel"]
build-backend = "setuptools.build_meta"

[project]
name = "google-generativeai"
version = "0.1.0"
description = "The official Python library for the Google Gemini API"
authors = [
    { name="Google", email="no-reply@google.com" }
]
dependencies = [
    "requests>=2.25.1",
    "numpy>=1.19.5"
]

setup.py

setup.py 是 Python 项目的安装脚本,用于定义项目的元数据和依赖项。

from setuptools import setup, find_packages

setup(
    name='google-generativeai',
    version='0.1.0',
    description='The official Python library for the Google Gemini API',
    author='Google',
    author_email='no-reply@google.com',
    packages=find_packages(),
    install_requires=[
        'requests>=2.25.1',
        'numpy>=1.19.5'
    ],
)

配置步骤

  1. 安装依赖项:运行 pip install -r requirements.txt
  2. 配置 API 密钥:在环境变量中设置 GEMINI_API_KEY

通过以上步骤,你可以成功启动和配置 Google Gemini 生成式 AI Python 项目。

登录后查看全文
热门项目推荐
相关项目推荐

项目优选

收起