Skip to main content

A Python library that encapsulates AI model APIs

Project description

soga_ai

soga_ai是一个封装了大模型相关接口的Python库,提供了统一的API来调用不同的AI供应商(如OpenAI、Google等)的服务。

特性

  • 统一的API接口,简化AI模型调用
  • 支持多种AI供应商(OpenAI、Google、Ollama等)
  • 可扩展的架构,易于添加新的供应商和协议
  • 强类型支持,提高代码健壮性
  • 灵活的供应商配置,支持动态添加供应商
  • 支持文本生成、图像生成和音频生成功能
  • 图像和音频自动生成并保存到文件
  • 简化的API密钥配置方式
  • 支持自定义保存路径

安装

pip install soga_ai

快速开始

首先设置环境变量:

export OPENAI_API_KEY="your-openai-api-key"
export GOOGLE_API_KEY="your-google-api-key"

然后在代码中使用:

from soga_ai import Client

# 创建客户端实例(API密钥从环境变量自动读取)
client = Client()

# 或者直接在代码中提供API密钥
# client = Client(
#     openai_api_key="your-openai-api-key",
#     google_api_key="your-google-api-key"
# )

# 使用默认供应商进行文本到文本转换
result = client.text_to_text("Translate the following English text to French: 'Hello, how are you?'")
print(result)

# 使用默认供应商进行文本到图像转换(图像会自动保存到文件)
image_path = client.text_to_image("A beautiful sunset over the ocean")
print(f"Image saved to: {image_path}")

# 使用默认供应商进行文本到音频转换(音频会自动保存到文件)
audio_path = client.text_to_audio("你好,这是一个文本转语音的示例。")
print(f"Audio saved to: {audio_path}")

# 指定特定供应商调用
result = client.text_to_text("Summarize this article", provider="google")

# 使用Ollama本地模型
result = client.text_to_text("Explain quantum computing in simple terms", provider="ollama")

# 使用自定义参数调用
result = client.text_to_text(
    "Write a short poem about technology",
    provider="openai",
    model="gpt-4o-mini",
    temperature=0.5,
    max_tokens=500
)

# 使用自定义保存路径
image_path = client.text_to_image(
    "A beautiful sunset over the ocean", 
    save_path="my_images/sunset.png"
)
print(f"Image saved to: {image_path}")

audio_path = client.text_to_audio(
    "你好,这是一个文本转语音的示例。",
    save_path="my_audio/example.mp3"
)
print(f"Audio saved to: {audio_path}")

使用Google图像生成

soga_ai支持使用Google的原生图像生成功能,生成的图像会自动保存到文件:

from soga_ai import Client

client = Client()

# 生成图像(自动保存到文件)
image_path = client.text_to_image(
    "A beautiful sunset over a futuristic city with flying cars",
    provider="google"
)

print(f"Image saved to: {image_path}")

# 使用自定义保存路径
image_path = client.text_to_image(
    "A beautiful sunset over a futuristic city with flying cars",
    provider="google",
    save_path="custom_images/google_sunset.png"
)

print(f"Image saved to: {image_path}")

使用Edge文本到音频

soga_ai支持使用Microsoft Edge的文本到语音功能(通过edge-tts库),生成的音频会自动保存到文件:

from soga_ai import Client

client = Client()

# 生成音频(自动保存到文件)
audio_path = client.text_to_audio(
    "你好,这是一个文本转语音的示例。",
    provider="edge"
)

print(f"Audio saved to: {audio_path}")

# 使用自定义参数
audio_path = client.text_to_audio(
    "This is a text to speech example with custom parameters.",
    provider="edge",
    model="en-US-JennyNeural",
    speed=1.25  # 1.25倍速
)

print(f"Audio saved to: {audio_path}")

# 使用自定义保存路径
audio_path = client.text_to_audio(
    "This is a text to speech example with a custom save path.",
    provider="edge",
    save_path="custom_audio/example.mp3"
)

print(f"Audio saved to: {audio_path}")

使用Ollama

要使用Ollama本地模型,需要先安装Ollama并拉取模型:

# 安装Ollama
curl -fsSL https://ollama.com/install.sh | sh

# 拉取gemma3模型
ollama run gemma3:1b

Ollama默认在http://localhost:11434运行,soga_ai已经配置好通过OpenAI兼容API与Ollama通信。

动态添加供应商

soga_ai支持动态添加新的供应商:

from soga_ai import Client, ProviderConfig

client = Client()

# 添加自定义供应商配置
custom_config = ProviderConfig(
    base_url="https://custom-api.example.com/v1",
    api_key="your-custom-key",
    default_models={
        "text_to_text": "custom-model-v1",
        "text_to_image": "custom-image-model-v1"
    },
    supported_protocols=["text_to_text", "text_to_image"]
)

# 添加供应商并指定使用的协议实现
client.add_provider("custom", custom_config, "openai")  # 使用OpenAI协议实现

开发

安装依赖

uv sync

运行测试

uv run pytest tests/

运行示例

# 运行基本使用示例
export OPENAI_API_KEY="your-openai-api-key"
export GOOGLE_API_KEY="your-google-api-key"
uv run python examples/basic_usage.py

# 运行Google图像生成示例
export GOOGLE_API_KEY="your-google-api-key"
uv run python examples/google_image_generation.py

# 运行Edge文本到音频示例
uv run python examples/edge_text_to_audio.py

# 运行动态添加供应商示例
uv run python examples/dynamic_provider.py

# 运行自定义保存路径示例
uv run python examples/custom_save_path_image.py
uv run python examples/custom_save_path_audio.py

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

soga_ai-0.1.1.tar.gz (12.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

soga_ai-0.1.1-py3-none-any.whl (13.2 kB view details)

Uploaded Python 3

File details

Details for the file soga_ai-0.1.1.tar.gz.

File metadata

  • Download URL: soga_ai-0.1.1.tar.gz
  • Upload date:
  • Size: 12.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.7

File hashes

Hashes for soga_ai-0.1.1.tar.gz
Algorithm Hash digest
SHA256 fbcd7f9613ada6e9d956932291c4ca81ce809a625049433ded2714666c1bc49e
MD5 9e3662489d4d3babce06f5376f5d4a70
BLAKE2b-256 7d62b05ed352ab96bb081c24a6bc31f05284f36f97c98e435e2f780d2120f47c

See more details on using hashes here.

File details

Details for the file soga_ai-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: soga_ai-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 13.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.7

File hashes

Hashes for soga_ai-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b5c7717ce5c16c950da571fe0306737d7fc147aaa5f80eddbf45d294c6787b6d
MD5 d518a6eaabcd12c5cfc1b4287a6eb1a0
BLAKE2b-256 ece5c28064436ce26f8d00b8600a11c11de46c9fab0793209c50581eb17fdfd9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page