Promptic:Python 中的 LLM 应用开辟利器

打印 上一主题 下一主题

主题 850|帖子 850|积分 2550



Promptic 是一个基于 Python 的轻量级库,旨在简化与大型语言模子(LLMs)的交互。它通过提供简洁的装饰器 API 和强大的功能,帮助开辟者高效地构建 LLM 应用步伐。Promptic 的设计理念是提供 90% 的 LLM 应用开辟所需功能,同时保持代码的简洁和易用性。
1. Promptic 的核心功能

1.1 简化 LLM 交互 Promptic 提供了一个装饰器 @llm,用于界说与 LLM 交互的函数。通过装饰器,你可以直接在函数的 docstring 中界说提示(prompt),并自动将函数参数插入到提示中。
Python复制
  1. from promptic import llm
  2. @llm
  3. def translate(text, language="Chinese"):
  4.     """Translate '{text}' to {language}"""
  5. print(translate("Hello world!"))
  6. # 输出:您好,世界!
复制代码
1.2 支持 Pydantic 模子 Promptic 支持利用 Pydantic 模子界说 LLM 的输出结构,确保 LLM 的响应符合预界说的模式。
Python复制
  1. from pydantic import BaseModel
  2. from promptic import llm
  3. class Forecast(BaseModel):
  4.     location: str
  5.     temperature: float
  6.     units: str
  7. @llm
  8. def get_weather(location, units: str = "fahrenheit") -> Forecast:
  9.     """What's the weather for {location} in {units}?"""
  10. print(get_weather("San Francisco", units="celsius"))
  11. # 输出:location='San Francisco' temperature=16.0 units='Celsius'
复制代码
1.3 流式响应 Promptic 支持流式响应,允许及时吸收 LLM 的输出,实用于长文本内容或交互式应用。
Python复制
  1. from promptic import llm
  2. @llm(stream=True)
  3. def write_poem(topic):
  4.     """Write a haiku about {topic}."""
  5. print("".join(write_poem(
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

立聪堂德州十三局店

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表