AIパーソナル・ラーニング
と実践的なガイダンス
豆包Marscode1

PydanticAI: PydanticでジェネレーティブAIアプリを作れば、本番レベルのAIアプリが簡単に作れる

はじめに

PydanticAIはPydanticベースのPythonエージェントフレームワークで、生成AIアプリケーションの開発を簡素化するために設計されています。Pydanticチームによって開発され、幅広いモデル(OpenAI、Gemini、Groqなど)をサポートし、型安全な制御フローとエージェントの組み合わせを提供します。PydanticAIは、構造化された応答の検証とストリーミング応答によって、生成AIアプリケーションの効率性と信頼性を保証します。PydanticAIは現在初期ベータ版であり、APIは変更される可能性がありますので、ユーザーからのフィードバックをお待ちしております。

 

機能一覧

  • マルチモデル対応OpenAI、Gemini、Groq、その他多くの生成AIモデルと互換性があります。
  • タイプセーフティPydanticを使用した構造化レスポンスの検証により、データ型のセキュリティを確保。
  • 依存性注入システム型安全な依存性注入を提供し、テストと反復開発を容易にします。
  • ストリーミング対応アプリケーションの応答性と信頼性を向上させるために、ストリーミング応答と検証をサポートします。
  • ログファイアとの統合ジェネレーティブAIアプリケーションのパフォーマンスと動作のデバッグとモニタリング用。
  • シンプルなインターフェース他のモデルとの拡張や統合を容易にする、クリーンなインターフェイスを提供します。

 

ヘルプの使用

設置プロセス

  1. PydanticAIのインストールPythonのバージョンが3.9以上であることを確認し、以下のコマンドでPydanticAIをインストールしてください:
   pip install pydantic-ai
  1. 依存関係のインストールPydanticAI は多くのコアライブラリと LLM API に依存しており、これらの依存関係はインストール時に自動的に処理されます。

使用ガイドライン

簡単なプロキシの作成

  1. エージェントの定義簡単なエージェントを作成し、使用するモデルを指定します。
   from pydantic_ai import Agent
agent = Agent(
'gemini-1.5-flash',
system_prompt='Be concise, reply with one sentence.',
)
  1. ランニング・エージェント簡単な対話のためのエージェントの同時実行。
   result = agent.run_sync('Where does "hello world" come from?')
print(result.data)

複雑なプロキシの例

  1. 依存モデルとアウトカムモデルの定義Pydanticを使って依存モデルと結果モデルを定義する。
   from dataclasses import dataclass
from pydantic import BaseModel, Field
from pydantic_ai import Agent, RunContext
from bank_database import DatabaseConn
@dataclass
class SupportDependencies:
customer_id: int
db: DatabaseConn
class SupportResult(BaseModel):
support_advice: str = Field(description='Advice returned to the customer')
block_card: bool = Field(description="Whether to block the customer's card")
risk: int = Field(description='Risk level of query', ge=0, le=10)
  1. サポートエージェントの作成システムのヒントと効用関数を定義する。
   support_agent = Agent(
'openai:gpt-4o',
deps_type=SupportDependencies,
result_type=SupportResult,
system_prompt=(
'You are a support agent in our bank, give the '
'customer support and judge the risk level of their query.'
),
)
@support_agent.system_prompt
async def add_customer_name(ctx: RunContext[SupportDependencies]) -> str:
customer_name = await ctx.deps.db.customer_name(id=ctx.deps.customer_id)
return f"The customer's name is {customer_name!r}"
@support_agent.tool
async def customer_balance(ctx: RunContext[SupportDependencies], include_pending: bool) -> float:
"""Returns the customer's current account balance."""
return await ctx.deps.db.customer_balance(id=ctx.deps.customer_id, include_pending=include_pending)
  1. サポートエージェントの実行定義された依存関係でエージェントを実行します。
   async def main():
deps = SupportDependencies(customer_id=123, db=DatabaseConn())
result = await support_agent.run('What is my balance?', deps=deps)
print(result.data)
# 输出示例:support_advice='Hello John, your current account balance, including pending transactions, is $123.45.' block_card=False risk=1
result = await support_agent.run('I just lost my card!', deps=deps)
print(result.data)
# 输出示例:support_advice="I'm sorry to hear that, John. Your card has been blocked for security reasons." block_card=True risk=7

デバッグとモニタリング

PydanticAIは、ジェネレーティブAIアプリケーションのパフォーマンスと動作のデバッグとモニタリングのためにLogfireを統合しています。Logfireを使用することで、エージェントの動作をリアルタイムで確認し、応答データを分析し、アプリケーションのパフォーマンスを最適化することができます。

フィードバックと改善

PydanticAIは現在初期ベータ版であり、APIは変更される可能性があります。使用中に問題が発生した場合や改善提案がある場合は、GitHubを通じてフィードバックをお寄せください。


無断転載を禁じます:チーフAIシェアリングサークル " PydanticAI: PydanticでジェネレーティブAIアプリを作れば、本番レベルのAIアプリが簡単に作れる
ja日本語