首页 > 其他分享 >pydantic ai agent 框架

pydantic ai agent 框架

时间:2024-12-03 19:32:51浏览次数:9  
标签:customer ai support agent deps type pydantic

pydantic 团队也开始搞ai agent 框架了,目前属于beta 版

使用pydantic ai 的一些原因(来自官方介绍)

  • 来自pydantic团队,不少llm python sdk 都使用了此包
  • 模型无关,尽管当前支持的还有限,但是提供了简单接口可以扩展
  • 类型安全
  • 支持基于普通python 代码的控制流以及agent 组合,和以前开发python 项目代码的方式类似
  • 支持结构化输出
  • 流响应
  • 类型安全的依赖注入系统
  • logfire 集成,可以方便的进行监控以及调试

一个简单示例

来自官方文档,可以看到代码更加符合python的编码方式

from dataclasses import dataclass

from pydantic import BaseModel, Field
from pydantic_ai import Agent, RunContext

from bank_database import DatabaseConn


# SupportDependencies is used to pass data, connections, and logic into the model that will be needed when running
# system prompt and tool functions. Dependency injection provides a type-safe way to customise the behavior of your agents.
@dataclass
class SupportDependencies:
    customer_id: int
    db: DatabaseConn


# This pydantic model defines the structure of the result returned by the agent.
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)


# This agent will act as first-tier support in a bank.
# Agents are generic in the type of dependencies they accept and the type of result they return.
# In this case, the support agent has type `Agent[SupportDependencies, SupportResult]`.
support_agent = Agent(
    'openai:gpt-4o',
    deps_type=SupportDependencies,
    # The response from the agent will, be guaranteed to be a SupportResult,
    # if validation fails the agent is prompted to try again.
    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.'
    ),
)


# Dynamic system prompts can can make use of dependency injection.
# Dependencies are carried via the `RunContext` argument, which is parameterized with the `deps_type` from above.
# If the type annotation here is wrong, static type checkers will catch it.
@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}"


# `tool` let you register functions which the LLM may call while responding to a user.
# Again, dependencies are carried via `RunContext`, any other arguments become the tool schema passed to the LLM.
# Pydantic is used to validate these arguments, and errors are passed back to the LLM so it can retry.
@support_agent.tool
async def customer_balance(
    ctx: RunContext[SupportDependencies], include_pending: bool
) -> float:
    """Returns the customer's current account balance."""
    # The docstring of a tool is also passed to the LLM as the description of the tool.
    # Parameter descriptions are extracted from the docstring and added to the parameter schema sent to the LLM.
    balance = await ctx.deps.db.customer_balance(
        id=ctx.deps.customer_id,
        include_pending=include_pending,
    )
    return balance


...  # In a real use case, you'd add more tools and a longer system prompt


async def main():
    deps = SupportDependencies(customer_id=123, db=DatabaseConn())
    # Run the agent asynchronously, conducting a conversation with the LLM until a final response is reached.
    # Even in this fairly simple case, the agent will exchange multiple messages with the LLM as tools are called to retrieve a result.
    result = await support_agent.run('What is my balance?', deps=deps)
    # The result will be validated with Pydantic to guarantee it is a `SupportResult`, since the agent is generic,
    # it'll also be typed as a `SupportResult` to aid with static type checking.
    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. We are temporarily blocking your card to prevent unauthorized transactions." block_card=True risk=8
    """

说明

尽管pydantic ai目前比较新,但是设计上还是很不错的,很值得尝试下,这个很pydantic

参考资料

https://ai.pydantic.dev/

https://github.com/pydantic/logfire

https://github.com/pydantic/pydantic-ai

标签:customer,ai,support,agent,deps,type,pydantic
From: https://www.cnblogs.com/rongfengliang/p/18584899

相关文章

  • AI自动批量混剪工具!
    今天再分享这个批量剪辑神器,非常适合短剧和带货短视频的制作,轻松过原创,提供了从视频分割、合成、混剪到格式转换的多项功能。以下是它的主要功能:视频分割与提取按时长或段数分割按镜头转场变化分割按语音内容自动分割提取无声视频或音频视频合成自动合成文......
  • 一键AI去除视频水印和字幕!关键还免费!
    我们做网创的,多多少少会去下载很多视频素材,但很多素材自己觉得非常好,但有那该死的水印和字幕就让人非常不爽,要么在剪辑的时候放大把字幕拉出屏幕外,又或者是用黑框该掉字幕,这着实影响美观!有什么办法可以直接去掉字幕和水印吗?答案是有的!今天就给大家分享这个神器:VideoSub......
  • AI自动批量混剪工具!
    今天再分享这个批量剪辑神器,非常适合短剧和带货短视频的制作,轻松过原创,提供了从视频分割、合成、混剪到格式转换的多项功能。以下是它的主要功能:视频分割与提取按时长或段数分割按镜头转场变化分割按语音内容自动分割提取无声视频或音频视频合成自动合成文......
  • 一键AI去除视频水印和字幕!关键还免费!
    我们做网创的,多多少少会去下载很多视频素材,但很多素材自己觉得非常好,但有那该死的水印和字幕就让人非常不爽,要么在剪辑的时候放大把字幕拉出屏幕外,又或者是用黑框该掉字幕,这着实影响美观!有什么办法可以直接去掉字幕和水印吗?答案是有的!今天就给大家分享这个神器:VideoSub......
  • AI自动批量混剪工具!
    今天再分享这个批量剪辑神器,非常适合短剧和带货短视频的制作,轻松过原创,提供了从视频分割、合成、混剪到格式转换的多项功能。以下是它的主要功能:视频分割与提取按时长或段数分割按镜头转场变化分割按语音内容自动分割提取无声视频或音频视频合成自动合成文......
  • 一键AI去除视频水印和字幕!关键还免费!
    我们做网创的,多多少少会去下载很多视频素材,但很多素材自己觉得非常好,但有那该死的水印和字幕就让人非常不爽,要么在剪辑的时候放大把字幕拉出屏幕外,又或者是用黑框该掉字幕,这着实影响美观!有什么办法可以直接去掉字幕和水印吗?答案是有的!今天就给大家分享这个神器:VideoSub......
  • AI自动批量混剪工具!
    今天再分享这个批量剪辑神器,非常适合短剧和带货短视频的制作,轻松过原创,提供了从视频分割、合成、混剪到格式转换的多项功能。以下是它的主要功能:视频分割与提取按时长或段数分割按镜头转场变化分割按语音内容自动分割提取无声视频或音频视频合成自动合成文......
  • 效率疯涨,这四个AI后悔没有早点用
    在繁忙的职场生活之中,时间就是最大的成本,大家都需要学习如何在职场生活中提高自己的效率和获取知识的速度。在人工智能技术高速发展的今天,催生出一些系列的AI工具,他们不仅能帮助我们高效的处理日常工作中遇到的难题,更能帮助我们提高效率1、文思助手:长文、公文写作助手文思......
  • 【ABAP——DAILOG_2】
    文章目录使用Tabstrip控件实现分页签效果标签页的修改使用TableControl控件实现表单输出表格控件使用向导创建TableControlTableControl列的修改用户通过界面输入数据,数据通过屏幕控件传递到ABAP/4程序中的变量,程序在PBO中准备数据并显示界面,用户回车后,PAI处理用......
  • In Cursor IDE, which one is better to process the above job, composer normal mo
    Forprocessingalarge-scaledocumentationorganizationtask,AgentmodeisbetterthanComposermodeinCursorIDE.Here'swhy:AgentModeAdvantages:ToolIntegrationDirectaccesstoall8agenttools:-list_dir-codebase_search-read_file......