Skip to main content

统一的 AI 访问接口 - 支持 OpenAI、Anthropic、Google 等多供应商

Project description

Dawn Shuttle Intelligence

统一的 AI 访问接口,类似 Vercel AI SDK。支持 OpenAI、Anthropic、Google、DeepSeek、Moonshot 等多供应商,提供一致的 API 体验。

特性

  • 🔄 统一接口 - 一套 API 访问多个 AI 供应商
  • 📦 开箱即用 - 支持 OpenAI、Anthropic、Google、DeepSeek、Moonshot
  • 🛠️ 工具调用 - 完整的 Function Calling 支持
  • 🌊 流式输出 - 异步生成器流式响应
  • 🖼️ 多模态 - 支持图片输入(Vision)
  • 异步优先 - 全异步设计,高性能
  • 🔌 易于扩展 - OpenAI 兼容基类,快速适配新供应商

安装

# 基础安装
pip install dawn_shuttle_intelligence

# 安装特定供应商
pip install dawn_shuttle_intelligence[openai]
pip install dawn_shuttle_intelligence[anthropic]
pip install dawn_shuttle_intelligence[google]

# 安装全部
pip install dawn_shuttle_intelligence[all]

快速开始

基础用法

import asyncio
from dawn_shuttle.dawn_shuttle_intelligence import OpenAIProvider, Message, GenerateConfig

async def main():
    # 创建供应商
    provider = OpenAIProvider(api_key="your-api-key")
    
    # 构建消息
    messages = [
        Message.system("你是一个有用的助手"),
        Message.user("你好!"),
    ]
    
    # 配置参数
    config = GenerateConfig(
        model="gpt-4o",
        temperature=0.7,
        max_tokens=1000,
    )
    
    # 生成响应
    response = await provider.generate(messages, config)
    print(response.text)

asyncio.run(main())

流式输出

from dawn_shuttle.dawn_shuttle_intelligence import OpenAIProvider, Message, GenerateConfig

async def stream_example():
    provider = OpenAIProvider(api_key="your-api-key")
    messages = [Message.user("讲一个故事")]
    config = GenerateConfig(model="gpt-4o", stream=True)
    
    async for chunk in provider.generate_stream(messages, config):
        if chunk.delta:
            print(chunk.delta, end="", flush=True)

多模态(图片)

from dawn_shuttle.dawn_shuttle_intelligence import Message, TextContent, ImageContent

messages = [
    Message.user([
        TextContent(text="描述这张图片"),
        ImageContent(image="https://example.com/image.png"),
    ])
]

支持的供应商

供应商 类名 模型示例
OpenAI OpenAIProvider gpt-4o, gpt-4-turbo, gpt-3.5-turbo
Anthropic AnthropicProvider claude-3-5-sonnet, claude-3-opus
Google GoogleProvider gemini-2.0-flash, gemini-1.5-pro
DeepSeek DeepSeekProvider deepseek-chat, deepseek-coder
Moonshot MoonshotProvider moonshot-v1-8k, moonshot-v1-32k

OpenAI 兼容供应商

DeepSeek 和 Moonshot 基于 OpenAICompatibleProvider,只需更改 base_url

from dawn_shuttle.dawn_shuttle_intelligence import DeepSeekProvider

provider = DeepSeekProvider(
    api_key="your-deepseek-key",
    # 自动使用 DeepSeek API 端点
)

工具调用(Function Calling)

定义工具

from dawn_shuttle.dawn_shuttle_intelligence import Tool, ToolResult, ToolParameter

class WeatherTool(Tool):
    """天气查询工具。"""
    
    name = "get_weather"
    description = "获取指定城市的天气信息"
    
    def get_parameters(self) -> list[ToolParameter]:
        return [
            ToolParameter(
                name="city",
                type="string",
                description="城市名称",
                required=True,
            ),
        ]
    
    async def execute(self, city: str) -> ToolResult:
        # 实现工具逻辑
        weather = f"{city}今天晴天,温度 25°C"
        return ToolResult(tool_call_id="", content=weather)

使用工具循环

from dawn_shuttle.dawn_shuttle_intelligence import (
    OpenAIProvider, Message, GenerateConfig,
    ToolRegistry, run_with_tools,
)

# 注册工具
registry = ToolRegistry()
registry.register(WeatherTool())

# 运行带工具的对话
result = await run_with_tools(
    messages=[Message.user("北京今天天气怎么样?")],
    provider=provider,
    tools=registry,
    config=GenerateConfig(model="gpt-4o"),
)

print(result.response.text)

错误处理

from dawn_shuttle.dawn_shuttle_intelligence import (
    AIError,
    AuthenticationError,
    RateLimitError,
    ModelNotFoundError,
    QuotaExceededError,
)

try:
    response = await provider.generate(messages, config)
except AuthenticationError as e:
    print(f"认证失败: {e}")
except RateLimitError as e:
    print(f"请求过快: {e}")
except ModelNotFoundError as e:
    print(f"模型不存在: {e}")
except AIError as e:
    print(f"AI 错误: {e}")

API 参考

GenerateConfig

参数 类型 说明
model str 模型标识
temperature float 采样温度 (0.0-2.0)
top_p float Top-p 采样
top_k int Top-k 采样
max_tokens int 最大输出 token
stop str/list 停止词
frequency_penalty float 频率惩罚
presence_penalty float 存在惩罚
seed int 随机种子
tools list 工具定义
tool_choice str/dict 工具选择策略
response_format dict 响应格式

Message

Message(role=Role.USER, content="Hello")
Message.user("Hello")           # 快捷方法
Message.system("You are...")    # 快捷方法
Message.assistant("Hi!")        # 快捷方法

GenerateResponse

response.text           # 响应文本
response.tool_calls     # 工具调用列表
response.finish_reason  # 结束原因
response.usage          # token 使用量
response.model          # 实际使用的模型

开发

# 安装开发依赖
pip install -e ".[dev]"

# 运行测试
pytest tests/ --cov=dawn_shuttle/dawn_shuttle_intelligence/src

# 代码检查
ruff check .
mypy .

许可证

GNU 宽松通用公共许可证 v2.1

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

dawn_shuttle_intelligence-0.2.1.tar.gz (89.9 kB view details)

Uploaded Source

Built Distribution

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

dawn_shuttle_intelligence-0.2.1-py3-none-any.whl (70.7 kB view details)

Uploaded Python 3

File details

Details for the file dawn_shuttle_intelligence-0.2.1.tar.gz.

File metadata

File hashes

Hashes for dawn_shuttle_intelligence-0.2.1.tar.gz
Algorithm Hash digest
SHA256 049f8dfdad20e02bed3b21666d5c4ce9cfac2452903a54725b6d9c4ec5fdf633
MD5 994ab6bab956906ec195fe1168b35797
BLAKE2b-256 699c5d5e4f5a4ad5d5773f5bc8d7712794b6ffe0ce9e0ff18fc7551debeb3f36

See more details on using hashes here.

Provenance

The following attestation bundles were made for dawn_shuttle_intelligence-0.2.1.tar.gz:

Publisher: publish.yml on dawn-shuttle/intelligence

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dawn_shuttle_intelligence-0.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for dawn_shuttle_intelligence-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0c4b72135762cb29d07a739111a0a4685ee6d956d5e6fc33740960070fd784a9
MD5 dbe2e42269d505502b3ea4faa3d35341
BLAKE2b-256 2146803b14ee4952ef56cda6ca392dd7e7528589d454888a1fe1283703f66db5

See more details on using hashes here.

Provenance

The following attestation bundles were made for dawn_shuttle_intelligence-0.2.1-py3-none-any.whl:

Publisher: publish.yml on dawn-shuttle/intelligence

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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