**wrappers_delight**是一个围绕OpenAI的ChatCompletion API构建的Python包装器。该包装器的主要特点包括:
要开始使用,你需要克隆此仓库:
git clone https://github.com/yoheinakajima/wrappers_delight.git
cd wrappers_delight
确保已安装OpenAI的Python客户端:
pip install openai
要使用包装器进行与模型的基本对话,加载包装器后,每个提示输入和输出都将被存储:<br> *除第2行外,这是标准的OpenAI ChatCompletion调用。
import openai
from wrappers_delight import ChatWrapper
# 设置你的OpenAI API密钥
openai.api_key = "YOUR_API_KEY"
# 与模型进行对话
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Tell me a fun fact."},
]
)
print(response.choices[0].message.content)
注册和使用自定义函数:<br> *除第2行外,这是使用函数的标准OpenAI ChatCompletion调用。
import openai
from wrappers_delight.wrapper import _enhanced_chat_completion
# 设置你的OpenAI API密钥
openai.api_key = "YOUR_OPENAI_KEY"
completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo-0613",
messages=[
{
"role": "user",
"content": "I visited Tokyo, then moved to San Francisco, and finally settled in Toronto."
}
],
functions=[
{
"name": "extract_locations",
"description": "Extract all locations mentioned in the text",
"parameters": {
"type": "object",
"properties": {
"locations": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {
"type": "string",
"description": "The name of the location"
},
"country_iso_alpha2": {
"type": "string",
"description": "The ISO alpha-2 code of the country where the location is situated"
}
},
"required": ["name", "country_iso_alpha2"]
}
}
},
"required": ["locations"],
},
},
],
function_call={"name": "extract_locations"}
)
response_data = completion.choices[0]['message']['function_call']['arguments']
print(response_data)
与模型的所有交互都会自动记录到log.ndjson文件中。NDJSON中的每一行都包含请求参数和相应的模型响应。
以下是log.ndjson中的两个示例输出。第一个是标准的ChatCompletion调用,第二个是函数调用:
{"timestamp": "2023-08-13 03:00:49", "response_time": 3.21, "params": {"model": "gpt-3.5-turbo", "messages": [{"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "Tell me a fun fact."}]}, "response": {"id": "chatcmpl-7mvdPu0H1QULvDZOUVJS6npdMslul", "object": "chat.completion", "created": 1691895647, "model": "gpt-3.5-turbo-0613", "choices": [{"index": 0, "message": {"role": "assistant", "content": "Sure! Here's a fun fact: Did you know that honey never spoils? Archaeologists have found pots of honey in ancient Egyptian tombs that are over 3,000 years old and still perfectly edible! Honey's low moisture content and acidic pH create an inhospitable environment for bacteria and other microorganisms, allowing it to last indefinitely."}, "finish_reason": "stop"}], "usage": {"prompt_tokens": 23, "completion_tokens": 70, "total_tokens": 93}}, "total_tokens": 93, "model": "gpt-3.5-turbo"}
{"timestamp": "2023-08-13 03:01:16", "response_time": 4.80, "params": {"model": "gpt-3.5-turbo-0613", "messages": [{"role": "user", "content": "I visited Tokyo, then moved to San Francisco, and finally settled in Toronto."}], "functions": [{"name": "extract_locations", "description": "Extract all locations mentioned in the text", "parameters": {"type": "object", "properties": {"locations": {"type": "array", "items": {"type": "object", "properties": {"name": {"type": "string", "description": "The name of the location"}, "country_iso_alpha2": {"type": "string", "description": "The ISO alpha-2 code of the country where the location is situated"}}, "required": ["name", "country_iso_alpha2"]}}}, "required": ["locations"]}}], "function_call": {"name": "extract_locations"}}, "response": {"id": "chatcmpl-7mvdqfScl0uQ2tfye1HdfcPEV5XYI", "object": "chat.completion", "created": 1691895674, "model": "gpt-3.5-turbo-0613", "choices": [{"index": 0, "message": {"role": "assistant", "content": null, "function_call": {"name": "extract_locations", "arguments": "{\n \"locations\": [\n {\n \"name\": \"Tokyo\",\n \"country_iso_alpha2\": \"JP\"\n },\n {\n \"name\": \"San Francisco\",\n \"country_iso_alpha2\": \"US\"\n },\n {\n \"name\": \"Toronto\",\n \"country_iso_alpha2\": \"CA\"\n }\n ]\n}"}}, "finish_reason": "stop"}], "usage": {"prompt_tokens": 83, "completion_tokens": 74, "total_tokens": 157}}, "total_tokens": 157, "model": "gpt-3.5-turbo-0613"}
要运行分析并可视化模型的使用情况,加载分析模块:
from wrappers_delight.analytics import plot_token_usage, plot_model_distribution, plot_response_time_distribution
plot_token_usage() plot_model_distribution() plot_response_time() plot_response_time_distribution()
# 查询日志
要启用日志查询功能,请加载以下两个查询函数:
from wrappers_delight.analytics import query_log, query_log_with_ai
*query_log()* 和 *query_log_with_ai()* 是用于从日志中检索特定条目的函数。query_log 根据各种参数直接查询日志,而 query_log_with_ai 使用 AI 模型根据上下文或更复杂的要求进一步优化结果。
## 前提条件
确保已安装所需的库。这包括 pandas、openai 以及其他根据需要的库。
## 使用 query_log()
参数:
* columns:要在结果中显示的列列表。
* start_date:要检索的日志的起始日期。
* end_date:日志的结束日期。
* min_tokens:响应中的最小令牌数。
* max_tokens:响应中的最大令牌数。
* filter_by:附加过滤条件。
* sort_by:结果应排序的列(默认为时间戳)。
* limit:限制结果中的行数。
* keyword:在用户消息或响应中搜索关键词。
* function_name:按函数名过滤。
* model_version:按模型版本过滤。
示例代码:
result = query_log(start_date="2023-08-01", end_date="2023-08-10", keyword="weather") print(result)
## 使用 query_log_with_ai
此函数解释自然语言用户查询,并将其转换为参数,使用 query_log 函数获取适当的日志条目。
### 使用示例:
用户查询:
result = query_log_with_ai("显示最后 3 条日志。")
query_log 的预期参数:
{ "limit": 3, "sort_by": "timestamp" }
用户查询:
result = query_log_with_ai("我想看到提到'天气'的前 5 条日志。")
query_log 的预期参数:
{ "keyword": "weather", "limit": 5 }
用户查询:
result = query_log_with_ai("你能按总令牌数对 gpt-3.5-turbo 模型的日志进行排序,但只显示时间戳、模型和总令牌数列吗?")
query_log 的预期参数:
{ "model_version": "gpt-3.5-turbo", "sort_by": "total_tokens", "columns": ["timestamp", "model", "total_tokens"] }
用户查询:
result = query_log_with_ai("我想要使用了 50 到 1000 个令牌并且只使用 gpt-3.5-turbo 模型的日志。")
query_log 的预期参数:
{ "min_tokens": 50, "max_tokens": 1000, "filter_by": { "model": "gpt-3.5-turbo" } }
用户查询:
result = query_log_with_ai("显示 8 月 12 日到 8 月 14 日的日志。")
query_log 的预期参数:
{ "start_date": "2023-08-12", "end_date": "2023-08-14" }
*`query_log_with_ai()`* 函数将自动使用生成的参数调用 *`query_log()`* 函数。上面的示例旨在帮助您理解 *`query_log_with_ai()`* 的功能,同时也为 *`query_log()`* 函数提供了额外的示例。
## 日志查询的自动存储
* 结果存储在 *log_queries* 目录中,使用唯一的文件名。
* 始终确保敏感数据得到适当处理,不会被暴露。
# 启用和使用反思功能
反思功能提供了 OpenAI 模型如何解释给定提示的洞察,评估其响应并建议潜在改进。此功能集成在 `wrapper.ChatWrapper` 中,使其易于启用或禁用。
## 如何启用:
1. 启用反思:只需使用以下行为所有后续的 ChatCompletion 调用启用反思功能:
wrapper.ChatWrapper.enable_reflection()
这将自动为使用 `ChatWrapper` 进行的所有聊天完成 API 调用启用反思。
2. 禁用反思:如果您希望关闭反思功能:
wrapper.ChatWrapper.disable_reflection()
## 反思的存储:
反思被记录并存储在 `prompt_reflections.ndjson` 文件中。此文件中的每个条目代表一个不同的反思,格式为新行分隔的 JSON(NDJSON)。这允许更容易地附加新的反思和简化解析。
每个反思条目的结构如下:
- **kwargs**:包含提供给 OpenAI 模型的输入提示。
- **reflection**:
- **Analysis**:AI 如何解释提示以及其响应背后的理由的洞察。
- **Is Satisfactory**:指示 AI 是否认为其响应令人满意。可以是 `True` 或 `False`。
- **Suggested Prompt**:如果 AI 认为提示可以更清晰或更优化,这里提供建议的改进。
# Wrappers Delight UI
寻找一个用户友好的界面来可视化和解释由 `wrappers_delight` 生成的日志?查看 [`wrappers_delight_ui`](https://github.com/yoheinakajima/wrappers_delight_ui)!
`wrappers_delight_ui` 提供了一种直观的方式来查看反思和提示-响应动态。获得洞察,优化提示,并提高您对 AI 如何处理和响应各种提示的理解。
[在 GitHub 上探索 Wrappers Delight UI](https://github.com/yoheinakajima/wrappers_delight_ui)
# 贡献
如果您想贡献,请 fork 仓库并按您的意愿进行更改。我们热烈欢迎拉取请求。
# 许可证
根据 MIT 许可证分发。有关更多信息,请参阅 LICENSE。


最适合小白的AI自动化工作流平台
无需编码,轻松生成可复用、可变现的AI自动化工作流

大模型驱动的Excel数据处理工具
基于大模型交互的表格处理系统,允许用户通过对话方式完成数据整理和可视化分析。系统采用机器学习算法解析用户指令,自动执行排序、公式计算和数据透视等操作,支持多种文件格式导入导出。数据处理响应速度保持在0.8秒以内,支持超过100万行数据的即时分析。


AI辅助编程,代码自动修复
Trae是一种自适应的集成开发环境(IDE),通过自动化和多元协作改变开发流程。利用Trae,团队能够更快速、精确地编写和部署代码,从而提高编程效率和项目交付速度。Trae具备上下文感知和代码自动完成功能,是提升开发效率的理想工具。


AI论文写作指导平台
AIWritePaper论文写作是一站式AI论文写作辅助工具,简化了选题、文献检索至论文撰写的整个过程。通过简单设定,平台可快速生成高质量论文大纲和全文,配合图表、参考文献等一应俱全,同时提供开题报告和答辩PPT等增值服务,保障数据安全,有效提升写作效率和论文质量。


AI一键生成PPT,就用博思AIPPT!
博思AIPPT,新一代的AI生成PPT平台,支持智能生成PPT、AI美化PPT、文本&链接生成PPT、导入Word/PDF/Markdown文档生成PPT等,内置海量精美PPT模板,涵盖商务、教育、科技等不同风格,同时针对每个页面提供多种版式,一键自适应切换,完美适配各种办公场景。


AI赋能电商视觉革命,一站式智能商拍平台
潮际好麦深耕服装行业,是国内AI试衣效果最好的软件。使用先进AIGC能力为电商卖家批量提供优质的、低成本的商拍图。合作品牌有Shein、Lazada、安踏、百丽等65个国内外头部品牌,以及国内10万+淘宝、天猫、京东等主流平台的品牌商家,为卖家节省将近85%的出图成本,提升约3倍出图效率,让品牌能够快速上架。


企业专属的AI法律顾问
iTerms是法大大集团旗下法律子品牌,基于最先进的大语言模型(LLM)、专业的法律知识库和强大的智能体架构,帮助企业扫清合规障碍,筑牢风控防线,成为您企业专属的AI法律顾问。


稳定高效的流量提升解决方案,助力品牌曝光
稳定高效的流量提升解决方案,助力品牌曝光


最新版Sora2模型免费使用,一键生成无水印视频
最新版Sora2模型免费使用,一键生成无水印视频


实时语音翻译/同声传译工具
Transly是一个多场景的AI大语言模型驱动的同声传译、专业翻译助手,它拥有超精准的音频识别翻译能力,几乎零延迟的使用体验和支持多国语言可以让你带它走遍全球,无论你是留学生、商务人士、韩剧美剧爱好者,还是出国游玩、多国会议、跨国追星等等,都可以满足你所有需要同传的场景需求,线上线下通用,扫除语言障碍,让全世界的语言交流不再有国界。
最新AI工具、AI资讯
独家AI资源、AI项目落地

微信扫一扫关注公众号