gpts-builder


Namegpts-builder JSON
Version 0.1.20 PyPI version JSON
download
home_pagehttps://github.com/yancyuu/gpts_builder.git
SummaryA Python library for quickly building applications with large language models.
upload_time2024-06-21 08:43:14
maintainerNone
docs_urlNone
authoryancyyu
requires_python>=3.6
licenseMIT
keywords gpt large language models ai application builder
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # GPTS Builder

GPTS Builder 是一个高度模块化的 Python 库,专为快速构建和部署基于大型语言模型(如 GPT-3 或 GPT-4)的应用而设计。它提供了一系列工具,使开发者能够轻松管理会话、创建提示词模板、构建对话流程,并进行配置管理。

## 特点

- **上下文管理器**:有效管理对话状态,支持多种存储模式。
- **提示词模板构建器**:快速生成和管理提示词模板,提高对话生成的效率和质量。
- **大模型对话构建器**:继承自提示词模板构建器,提供高效的对话管理。
- **配置管理器**:灵活配置模型参数,包括模型名称和最大 Token 数量。
- **存储模式支持**:支持全局变量和 Redis 存储,确保灵活性和扩展性。

## 架构概览

GPTS Builder 采用模块化设计,主要包括以下四个核心组件:

1. **上下文管理器**:管理用户会话和状态,支持全局变量和 Redis 存储模式。
2. **提示词模板构建器**:构建和维护用于生成模型提示词的模板。
3. **大模型对话构建器**:基于提示词模板生成对话,支持快速部署和测试模型应用。
4. **配置管理器**:中心化管理所有模型和应用配置。

## 开发计划

| 功能                           | 状态        |
| ------------------------------ | ----------- |
| 上下文管理器                   | ✅           |
| 提示词模板构建器               | ✅           |
| 大模型对话构建器               | ✅           |
| 配置管理器                     | ✅           |
| Redis 存储模式支持             | ✅           |
| 全局变量存储模式               | ✅           |
| 支持流式输出                   | ✅          |
| 知识库集成                     | ✅           |
| GPTS缓存                       | ☐           |

> 知识库集成:将提供与大型知识库的集成,增强模型的应答能力。
> GPTS缓存:为了提高响应速度和减少API调用成本,将实现请求结果的缓存机制。

# 快速开始

## LLM 部分

### 大模型对话构建器

大模型对话构建器继承自提示词模板构建器,集成模板管理与对话生成的功能,支持复杂的对话流程控制,使得生成的对话更加自然和流畅。

### 可用类:
- `LLM`: 大模型对话管理。
- `LLMAsync`: 异步大模型对话管理。

```python

from gpts_builder.builder import LLM
from gpts_builder.builder_async import LLMAsync
from gpts_builder.session_manager.storage.redis_storage_async import RedisStorageAsync

from gpts_builder.config import config_manager

from gpts_builder.util.logger import logger

import asyncio

"""
大语言模型的demo
"""

‘’‘python
# 查看所有模型的配置
configs = config_manager.list_models_config()
print(configs)
config_manager.base_url = "https://www.lazygpt.cn/api【可以改成自己的url,我这里使用的代理】"
config_manager.apikey = "lazygpt-XXXXXXXXXXXX"【改成自己的apikey】


async def llm_async_demo():
    """
    llm_async_demo 测试异步LLM的demo
    """
    ### example0: 初始化LLM(开启一轮对话)
    # 异步的session存储需要设置,目前只支持异步redis存储
    session_storage = RedisStorageAsync("redis://localhost:6379")
    llm = LLMAsync(model="gpt-3.5-turbo", session_storage=session_storage)
    # 设置系统提示词和用户输入
    await llm.set_system("你是一个AI助理").set_prompt("测试回复").build()

    ### example1: 测试一轮对话,返回所有结果
    # 获取非流式回复
    reply = await llm.chat_completions()
    print(f"Received reply {reply}")
    content = reply.get("choices")[0].get("message",{}).get("content") if reply.get("choices") else ""
    print(f"Received content {content}")

    ### example2: 测试一轮对话,流式返回所有结果(获取流式回复:流式返回为一个异步生成器,需要迭代生成)
    async for content in llm.chat_completions_stream():
        print(f"Received content: {content}")
    
    ### example3: 查看当前会话的历史消息
    session = llm.session
    print(f"Session messages: {session.to_json()}")


def llm_sync_demo():
    """
    llm_sync_demo 测试同步LLM的demo
    """
    ### example0: 初始化LLM(开启一轮对话)
    # session存储支持redis存储|全局变量存储,如果不传session_storage则默认使用全局变量存储
    llm = LLM(model="gpt-3.5-turbo")
    llm.set_system("这里填系统提示词").set_prompt("这里填提示词模板和参数或者内容").build()

    ### example1: 测试一轮对话,返回所有结果
    replay = llm.chat_completions()
    content = replay.get("choices")[0].get("message",{}).get("content") if replay.get("choices") else ""
    print(f"Received content: {content}")

    ### example2: 测试一轮对话,流式返回所有结果(流式返回为一个异步生成器,需要迭代生成)
    for content in llm.chat_completions_stream():
        print(f"Received content: {content}")

    ### example3: 查看当前会话的历史消息
    session = llm.session
    print(f"Session messages: {session.to_json()}")

    ### example4: 清除当前会话(如果不需要上下文管理,存储设置成全局变量存储,然后手动清除会话即可)
    llm.clear_session()
    

if __name__ == "__main__":
    # asyncio.run(llm_async_demo())
    llm_sync_demo()

```

## 数据检索增强模块测试代码

为什么要做检索增强(参考文档)

### 数据检索增强模块测试代码:为什么要做检索增强(https://platform.openai.com/docs/guides/prompt-engineering/strategy-use-external-tools)

### 可用类:
- `DatasetBuilder`: 知识库管理(检索增强用)。
- `DatasetBuilderAsync` 异步知识库管理(检索增强用)。

```python
from gpts_builder.session_manager.storage.redis_storage import RedisStorage
from gpts_builder.session_manager.storage.redis_storage_async import RedisStorageAsync

from gpts_builder.config import config_manager

import asyncio

config_manager.base_url = "https://www.lazygpt.cn/api【可以改成自己的url,我这里使用的代理】"
config_manager.apikey = "lazygpt-XXXXXXXXXXXX"【改成自己的apikey】


def dataset_demo():
    """
    dataset_demo 检索增强数据集的demo
    """
    from gpts_builder.builder import DatasetBuilder
    from gpts_builder.util import PostgresVector
    # 使用知识库,知识库需要一个向量数据库(目前只支持pgvector)
    ### example1: 初始化向量数据库
    db_driver = PostgresVector(dbname="postgres", user="myuser", password="mypassword", host="localhost", port=5432)
    dataset_builder = DatasetBuilder(db_driver=db_driver)
    dataset_id = dataset_builder.create_dataset("测试知识库")

    ### example2: 根据id获取知识库详情
    dataset_details = dataset_builder.get_dataset(filters={dataset_builder.dataset_schema.id: dataset_id})
    print(f"dataset_details1: {dataset_details}")
    
    ### example3: 根据名称获取知识库详情
    dataset_details = dataset_builder.get_dataset(filters={dataset_builder.dataset_schema.name: "测试知识库"})
    print(f"dataset_details2: {dataset_details}")

    ### example4: 向知识库中增加数据【一个答案(内容)对应多个问题(索引)】
    res = dataset_builder.create_datas(dataset_id, "这是一个答案", ["问题1", "问题2"])
    print(f"datas: {res}")

    ### example5: 根据文本在库中查询相似度
    datas_similarity = dataset_builder.query_similarity(text="这是测试数据", dataset_ids=[1, 2, dataset_id])
    print(f"datas_similarity: {datas_similarity}")

    ### example6: 根据文本在库中正则匹配
    datas_regex = dataset_builder.query_regex(regex=".*问题.*", dataset_ids=[dataset_id])
    print(f"datas_regex: {datas_regex}")

async def dataset_asnyc_demo():
    """
    dataset_asnyc_demo 异步操作知识库模块
    """
    from gpts_builder.builder_async import DatasetBuilderAsync
    from gpts_builder.util import PostgresVectorAsync
    # 使用知识库,知识库需要一个向量数据库(目前只支持pgvector)
    db_driver = PostgresVectorAsync(dbname="postgres", user="myuser", password="mypassword", host="127.0.0.1", port=5432)
    dataset_builder = DatasetBuilderAsync(db_driver=db_driver)
    
    ### example1: 创建知识库
    dataset_id = await dataset_builder.create_dataset("测试知识库")

    ### example2: 通过名称获取知识库详情
    dataset_details = await dataset_builder.get_dataset(filters={dataset_builder.dataset_schema.id: dataset_id})

    ### example3: 根据名称获取知识库详情
    dataset_details = await dataset_builder.get_dataset(filters={dataset_builder.dataset_schema.name: "测试知识库"})
    print(f"dataset_details: {dataset_details}")

    ### example4: 向知识库中增加数据【一个答案(内容)对应多个问题(索引)】
    datas = await dataset_builder.create_datas(dataset_id, "这是一个答案", ["问题1", "问题2"])
    print(f"datas: {datas}")

    ### example5: 根据文本在库中查询相似度
    datas_similarity = await dataset_builder.query_similarity(text="这是测试文本", dataset_ids=[dataset_id])
    print(f"datas_similarity: {datas_similarity}")

    ### example6: 根据文本在库中正则匹配
    datas_regex = await dataset_builder.query_regex(regex="*问题*", dataset_ids=[dataset_id])
    print(f"datas_regex: {datas_regex}")


if __name__ == "__main__":
    #dataset_demo()
    asyncio.run(dataset_asnyc_demo())

```


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/yancyuu/gpts_builder.git",
    "name": "gpts-builder",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.6",
    "maintainer_email": null,
    "keywords": "GPT, large language models, AI, application builder",
    "author": "yancyyu",
    "author_email": "yancyyu.ok@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/04/d6/0baa03177563792e17e19619985943a74f7b13f8aab38afb5d5f315f16cb/gpts_builder-0.1.20.tar.gz",
    "platform": null,
    "description": "# GPTS Builder\n\nGPTS Builder \u662f\u4e00\u4e2a\u9ad8\u5ea6\u6a21\u5757\u5316\u7684 Python \u5e93\uff0c\u4e13\u4e3a\u5feb\u901f\u6784\u5efa\u548c\u90e8\u7f72\u57fa\u4e8e\u5927\u578b\u8bed\u8a00\u6a21\u578b\uff08\u5982 GPT-3 \u6216 GPT-4\uff09\u7684\u5e94\u7528\u800c\u8bbe\u8ba1\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u5de5\u5177\uff0c\u4f7f\u5f00\u53d1\u8005\u80fd\u591f\u8f7b\u677e\u7ba1\u7406\u4f1a\u8bdd\u3001\u521b\u5efa\u63d0\u793a\u8bcd\u6a21\u677f\u3001\u6784\u5efa\u5bf9\u8bdd\u6d41\u7a0b\uff0c\u5e76\u8fdb\u884c\u914d\u7f6e\u7ba1\u7406\u3002\n\n## \u7279\u70b9\n\n- **\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668**\uff1a\u6709\u6548\u7ba1\u7406\u5bf9\u8bdd\u72b6\u6001\uff0c\u652f\u6301\u591a\u79cd\u5b58\u50a8\u6a21\u5f0f\u3002\n- **\u63d0\u793a\u8bcd\u6a21\u677f\u6784\u5efa\u5668**\uff1a\u5feb\u901f\u751f\u6210\u548c\u7ba1\u7406\u63d0\u793a\u8bcd\u6a21\u677f\uff0c\u63d0\u9ad8\u5bf9\u8bdd\u751f\u6210\u7684\u6548\u7387\u548c\u8d28\u91cf\u3002\n- **\u5927\u6a21\u578b\u5bf9\u8bdd\u6784\u5efa\u5668**\uff1a\u7ee7\u627f\u81ea\u63d0\u793a\u8bcd\u6a21\u677f\u6784\u5efa\u5668\uff0c\u63d0\u4f9b\u9ad8\u6548\u7684\u5bf9\u8bdd\u7ba1\u7406\u3002\n- **\u914d\u7f6e\u7ba1\u7406\u5668**\uff1a\u7075\u6d3b\u914d\u7f6e\u6a21\u578b\u53c2\u6570\uff0c\u5305\u62ec\u6a21\u578b\u540d\u79f0\u548c\u6700\u5927 Token \u6570\u91cf\u3002\n- **\u5b58\u50a8\u6a21\u5f0f\u652f\u6301**\uff1a\u652f\u6301\u5168\u5c40\u53d8\u91cf\u548c Redis \u5b58\u50a8\uff0c\u786e\u4fdd\u7075\u6d3b\u6027\u548c\u6269\u5c55\u6027\u3002\n\n## \u67b6\u6784\u6982\u89c8\n\nGPTS Builder \u91c7\u7528\u6a21\u5757\u5316\u8bbe\u8ba1\uff0c\u4e3b\u8981\u5305\u62ec\u4ee5\u4e0b\u56db\u4e2a\u6838\u5fc3\u7ec4\u4ef6\uff1a\n\n1. **\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668**\uff1a\u7ba1\u7406\u7528\u6237\u4f1a\u8bdd\u548c\u72b6\u6001\uff0c\u652f\u6301\u5168\u5c40\u53d8\u91cf\u548c Redis \u5b58\u50a8\u6a21\u5f0f\u3002\n2. **\u63d0\u793a\u8bcd\u6a21\u677f\u6784\u5efa\u5668**\uff1a\u6784\u5efa\u548c\u7ef4\u62a4\u7528\u4e8e\u751f\u6210\u6a21\u578b\u63d0\u793a\u8bcd\u7684\u6a21\u677f\u3002\n3. **\u5927\u6a21\u578b\u5bf9\u8bdd\u6784\u5efa\u5668**\uff1a\u57fa\u4e8e\u63d0\u793a\u8bcd\u6a21\u677f\u751f\u6210\u5bf9\u8bdd\uff0c\u652f\u6301\u5feb\u901f\u90e8\u7f72\u548c\u6d4b\u8bd5\u6a21\u578b\u5e94\u7528\u3002\n4. **\u914d\u7f6e\u7ba1\u7406\u5668**\uff1a\u4e2d\u5fc3\u5316\u7ba1\u7406\u6240\u6709\u6a21\u578b\u548c\u5e94\u7528\u914d\u7f6e\u3002\n\n## \u5f00\u53d1\u8ba1\u5212\n\n| \u529f\u80fd                           | \u72b6\u6001        |\n| ------------------------------ | ----------- |\n| \u4e0a\u4e0b\u6587\u7ba1\u7406\u5668                   | \u2705           |\n| \u63d0\u793a\u8bcd\u6a21\u677f\u6784\u5efa\u5668               | \u2705           |\n| \u5927\u6a21\u578b\u5bf9\u8bdd\u6784\u5efa\u5668               | \u2705           |\n| \u914d\u7f6e\u7ba1\u7406\u5668                     | \u2705           |\n| Redis \u5b58\u50a8\u6a21\u5f0f\u652f\u6301             | \u2705           |\n| \u5168\u5c40\u53d8\u91cf\u5b58\u50a8\u6a21\u5f0f               | \u2705           |\n| \u652f\u6301\u6d41\u5f0f\u8f93\u51fa                   | \u2705          |\n| \u77e5\u8bc6\u5e93\u96c6\u6210                     | \u2705           |\n| GPTS\u7f13\u5b58                       | \u2610           |\n\n> \u77e5\u8bc6\u5e93\u96c6\u6210\uff1a\u5c06\u63d0\u4f9b\u4e0e\u5927\u578b\u77e5\u8bc6\u5e93\u7684\u96c6\u6210\uff0c\u589e\u5f3a\u6a21\u578b\u7684\u5e94\u7b54\u80fd\u529b\u3002\n> GPTS\u7f13\u5b58\uff1a\u4e3a\u4e86\u63d0\u9ad8\u54cd\u5e94\u901f\u5ea6\u548c\u51cf\u5c11API\u8c03\u7528\u6210\u672c\uff0c\u5c06\u5b9e\u73b0\u8bf7\u6c42\u7ed3\u679c\u7684\u7f13\u5b58\u673a\u5236\u3002\n\n# \u5feb\u901f\u5f00\u59cb\n\n## LLM \u90e8\u5206\n\n### \u5927\u6a21\u578b\u5bf9\u8bdd\u6784\u5efa\u5668\n\n\u5927\u6a21\u578b\u5bf9\u8bdd\u6784\u5efa\u5668\u7ee7\u627f\u81ea\u63d0\u793a\u8bcd\u6a21\u677f\u6784\u5efa\u5668\uff0c\u96c6\u6210\u6a21\u677f\u7ba1\u7406\u4e0e\u5bf9\u8bdd\u751f\u6210\u7684\u529f\u80fd\uff0c\u652f\u6301\u590d\u6742\u7684\u5bf9\u8bdd\u6d41\u7a0b\u63a7\u5236\uff0c\u4f7f\u5f97\u751f\u6210\u7684\u5bf9\u8bdd\u66f4\u52a0\u81ea\u7136\u548c\u6d41\u7545\u3002\n\n### \u53ef\u7528\u7c7b\uff1a\n- `LLM`: \u5927\u6a21\u578b\u5bf9\u8bdd\u7ba1\u7406\u3002\n- `LLMAsync`: \u5f02\u6b65\u5927\u6a21\u578b\u5bf9\u8bdd\u7ba1\u7406\u3002\n\n```python\n\nfrom gpts_builder.builder import LLM\nfrom gpts_builder.builder_async import LLMAsync\nfrom gpts_builder.session_manager.storage.redis_storage_async import RedisStorageAsync\n\nfrom gpts_builder.config import config_manager\n\nfrom gpts_builder.util.logger import logger\n\nimport asyncio\n\n\"\"\"\n\u5927\u8bed\u8a00\u6a21\u578b\u7684demo\n\"\"\"\n\n\u2018\u2019\u2018python\n# \u67e5\u770b\u6240\u6709\u6a21\u578b\u7684\u914d\u7f6e\nconfigs = config_manager.list_models_config()\nprint(configs)\nconfig_manager.base_url = \"https://www.lazygpt.cn/api\u3010\u53ef\u4ee5\u6539\u6210\u81ea\u5df1\u7684url\uff0c\u6211\u8fd9\u91cc\u4f7f\u7528\u7684\u4ee3\u7406\u3011\"\nconfig_manager.apikey = \"lazygpt-XXXXXXXXXXXX\"\u3010\u6539\u6210\u81ea\u5df1\u7684apikey\u3011\n\n\nasync def llm_async_demo():\n    \"\"\"\n    llm_async_demo \u6d4b\u8bd5\u5f02\u6b65LLM\u7684demo\n    \"\"\"\n    ### example0: \u521d\u59cb\u5316LLM\uff08\u5f00\u542f\u4e00\u8f6e\u5bf9\u8bdd\uff09\n    # \u5f02\u6b65\u7684session\u5b58\u50a8\u9700\u8981\u8bbe\u7f6e\uff0c\u76ee\u524d\u53ea\u652f\u6301\u5f02\u6b65redis\u5b58\u50a8\n    session_storage = RedisStorageAsync(\"redis://localhost:6379\")\n    llm = LLMAsync(model=\"gpt-3.5-turbo\", session_storage=session_storage)\n    # \u8bbe\u7f6e\u7cfb\u7edf\u63d0\u793a\u8bcd\u548c\u7528\u6237\u8f93\u5165\n    await llm.set_system(\"\u4f60\u662f\u4e00\u4e2aAI\u52a9\u7406\").set_prompt(\"\u6d4b\u8bd5\u56de\u590d\").build()\n\n    ### example1: \u6d4b\u8bd5\u4e00\u8f6e\u5bf9\u8bdd\uff0c\u8fd4\u56de\u6240\u6709\u7ed3\u679c\n    # \u83b7\u53d6\u975e\u6d41\u5f0f\u56de\u590d\n    reply = await llm.chat_completions()\n    print(f\"Received reply {reply}\")\n    content = reply.get(\"choices\")[0].get(\"message\",{}).get(\"content\") if reply.get(\"choices\") else \"\"\n    print(f\"Received content {content}\")\n\n    ### example2: \u6d4b\u8bd5\u4e00\u8f6e\u5bf9\u8bdd\uff0c\u6d41\u5f0f\u8fd4\u56de\u6240\u6709\u7ed3\u679c\uff08\u83b7\u53d6\u6d41\u5f0f\u56de\u590d\uff1a\u6d41\u5f0f\u8fd4\u56de\u4e3a\u4e00\u4e2a\u5f02\u6b65\u751f\u6210\u5668\uff0c\u9700\u8981\u8fed\u4ee3\u751f\u6210\uff09\n    async for content in llm.chat_completions_stream():\n        print(f\"Received content: {content}\")\n    \n    ### example3: \u67e5\u770b\u5f53\u524d\u4f1a\u8bdd\u7684\u5386\u53f2\u6d88\u606f\n    session = llm.session\n    print(f\"Session messages: {session.to_json()}\")\n\n\ndef llm_sync_demo():\n    \"\"\"\n    llm_sync_demo \u6d4b\u8bd5\u540c\u6b65LLM\u7684demo\n    \"\"\"\n    ### example0: \u521d\u59cb\u5316LLM\uff08\u5f00\u542f\u4e00\u8f6e\u5bf9\u8bdd\uff09\n    # session\u5b58\u50a8\u652f\u6301redis\u5b58\u50a8\uff5c\u5168\u5c40\u53d8\u91cf\u5b58\u50a8\uff0c\u5982\u679c\u4e0d\u4f20session_storage\u5219\u9ed8\u8ba4\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u5b58\u50a8\n    llm = LLM(model=\"gpt-3.5-turbo\")\n    llm.set_system(\"\u8fd9\u91cc\u586b\u7cfb\u7edf\u63d0\u793a\u8bcd\").set_prompt(\"\u8fd9\u91cc\u586b\u63d0\u793a\u8bcd\u6a21\u677f\u548c\u53c2\u6570\u6216\u8005\u5185\u5bb9\").build()\n\n    ### example1: \u6d4b\u8bd5\u4e00\u8f6e\u5bf9\u8bdd\uff0c\u8fd4\u56de\u6240\u6709\u7ed3\u679c\n    replay = llm.chat_completions()\n    content = replay.get(\"choices\")[0].get(\"message\",{}).get(\"content\") if replay.get(\"choices\") else \"\"\n    print(f\"Received content: {content}\")\n\n    ### example2: \u6d4b\u8bd5\u4e00\u8f6e\u5bf9\u8bdd\uff0c\u6d41\u5f0f\u8fd4\u56de\u6240\u6709\u7ed3\u679c\uff08\u6d41\u5f0f\u8fd4\u56de\u4e3a\u4e00\u4e2a\u5f02\u6b65\u751f\u6210\u5668\uff0c\u9700\u8981\u8fed\u4ee3\u751f\u6210\uff09\n    for content in llm.chat_completions_stream():\n        print(f\"Received content: {content}\")\n\n    ### example3: \u67e5\u770b\u5f53\u524d\u4f1a\u8bdd\u7684\u5386\u53f2\u6d88\u606f\n    session = llm.session\n    print(f\"Session messages: {session.to_json()}\")\n\n    ### example4: \u6e05\u9664\u5f53\u524d\u4f1a\u8bdd(\u5982\u679c\u4e0d\u9700\u8981\u4e0a\u4e0b\u6587\u7ba1\u7406\uff0c\u5b58\u50a8\u8bbe\u7f6e\u6210\u5168\u5c40\u53d8\u91cf\u5b58\u50a8\uff0c\u7136\u540e\u624b\u52a8\u6e05\u9664\u4f1a\u8bdd\u5373\u53ef)\n    llm.clear_session()\n    \n\nif __name__ == \"__main__\":\n    # asyncio.run(llm_async_demo())\n    llm_sync_demo()\n\n```\n\n## \u6570\u636e\u68c0\u7d22\u589e\u5f3a\u6a21\u5757\u6d4b\u8bd5\u4ee3\u7801\n\n\u4e3a\u4ec0\u4e48\u8981\u505a\u68c0\u7d22\u589e\u5f3a\uff08\u53c2\u8003\u6587\u6863\uff09\n\n### \u6570\u636e\u68c0\u7d22\u589e\u5f3a\u6a21\u5757\u6d4b\u8bd5\u4ee3\u7801\uff1a\u4e3a\u4ec0\u4e48\u8981\u505a\u68c0\u7d22\u589e\u5f3a\uff08https://platform.openai.com/docs/guides/prompt-engineering/strategy-use-external-tools\uff09\n\n### \u53ef\u7528\u7c7b\uff1a\n- `DatasetBuilder`: \u77e5\u8bc6\u5e93\u7ba1\u7406\uff08\u68c0\u7d22\u589e\u5f3a\u7528\uff09\u3002\n- `DatasetBuilderAsync` \u5f02\u6b65\u77e5\u8bc6\u5e93\u7ba1\u7406\uff08\u68c0\u7d22\u589e\u5f3a\u7528\uff09\u3002\n\n```python\nfrom gpts_builder.session_manager.storage.redis_storage import RedisStorage\nfrom gpts_builder.session_manager.storage.redis_storage_async import RedisStorageAsync\n\nfrom gpts_builder.config import config_manager\n\nimport asyncio\n\nconfig_manager.base_url = \"https://www.lazygpt.cn/api\u3010\u53ef\u4ee5\u6539\u6210\u81ea\u5df1\u7684url\uff0c\u6211\u8fd9\u91cc\u4f7f\u7528\u7684\u4ee3\u7406\u3011\"\nconfig_manager.apikey = \"lazygpt-XXXXXXXXXXXX\"\u3010\u6539\u6210\u81ea\u5df1\u7684apikey\u3011\n\n\ndef dataset_demo():\n    \"\"\"\n    dataset_demo \u68c0\u7d22\u589e\u5f3a\u6570\u636e\u96c6\u7684demo\n    \"\"\"\n    from gpts_builder.builder import DatasetBuilder\n    from gpts_builder.util import PostgresVector\n    # \u4f7f\u7528\u77e5\u8bc6\u5e93\uff0c\u77e5\u8bc6\u5e93\u9700\u8981\u4e00\u4e2a\u5411\u91cf\u6570\u636e\u5e93\uff08\u76ee\u524d\u53ea\u652f\u6301pgvector\uff09\n    ### example1: \u521d\u59cb\u5316\u5411\u91cf\u6570\u636e\u5e93\n    db_driver = PostgresVector(dbname=\"postgres\", user=\"myuser\", password=\"mypassword\", host=\"localhost\", port=5432)\n    dataset_builder = DatasetBuilder(db_driver=db_driver)\n    dataset_id = dataset_builder.create_dataset(\"\u6d4b\u8bd5\u77e5\u8bc6\u5e93\")\n\n    ### example2: \u6839\u636eid\u83b7\u53d6\u77e5\u8bc6\u5e93\u8be6\u60c5\n    dataset_details = dataset_builder.get_dataset(filters={dataset_builder.dataset_schema.id: dataset_id})\n    print(f\"dataset_details1: {dataset_details}\")\n    \n    ### example3: \u6839\u636e\u540d\u79f0\u83b7\u53d6\u77e5\u8bc6\u5e93\u8be6\u60c5\n    dataset_details = dataset_builder.get_dataset(filters={dataset_builder.dataset_schema.name: \"\u6d4b\u8bd5\u77e5\u8bc6\u5e93\"})\n    print(f\"dataset_details2: {dataset_details}\")\n\n    ### example4: \u5411\u77e5\u8bc6\u5e93\u4e2d\u589e\u52a0\u6570\u636e\u3010\u4e00\u4e2a\u7b54\u6848\uff08\u5185\u5bb9\uff09\u5bf9\u5e94\u591a\u4e2a\u95ee\u9898\uff08\u7d22\u5f15\uff09\u3011\n    res = dataset_builder.create_datas(dataset_id, \"\u8fd9\u662f\u4e00\u4e2a\u7b54\u6848\", [\"\u95ee\u98981\", \"\u95ee\u98982\"])\n    print(f\"datas: {res}\")\n\n    ### example5: \u6839\u636e\u6587\u672c\u5728\u5e93\u4e2d\u67e5\u8be2\u76f8\u4f3c\u5ea6\n    datas_similarity = dataset_builder.query_similarity(text=\"\u8fd9\u662f\u6d4b\u8bd5\u6570\u636e\", dataset_ids=[1, 2, dataset_id])\n    print(f\"datas_similarity: {datas_similarity}\")\n\n    ### example6: \u6839\u636e\u6587\u672c\u5728\u5e93\u4e2d\u6b63\u5219\u5339\u914d\n    datas_regex = dataset_builder.query_regex(regex=\".*\u95ee\u9898.*\", dataset_ids=[dataset_id])\n    print(f\"datas_regex: {datas_regex}\")\n\nasync def dataset_asnyc_demo():\n    \"\"\"\n    dataset_asnyc_demo \u5f02\u6b65\u64cd\u4f5c\u77e5\u8bc6\u5e93\u6a21\u5757\n    \"\"\"\n    from gpts_builder.builder_async import DatasetBuilderAsync\n    from gpts_builder.util import PostgresVectorAsync\n    # \u4f7f\u7528\u77e5\u8bc6\u5e93\uff0c\u77e5\u8bc6\u5e93\u9700\u8981\u4e00\u4e2a\u5411\u91cf\u6570\u636e\u5e93\uff08\u76ee\u524d\u53ea\u652f\u6301pgvector\uff09\n    db_driver = PostgresVectorAsync(dbname=\"postgres\", user=\"myuser\", password=\"mypassword\", host=\"127.0.0.1\", port=5432)\n    dataset_builder = DatasetBuilderAsync(db_driver=db_driver)\n    \n    ### example1: \u521b\u5efa\u77e5\u8bc6\u5e93\n    dataset_id = await dataset_builder.create_dataset(\"\u6d4b\u8bd5\u77e5\u8bc6\u5e93\")\n\n    ### example2: \u901a\u8fc7\u540d\u79f0\u83b7\u53d6\u77e5\u8bc6\u5e93\u8be6\u60c5\n    dataset_details = await dataset_builder.get_dataset(filters={dataset_builder.dataset_schema.id: dataset_id})\n\n    ### example3: \u6839\u636e\u540d\u79f0\u83b7\u53d6\u77e5\u8bc6\u5e93\u8be6\u60c5\n    dataset_details = await dataset_builder.get_dataset(filters={dataset_builder.dataset_schema.name: \"\u6d4b\u8bd5\u77e5\u8bc6\u5e93\"})\n    print(f\"dataset_details: {dataset_details}\")\n\n    ### example4: \u5411\u77e5\u8bc6\u5e93\u4e2d\u589e\u52a0\u6570\u636e\u3010\u4e00\u4e2a\u7b54\u6848\uff08\u5185\u5bb9\uff09\u5bf9\u5e94\u591a\u4e2a\u95ee\u9898\uff08\u7d22\u5f15\uff09\u3011\n    datas = await dataset_builder.create_datas(dataset_id, \"\u8fd9\u662f\u4e00\u4e2a\u7b54\u6848\", [\"\u95ee\u98981\", \"\u95ee\u98982\"])\n    print(f\"datas: {datas}\")\n\n    ### example5: \u6839\u636e\u6587\u672c\u5728\u5e93\u4e2d\u67e5\u8be2\u76f8\u4f3c\u5ea6\n    datas_similarity = await dataset_builder.query_similarity(text=\"\u8fd9\u662f\u6d4b\u8bd5\u6587\u672c\", dataset_ids=[dataset_id])\n    print(f\"datas_similarity: {datas_similarity}\")\n\n    ### example6: \u6839\u636e\u6587\u672c\u5728\u5e93\u4e2d\u6b63\u5219\u5339\u914d\n    datas_regex = await dataset_builder.query_regex(regex=\"*\u95ee\u9898*\", dataset_ids=[dataset_id])\n    print(f\"datas_regex: {datas_regex}\")\n\n\nif __name__ == \"__main__\":\n    #dataset_demo()\n    asyncio.run(dataset_asnyc_demo())\n\n```\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A Python library for quickly building applications with large language models.",
    "version": "0.1.20",
    "project_urls": {
        "Homepage": "https://github.com/yancyuu/gpts_builder.git"
    },
    "split_keywords": [
        "gpt",
        " large language models",
        " ai",
        " application builder"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "f4bf57e2871e292c0dadd2573a0ebd9df0ce4fa785152065b725528c390ec95a",
                "md5": "9139b7d37fb9113de6f82eafb63e5ea0",
                "sha256": "871681c16338e30c19b7847eec6f4121e8aab06e1854a7a29d789fd61d959768"
            },
            "downloads": -1,
            "filename": "gpts_builder-0.1.20-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "9139b7d37fb9113de6f82eafb63e5ea0",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.6",
            "size": 50602,
            "upload_time": "2024-06-21T08:43:12",
            "upload_time_iso_8601": "2024-06-21T08:43:12.157632Z",
            "url": "https://files.pythonhosted.org/packages/f4/bf/57e2871e292c0dadd2573a0ebd9df0ce4fa785152065b725528c390ec95a/gpts_builder-0.1.20-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "04d60baa03177563792e17e19619985943a74f7b13f8aab38afb5d5f315f16cb",
                "md5": "7d8e9d066624254cdd61641ef9b84254",
                "sha256": "a15abcad8cb29d522c0efbd6229e1b8dadfe9bd18b207f1360e5c0acda521131"
            },
            "downloads": -1,
            "filename": "gpts_builder-0.1.20.tar.gz",
            "has_sig": false,
            "md5_digest": "7d8e9d066624254cdd61641ef9b84254",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.6",
            "size": 36647,
            "upload_time": "2024-06-21T08:43:14",
            "upload_time_iso_8601": "2024-06-21T08:43:14.713694Z",
            "url": "https://files.pythonhosted.org/packages/04/d6/0baa03177563792e17e19619985943a74f7b13f8aab38afb5d5f315f16cb/gpts_builder-0.1.20.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-06-21 08:43:14",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "yancyuu",
    "github_project": "gpts_builder",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [],
    "lcname": "gpts-builder"
}
        
Elapsed time: 4.01749s